Merge pull request #144 from dcousens/b58clean

Base58 test cleanup and extracted vectors
This commit is contained in:
Kyle Drake 2014-04-21 09:33:41 -07:00
commit 1349ab2b92
5 changed files with 237 additions and 104 deletions

View file

@ -1,5 +1,7 @@
var assert = require('assert') var assert = require('assert')
var Address = require('..').Address var Address = require('..').Address
var b58fixtures = require('./fixtures/base58')
var fixtures = require('./fixtures/address') var fixtures = require('./fixtures/address')
describe('Address', function() { describe('Address', function() {
@ -19,9 +21,9 @@ describe('Address', function() {
describe('fromBase58Check', function() { describe('fromBase58Check', function() {
it('throws on invalid base58check', function() { it('throws on invalid base58check', function() {
fixtures.malformed.forEach(function(f) { b58fixtures.invalid.forEach(function(f) {
assert.throws(function() { assert.throws(function() {
Address.fromBase58Check(f.base58check) Address.fromBase58Check(f)
}) })
}) })
}) })

View file

@ -1,49 +1,27 @@
var assert = require('assert') var assert = require('assert')
var base58 = require('../').base58 var base58 = require('../').base58
var fixtures = require('./fixtures/base58')
describe('base58', function() { describe('base58', function() {
var evec, dvec
beforeEach(function() {
// base58 encoded strings
evec = [
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAbuatmU', // 0x00 WIF
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf', // 0x01 WIF
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreQyNNN1W', // 0x7f WIF
'1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm', // uncompressed 0x01 address
'1FB8cZijTpRQp3HX8AEkNuQJBqApqfTcX7' // uncompressed 0x7f address
]
// decoded equivalent of above
dvec = [
'8000000000000000000000000000000000000000000000000000000000000000000565fba7',
'800000000000000000000000000000000000000000000000000000000000000001a85aa87e',
'80000000000000000000000000000000000000000000000000000000000000007f64046be9',
'0091b24bf9f5288532960ac687abb035127b1d28a50074ffe0',
'009b7c46977b68474e12066a370b169ec6b9b026444d210d6e'
].map(function(h) {
return new Buffer(h, 'hex')
})
})
describe('decode', function() { describe('decode', function() {
it('decodes the test vectors', function() { it('decodes the test vectors', function() {
evec.forEach(function(x, i) { fixtures.valid.forEach(function(f) {
var actual = base58.decode(x) var actual = base58.decode(f.encoded.string)
var expected = dvec[i] var expected = f.encoded.hex
assert.deepEqual(actual, expected) assert.equal(actual.toString('hex'), expected)
}) })
}) })
}) })
describe('encode', function() { describe('encode', function() {
it('encodes the test vectors', function() { it('encodes the test vectors', function() {
dvec.forEach(function(x, i) { fixtures.valid.forEach(function(f) {
var actual = base58.encode(x) var actual = base58.encode(new Buffer(f.encoded.hex, 'hex'))
var expected = evec[i] var expected = f.encoded.string
assert.deepEqual(actual, expected) assert.equal(actual, expected)
}) })
}) })
}) })

View file

@ -1,75 +1,42 @@
var assert = require('assert') var assert = require('assert')
var base58check = require('../').base58check var base58check = require('../').base58check
var fixtures = require('./fixtures/base58')
describe('base58check', function() { describe('base58check', function() {
var evec, dvec
beforeEach(function() {
function fromHex(h) { return new Buffer(h, 'hex') }
// base58check encoded strings
evec = [
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAbuatmU', // 0x00 WIF
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf', // 0x01 WIF
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreQyNNN1W', // 0x7f WIF
'1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm', // uncompressed 0x01 address
'1FB8cZijTpRQp3HX8AEkNuQJBqApqfTcX7' // uncompressed 0x7f address
]
// decoded equivalent of above
dvec = [
{
version: 0x80,
payload: '0000000000000000000000000000000000000000000000000000000000000000',
checksum: '0565fba7'
},
{
version: 0x80,
payload: '0000000000000000000000000000000000000000000000000000000000000001',
checksum: 'a85aa87e',
},
{
version: 0x80,
payload: '000000000000000000000000000000000000000000000000000000000000007f',
checksum: '64046be9',
},
{
version: 0x00,
payload: '91b24bf9f5288532960ac687abb035127b1d28a5',
checksum: '0074ffe0',
},
{
version: 0x00,
payload: '9b7c46977b68474e12066a370b169ec6b9b02644',
checksum: '4d210d6e'
}
].map(function(x) {
return {
version: x.version,
payload: fromHex(x.payload),
checksum: fromHex(x.checksum)
}
})
})
describe('decode', function() { describe('decode', function() {
it('decodes the test vectors', function() { it('decodes the test vectors', function() {
evec.forEach(function(x, i) { fixtures.valid.forEach(function(f) {
var actual = base58check.decode(x) var actual = base58check.decode(f.encoded.string)
var expected = dvec[i] var expected = f.decoded
assert.deepEqual(actual, expected) assert.deepEqual({
version: actual.version,
payload: actual.payload.toString('hex'),
checksum: actual.checksum.toString('hex')
}, expected)
})
})
it('throws on invalid strings', function() {
fixtures.invalid.forEach(function(f) {
assert.throws(function() {
base58check.decode(f)
})
}) })
}) })
}) })
describe('encode', function() { describe('encode', function() {
it('encodes the test vectors', function() { it('encodes the test vectors', function() {
dvec.forEach(function(x, i) { fixtures.valid.forEach(function(f) {
var actual = base58check.encode(x.payload, x.version) var actual = base58check.encode(
var expected = evec[i] new Buffer(f.decoded.payload, 'hex'),
f.decoded.version
)
var expected = f.encoded.string
assert.deepEqual(actual, expected) assert.equal(actual, expected)
}) })
}) })
}) })

View file

@ -26,17 +26,5 @@ module.exports = {
hex: 'cd7b44d0b03f2d026d1e586d7ae18903b0d385f6', hex: 'cd7b44d0b03f2d026d1e586d7ae18903b0d385f6',
base58check: '2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w' base58check: '2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w'
} }
],
malformed: [
'45k2PvUfZw',
'8cVHMKGRJGMEVz',
'AMPCMAGBmj9EE9oGED',
'oJPsqvHTSFFWMcmNS3aDidZexw',
'bpiuHmqwCdiHx4ASNLGvZeBw9taY',
'2ansc1MsREU2HetNdPGs2eHXTY16ircdyaH',
'iTKsHH39ooQPFxzX6RFtjPESpQ1',
'4TU74v3jnoTZGV5UuJGcr7XRg7hU',
'2a3wk37F1YmfqVtBam4gEn63oNuj',
'3rtH2aquyk4q1KGaXuiMGxaGfVPH'
] ]
} }

198
test/fixtures/base58.js vendored Normal file
View file

@ -0,0 +1,198 @@
module.exports = {
valid: [
{
encoded: {
hex: '800000000000000000000000000000000000000000000000000000000000000001014671fc3f',
string: 'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn'
},
decoded: {
version: 128,
payload: '000000000000000000000000000000000000000000000000000000000000000101',
checksum: '4671fc3f'
}
},
{
encoded: {
hex: '00751e76e8199196d454941c45d1b3a323f1433bd6510d1634',
string: '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH'
},
decoded: {
version: 0,
payload: '751e76e8199196d454941c45d1b3a323f1433bd6',
checksum: '510d1634'
}
},
{
encoded: {
hex: '80000000000000000000000000000000007777777777777777777777777777777701f0845454',
string: 'KwDiBf89QgGbjEhKnhXJuH7UChJaB4ZGvCvAAf7taDvRnbQnjxps'
},
decoded: {
version: 128,
payload: '000000000000000000000000000000007777777777777777777777777777777701',
checksum: 'f0845454'
}
},
{
encoded: {
hex: '006b420cc35afb4d96ea118dbf1fc8ef4b0990798cb4f216d9',
string: '1An8RkWzqVF9GvpQFVX5AM4jpFe43eoPfe'
},
decoded: {
version: 0,
payload: '6b420cc35afb4d96ea118dbf1fc8ef4b0990798c',
checksum: 'b4f216d9'
}
},
{
encoded: {
hex: '80fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036414001608f2ef0',
string: 'L5oLkpV3aqBjhki6LmvChTCV6odsp4SXM6FfU2Gppt5kFLaHLuZ9'
},
decoded: {
version: 128,
payload: 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036414001',
checksum: '608f2ef0'
}
},
{
encoded: {
hex: '00adde4c73c7b9cee17da6c7b3e2b2eea1a0dcbe672c2b0787',
string: '1GrLCmVQXoyJXaPJQdqssNqwxvha1eUo2E'
},
decoded: {
version: 0,
payload: 'adde4c73c7b9cee17da6c7b3e2b2eea1a0dcbe67',
checksum: '2c2b0787'
}
},
{
encoded: {
hex: '806685bb38cf8d16d22741f00140622fccf3ab21fe6804d433140a4cf301e6b4dd01c5de2a2f',
string: 'Kzf11e3pDUh5ZRjcQKwvT3BeA25X5ErdccGatoTx8WJi6f49qzrr'
},
decoded: {
version: 128,
payload: '6685bb38cf8d16d22741f00140622fccf3ab21fe6804d433140a4cf301e6b4dd01',
checksum: 'c5de2a2f'
}
},
{
encoded: {
hex: '00cce94f5f00db418576b78c6a4dae2366fae89638d0b3efb8',
string: '1KgULoNDuBeHovVskY3MAfNFonzBMETt6B'
},
decoded: {
version: 0,
payload: 'cce94f5f00db418576b78c6a4dae2366fae89638',
checksum: 'd0b3efb8'
}
},
{
encoded: {
hex: 'ef00000000000000000000000000000000000000000000000000000000000000010184e38d1f',
string: 'cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87JcbXMTcA'
},
decoded: {
version: 239,
payload: '000000000000000000000000000000000000000000000000000000000000000101',
checksum: '84e38d1f'
}
},
{
encoded: {
hex: '6f751e76e8199196d454941c45d1b3a323f1433bd655c484e3',
string: 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r'
},
decoded: {
version: 111,
payload: '751e76e8199196d454941c45d1b3a323f1433bd6',
checksum: '55c484e3'
}
},
{
encoded: {
hex: 'ef0000000000000000000000000000000077777777777777777777777777777777014a7bd246',
string: 'cMahea7zqjxrtgAbB7LSGbcXpvbyqWexzF4dH5aQ5LaS3LRBaSUV'
},
decoded: {
version: 239,
payload: '000000000000000000000000000000007777777777777777777777777777777701',
checksum: '4a7bd246'
}
},
{
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'
}
}
],
invalid: [
'fN2C5x86J',
'BiLLNZvcHTJWoL',
'MP3zZBT27cEeP4asJD',
'kfjbP4kdqZcZ5tThHpxmgWQ8vk',
'oM6DB1STJKkv9NtPs2vHer6yUBh',
'wkC5tvzZHuKmnrWwVMioYueXCSx',
'BoW61uTv9aHNZtfZzcGXMSJvuc3',
'HhRv5VFtWmf6y28fWUC4WijZ6UV',
'2QPqahfnSzPRScGyaCSFL8JUZkar',
'2RpPJRJyxYz1MTShU2gV8Gy1oDdM',
'2p1tshmtxKWeovXFe68Qu6wNFNqg',
'2bYahuZyEDrw12USasbLdxGfkDH9',
'2Pfp453t1TEkj116QEDaW5hkmf3y',
'47aZqGXrM3XzEy5mWs3wC4guA7hJ',
'BUaVB8rxkTyTPntSRV4sw32vLKUZS',
'2S7rqjHw5yypNZ7WrCCAr9HCQ9Va46WJnNb'
]
}