From 540b70ccca114fd9953b5b6656ee3cea9d0b4242 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 21 Apr 2014 06:19:49 +1000 Subject: [PATCH 1/3] Adds script to create Bitcoin-core compatible test fixtures --- qa/coretests.js | 128 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 qa/coretests.js diff --git a/qa/coretests.js b/qa/coretests.js new file mode 100644 index 0000000..e2d951e --- /dev/null +++ b/qa/coretests.js @@ -0,0 +1,128 @@ +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') } +function randomBuf(s) { + return new Buffer(secureRandom(s)) +} + +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) + + var data = JSON.parse(body) + var valid = data.map(function(x) { + 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 ' +// }) + + var fixtureJSON = JSON.stringify({ + 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, ' ') + + fs.writeFileSync('./test/fixtures/base58.js', 'module.exports = ' + fixtureJSON) +}) + +request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/base58_keys_valid.json', function (error, response, body) { +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) + + var validData = JSON.parse(body) + var invalidData = JSON.parse(body2) + + var valid = validData.map(function(x) { + var string = x[0] + var hex = x[1] + var params = x[2] + + if (params.isCompressed) { + hex += '01' + } + assert.equal(b2h(base58check.decode(string).payload), hex) + + return { + string: string, + decode: { + version: base58check.decode(string).version, + payload: hex, + checksum: b2h(base58check.decode(string).checksum), + } + } + }) + var invalid2 = invalidData.map(function(x) { return x[0] }) + + // Our own tests + var hash = crypto.hash160(randomBuf(65)) + var checksum = base58check.decode(base58check.encode(hash)).checksum + + var fixtureJSON = JSON.stringify({ + valid: valid, + invalid: [ + { + 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', + } + ], + invalid2: invalid2 + }, null, ' ') + + fs.writeFileSync('./test/fixtures/base58check.js', 'module.exports = ' + fixtureJSON) +}) +}) From c1788043566711b9d47ebe3c80bfb720a30b38f5 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 21 Apr 2014 05:45:55 +1000 Subject: [PATCH 2/3] Updates test fixtures to use bitcoin core test data --- test/base58.js | 28 +- test/base58check.js | 46 ++-- test/fixtures/base58.js | 218 ++++------------ test/fixtures/base58check.js | 478 +++++++++++++++++++++++++++++++++++ 4 files changed, 571 insertions(+), 199 deletions(-) create mode 100644 test/fixtures/base58check.js diff --git a/test/base58.js b/test/base58.js index c5778e6..93f2c1c 100644 --- a/test/base58.js +++ b/test/base58.js @@ -1,27 +1,37 @@ var assert = require('assert') var base58 = require('../').base58 - var fixtures = require('./fixtures/base58') +function b2h(b) { return new Buffer(b).toString('hex') } +function h2b(h) { return new Buffer(h, 'hex') } + describe('base58', function() { describe('decode', function() { - it('decodes the test vectors', function() { + it('can decode Bitcoin core test data', function() { fixtures.valid.forEach(function(f) { - var actual = base58.decode(f.encoded.string) - var expected = f.encoded.hex + var actual = base58.decode(f.string) + var expected = f.hex - assert.equal(actual.toString('hex'), expected) + assert.strictEqual(b2h(actual), expected) + }) + }) + + fixtures.invalid.forEach(function(f) { + it('throws on ' + f.description, function() { + assert.throws(function() { + base58.decode(f.string) + }) }) }) }) describe('encode', function() { - it('encodes the test vectors', function() { + it('can encode Bitcoin core test data', function() { fixtures.valid.forEach(function(f) { - var actual = base58.encode(new Buffer(f.encoded.hex, 'hex')) - var expected = f.encoded.string + var actual = base58.encode(h2b(f.hex)) + var expected = f.string.trim() - assert.equal(actual, expected) + assert.strictEqual(actual, expected) }) }) }) diff --git a/test/base58check.js b/test/base58check.js index 110f988..e255c85 100644 --- a/test/base58check.js +++ b/test/base58check.js @@ -1,43 +1,51 @@ var assert = require('assert') var base58check = require('../').base58check +var fixtures = require('./fixtures/base58check') -var fixtures = require('./fixtures/base58') +function b2h(b) { return new Buffer(b).toString('hex') } +function h2b(h) { return new Buffer(h, 'hex') } describe('base58check', function() { describe('decode', function() { - it('decodes the test vectors', function() { + it('can decode Bitcoin core test data', function() { fixtures.valid.forEach(function(f) { - var actual = base58check.decode(f.encoded.string) - var expected = f.decoded + var actual = base58check.decode(f.string) + var expected = { + version: f.decode.version, + payload: h2b(f.decode.payload), + checksum: h2b(f.decode.checksum) + } - assert.deepEqual({ - version: actual.version, - payload: actual.payload.toString('hex'), - checksum: actual.checksum.toString('hex') - }, expected) + assert.deepEqual(actual, expected) }) }) - it('throws on invalid strings', function() { - fixtures.invalid.forEach(function(f) { + fixtures.invalid.forEach(function(f) { + it('throws on ' + f.description, function() { assert.throws(function() { - base58check.decode(f) + base58check.decode(f.string) + }) + }) + }) + + it('throws on [invalid] Bitcoin core test data', function() { + fixtures.invalid2.forEach(function(f) { + assert.throws(function() { + base58check.decode(f.string) }) }) }) }) describe('encode', function() { - it('encodes the test vectors', function() { + it('can encode Bitcoin core test data', function() { fixtures.valid.forEach(function(f) { - var actual = base58check.encode( - new Buffer(f.decoded.payload, 'hex'), - f.decoded.version - ) - var expected = f.encoded.string + var actual = base58check.encode(h2b(f.decode.payload), f.decode.version) + var expected = f.string - assert.equal(actual, expected) + assert.strictEqual(actual, expected) }) }) }) }) + diff --git a/test/fixtures/base58.js b/test/fixtures/base58.js index e9dfb8b..d19683d 100644 --- a/test/fixtures/base58.js +++ b/test/fixtures/base58.js @@ -1,198 +1,74 @@ module.exports = { - valid: [ + "valid": [ { - encoded: { - hex: '800000000000000000000000000000000000000000000000000000000000000001014671fc3f', - string: 'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn' - }, - decoded: { - version: 128, - payload: '000000000000000000000000000000000000000000000000000000000000000101', - checksum: '4671fc3f' - } + "hex": "", + "string": "" }, { - encoded: { - hex: '00751e76e8199196d454941c45d1b3a323f1433bd6510d1634', - string: '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH' - }, - decoded: { - version: 0, - payload: '751e76e8199196d454941c45d1b3a323f1433bd6', - checksum: '510d1634' - } + "hex": "61", + "string": "2g" }, { - encoded: { - hex: '80000000000000000000000000000000007777777777777777777777777777777701f0845454', - string: 'KwDiBf89QgGbjEhKnhXJuH7UChJaB4ZGvCvAAf7taDvRnbQnjxps' - }, - decoded: { - version: 128, - payload: '000000000000000000000000000000007777777777777777777777777777777701', - checksum: 'f0845454' - } + "hex": "626262", + "string": "a3gV" }, { - encoded: { - hex: '006b420cc35afb4d96ea118dbf1fc8ef4b0990798cb4f216d9', - string: '1An8RkWzqVF9GvpQFVX5AM4jpFe43eoPfe' - }, - decoded: { - version: 0, - payload: '6b420cc35afb4d96ea118dbf1fc8ef4b0990798c', - checksum: 'b4f216d9' - } + "hex": "636363", + "string": "aPEr" }, { - encoded: { - hex: '80fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036414001608f2ef0', - string: 'L5oLkpV3aqBjhki6LmvChTCV6odsp4SXM6FfU2Gppt5kFLaHLuZ9' - }, - decoded: { - version: 128, - payload: 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036414001', - checksum: '608f2ef0' - } + "hex": "73696d706c792061206c6f6e6720737472696e67", + "string": "2cFupjhnEsSn59qHXstmK2ffpLv2" }, { - encoded: { - hex: '00adde4c73c7b9cee17da6c7b3e2b2eea1a0dcbe672c2b0787', - string: '1GrLCmVQXoyJXaPJQdqssNqwxvha1eUo2E' - }, - decoded: { - version: 0, - payload: 'adde4c73c7b9cee17da6c7b3e2b2eea1a0dcbe67', - checksum: '2c2b0787' - } + "hex": "00eb15231dfceb60925886b67d065299925915aeb172c06647", + "string": "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L" }, { - encoded: { - hex: '806685bb38cf8d16d22741f00140622fccf3ab21fe6804d433140a4cf301e6b4dd01c5de2a2f', - string: 'Kzf11e3pDUh5ZRjcQKwvT3BeA25X5ErdccGatoTx8WJi6f49qzrr' - }, - decoded: { - version: 128, - payload: '6685bb38cf8d16d22741f00140622fccf3ab21fe6804d433140a4cf301e6b4dd01', - checksum: 'c5de2a2f' - } + "hex": "516b6fcd0f", + "string": "ABnLTmg" }, { - encoded: { - hex: '00cce94f5f00db418576b78c6a4dae2366fae89638d0b3efb8', - string: '1KgULoNDuBeHovVskY3MAfNFonzBMETt6B' - }, - decoded: { - version: 0, - payload: 'cce94f5f00db418576b78c6a4dae2366fae89638', - checksum: 'd0b3efb8' - } + "hex": "bf4f89001e670274dd", + "string": "3SEo3LWLoPntC" }, { - encoded: { - hex: 'ef00000000000000000000000000000000000000000000000000000000000000010184e38d1f', - string: 'cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87JcbXMTcA' - }, - decoded: { - version: 239, - payload: '000000000000000000000000000000000000000000000000000000000000000101', - checksum: '84e38d1f' - } + "hex": "572e4794", + "string": "3EFU7m" }, { - encoded: { - hex: '6f751e76e8199196d454941c45d1b3a323f1433bd655c484e3', - string: 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r' - }, - decoded: { - version: 111, - payload: '751e76e8199196d454941c45d1b3a323f1433bd6', - checksum: '55c484e3' - } + "hex": "ecac89cad93923c02321", + "string": "EJDM8drfXA6uyA" }, { - encoded: { - hex: 'ef0000000000000000000000000000000077777777777777777777777777777777014a7bd246', - string: 'cMahea7zqjxrtgAbB7LSGbcXpvbyqWexzF4dH5aQ5LaS3LRBaSUV' - }, - decoded: { - version: 239, - payload: '000000000000000000000000000000007777777777777777777777777777777701', - checksum: '4a7bd246' - } + "hex": "10c8511e", + "string": "Rt5zm" }, { - encoded: { - hex: '6f6b420cc35afb4d96ea118dbf1fc8ef4b0990798cc00f9280', - string: 'mqJ5iobyeWgQ43J1y4VSzGH4gFEkxw2dFV' - }, - decoded: { - version: 111, - payload: '6b420cc35afb4d96ea118dbf1fc8ef4b0990798c', - checksum: 'c00f9280' - } - }, - { - encoded: { - hex: 'effffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd03641400143ef4bf2', - string: 'cWALDjUu1tszsCBMjBjL4mhYj2wHUWYDR8Q8aSjLKzjkW5eBtpzu' - }, - decoded: { - version: 239, - payload: 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036414001', - checksum: '43ef4bf2' - } - }, - { - encoded: { - hex: '6fadde4c73c7b9cee17da6c7b3e2b2eea1a0dcbe670a342a51', - string: 'mwNHVpaPLqQZJgrv8CpFhJ4GpvJGumskXi' - }, - decoded: { - version: 111, - payload: 'adde4c73c7b9cee17da6c7b3e2b2eea1a0dcbe67', - checksum: '0a342a51' - } - }, - { - encoded: { - hex: 'ef6685bb38cf8d16d22741f00140622fccf3ab21fe6804d433140a4cf301e6b4dd0124df07e1', - string: 'cR1zUZ3feYPLisCsnjm3pMghnFNvjgxKgeR41DvTdcxiMQ4g9ZYC' - }, - decoded: { - version: 239, - payload: '6685bb38cf8d16d22741f00140622fccf3ab21fe6804d433140a4cf301e6b4dd01', - checksum: '24df07e1' - } - }, - { - encoded: { - hex: '6fcce94f5f00db418576b78c6a4dae2366fae8963831693070', - string: 'mzCRdrTCiD5Yb2yVU71izaaafnatCA4DCT' - }, - decoded: { - version: 111, - payload: 'cce94f5f00db418576b78c6a4dae2366fae89638', - checksum: '31693070' - } + "hex": "00000000000000000000", + "string": "1111111111" } ], - invalid: [ - 'fN2C5x86J', - 'BiLLNZvcHTJWoL', - 'MP3zZBT27cEeP4asJD', - 'kfjbP4kdqZcZ5tThHpxmgWQ8vk', - 'oM6DB1STJKkv9NtPs2vHer6yUBh', - 'wkC5tvzZHuKmnrWwVMioYueXCSx', - 'BoW61uTv9aHNZtfZzcGXMSJvuc3', - 'HhRv5VFtWmf6y28fWUC4WijZ6UV', - '2QPqahfnSzPRScGyaCSFL8JUZkar', - '2RpPJRJyxYz1MTShU2gV8Gy1oDdM', - '2p1tshmtxKWeovXFe68Qu6wNFNqg', - '2bYahuZyEDrw12USasbLdxGfkDH9', - '2Pfp453t1TEkj116QEDaW5hkmf3y', - '47aZqGXrM3XzEy5mWs3wC4guA7hJ', - 'BUaVB8rxkTyTPntSRV4sw32vLKUZS', - '2S7rqjHw5yypNZ7WrCCAr9HCQ9Va46WJnNb' + "invalid": [ + { + "description": "non-base58 string", + "string": "invalid" + }, + { + "description": "non-base58 alphabet", + "string": "c2F0b3NoaQo=" + }, + { + "description": "leading whitespace", + "string": " 1111111111" + }, + { + "description": "trailing whitespace", + "string": "1111111111 " + }, + { + "description": "unexpected character after whitespace", + "string": " \t\n\u000b\f\r skip \r\f\u000b\n\t a" + } ] -} +} \ No newline at end of file diff --git a/test/fixtures/base58check.js b/test/fixtures/base58check.js new file mode 100644 index 0000000..cf0781d --- /dev/null +++ b/test/fixtures/base58check.js @@ -0,0 +1,478 @@ +module.exports = { + "valid": [ + { + "string": "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", + "decode": { + "version": 0, + "payload": "65a16059864a2fdbc7c99a4723a8395bc6f188eb", + "checksum": "c046b2ff" + } + }, + { + "string": "3CMNFxN1oHBc4R1EpboAL5yzHGgE611Xou", + "decode": { + "version": 5, + "payload": "74f209f6ea907e2ea48f74fae05782ae8a665257", + "checksum": "a2353eb8" + } + }, + { + "string": "mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", + "decode": { + "version": 111, + "payload": "53c0307d6851aa0ce7825ba883c6bd9ad242b486", + "checksum": "3aa11ed6" + } + }, + { + "string": "2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", + "decode": { + "version": 196, + "payload": "6349a418fc4578d10a372b54b45c280cc8c4382f", + "checksum": "c4a2b951" + } + }, + { + "string": "5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr", + "decode": { + "version": 128, + "payload": "eddbdc1168f1daeadbd3e44c1e3f8f5a284c2029f78ad26af98583a499de5b19", + "checksum": "13a4f863" + } + }, + { + "string": "Kz6UJmQACJmLtaQj5A3JAge4kVTNQ8gbvXuwbmCj7bsaabudb3RD", + "decode": { + "version": 128, + "payload": "55c9bccb9ed68446d1b75273bbce89d7fe013a8acd1625514420fb2aca1a21c401", + "checksum": "99a16fd4" + } + }, + { + "string": "9213qJab2HNEpMpYNBa7wHGFKKbkDn24jpANDs2huN3yi4J11ko", + "decode": { + "version": 239, + "payload": "36cb93b9ab1bdabf7fb9f2c04f1b9cc879933530ae7842398eef5a63a56800c2", + "checksum": "6a84391c" + } + }, + { + "string": "cTpB4YiyKiBcPxnefsDpbnDxFDffjqJob8wGCEDXxgQ7zQoMXJdH", + "decode": { + "version": 239, + "payload": "b9f4892c9e8282028fea1d2667c4dc5213564d41fc5783896a0d843fc15089f301", + "checksum": "b008368c" + } + }, + { + "string": "1Ax4gZtb7gAit2TivwejZHYtNNLT18PUXJ", + "decode": { + "version": 0, + "payload": "6d23156cbbdcc82a5a47eee4c2c7c583c18b6bf4", + "checksum": "0680e989" + } + }, + { + "string": "3QjYXhTkvuj8qPaXHTTWb5wjXhdsLAAWVy", + "decode": { + "version": 5, + "payload": "fcc5460dd6e2487c7d75b1963625da0e8f4c5975", + "checksum": "8b495dfc" + } + }, + { + "string": "n3ZddxzLvAY9o7184TB4c6FJasAybsw4HZ", + "decode": { + "version": 111, + "payload": "f1d470f9b02370fdec2e6b708b08ac431bf7a5f7", + "checksum": "b663687c" + } + }, + { + "string": "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n", + "decode": { + "version": 196, + "payload": "c579342c2c4c9220205e2cdc285617040c924a0a", + "checksum": "ba7d11ad" + } + }, + { + "string": "5K494XZwps2bGyeL71pWid4noiSNA2cfCibrvRWqcHSptoFn7rc", + "decode": { + "version": 128, + "payload": "a326b95ebae30164217d7a7f57d72ab2b54e3be64928a19da0210b9568d4015e", + "checksum": "171a2dfd" + } + }, + { + "string": "L1RrrnXkcKut5DEMwtDthjwRcTTwED36thyL1DebVrKuwvohjMNi", + "decode": { + "version": 128, + "payload": "7d998b45c219a1e38e99e7cbd312ef67f77a455a9b50c730c27f02c6f730dfb401", + "checksum": "cd40f1cb" + } + }, + { + "string": "93DVKyFYwSN6wEo3E2fCrFPUp17FtrtNi2Lf7n4G3garFb16CRj", + "decode": { + "version": 239, + "payload": "d6bca256b5abc5602ec2e1c121a08b0da2556587430bcf7e1898af2224885203", + "checksum": "f0bf192e" + } + }, + { + "string": "cTDVKtMGVYWTHCb1AFjmVbEbWjvKpKqKgMaR3QJxToMSQAhmCeTN", + "decode": { + "version": 239, + "payload": "a81ca4e8f90181ec4b61b6a7eb998af17b2cb04de8a03b504b9e34c4c61db7d901", + "checksum": "7f570e65" + } + }, + { + "string": "1C5bSj1iEGUgSTbziymG7Cn18ENQuT36vv", + "decode": { + "version": 0, + "payload": "7987ccaa53d02c8873487ef919677cd3db7a6912", + "checksum": "11a2e19b" + } + }, + { + "string": "3AnNxabYGoTxYiTEZwFEnerUoeFXK2Zoks", + "decode": { + "version": 5, + "payload": "63bcc565f9e68ee0189dd5cc67f1b0e5f02f45cb", + "checksum": "35e6c138" + } + }, + { + "string": "n3LnJXCqbPjghuVs8ph9CYsAe4Sh4j97wk", + "decode": { + "version": 111, + "payload": "ef66444b5b17f14e8fae6e7e19b045a78c54fd79", + "checksum": "7d48bb7f" + } + }, + { + "string": "2NB72XtkjpnATMggui83aEtPawyyKvnbX2o", + "decode": { + "version": 196, + "payload": "c3e55fceceaa4391ed2a9677f4a4d34eacd021a0", + "checksum": "03bf5e60" + } + }, + { + "string": "5KaBW9vNtWNhc3ZEDyNCiXLPdVPHCikRxSBWwV9NrpLLa4LsXi9", + "decode": { + "version": 128, + "payload": "e75d936d56377f432f404aabb406601f892fd49da90eb6ac558a733c93b47252", + "checksum": "135d832a" + } + }, + { + "string": "L1axzbSyynNYA8mCAhzxkipKkfHtAXYF4YQnhSKcLV8YXA874fgT", + "decode": { + "version": 128, + "payload": "8248bd0375f2f75d7e274ae544fb920f51784480866b102384190b1addfbaa5c01", + "checksum": "2daf30c0" + } + }, + { + "string": "927CnUkUbasYtDwYwVn2j8GdTuACNnKkjZ1rpZd2yBB1CLcnXpo", + "decode": { + "version": 239, + "payload": "44c4f6a096eac5238291a94cc24c01e3b19b8d8cef72874a079e00a242237a52", + "checksum": "bc446ce4" + } + }, + { + "string": "cUcfCMRjiQf85YMzzQEk9d1s5A4K7xL5SmBCLrezqXFuTVefyhY7", + "decode": { + "version": 239, + "payload": "d1de707020a9059d6d3abaf85e17967c6555151143db13dbb06db78df0f15c6901", + "checksum": "db1645ec" + } + }, + { + "string": "1Gqk4Tv79P91Cc1STQtU3s1W6277M2CVWu", + "decode": { + "version": 0, + "payload": "adc1cc2081a27206fae25792f28bbc55b831549d", + "checksum": "4c762f7e" + } + }, + { + "string": "33vt8ViH5jsr115AGkW6cEmEz9MpvJSwDk", + "decode": { + "version": 5, + "payload": "188f91a931947eddd7432d6e614387e32b244709", + "checksum": "36a8fc53" + } + }, + { + "string": "mhaMcBxNh5cqXm4aTQ6EcVbKtfL6LGyK2H", + "decode": { + "version": 111, + "payload": "1694f5bc1a7295b600f40018a618a6ea48eeb498", + "checksum": "047e9722" + } + }, + { + "string": "2MxgPqX1iThW3oZVk9KoFcE5M4JpiETssVN", + "decode": { + "version": 196, + "payload": "3b9b3fd7a50d4f08d1a5b0f62f644fa7115ae2f3", + "checksum": "0fd6ecc5" + } + }, + { + "string": "5HtH6GdcwCJA4ggWEL1B3jzBBUB8HPiBi9SBc5h9i4Wk4PSeApR", + "decode": { + "version": 128, + "payload": "091035445ef105fa1bb125eccfb1882f3fe69592265956ade751fd095033d8d0", + "checksum": "9c5b305a" + } + }, + { + "string": "L2xSYmMeVo3Zek3ZTsv9xUrXVAmrWxJ8Ua4cw8pkfbQhcEFhkXT8", + "decode": { + "version": 128, + "payload": "ab2b4bcdfc91d34dee0ae2a8c6b6668dadaeb3a88b9859743156f462325187af01", + "checksum": "271c74db" + } + }, + { + "string": "92xFEve1Z9N8Z641KQQS7ByCSb8kGjsDzw6fAmjHN1LZGKQXyMq", + "decode": { + "version": 239, + "payload": "b4204389cef18bbe2b353623cbf93e8678fbc92a475b664ae98ed594e6cf0856", + "checksum": "13985bb8" + } + }, + { + "string": "cVM65tdYu1YK37tNoAyGoJTR13VBYFva1vg9FLuPAsJijGvG6NEA", + "decode": { + "version": 239, + "payload": "e7b230133f1b5489843260236b06edca25f66adb1be455fbd38d4010d48faeef01", + "checksum": "45de6187" + } + }, + { + "string": "1JwMWBVLtiqtscbaRHai4pqHokhFCbtoB4", + "decode": { + "version": 0, + "payload": "c4c1b72491ede1eedaca00618407ee0b772cad0d", + "checksum": "27a58137" + } + }, + { + "string": "3QCzvfL4ZRvmJFiWWBVwxfdaNBT8EtxB5y", + "decode": { + "version": 5, + "payload": "f6fe69bcb548a829cce4c57bf6fff8af3a5981f9", + "checksum": "80242330" + } + }, + { + "string": "mizXiucXRCsEriQCHUkCqef9ph9qtPbZZ6", + "decode": { + "version": 111, + "payload": "261f83568a098a8638844bd7aeca039d5f2352c0", + "checksum": "56892155" + } + }, + { + "string": "2NEWDzHWwY5ZZp8CQWbB7ouNMLqCia6YRda", + "decode": { + "version": 196, + "payload": "e930e1834a4d234702773951d627cce82fbb5d2e", + "checksum": "3ac92331" + } + }, + { + "string": "5KQmDryMNDcisTzRp3zEq9e4awRmJrEVU1j5vFRTKpRNYPqYrMg", + "decode": { + "version": 128, + "payload": "d1fab7ab7385ad26872237f1eb9789aa25cc986bacc695e07ac571d6cdac8bc0", + "checksum": "d6f7166b" + } + }, + { + "string": "L39Fy7AC2Hhj95gh3Yb2AU5YHh1mQSAHgpNixvm27poizcJyLtUi", + "decode": { + "version": 128, + "payload": "b0bbede33ef254e8376aceb1510253fc3550efd0fcf84dcd0c9998b288f166b301", + "checksum": "e396e2cb" + } + }, + { + "string": "91cTVUcgydqyZLgaANpf1fvL55FH53QMm4BsnCADVNYuWuqdVys", + "decode": { + "version": 239, + "payload": "037f4192c630f399d9271e26c575269b1d15be553ea1a7217f0cb8513cef41cb", + "checksum": "504ffa32" + } + }, + { + "string": "cQspfSzsgLeiJGB2u8vrAiWpCU4MxUT6JseWo2SjXy4Qbzn2fwDw", + "decode": { + "version": 239, + "payload": "6251e205e8ad508bab5596bee086ef16cd4b239e0cc0c5d7c4e6035441e7d5de01", + "checksum": "070c22e6" + } + }, + { + "string": "19dcawoKcZdQz365WpXWMhX6QCUpR9SY4r", + "decode": { + "version": 0, + "payload": "5eadaf9bb7121f0f192561a5a62f5e5f54210292", + "checksum": "ef7a7be3" + } + }, + { + "string": "37Sp6Rv3y4kVd1nQ1JV5pfqXccHNyZm1x3", + "decode": { + "version": 5, + "payload": "3f210e7277c899c3a155cc1c90f4106cbddeec6e", + "checksum": "0f6e5c98" + } + }, + { + "string": "myoqcgYiehufrsnnkqdqbp69dddVDMopJu", + "decode": { + "version": 111, + "payload": "c8a3c2a09a298592c3e180f02487cd91ba3400b5", + "checksum": "30c3769a" + } + }, + { + "string": "2N7FuwuUuoTBrDFdrAZ9KxBmtqMLxce9i1C", + "decode": { + "version": 196, + "payload": "99b31df7c9068d1481b596578ddbb4d3bd90baeb", + "checksum": "02eca37f" + } + }, + { + "string": "5KL6zEaMtPRXZKo1bbMq7JDjjo1bJuQcsgL33je3oY8uSJCR5b4", + "decode": { + "version": 128, + "payload": "c7666842503db6dc6ea061f092cfb9c388448629a6fe868d068c42a488b478ae", + "checksum": "88451297" + } + }, + { + "string": "KwV9KAfwbwt51veZWNscRTeZs9CKpojyu1MsPnaKTF5kz69H1UN2", + "decode": { + "version": 128, + "payload": "07f0803fc5399e773555ab1e8939907e9badacc17ca129e67a2f5f2ff84351dd01", + "checksum": "004d964f" + } + }, + { + "string": "93N87D6uxSBzwXvpokpzg8FFmfQPmvX4xHoWQe3pLdYpbiwT5YV", + "decode": { + "version": 239, + "payload": "ea577acfb5d1d14d3b7b195c321566f12f87d2b77ea3a53f68df7ebf8604a801", + "checksum": "babac8c2" + } + }, + { + "string": "cMxXusSihaX58wpJ3tNuuUcZEQGt6DKJ1wEpxys88FFaQCYjku9h", + "decode": { + "version": 239, + "payload": "0b3b34f0958d8a268193a9814da92c3e8b58b4a4378a542863e34ac289cd830c01", + "checksum": "61073840" + } + }, + { + "string": "13p1ijLwsnrcuyqcTvJXkq2ASdXqcnEBLE", + "decode": { + "version": 0, + "payload": "1ed467017f043e91ed4c44b4e8dd674db211c4e6", + "checksum": "6cc37d7b" + } + }, + { + "string": "3ALJH9Y951VCGcVZYAdpA3KchoP9McEj1G", + "decode": { + "version": 5, + "payload": "5ece0cadddc415b1980f001785947120acdb36fc", + "checksum": "b43c48af" + } + } + ], + "invalid": [ + { + "base58check": "7SeEnXWPaCCALbVrTnszCVGfRU8cGfx", + "description": "hash too short" + }, + { + "base58check": "j9ywUkWg2fTQrouxxh5rSZhRvrjMkEUfuiKe", + "description": "hash too long" + }, + { + "base58check": "Z9inZq4e2HGQRZQezDjFMmqgUE8NwMRok", + "description": "bad version byte" + }, + { + "base58check": "3HK7MezAm6qEZQUMPRf8jX7wDv6zig6Ky8", + "description": "bad payload" + }, + { + "base58check": "3AW8j12DUk8mgA7kkfZ1BrrzCVFuH1LsXS", + "description": "bad SHA256 checksum" + } + ], + "invalid2": [ + "", + "x", + "37qgekLpCCHrQuSjvX3fs496FWTGsHFHizjJAs6NPcR47aefnnCWECAhHV6E3g4YN7u7Yuwod5Y", + "dzb7VV1Ui55BARxv7ATxAtCUeJsANKovDGWFVgpTbhq9gvPqP3yv", + "MuNu7ZAEDFiHthiunm7dPjwKqrVNCM3mAz6rP9zFveQu14YA8CxExSJTHcVP9DErn6u84E6Ej7S", + "rPpQpYknyNQ5AEHuY6H8ijJJrYc2nDKKk9jjmKEXsWzyAQcFGpDLU2Zvsmoi8JLR7hAwoy3RQWf", + "4Uc3FmN6NQ6zLBK5QQBXRBUREaaHwCZYsGCueHauuDmJpZKn6jkEskMB2Zi2CNgtb5r6epWEFfUJq", + "7aQgR5DFQ25vyXmqZAWmnVCjL3PkBcdVkBUpjrjMTcghHx3E8wb", + "17QpPprjeg69fW1DV8DcYYCKvWjYhXvWkov6MJ1iTTvMFj6weAqW7wybZeH57WTNxXVCRH4veVs", + "KxuACDviz8Xvpn1xAh9MfopySZNuyajYMZWz16Dv2mHHryznWUp3", + "7nK3GSmqdXJQtdohvGfJ7KsSmn3TmGqExug49583bDAL91pVSGq5xS9SHoAYL3Wv3ijKTit65th", + "cTivdBmq7bay3RFGEBBuNfMh2P1pDCgRYN2Wbxmgwr4ki3jNUL2va", + "gjMV4vjNjyMrna4fsAr8bWxAbwtmMUBXJS3zL4NJt5qjozpbQLmAfK1uA3CquSqsZQMpoD1g2nk", + "emXm1naBMoVzPjbk7xpeTVMFy4oDEe25UmoyGgKEB1gGWsK8kRGs", + "7VThQnNRj1o3Zyvc7XHPRrjDf8j2oivPTeDXnRPYWeYGE4pXeRJDZgf28ppti5hsHWXS2GSobdqyo", + "1G9u6oCVCPh2o8m3t55ACiYvG1y5BHewUkDSdiQarDcYXXhFHYdzMdYfUAhfxn5vNZBwpgUNpso", + "31QQ7ZMLkScDiB4VyZjuptr7AEc9j1SjstF7pRoLhHTGkW4Q2y9XELobQmhhWxeRvqcukGd1XCq", + "DHqKSnpxa8ZdQyH8keAhvLTrfkyBMQxqngcQA5N8LQ9KVt25kmGN", + "2LUHcJPbwLCy9GLH1qXmfmAwvadWw4bp4PCpDfduLqV17s6iDcy1imUwhQJhAoNoN1XNmweiJP4i", + "7USRzBXAnmck8fX9HmW7RAb4qt92VFX6soCnts9s74wxm4gguVhtG5of8fZGbNPJA83irHVY6bCos", + "1DGezo7BfVebZxAbNT3XGujdeHyNNBF3vnficYoTSp4PfK2QaML9bHzAMxke3wdKdHYWmsMTJVu", + "2D12DqDZKwCxxkzs1ZATJWvgJGhQ4cFi3WrizQ5zLAyhN5HxuAJ1yMYaJp8GuYsTLLxTAz6otCfb", + "8AFJzuTujXjw1Z6M3fWhQ1ujDW7zsV4ePeVjVo7D1egERqSW9nZ", + "163Q17qLbTCue8YY3AvjpUhotuaodLm2uqMhpYirsKjVqnxJRWTEoywMVY3NbBAHuhAJ2cF9GAZ", + "2MnmgiRH4eGLyLc9eAqStzk7dFgBjFtUCtu", + "461QQ2sYWxU7H2PV4oBwJGNch8XVTYYbZxU", + "2UCtv53VttmQYkVU4VMtXB31REvQg4ABzs41AEKZ8UcB7DAfVzdkV9JDErwGwyj5AUHLkmgZeobs", + "cSNjAsnhgtiFMi6MtfvgscMB2Cbhn2v1FUYfviJ1CdjfidvmeW6mn", + "gmsow2Y6EWAFDFE1CE4Hd3Tpu2BvfmBfG1SXsuRARbnt1WjkZnFh1qGTiptWWbjsq2Q6qvpgJVj", + "nksUKSkzS76v8EsSgozXGMoQFiCoCHzCVajFKAXqzK5on9ZJYVHMD5CKwgmX3S3c7M1U3xabUny", + "L3favK1UzFGgdzYBF2oBT5tbayCo4vtVBLJhg2iYuMeePxWG8SQc", + "7VxLxGGtYT6N99GdEfi6xz56xdQ8nP2dG1CavuXx7Rf2PrvNMTBNevjkfgs9JmkcGm6EXpj8ipyPZ", + "2mbZwFXF6cxShaCo2czTRB62WTx9LxhTtpP", + "dB7cwYdcPSgiyAwKWL3JwCVwSk6epU2txw", + "HPhFUhUAh8ZQQisH8QQWafAxtQYju3SFTX", + "4ctAH6AkHzq5ioiM1m9T3E2hiYEev5mTsB", + "Hn1uFi4dNexWrqARpjMqgT6cX1UsNPuV3cHdGg9ExyXw8HTKadbktRDtdeVmY3M1BxJStiL4vjJ", + "Sq3fDbvutABmnAHHExJDgPLQn44KnNC7UsXuT7KZecpaYDMU9Txs", + "6TqWyrqdgUEYDQU1aChMuFMMEimHX44qHFzCUgGfqxGgZNMUVWJ", + "giqJo7oWqFxNKWyrgcBxAVHXnjJ1t6cGoEffce5Y1y7u649Noj5wJ4mmiUAKEVVrYAGg2KPB3Y4", + "cNzHY5e8vcmM3QVJUcjCyiKMYfeYvyueq5qCMV3kqcySoLyGLYUK", + "37uTe568EYc9WLoHEd9jXEvUiWbq5LFLscNyqvAzLU5vBArUJA6eydkLmnMwJDjkL5kXc2VK7ig", + "EsYbG4tWWWY45G31nox838qNdzksbPySWc", + "nbuzhfwMoNzA3PaFnyLcRxE9bTJPDkjZ6Rf6Y6o2ckXZfzZzXBT", + "cQN9PoxZeCWK1x56xnz6QYAsvR11XAce3Ehp3gMUdfSQ53Y2mPzx", + "1Gm3N3rkef6iMbx4voBzaxtXcmmiMTqZPhcuAepRzYUJQW4qRpEnHvMojzof42hjFRf8PE2jPde", + "2TAq2tuN6x6m233bpT7yqdYQPELdTDJn1eU", + "ntEtnnGhqPii4joABvBtSEJG6BxjT2tUZqE8PcVYgk3RHpgxgHDCQxNbLJf7ardf1dDk2oCQ7Cf", + "Ky1YjoZNgQ196HJV3HpdkecfhRBmRZdMJk89Hi5KGfpfPwS2bUbfd", + "2A1q1YsMZowabbvta7kTy2Fd6qN4r5ZCeG3qLpvZBMzCixMUdkN2Y4dHB1wPsZAeVXUGD83MfRED" + ] +} \ No newline at end of file From fb6c76a976ca296617dec82df292cbe80b9f14d0 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 21 Apr 2014 04:44:05 +1000 Subject: [PATCH 3/3] Merges Buffer refactorings from base58-native by Stephen Pair --- src/base58.js | 89 ++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 51 deletions(-) diff --git a/src/base58.js b/src/base58.js index 3715c3b..7ba4b04 100644 --- a/src/base58.js +++ b/src/base58.js @@ -2,78 +2,65 @@ // Originally written by Mike Hearn for BitcoinJ // Copyright (c) 2011 Google Inc // Ported to JavaScript by Stefan Thomas +// Merged Buffer refactorings from base58-native by Stephen Pair +// Copyright (c) 2013 BitPay Inc var BigInteger = require('./jsbn/jsbn') -// FIXME: ? This is a Base58Check alphabet -var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" -var base = BigInteger.valueOf(58) - -var alphabetMap = {} -for (var i=0; i= 0) { - var mod = bi.mod(base) - bi = bi.subtract(mod).divide(base) + var i = result.length - 1 + while (bi.compareTo(BigInteger.ZERO) > 0) { + var remainder = bi.mod(BASE) + bi = bi.divide(BASE) - chars.push(alphabet[mod.intValue()]) + result[i] = ALPHABET_BUF[remainder.intValue()] + i-- } - chars.push(alphabet[bi.intValue()]) - - // Convert leading zeros too. - for (var i=0; i 0) { - bytes.unshift(0) + // deal with leading zeros + var i = 0 + while ((i < string.length) && (string[i] === ALPHABET[0])) { + i++ } - return new Buffer(bytes) + // FIXME: If BigInteger supported buffers, this could be a copy + var buffer = new Buffer(num.toByteArrayUnsigned()) + var padding = new Buffer(i) + padding.fill(0) + + return Buffer.concat([padding, buffer]) } module.exports = {