Browse Source

HDNode: adopt new strict network list parameters

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
f47decaa2f
  1. 4
      src/ecpair.js
  2. 33
      src/hdnode.js
  3. 2
      test/ecpair.js
  4. 5
      test/fixtures/hdnode.json
  5. 39
      test/hdnode.js

4
src/ecpair.js

@ -83,7 +83,7 @@ ECPair.fromWIF = function (string, networks) {
// list of networks?
if (Array.isArray(networks)) {
network = networks.filter(function (network) {
return network.wif === version
return version === network.wif
}).pop() || {}
// otherwise, assume a network object (or default to bitcoin)
@ -91,7 +91,7 @@ ECPair.fromWIF = function (string, networks) {
network = networks || NETWORKS.bitcoin
}
if (network.wif !== version) throw new Error('Invalid network')
if (version !== network.wif) throw new Error('Invalid network')
var d = BigInteger.fromBuffer(payload)

33
src/hdnode.js

@ -2,7 +2,7 @@ var base58check = require('bs58check')
var bcrypto = require('./crypto')
var createHmac = require('create-hmac')
var typeForce = require('typeforce')
var networks = require('./networks')
var NETWORKS = require('./networks')
var BigInteger = require('bigi')
var ECPair = require('./ecpair')
@ -10,18 +10,6 @@ var ECPair = require('./ecpair')
var ecurve = require('ecurve')
var curve = ecurve.getCurveByName('secp256k1')
function findBIP32NetworkByVersion (version) {
for (var name in networks) {
var network = networks[name]
if (version === network.bip32.private || version === network.bip32.public) {
return network
}
}
throw new Error('Could not find network for ' + version.toString(16))
}
function HDNode (keyPair, chainCode) {
typeForce('ECPair', keyPair)
typeForce('Buffer', chainCode)
@ -65,22 +53,29 @@ HDNode.fromSeedHex = function (hex, network) {
return HDNode.fromSeedBuffer(new Buffer(hex, 'hex'), network)
}
HDNode.fromBase58 = function (string, network) {
HDNode.fromBase58 = function (string, networks) {
var buffer = base58check.decode(string)
if (buffer.length !== HDNode.LENGTH) throw new Error('Invalid buffer length')
// 4 byte: version bytes
var version = buffer.readUInt32BE(0)
var network
if (network) {
if (version !== network.bip32.private &&
version !== network.bip32.public) throw new Error('Invalid network')
// list of networks?
if (Array.isArray(networks)) {
network = networks.filter(function (network) {
return version === network.bip32.private ||
version === network.bip32.public
}).pop() || {}
// auto-detect
// otherwise, assume a network object (or default to bitcoin)
} else {
network = findBIP32NetworkByVersion(version)
network = networks || NETWORKS.bitcoin
}
if (version !== network.bip32.private &&
version !== network.bip32.public) throw new Error('Invalid network')
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ...
var depth = buffer.readUInt8(4)

2
test/ecpair.js

@ -4,7 +4,6 @@
var assert = require('assert')
var ecdsa = require('../src/ecdsa')
var ecurve = require('ecurve')
var NETWORKS = require('../src/networks')
var proxyquire = require('proxyquire')
var sinon = require('sinon')
@ -13,6 +12,7 @@ var ECPair = require('../src/ecpair')
var fixtures = require('./fixtures/ecpair.json')
var NETWORKS = require('../src/networks')
var NETWORKS_LIST = [] // Object.values(NETWORKS)
for (var networkName in NETWORKS) {
NETWORKS_LIST.push(NETWORKS[networkName])

5
test/fixtures/hdnode.json

@ -199,13 +199,12 @@
"string": "xprvQQQQQQQQQQQQQQQQCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334"
},
{
"exception": "Could not find network for 0",
"exception": "Invalid network",
"string": "1111111111111adADjFaSNPxwXqLjHLj4mBfYxuewDPbw9hEj1uaXCzMxRPXDFF3cUoezTFYom4sEmEVSQmENPPR315cFk9YUFVek73wE9"
},
{
"exception": "Invalid network",
"string": "Ltpv73XYpw28ZyVe2zEVyiFnxUZxoKLGQNdZ8NxUi1WcqjNmMBgtLbh3KimGSnPHCoLv1RmvxHs4dnKmo1oXQ8dXuDu8uroxrbVxZPA1gXboYvx",
"network": "bitcoin"
"string": "Ltpv73XYpw28ZyVe2zEVyiFnxUZxoKLGQNdZ8NxUi1WcqjNmMBgtLbh3KimGSnPHCoLv1RmvxHs4dnKmo1oXQ8dXuDu8uroxrbVxZPA1gXboYvx"
}
],
"fromBuffer": [

39
test/hdnode.js

@ -2,7 +2,6 @@
/* eslint-disable no-new */
var assert = require('assert')
var networks = require('../src/networks')
var sinon = require('sinon')
var BigInteger = require('bigi')
@ -11,6 +10,12 @@ var HDNode = require('../src/hdnode')
var fixtures = require('./fixtures/hdnode.json')
var NETWORKS = require('../src/networks')
var NETWORKS_LIST = [] // Object.values(NETWORKS)
for (var networkName in NETWORKS) {
NETWORKS_LIST.push(NETWORKS[networkName])
}
describe('HDNode', function () {
describe('Constructor', function () {
var keyPair, chainCode
@ -63,7 +68,7 @@ describe('HDNode', function () {
describe('fromSeed*', function () {
fixtures.valid.forEach(function (f) {
it('calculates privKey and chainCode for ' + f.master.fingerprint, function () {
var network = networks[f.network]
var network = NETWORKS[f.network]
var hd = HDNode.fromSeedHex(f.master.seed, network)
assert.strictEqual(hd.keyPair.toWIF(), f.master.wif)
@ -87,7 +92,7 @@ describe('HDNode', function () {
describe('toBase58', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.master.base58 + ' (public) correctly', function () {
var network = networks[f.network]
var network = NETWORKS[f.network]
var hd = HDNode.fromSeedHex(f.master.seed, network).neutered()
assert.strictEqual(hd.toBase58(), f.master.base58)
@ -96,7 +101,7 @@ describe('HDNode', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.master.base58Priv + ' (private) correctly', function () {
var network = networks[f.network]
var network = NETWORKS[f.network]
var hd = HDNode.fromSeedHex(f.master.seed, network)
assert.strictEqual(hd.toBase58(), f.master.base58Priv)
@ -107,8 +112,8 @@ describe('HDNode', function () {
describe('fromBase58', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.master.base58 + ' (public) correctly', function () {
var network = networks[f.network]
var hd = HDNode.fromBase58(f.master.base58)
var network = NETWORKS[f.network]
var hd = HDNode.fromBase58(f.master.base58, network)
assert.strictEqual(hd.toBase58(), f.master.base58)
assert.strictEqual(hd.keyPair.network, network)
@ -117,7 +122,7 @@ describe('HDNode', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.master.base58Priv + ' (private) correctly', function () {
var network = networks[f.network]
var network = NETWORKS[f.network]
var hd = HDNode.fromBase58(f.master.base58Priv, network)
assert.strictEqual(hd.toBase58(), f.master.base58Priv)
@ -128,7 +133,7 @@ describe('HDNode', function () {
fixtures.invalid.fromBase58.forEach(function (f) {
it('throws on ' + f.string, function () {
assert.throws(function () {
var network = networks[f.network]
var network = NETWORKS[f.network]
HDNode.fromBase58(f.string, network)
}, new RegExp(f.exception))
@ -140,7 +145,7 @@ describe('HDNode', function () {
var f = fixtures.valid[0]
it('returns the identifier for ' + f.master.fingerprint, function () {
var hd = HDNode.fromBase58(f.master.base58)
var hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
assert.strictEqual(hd.getIdentifier().toString('hex'), f.master.identifier)
})
@ -150,7 +155,7 @@ describe('HDNode', function () {
var f = fixtures.valid[0]
it('returns the fingerprint for ' + f.master.fingerprint, function () {
var hd = HDNode.fromBase58(f.master.base58)
var hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
assert.strictEqual(hd.getFingerprint().toString('hex'), f.master.fingerprint)
})
@ -162,7 +167,7 @@ describe('HDNode', function () {
beforeEach(function () {
var f = fixtures.valid[0]
hd = HDNode.fromBase58(f.master.base58)
hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
})
it('wraps ECPair.getAddress', sinon.test(function () {
@ -177,7 +182,7 @@ describe('HDNode', function () {
var f = fixtures.valid[0]
it('strips all private information', function () {
var hd = HDNode.fromBase58(f.master.base58)
var hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
var hdn = hd.neutered()
assert.strictEqual(hdn.keyPair.d, undefined)
@ -203,7 +208,7 @@ describe('HDNode', function () {
}
fixtures.valid.forEach(function (f) {
var network = networks[f.network]
var network = NETWORKS[f.network]
var hd = HDNode.fromSeedHex(f.master.seed, network)
// FIXME: test data is only testing Private -> private for now
@ -224,7 +229,7 @@ describe('HDNode', function () {
var f = fixtures.valid[1]
var c = f.children[0]
var master = HDNode.fromBase58(f.master.base58Priv)
var master = HDNode.fromBase58(f.master.base58Priv, NETWORKS_LIST)
var child = master.derive(c.m).neutered()
assert.strictEqual(child.toBase58(), c.base58)
@ -234,7 +239,7 @@ describe('HDNode', function () {
var f = fixtures.valid[0]
var c = f.children[0]
var master = HDNode.fromBase58(f.master.base58Priv)
var master = HDNode.fromBase58(f.master.base58Priv, NETWORKS_LIST)
var child = master.deriveHardened(c.m).neutered()
assert.strictEqual(child.toBase58(), c.base58)
@ -244,7 +249,7 @@ describe('HDNode', function () {
var f = fixtures.valid[1]
var c = f.children[0]
var master = HDNode.fromBase58(f.master.base58)
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
var child = master.derive(c.m)
assert.strictEqual(child.toBase58(), c.base58)
@ -254,7 +259,7 @@ describe('HDNode', function () {
var f = fixtures.valid[0]
var c = f.children[0]
var master = HDNode.fromBase58(f.master.base58)
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
assert.throws(function () {
master.deriveHardened(c.m)

Loading…
Cancel
Save