Isolates Base58Check encoding from Base58 code/tests
This commit is contained in:
parent
4f578dd5c1
commit
f53b821cc9
5 changed files with 198 additions and 98 deletions
|
@ -1,66 +1,65 @@
|
||||||
// https://en.bitcoin.it/wiki/Base58Check_encoding
|
// Base58 encoding/decoding
|
||||||
|
// Originally written by Mike Hearn for BitcoinJ
|
||||||
|
// Copyright (c) 2011 Google Inc
|
||||||
|
// Ported to JavaScript by Stefan Thomas
|
||||||
|
|
||||||
var BigInteger = require('./jsbn/jsbn')
|
var BigInteger = require('./jsbn/jsbn')
|
||||||
var Crypto = require('crypto-js')
|
|
||||||
var convert = require('./convert')
|
|
||||||
var SHA256 = Crypto.SHA256
|
|
||||||
|
|
||||||
|
// FIXME: ? This is a Base58Check alphabet
|
||||||
var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||||
var base = BigInteger.valueOf(58)
|
var base = BigInteger.valueOf(58)
|
||||||
|
|
||||||
var positions = {}
|
var alphabetMap = {}
|
||||||
for (var i=0; i<alphabet.length; ++i) {
|
for (var i=0; i<alphabet.length; ++i) {
|
||||||
positions[alphabet[i]] = i
|
var chr = alphabet[i]
|
||||||
|
alphabetMap[chr] = BigInteger.valueOf(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert a byte array to a base58-encoded string.
|
// encode a byte array into a base58 encoded String
|
||||||
// Written by Mike Hearn for BitcoinJ.
|
// @return String
|
||||||
// Copyright (c) 2011 Google Inc.
|
function encode(buffer) {
|
||||||
// Ported to JavaScript by Stefan Thomas.
|
var bi = BigInteger.fromByteArrayUnsigned(buffer)
|
||||||
function encode(input) {
|
|
||||||
var bi = BigInteger.fromByteArrayUnsigned(input)
|
|
||||||
var chars = []
|
var chars = []
|
||||||
|
|
||||||
while (bi.compareTo(base) >= 0) {
|
while (bi.compareTo(base) >= 0) {
|
||||||
var mod = bi.mod(base)
|
var mod = bi.mod(base)
|
||||||
chars.push(alphabet[mod.intValue()])
|
|
||||||
bi = bi.subtract(mod).divide(base)
|
bi = bi.subtract(mod).divide(base)
|
||||||
|
|
||||||
|
chars.push(alphabet[mod.intValue()])
|
||||||
}
|
}
|
||||||
|
|
||||||
chars.push(alphabet[bi.intValue()])
|
chars.push(alphabet[bi.intValue()])
|
||||||
|
|
||||||
// Convert leading zeros too.
|
// Convert leading zeros too.
|
||||||
for (var i=0; i<input.length; i++) {
|
for (var i=0; i<buffer.length; i++) {
|
||||||
if (input[i] == 0x00) {
|
if (buffer[i] !== 0x00) break
|
||||||
chars.push(alphabet[0])
|
|
||||||
} else break
|
chars.push(alphabet[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
return chars.reverse().join('')
|
return chars.reverse().join('')
|
||||||
}
|
}
|
||||||
|
|
||||||
// decode a base58 string into a byte array
|
// decode a base58 encoded String into a byte array
|
||||||
// input should be a base58 encoded string
|
|
||||||
// @return Array
|
// @return Array
|
||||||
function decode(input) {
|
function decode(str) {
|
||||||
var base = BigInteger.valueOf(58)
|
|
||||||
|
|
||||||
var length = input.length
|
|
||||||
var num = BigInteger.valueOf(0)
|
var num = BigInteger.valueOf(0)
|
||||||
|
|
||||||
var leading_zero = 0
|
var leading_zero = 0
|
||||||
var seen_other = false
|
var seen_other = false
|
||||||
for (var i=0; i<length; ++i) {
|
|
||||||
var chr = input[i]
|
for (var i=0; i<str.length; ++i) {
|
||||||
var p = positions[chr]
|
var chr = str[i]
|
||||||
|
var bi = alphabetMap[chr]
|
||||||
|
|
||||||
// if we encounter an invalid character, decoding fails
|
// if we encounter an invalid character, decoding fails
|
||||||
if (p === undefined) {
|
if (bi === undefined) {
|
||||||
throw new Error('invalid base58 string: ' + input)
|
throw new Error('invalid base58 string: ' + str)
|
||||||
}
|
}
|
||||||
|
|
||||||
num = num.multiply(base).add(BigInteger.valueOf(p))
|
num = num.multiply(base).add(bi)
|
||||||
|
|
||||||
if (chr == '1' && !seen_other) {
|
if (chr === '1' && !seen_other) {
|
||||||
++leading_zero
|
++leading_zero
|
||||||
} else {
|
} else {
|
||||||
seen_other = true
|
seen_other = true
|
||||||
|
@ -74,41 +73,10 @@ function decode(input) {
|
||||||
bytes.unshift(0)
|
bytes.unshift(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytes
|
return new Buffer(bytes)
|
||||||
}
|
|
||||||
|
|
||||||
function checkEncode(input, vbyte) {
|
|
||||||
vbyte = vbyte || 0
|
|
||||||
|
|
||||||
var front = [vbyte].concat(input)
|
|
||||||
return encode(front.concat(getChecksum(front)))
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkDecode(input) {
|
|
||||||
var bytes = decode(input),
|
|
||||||
front = bytes.slice(0, bytes.length-4),
|
|
||||||
back = bytes.slice(bytes.length-4)
|
|
||||||
|
|
||||||
var checksum = getChecksum(front)
|
|
||||||
|
|
||||||
if ("" + checksum != "" + back) {
|
|
||||||
throw new Error("Checksum failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
var o = front.slice(1)
|
|
||||||
o.version = front[0]
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
function getChecksum(bytes) {
|
|
||||||
var wordArray = convert.bytesToWordArray(bytes)
|
|
||||||
return convert.hexToBytes(SHA256(SHA256(wordArray)).toString()).slice(0, 4)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
encode: encode,
|
encode: encode,
|
||||||
decode: decode,
|
decode: decode
|
||||||
checkEncode: checkEncode,
|
|
||||||
checkDecode: checkDecode,
|
|
||||||
getChecksum: getChecksum
|
|
||||||
}
|
}
|
||||||
|
|
53
src/base58check.js
Normal file
53
src/base58check.js
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -21,5 +21,6 @@ module.exports = {
|
||||||
ecdsa: require('./ecdsa'),
|
ecdsa: require('./ecdsa'),
|
||||||
HDWallet: require('./hdwallet.js'),
|
HDWallet: require('./hdwallet.js'),
|
||||||
base58: require('./base58'),
|
base58: require('./base58'),
|
||||||
|
base58check: require('./base58check'),
|
||||||
convert: require('./convert')
|
convert: require('./convert')
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,48 +1,50 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var base58 = require('../').base58
|
var base58 = require('../').base58
|
||||||
var convert = require('../').convert
|
|
||||||
|
|
||||||
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('validates known examples', function() {
|
it('decodes the test vectors', function() {
|
||||||
var enc = '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'
|
evec.forEach(function(x, i) {
|
||||||
var hex = '800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d'
|
var actual = base58.decode(x)
|
||||||
assert.deepEqual(base58.decode(enc), convert.hexToBytes(hex))
|
var expected = dvec[i]
|
||||||
|
|
||||||
|
assert.deepEqual(expected, actual)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('encode', function() {
|
describe('encode', function() {
|
||||||
it('handles known examples', function() {
|
it('encodes the test vectors', function() {
|
||||||
var enc = '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'
|
dvec.forEach(function(x, i) {
|
||||||
var hex = '800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d'
|
var actual = base58.encode(x)
|
||||||
assert.equal(base58.encode(convert.hexToBytes(hex)), enc)
|
var expected = evec[i]
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('checkEncode', function() {
|
assert.deepEqual(expected, actual)
|
||||||
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
|
|
||||||
|
|
||||||
assert.equal(base58.checkEncode(input, vbyte),
|
|
||||||
'92tb9mjz6q9eKZjYvLsgk87kPrMoh7BGRumSzPeUGhmigtsfrbP')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
assert.deepEqual(base58.checkDecode(input), expected)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
76
test/base58check.js
Normal file
76
test/base58check.js
Normal file
|
@ -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…
Add table
Reference in a new issue