2014-05-05 07:46:00 +02:00
|
|
|
var assert = require('assert')
|
|
|
|
var base58 = require('../../').base58
|
|
|
|
var base58check = require('../../').base58check
|
|
|
|
var crypto = require('../../').crypto
|
|
|
|
var fs = require('fs')
|
|
|
|
var request = require('request')
|
|
|
|
var secureRandom = require('secure-random')
|
|
|
|
|
|
|
|
function b2h(b) { return new Buffer(b).toString('hex') }
|
|
|
|
function h2b(h) { return new Buffer(h, 'hex') }
|
2014-05-08 05:23:31 +02:00
|
|
|
function randomBuf(s) { return new Buffer(secureRandom(s)) }
|
2014-05-05 07:46:00 +02:00
|
|
|
|
|
|
|
request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/base58_encode_decode.json', function (error, response, body) {
|
|
|
|
assert.ifError(error)
|
|
|
|
assert.equal(response.statusCode, 200)
|
|
|
|
|
2014-05-08 05:23:31 +02:00
|
|
|
var valid = JSON.parse(body).map(function(x) {
|
2014-05-05 07:46:00 +02:00
|
|
|
return {
|
|
|
|
hex: x[0],
|
|
|
|
string: x[1]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// https://github.com/bitcoin/bitcoin/blob/master/src/test/base58_tests.cpp#L73
|
|
|
|
// FIXME: Doesn't work TODO
|
|
|
|
// valid.push({
|
|
|
|
// hex: '971a55',
|
|
|
|
// string: ' \t\n\v\f\r skip \r\f\v\n\t '
|
|
|
|
// })
|
|
|
|
|
2014-05-08 05:23:31 +02:00
|
|
|
var fixture = JSON.stringify({
|
2014-05-05 07:46:00 +02:00
|
|
|
valid: valid,
|
|
|
|
invalid: [
|
|
|
|
{
|
|
|
|
description: 'non-base58 string',
|
|
|
|
string: 'invalid'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'non-base58 alphabet',
|
|
|
|
string: 'c2F0b3NoaQo='
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'leading whitespace',
|
|
|
|
string: ' 1111111111'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'trailing whitespace',
|
|
|
|
string: '1111111111 '
|
|
|
|
},
|
|
|
|
// https://github.com/bitcoin/bitcoin/blob/master/src/test/base58_tests.cpp#L72
|
|
|
|
{
|
|
|
|
description: 'unexpected character after whitespace',
|
|
|
|
string: ' \t\n\v\f\r skip \r\f\v\n\t a'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, null, ' ')
|
|
|
|
|
2014-05-18 11:47:39 +02:00
|
|
|
fs.writeFileSync('./test/fixtures/base58.json', fixture)
|
2014-05-05 07:46:00 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/base58_keys_valid.json', function (error, response, body) {
|
2014-05-08 05:23:31 +02:00
|
|
|
request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/base58_keys_invalid.json', function (error2, response2, body2) {
|
|
|
|
assert.ifError(error)
|
|
|
|
assert.ifError(error2)
|
|
|
|
assert.equal(response.statusCode, 200)
|
|
|
|
assert.equal(response2.statusCode, 200)
|
2014-05-05 07:46:00 +02:00
|
|
|
|
2014-05-08 05:23:31 +02:00
|
|
|
var valid = JSON.parse(body).map(function(x) {
|
|
|
|
var string = x[0]
|
|
|
|
var hex = x[1]
|
|
|
|
var params = x[2]
|
2014-05-05 07:46:00 +02:00
|
|
|
|
2014-05-08 05:23:31 +02:00
|
|
|
if (params.isCompressed) hex += '01'
|
|
|
|
assert.equal(b2h(base58check.decode(string).payload), hex)
|
2014-05-05 07:46:00 +02:00
|
|
|
|
2014-05-08 05:23:31 +02:00
|
|
|
return {
|
|
|
|
string: string,
|
|
|
|
decode: {
|
|
|
|
version: base58check.decode(string).version,
|
|
|
|
payload: hex,
|
|
|
|
checksum: b2h(base58check.decode(string).checksum),
|
2014-05-05 07:46:00 +02:00
|
|
|
}
|
2014-05-08 05:23:31 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
var invalid2 = JSON.parse(body2).map(function(x) { return x[0] })
|
2014-05-05 07:46:00 +02:00
|
|
|
|
2014-05-08 05:23:31 +02:00
|
|
|
// Our own tests
|
|
|
|
var hash = crypto.hash160(randomBuf(65))
|
|
|
|
var checksum = base58check.decode(base58check.encode(hash)).checksum
|
2014-05-05 07:46:00 +02:00
|
|
|
|
2014-05-08 05:23:31 +02:00
|
|
|
var fixture = JSON.stringify({
|
|
|
|
valid: valid,
|
|
|
|
invalid: [
|
|
|
|
{
|
2014-05-05 07:46:00 +02:00
|
|
|
base58check: base58check.encode(hash.slice(0, 18), 0x05),
|
|
|
|
description: 'hash too short'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
base58check: base58check.encode(Buffer.concat([hash, randomBuf(2)]), 0x05),
|
|
|
|
description: 'hash too long'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
base58check: base58.encode(Buffer.concat([new Buffer([0x01]), hash, checksum])),
|
|
|
|
description: 'bad version byte',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
base58check: base58.encode(Buffer.concat([new Buffer([0x05]), randomBuf(20), checksum])),
|
|
|
|
description: 'bad payload',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
base58check: base58.encode(Buffer.concat([new Buffer([0x05]), hash, randomBuf(4)])),
|
|
|
|
description: 'bad SHA256 checksum',
|
|
|
|
}
|
2014-05-08 05:23:31 +02:00
|
|
|
],
|
|
|
|
invalid2: invalid2
|
|
|
|
}, null, ' ')
|
2014-05-05 07:46:00 +02:00
|
|
|
|
2014-05-18 11:47:39 +02:00
|
|
|
fs.writeFileSync('./test/fixtures/base58check.json', fixture)
|
2014-05-08 05:23:31 +02:00
|
|
|
})
|
2014-05-05 07:46:00 +02:00
|
|
|
})
|