Merge pull request #112 from dcousens/b58
Base58 isolation and preparation for Buffers
This commit is contained in:
commit
703275f235
12 changed files with 281 additions and 150 deletions
|
@ -1,4 +1,5 @@
|
||||||
var base58 = require('./base58')
|
var base58 = require('./base58')
|
||||||
|
var base58check = require('./base58check')
|
||||||
var convert = require('./convert')
|
var convert = require('./convert')
|
||||||
var error = require('./util').error
|
var error = require('./util').error
|
||||||
var mainnet = require('./network').mainnet.addressVersion
|
var mainnet = require('./network').mainnet.addressVersion
|
||||||
|
@ -13,8 +14,19 @@ function Address(bytes, version) {
|
||||||
this.version = bytes.version
|
this.version = bytes.version
|
||||||
}
|
}
|
||||||
else if (typeof bytes === 'string') {
|
else if (typeof bytes === 'string') {
|
||||||
this.hash = stringToHash(bytes)
|
if (bytes.length <= 35) {
|
||||||
this.version = version || this.hash.version || mainnet
|
var decode = base58check.decode(bytes)
|
||||||
|
|
||||||
|
this.hash = decode.payload
|
||||||
|
this.version = decode.version
|
||||||
|
}
|
||||||
|
else if (bytes.length <= 40) {
|
||||||
|
this.hash = convert.hexToBytes(bytes)
|
||||||
|
this.version = version || mainnet
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
error('invalid or unrecognized input')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.hash = bytes
|
this.hash = bytes
|
||||||
|
@ -22,22 +34,12 @@ function Address(bytes, version) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function stringToHash(str) {
|
|
||||||
if (str.length <= 35) {
|
|
||||||
return base58.checkDecode(str)
|
|
||||||
}
|
|
||||||
if (str.length <= 40) {
|
|
||||||
return convert.hexToBytes(str)
|
|
||||||
}
|
|
||||||
error('invalid or unrecognized input')
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialize this object as a standard Bitcoin address.
|
* Serialize this object as a standard Bitcoin address.
|
||||||
* Returns the address as a base58-encoded string in the standardized format.
|
* Returns the address as a base58-encoded string in the standardized format.
|
||||||
*/
|
*/
|
||||||
Address.prototype.toString = function () {
|
Address.prototype.toString = function () {
|
||||||
return base58.checkEncode(this.hash.slice(0), this.version)
|
return base58check.encode(this.hash.slice(0), this.version)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,7 +55,7 @@ Address.getVersion = function (address) {
|
||||||
*/
|
*/
|
||||||
Address.validate = function (address) {
|
Address.validate = function (address) {
|
||||||
try {
|
try {
|
||||||
base58.checkDecode(address)
|
base58check.decode(address)
|
||||||
return true
|
return true
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -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])
|
chars.push(alphabet[0])
|
||||||
} else break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
}
|
|
@ -8,6 +8,11 @@ function lpad(str, padString, length) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function bytesToHex(bytes) {
|
function bytesToHex(bytes) {
|
||||||
|
// FIXME: transitionary fix
|
||||||
|
if (Buffer.isBuffer(bytes)) {
|
||||||
|
return bytes.toString('hex')
|
||||||
|
}
|
||||||
|
|
||||||
return bytes.map(function(x) {
|
return bytes.map(function(x) {
|
||||||
return lpad(x.toString(16), '0', 2)
|
return lpad(x.toString(16), '0', 2)
|
||||||
}).join('')
|
}).join('')
|
||||||
|
|
14
src/eckey.js
14
src/eckey.js
|
@ -1,7 +1,7 @@
|
||||||
var Address = require('./address')
|
var Address = require('./address')
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var convert = require('./convert')
|
var convert = require('./convert')
|
||||||
var base58 = require('./base58')
|
var base58check = require('./base58check')
|
||||||
var BigInteger = require('./jsbn/jsbn')
|
var BigInteger = require('./jsbn/jsbn')
|
||||||
var ecdsa = require('./ecdsa')
|
var ecdsa = require('./ecdsa')
|
||||||
var ECPointFp = require('./jsbn/ec').ECPointFp
|
var ECPointFp = require('./jsbn/ec').ECPointFp
|
||||||
|
@ -32,10 +32,10 @@ ECKey.prototype.import = function (input, compressed) {
|
||||||
: Array.isArray(input) ? fromBin(input.slice(0, 32))
|
: Array.isArray(input) ? fromBin(input.slice(0, 32))
|
||||||
: typeof input != "string" ? null
|
: typeof input != "string" ? null
|
||||||
: input.length == 44 ? fromBin(convert.base64ToBytes(input))
|
: input.length == 44 ? fromBin(convert.base64ToBytes(input))
|
||||||
: input.length == 51 && input[0] == '5' ? fromBin(base58.checkDecode(input))
|
: input.length == 51 && input[0] == '5' ? fromBin(base58check.decode(input).payload)
|
||||||
: input.length == 51 && input[0] == '9' ? fromBin(base58.checkDecode(input))
|
: input.length == 51 && input[0] == '9' ? fromBin(base58check.decode(input).payload)
|
||||||
: input.length == 52 && has('LK', input[0]) ? fromBin(base58.checkDecode(input).slice(0, 32))
|
: input.length == 52 && has('LK', input[0]) ? fromBin(base58check.decode(input).payload.slice(0, 32))
|
||||||
: input.length == 52 && input[0] == 'c' ? fromBin(base58.checkDecode(input).slice(0, 32))
|
: input.length == 52 && input[0] == 'c' ? fromBin(base58check.decode(input).payload.slice(0, 32))
|
||||||
: has([64,65],input.length) ? fromBin(convert.hexToBytes(input.slice(0, 64)))
|
: has([64,65],input.length) ? fromBin(convert.hexToBytes(input.slice(0, 64)))
|
||||||
: null
|
: null
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ ECKey.version_bytes = {
|
||||||
ECKey.prototype.toWif = function(version) {
|
ECKey.prototype.toWif = function(version) {
|
||||||
version = version || Network.mainnet.addressVersion
|
version = version || Network.mainnet.addressVersion
|
||||||
|
|
||||||
return base58.checkEncode(this.toBytes(), ECKey.version_bytes[version])
|
return base58check.encode(this.toBytes(), ECKey.version_bytes[version])
|
||||||
}
|
}
|
||||||
|
|
||||||
ECKey.prototype.toHex = function() {
|
ECKey.prototype.toHex = function() {
|
||||||
|
@ -167,7 +167,7 @@ ECPubKey.prototype.toBin = function(compressed) {
|
||||||
ECPubKey.prototype.toWif = function(version) {
|
ECPubKey.prototype.toWif = function(version) {
|
||||||
version = version || Network.mainnet.addressVersion
|
version = version || Network.mainnet.addressVersion
|
||||||
|
|
||||||
return base58.checkEncode(this.toBytes(), version)
|
return base58check.encode(this.toBytes(), version)
|
||||||
}
|
}
|
||||||
|
|
||||||
ECPubKey.prototype.toString = ECPubKey.prototype.toHex
|
ECPubKey.prototype.toString = ECPubKey.prototype.toHex
|
||||||
|
|
|
@ -11,7 +11,16 @@ var ECPubKey = require('./eckey.js').ECPubKey
|
||||||
var Address = require('./address.js')
|
var Address = require('./address.js')
|
||||||
var Network = require('./network')
|
var Network = require('./network')
|
||||||
|
|
||||||
var HDWallet = module.exports = function(seed, network) {
|
var crypto = require('crypto')
|
||||||
|
|
||||||
|
function sha256(buf) {
|
||||||
|
var hash = crypto.createHash('sha256')
|
||||||
|
hash.update(buf)
|
||||||
|
|
||||||
|
return hash.digest()
|
||||||
|
}
|
||||||
|
|
||||||
|
function HDWallet(seed, network) {
|
||||||
if (seed === undefined) return;
|
if (seed === undefined) return;
|
||||||
|
|
||||||
var seedWords = convert.bytesToWordArray(seed)
|
var seedWords = convert.bytesToWordArray(seed)
|
||||||
|
@ -35,8 +44,6 @@ function arrayEqual(a, b) {
|
||||||
return !(a < b || a > b)
|
return !(a < b || a > b)
|
||||||
}
|
}
|
||||||
|
|
||||||
HDWallet.getChecksum = base58.getChecksum
|
|
||||||
|
|
||||||
HDWallet.fromSeedHex = function(hex, network) {
|
HDWallet.fromSeedHex = function(hex, network) {
|
||||||
return new HDWallet(convert.hexToBytes(hex), network)
|
return new HDWallet(convert.hexToBytes(hex), network)
|
||||||
}
|
}
|
||||||
|
@ -45,20 +52,17 @@ HDWallet.fromSeedString = function(string, network) {
|
||||||
return new HDWallet(convert.stringToBytes(string), network)
|
return new HDWallet(convert.stringToBytes(string), network)
|
||||||
}
|
}
|
||||||
|
|
||||||
HDWallet.fromBase58 = function(input) {
|
HDWallet.fromBase58 = function(string) {
|
||||||
var buffer = base58.decode(input)
|
var buffer = base58.decode(string)
|
||||||
|
|
||||||
if (buffer.length == HDWallet.LENGTH + 4) {
|
var payload = buffer.slice(0, -4)
|
||||||
var expectedChecksum = buffer.slice(HDWallet.LENGTH, HDWallet.LENGTH + 4)
|
var checksum = buffer.slice(-4)
|
||||||
buffer = buffer.slice(0, HDWallet.LENGTH)
|
var newChecksum = sha256(sha256(payload)).slice(0, 4)
|
||||||
var actualChecksum = HDWallet.getChecksum(buffer)
|
|
||||||
|
|
||||||
if (!arrayEqual(expectedChecksum, actualChecksum)) {
|
assert.deepEqual(newChecksum, checksum)
|
||||||
throw new Error('Checksum mismatch')
|
assert.equal(payload.length, HDWallet.LENGTH)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return HDWallet.fromBytes(buffer)
|
return HDWallet.fromBytes(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
HDWallet.fromHex = function(input) {
|
HDWallet.fromHex = function(input) {
|
||||||
|
@ -71,6 +75,11 @@ HDWallet.fromBytes = function(input) {
|
||||||
throw new Error(format('Invalid input length, %s. Expected %s.', input.length, HDWallet.LENGTH))
|
throw new Error(format('Invalid input length, %s. Expected %s.', input.length, HDWallet.LENGTH))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: transitionary fix
|
||||||
|
if (Buffer.isBuffer(input)) {
|
||||||
|
input = Array.prototype.map.bind(input, function(x) { return x })()
|
||||||
|
}
|
||||||
|
|
||||||
var hd = new HDWallet()
|
var hd = new HDWallet()
|
||||||
|
|
||||||
// 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private
|
// 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private
|
||||||
|
@ -182,10 +191,13 @@ HDWallet.prototype.toHex = function(priv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
HDWallet.prototype.toBase58 = function(priv) {
|
HDWallet.prototype.toBase58 = function(priv) {
|
||||||
var buffer = this.toBytes(priv)
|
var buffer = new Buffer(this.toBytes(priv))
|
||||||
, checksum = HDWallet.getChecksum(buffer)
|
var checksum = sha256(sha256(buffer)).slice(0, 4)
|
||||||
buffer = buffer.concat(checksum)
|
|
||||||
return base58.encode(buffer)
|
return base58.encode(Buffer.concat([
|
||||||
|
buffer,
|
||||||
|
checksum
|
||||||
|
]))
|
||||||
}
|
}
|
||||||
|
|
||||||
HDWallet.prototype.derive = function(i) {
|
HDWallet.prototype.derive = function(i) {
|
||||||
|
@ -252,3 +264,4 @@ function HmacFromBytesToBytes(hasher, message, key) {
|
||||||
return convert.wordArrayToBytes(hmac.finalize())
|
return convert.wordArrayToBytes(hmac.finalize())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = HDWallet
|
||||||
|
|
|
@ -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')
|
||||||
}
|
}
|
||||||
|
|
|
@ -1200,6 +1200,11 @@ BigInteger.valueOf = nbv;
|
||||||
* endian notation and ignore leading zeros.
|
* endian notation and ignore leading zeros.
|
||||||
*/
|
*/
|
||||||
BigInteger.fromByteArrayUnsigned = function(ba) {
|
BigInteger.fromByteArrayUnsigned = function(ba) {
|
||||||
|
// FIXME: BigInteger doesn't yet support Buffers
|
||||||
|
if (Buffer.isBuffer(ba)) {
|
||||||
|
ba = Array.prototype.map.bind(ba, function(x) { return x })()
|
||||||
|
}
|
||||||
|
|
||||||
if (!ba.length) {
|
if (!ba.length) {
|
||||||
return new BigInteger.valueOf(0);
|
return new BigInteger.valueOf(0);
|
||||||
} else if (ba[0] & 0x80) {
|
} else if (ba[0] & 0x80) {
|
||||||
|
|
|
@ -279,6 +279,11 @@ Script.prototype.writeOp = function(opcode) {
|
||||||
* Add a data chunk to the script.
|
* Add a data chunk to the script.
|
||||||
*/
|
*/
|
||||||
Script.prototype.writeBytes = function(data) {
|
Script.prototype.writeBytes = function(data) {
|
||||||
|
// FIXME: Script module doesn't support buffers yet
|
||||||
|
if (Buffer.isBuffer(data)) {
|
||||||
|
data = Array.prototype.map.bind(data, function(x) { return x })()
|
||||||
|
}
|
||||||
|
|
||||||
if (data.length < Opcode.map.OP_PUSHDATA1) {
|
if (data.length < Opcode.map.OP_PUSHDATA1) {
|
||||||
this.buffer.push(data.length)
|
this.buffer.push(data.length)
|
||||||
} else if (data.length <= 0xff) {
|
} else if (data.length <= 0xff) {
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var Address = require('../src/address.js')
|
var Address = require('../src/address')
|
||||||
var network = require('../src/network.js')
|
var network = require('../src/network')
|
||||||
var base58 = require('../src/base58.js')
|
var base58 = require('../src/base58')
|
||||||
|
var base58check = require('../src/base58check')
|
||||||
var mainnet = network.mainnet.addressVersion
|
var mainnet = network.mainnet.addressVersion
|
||||||
var testnet = network.testnet.addressVersion
|
var testnet = network.testnet.addressVersion
|
||||||
|
|
||||||
|
@ -10,10 +11,10 @@ describe('Address', function() {
|
||||||
var testnetP2shAddress, mainnetP2shAddress
|
var testnetP2shAddress, mainnetP2shAddress
|
||||||
|
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
testnetAddress = 'mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef'
|
|
||||||
mainnetAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
|
mainnetAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
|
||||||
testnetP2shAddress = '2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7'
|
testnetAddress = 'mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef'
|
||||||
mainnetP2shAddress = '3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt'
|
mainnetP2shAddress = '3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt'
|
||||||
|
testnetP2shAddress = '2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7'
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('parsing', function() {
|
describe('parsing', function() {
|
||||||
|
@ -36,14 +37,14 @@ describe('Address', function() {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('works for byte input', function() {
|
it('works for byte input', function() {
|
||||||
var hash = base58.checkDecode('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa')
|
var hash = base58check.decode(mainnetAddress)
|
||||||
var addr = new Address(hash)
|
var addr = new Address(hash.payload)
|
||||||
assert.equal(addr.hash, hash)
|
assert.equal(addr.hash, hash.payload)
|
||||||
assert.equal(network.mainnet.addressVersion, hash.version)
|
assert.equal(network.mainnet.addressVersion, hash.version)
|
||||||
|
|
||||||
var hash = base58.checkDecode('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef')
|
var hash = base58check.decode(testnetAddress)
|
||||||
var addr = new Address(hash)
|
var addr = new Address(hash.payload)
|
||||||
assert.equal(addr.hash, hash)
|
assert.equal(addr.hash, hash.payload)
|
||||||
assert.equal(network.testnet.addressVersion, hash.version)
|
assert.equal(network.testnet.addressVersion, hash.version)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -56,8 +57,8 @@ describe('Address', function() {
|
||||||
|
|
||||||
describe('getVersion', function() {
|
describe('getVersion', function() {
|
||||||
it('returns the proper address version', function() {
|
it('returns the proper address version', function() {
|
||||||
assert.equal(Address.getVersion('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'), network.mainnet.addressVersion)
|
assert.equal(Address.getVersion(mainnetAddress), network.mainnet.addressVersion)
|
||||||
assert.equal(Address.getVersion('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef'), network.testnet.addressVersion)
|
assert.equal(Address.getVersion(testnetAddress), network.testnet.addressVersion)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -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…
Reference in a new issue