Browse Source

Isolates Base58Check encoding from Base58 code/tests

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
f53b821cc9
  1. 94
      src/base58.js
  2. 53
      src/base58check.js
  3. 1
      src/index.js
  4. 66
      test/base58.js
  5. 76
      test/base58check.js

94
src/base58.js

@ -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 Crypto = require('crypto-js')
var convert = require('./convert')
var SHA256 = Crypto.SHA256
// FIXME: ? This is a Base58Check alphabet
var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
var base = BigInteger.valueOf(58)
var positions = {}
var alphabetMap = {}
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.
// Written by Mike Hearn for BitcoinJ.
// Copyright (c) 2011 Google Inc.
// Ported to JavaScript by Stefan Thomas.
function encode(input) {
var bi = BigInteger.fromByteArrayUnsigned(input)
// encode a byte array into a base58 encoded String
// @return String
function encode(buffer) {
var bi = BigInteger.fromByteArrayUnsigned(buffer)
var chars = []
while (bi.compareTo(base) >= 0) {
var mod = bi.mod(base)
chars.push(alphabet[mod.intValue()])
bi = bi.subtract(mod).divide(base)
chars.push(alphabet[mod.intValue()])
}
chars.push(alphabet[bi.intValue()])
// Convert leading zeros too.
for (var i=0; i<input.length; i++) {
if (input[i] == 0x00) {
chars.push(alphabet[0])
} else break
for (var i=0; i<buffer.length; i++) {
if (buffer[i] !== 0x00) break
chars.push(alphabet[0])
}
return chars.reverse().join('')
}
// decode a base58 string into a byte array
// input should be a base58 encoded string
// decode a base58 encoded String into a byte array
// @return Array
function decode(input) {
var base = BigInteger.valueOf(58)
var length = input.length
function decode(str) {
var num = BigInteger.valueOf(0)
var leading_zero = 0
var seen_other = false
for (var i=0; i<length; ++i) {
var chr = input[i]
var p = positions[chr]
for (var i=0; i<str.length; ++i) {
var chr = str[i]
var bi = alphabetMap[chr]
// if we encounter an invalid character, decoding fails
if (p === undefined) {
throw new Error('invalid base58 string: ' + input)
if (bi === undefined) {
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
} else {
seen_other = true
@ -74,41 +73,10 @@ function decode(input) {
bytes.unshift(0)
}
return 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)
return new Buffer(bytes)
}
module.exports = {
encode: encode,
decode: decode,
checkEncode: checkEncode,
checkDecode: checkDecode,
getChecksum: getChecksum
decode: decode
}

53
src/base58check.js

@ -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
src/index.js

@ -21,5 +21,6 @@ module.exports = {
ecdsa: require('./ecdsa'),
HDWallet: require('./hdwallet.js'),
base58: require('./base58'),
base58check: require('./base58check'),
convert: require('./convert')
}

66
test/base58.js

@ -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)
})
})
})
})

76
test/base58check.js

@ -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…
Cancel
Save