2015-03-19 03:35:25 +01:00
|
|
|
/* global describe, it, beforeEach */
|
2015-02-23 00:36:57 +01:00
|
|
|
/* eslint-disable no-new */
|
|
|
|
|
2014-02-28 10:26:16 +01:00
|
|
|
var assert = require('assert')
|
2015-03-02 06:48:36 +01:00
|
|
|
var sinon = require('sinon')
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
var BigInteger = require('bigi')
|
2015-03-02 06:48:36 +01:00
|
|
|
var ECPair = require('../src/ecpair')
|
2014-06-03 09:08:42 +02:00
|
|
|
var HDNode = require('../src/hdnode')
|
2014-04-16 22:18:31 +02:00
|
|
|
|
2014-06-03 09:08:42 +02:00
|
|
|
var fixtures = require('./fixtures/hdnode.json')
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2015-07-28 09:47:18 +02:00
|
|
|
var NETWORKS = require('../src/networks')
|
|
|
|
var NETWORKS_LIST = [] // Object.values(NETWORKS)
|
|
|
|
for (var networkName in NETWORKS) {
|
|
|
|
NETWORKS_LIST.push(NETWORKS[networkName])
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('HDNode', function () {
|
|
|
|
describe('Constructor', function () {
|
2015-03-02 06:48:36 +01:00
|
|
|
var keyPair, chainCode
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
beforeEach(function () {
|
|
|
|
var d = BigInteger.ONE
|
2015-02-24 02:23:31 +01:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
keyPair = new ECPair(d, null)
|
|
|
|
chainCode = new Buffer(32)
|
|
|
|
chainCode.fill(1)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
|
2015-05-07 03:24:53 +02:00
|
|
|
it('stores the keyPair/chainCode directly', function () {
|
|
|
|
var hd = new HDNode(keyPair, chainCode)
|
|
|
|
|
|
|
|
assert.strictEqual(hd.keyPair, keyPair)
|
|
|
|
assert.strictEqual(hd.chainCode, chainCode)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('has a default depth/index of 0', function () {
|
2015-03-02 06:48:36 +01:00
|
|
|
var hd = new HDNode(keyPair, chainCode)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
|
|
|
assert.strictEqual(hd.depth, 0)
|
|
|
|
assert.strictEqual(hd.index, 0)
|
|
|
|
})
|
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
it('throws on uncompressed keyPair', function () {
|
|
|
|
keyPair.compressed = false
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
assert.throws(function () {
|
|
|
|
new HDNode(keyPair, chainCode)
|
|
|
|
}, /BIP32 only allows compressed keyPairs/)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws when an invalid length chain code is given', function () {
|
|
|
|
assert.throws(function () {
|
2015-08-18 06:21:38 +02:00
|
|
|
new HDNode(keyPair, new Buffer(20))
|
|
|
|
}, /Expected 256-bit Buffer, got 160-bit/)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('fromSeed*', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('calculates privKey and chainCode for ' + f.master.fingerprint, function () {
|
2015-07-28 09:47:18 +02:00
|
|
|
var network = NETWORKS[f.network]
|
2014-11-29 02:04:02 +01:00
|
|
|
var hd = HDNode.fromSeedHex(f.master.seed, network)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.keyPair.toWIF(), f.master.wif)
|
|
|
|
assert.strictEqual(hd.chainCode.toString('hex'), f.master.chainCode)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
})
|
2014-07-11 08:33:39 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws on low entropy seed', function () {
|
|
|
|
assert.throws(function () {
|
2014-07-11 08:33:39 +02:00
|
|
|
HDNode.fromSeedHex('ffffffffff')
|
2014-07-11 11:15:56 +02:00
|
|
|
}, /Seed should be at least 128 bits/)
|
2014-07-11 08:33:39 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws on too high entropy seed', function () {
|
|
|
|
assert.throws(function () {
|
2014-07-11 08:33:39 +02:00
|
|
|
HDNode.fromSeedHex('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
|
2014-07-11 11:15:56 +02:00
|
|
|
}, /Seed should be at most 512 bits/)
|
2014-07-11 08:33:39 +02:00
|
|
|
})
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('toBase58', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('exports ' + f.master.base58 + ' (public) correctly', function () {
|
2015-07-28 09:47:18 +02:00
|
|
|
var network = NETWORKS[f.network]
|
2014-11-29 02:04:02 +01:00
|
|
|
var hd = HDNode.fromSeedHex(f.master.seed, network).neutered()
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.toBase58(), f.master.base58)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
})
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('exports ' + f.master.base58Priv + ' (private) correctly', function () {
|
2015-07-28 09:47:18 +02:00
|
|
|
var network = NETWORKS[f.network]
|
2014-11-29 02:04:02 +01:00
|
|
|
var hd = HDNode.fromSeedHex(f.master.seed, network)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.toBase58(), f.master.base58Priv)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
|
|
|
})
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('fromBase58', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports ' + f.master.base58 + ' (public) correctly', function () {
|
2015-07-28 09:47:18 +02:00
|
|
|
var network = NETWORKS[f.network]
|
|
|
|
var hd = HDNode.fromBase58(f.master.base58, network)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.toBase58(), f.master.base58)
|
|
|
|
assert.strictEqual(hd.keyPair.network, network)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports ' + f.master.base58Priv + ' (private) correctly', function () {
|
2015-07-28 09:47:18 +02:00
|
|
|
var network = NETWORKS[f.network]
|
2015-03-16 01:26:16 +01:00
|
|
|
var hd = HDNode.fromBase58(f.master.base58Priv, network)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.toBase58(), f.master.base58Priv)
|
|
|
|
assert.strictEqual(hd.keyPair.network, network)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.fromBase58.forEach(function (f) {
|
|
|
|
it('throws on ' + f.string, function () {
|
|
|
|
assert.throws(function () {
|
2015-07-28 09:47:18 +02:00
|
|
|
var network = NETWORKS[f.network]
|
2014-11-29 02:12:24 +01:00
|
|
|
|
|
|
|
HDNode.fromBase58(f.string, network)
|
2014-05-30 11:00:49 +02:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('getIdentifier', function () {
|
2014-06-01 08:05:47 +02:00
|
|
|
var f = fixtures.valid[0]
|
2014-05-25 03:48:16 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns the identifier for ' + f.master.fingerprint, function () {
|
2015-07-28 09:47:18 +02:00
|
|
|
var hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
|
2014-05-25 03:48:16 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.getIdentifier().toString('hex'), f.master.identifier)
|
2014-01-16 08:03:09 +01:00
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-02-28 10:26:16 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('getFingerprint', function () {
|
2014-06-01 08:05:47 +02:00
|
|
|
var f = fixtures.valid[0]
|
2014-05-04 03:40:48 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns the fingerprint for ' + f.master.fingerprint, function () {
|
2015-07-28 09:47:18 +02:00
|
|
|
var hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
|
2014-05-04 04:13:25 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.getFingerprint().toString('hex'), f.master.fingerprint)
|
2014-05-04 03:40:48 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('getAddress', function () {
|
2015-03-02 06:48:36 +01:00
|
|
|
var hd
|
2014-05-30 10:41:03 +02:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
beforeEach(function () {
|
|
|
|
var f = fixtures.valid[0]
|
|
|
|
|
2015-07-28 09:47:18 +02:00
|
|
|
hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
|
2014-05-30 10:41:03 +02:00
|
|
|
})
|
2015-03-02 06:48:36 +01:00
|
|
|
|
|
|
|
it('wraps ECPair.getAddress', sinon.test(function () {
|
|
|
|
this.mock(hd.keyPair).expects('getAddress')
|
|
|
|
.once().returns('foobar')
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.getAddress(), 'foobar')
|
2015-03-02 06:48:36 +01:00
|
|
|
}))
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
2014-05-30 10:41:03 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('neutered', function () {
|
2014-07-28 17:34:46 +02:00
|
|
|
var f = fixtures.valid[0]
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('strips all private information', function () {
|
2015-07-28 09:47:18 +02:00
|
|
|
var hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
|
2014-07-28 17:34:46 +02:00
|
|
|
var hdn = hd.neutered()
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hdn.keyPair.d, undefined)
|
|
|
|
assert.strictEqual(hdn.keyPair.Q, hd.keyPair.Q)
|
|
|
|
assert.strictEqual(hdn.chainCode, hd.chainCode)
|
|
|
|
assert.strictEqual(hdn.depth, hd.depth)
|
|
|
|
assert.strictEqual(hdn.index, hd.index)
|
2014-07-28 17:34:46 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('derive', function () {
|
2015-03-02 06:48:36 +01:00
|
|
|
function verifyVector (hd, v, depth) {
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.keyPair.toWIF(), v.wif)
|
|
|
|
assert.strictEqual(hd.keyPair.getPublicKeyBuffer().toString('hex'), v.pubKey)
|
|
|
|
assert.strictEqual(hd.chainCode.toString('hex'), v.chainCode)
|
|
|
|
assert.strictEqual(hd.depth, depth || 0)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2014-06-03 08:12:36 +02:00
|
|
|
if (v.hardened) {
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.index, v.m + HDNode.HIGHEST_BIT)
|
2014-06-01 08:05:47 +02:00
|
|
|
} else {
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(hd.index, v.m)
|
2014-06-01 08:05:47 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-30 10:41:03 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
2015-07-28 09:47:18 +02:00
|
|
|
var network = NETWORKS[f.network]
|
2014-11-29 02:04:02 +01:00
|
|
|
var hd = HDNode.fromSeedHex(f.master.seed, network)
|
2014-02-28 10:26:16 +01:00
|
|
|
|
2014-06-03 09:22:13 +02:00
|
|
|
// FIXME: test data is only testing Private -> private for now
|
2015-02-23 00:36:57 +01:00
|
|
|
f.children.forEach(function (c, i) {
|
|
|
|
it(c.description + ' from ' + f.master.fingerprint, function () {
|
2014-06-03 08:12:36 +02:00
|
|
|
if (c.hardened) {
|
2014-06-03 08:14:15 +02:00
|
|
|
hd = hd.deriveHardened(c.m)
|
2014-06-01 08:05:47 +02:00
|
|
|
} else {
|
|
|
|
hd = hd.derive(c.m)
|
|
|
|
}
|
2014-05-25 03:58:10 +02:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
verifyVector(hd, c, i + 1)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
})
|
2014-05-25 03:58:10 +02:00
|
|
|
})
|
2014-06-03 08:13:20 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('works for Private -> public (neutered)', function () {
|
2014-06-03 09:22:13 +02:00
|
|
|
var f = fixtures.valid[1]
|
|
|
|
var c = f.children[0]
|
|
|
|
|
2015-07-28 09:47:18 +02:00
|
|
|
var master = HDNode.fromBase58(f.master.base58Priv, NETWORKS_LIST)
|
2014-07-28 17:34:56 +02:00
|
|
|
var child = master.derive(c.m).neutered()
|
2014-06-03 09:22:13 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(child.toBase58(), c.base58)
|
2014-06-03 09:22:13 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('works for Private -> public (neutered, hardened)', function () {
|
2014-06-03 09:22:13 +02:00
|
|
|
var f = fixtures.valid[0]
|
|
|
|
var c = f.children[0]
|
|
|
|
|
2015-07-28 09:47:18 +02:00
|
|
|
var master = HDNode.fromBase58(f.master.base58Priv, NETWORKS_LIST)
|
2014-07-28 17:34:56 +02:00
|
|
|
var child = master.deriveHardened(c.m).neutered()
|
2014-06-03 09:22:13 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(child.toBase58(), c.base58)
|
2014-06-03 09:22:13 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('works for Public -> public', function () {
|
2014-06-03 08:13:20 +02:00
|
|
|
var f = fixtures.valid[1]
|
|
|
|
var c = f.children[0]
|
|
|
|
|
2015-07-28 09:47:18 +02:00
|
|
|
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
|
2014-07-28 17:34:56 +02:00
|
|
|
var child = master.derive(c.m)
|
2014-06-03 08:13:20 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(child.toBase58(), c.base58)
|
2014-06-03 08:13:20 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws on Public -> public (hardened)', function () {
|
2014-06-03 09:22:13 +02:00
|
|
|
var f = fixtures.valid[0]
|
2014-06-03 08:13:20 +02:00
|
|
|
var c = f.children[0]
|
|
|
|
|
2015-07-28 09:47:18 +02:00
|
|
|
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
|
2014-06-03 08:13:20 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-07-28 17:34:56 +02:00
|
|
|
master.deriveHardened(c.m)
|
2014-06-03 08:13:20 +02:00
|
|
|
}, /Could not derive hardened child key/)
|
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-01-16 08:03:09 +01:00
|
|
|
})
|