Daniel Cousens
11 years ago
5 changed files with 195 additions and 95 deletions
@ -0,0 +1,53 @@ |
|||
// https://en.bitcoin.it/wiki/Base58Check_encoding
|
|||
var assert = require('assert') |
|||
var base58 = require('./base58') |
|||
var crypto = require('crypto') |
|||
|
|||
function sha256(buf) { |
|||
var hash = crypto.createHash('sha256') |
|||
hash.update(buf) |
|||
|
|||
return hash.digest() |
|||
} |
|||
|
|||
// Encode a buffer as a base58-check-encoded string
|
|||
function encode(buffer, version) { |
|||
version = version || 0 |
|||
|
|||
// FIXME: `new Buffer(buffer)` is unnecessary if input is a Buffer
|
|||
var version = new Buffer([version]) |
|||
var payload = new Buffer(buffer) |
|||
|
|||
var message = Buffer.concat([version, payload]) |
|||
var checksum = sha256(sha256(message)).slice(0, 4) |
|||
|
|||
return base58.encode(Buffer.concat([ |
|||
message, |
|||
checksum |
|||
])) |
|||
} |
|||
|
|||
// Decode a base58-check-encoded string to a buffer
|
|||
function decode(string) { |
|||
var buffer = base58.decode(string) |
|||
|
|||
var message = buffer.slice(0, -4) |
|||
var checksum = buffer.slice(-4) |
|||
var newChecksum = sha256(sha256(message)).slice(0, 4) |
|||
|
|||
assert.deepEqual(newChecksum, checksum) |
|||
|
|||
var version = message.readUInt8(0) |
|||
var payload = message.slice(1) |
|||
|
|||
return { |
|||
version: version, |
|||
payload: payload, |
|||
checksum: checksum |
|||
} |
|||
} |
|||
|
|||
module.exports = { |
|||
encode: encode, |
|||
decode: decode |
|||
} |
@ -1,48 +1,50 @@ |
|||
var assert = require('assert') |
|||
var base58 = require('../').base58 |
|||
var convert = require('../').convert |
|||
|
|||
describe('base58', function() { |
|||
describe('decode', function() { |
|||
it('validates known examples', function() { |
|||
var enc = '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ' |
|||
var hex = '800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d' |
|||
assert.deepEqual(base58.decode(enc), convert.hexToBytes(hex)) |
|||
}) |
|||
}) |
|||
var evec, dvec |
|||
|
|||
describe('encode', function() { |
|||
it('handles known examples', function() { |
|||
var enc = '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ' |
|||
var hex = '800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d' |
|||
assert.equal(base58.encode(convert.hexToBytes(hex)), enc) |
|||
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('checkEncode', function() { |
|||
it('handles known examples', function() { |
|||
var input = [ |
|||
171, 210, 178, 125, 2, 16, 86, 184, 248, 88, 235, |
|||
163, 244, 160, 83, 156, 184, 186, 45, 167, 169, 164, |
|||
67, 125, 163, 89, 106, 243, 207, 193, 149, 206 |
|||
] |
|||
var vbyte = 239 |
|||
describe('decode', function() { |
|||
it('decodes the test vectors', function() { |
|||
evec.forEach(function(x, i) { |
|||
var actual = base58.decode(x) |
|||
var expected = dvec[i] |
|||
|
|||
assert.equal(base58.checkEncode(input, vbyte), |
|||
'92tb9mjz6q9eKZjYvLsgk87kPrMoh7BGRumSzPeUGhmigtsfrbP') |
|||
assert.deepEqual(expected, actual) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('checkDecode', function() { |
|||
it('handles known examples', function() { |
|||
var input = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa' |
|||
var expected = [ |
|||
98, 233, 7, 177, 92, 191, 39, 213, 66, 83, |
|||
153, 235, 246, 240, 251, 80, 235, 184, 143, 24 |
|||
] |
|||
expected.version = 0 |
|||
describe('encode', function() { |
|||
it('encodes the test vectors', function() { |
|||
dvec.forEach(function(x, i) { |
|||
var actual = base58.encode(x) |
|||
var expected = evec[i] |
|||
|
|||
assert.deepEqual(base58.checkDecode(input), expected) |
|||
assert.deepEqual(expected, actual) |
|||
}) |
|||
}) |
|||
}) |
|||
}) |
|||
|
@ -0,0 +1,76 @@ |
|||
var assert = require('assert') |
|||
var base58check = require('../').base58check |
|||
|
|||
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() { |
|||
it('decodes the test vectors', function() { |
|||
evec.forEach(function(x, i) { |
|||
var actual = base58check.decode(x) |
|||
var expected = dvec[i] |
|||
|
|||
assert.deepEqual(expected, actual) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('encode', function() { |
|||
it('encodes the test vectors', function() { |
|||
dvec.forEach(function(x, i) { |
|||
var actual = base58check.encode(x.payload, x.version) |
|||
var expected = evec[i] |
|||
|
|||
assert.deepEqual(expected, actual) |
|||
}) |
|||
}) |
|||
}) |
|||
}) |
Loading…
Reference in new issue