2014-02-28 10:26:16 +01:00
|
|
|
var assert = require('assert')
|
2014-05-30 10:41:03 +02:00
|
|
|
var networks = require('../src/networks')
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
var BigInteger = require('bigi')
|
2015-02-24 02:23:31 +01:00
|
|
|
var ECKey = require('../src/eckey')
|
|
|
|
var ECPubKey = require('../src/ecpubkey')
|
2014-06-03 09:08:42 +02:00
|
|
|
var HDNode = require('../src/hdnode')
|
2014-04-16 22:18:31 +02:00
|
|
|
|
2014-06-07 08:24:27 +02:00
|
|
|
var ecurve = require('ecurve')
|
|
|
|
var curve = ecurve.getCurveByName('secp256k1')
|
|
|
|
|
2014-06-03 09:08:42 +02:00
|
|
|
var fixtures = require('./fixtures/hdnode.json')
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2014-06-03 09:08:42 +02:00
|
|
|
describe('HDNode', function() {
|
2014-06-01 08:05:47 +02:00
|
|
|
describe('Constructor', function() {
|
2014-06-07 10:24:16 +02:00
|
|
|
var d = BigInteger.ONE
|
2014-06-15 17:36:05 +02:00
|
|
|
var Q = curve.G.multiply(d)
|
2014-06-01 08:05:47 +02:00
|
|
|
var chainCode = new Buffer(32)
|
|
|
|
chainCode.fill(1)
|
|
|
|
|
|
|
|
it('calculates the publicKey from a BigInteger', function() {
|
2014-06-07 10:24:16 +02:00
|
|
|
var hd = new HDNode(d, chainCode)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2014-06-03 09:30:05 +02:00
|
|
|
assert(hd.pubKey.Q.equals(Q))
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
|
2015-02-24 02:23:31 +01:00
|
|
|
it('allows initialization directly from an ECKey', function() {
|
|
|
|
var ek = new ECKey(d)
|
|
|
|
var hd = new HDNode(ek, chainCode)
|
|
|
|
|
|
|
|
assert.equal(hd.privKey, ek)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('allows initialization directly from an ECPubKey', function() {
|
|
|
|
var ek = new ECPubKey(Q)
|
|
|
|
var hd = new HDNode(ek, chainCode)
|
|
|
|
|
|
|
|
assert.equal(hd.pubKey, ek)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws if ECKey is not compressed', function() {
|
|
|
|
var ek = new ECKey(d, false)
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
new HDNode(ek, chainCode)
|
|
|
|
}, /ECKey must be compressed/)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws if ECPubKey is not compressed', function() {
|
|
|
|
var ek = new ECPubKey(Q, false)
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
new HDNode(ek, chainCode)
|
|
|
|
}, /ECPubKey must be compressed/)
|
|
|
|
})
|
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
it('only uses compressed points', function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = new HDNode(Q, chainCode)
|
2014-06-07 10:24:16 +02:00
|
|
|
var hdP = new HDNode(d, chainCode)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2014-06-03 09:30:05 +02:00
|
|
|
assert.strictEqual(hd.pubKey.compressed, true)
|
|
|
|
assert.strictEqual(hdP.pubKey.compressed, true)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('has a default depth/index of 0', function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = new HDNode(Q, chainCode)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
|
|
|
assert.strictEqual(hd.depth, 0)
|
|
|
|
assert.strictEqual(hd.index, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('defaults to the bitcoin network', function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = new HDNode(Q, chainCode)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
|
|
|
assert.equal(hd.network, networks.bitcoin)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('supports alternative networks', function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = new HDNode(Q, chainCode, networks.testnet)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
|
|
|
assert.equal(hd.network, networks.testnet)
|
|
|
|
})
|
|
|
|
|
2014-07-28 17:30:51 +02:00
|
|
|
it('throws when an invalid length chain code is given', function() {
|
|
|
|
assert.throws(function() {
|
|
|
|
new HDNode(d, chainCode.slice(0, 20), networks.testnet)
|
|
|
|
}, /Expected chainCode length of 32, got 20/)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws when an unknown network is given', function() {
|
2014-06-01 08:05:47 +02:00
|
|
|
assert.throws(function() {
|
2014-06-07 10:24:16 +02:00
|
|
|
new HDNode(d, chainCode, {})
|
2014-06-01 08:05:47 +02:00
|
|
|
}, /Unknown BIP32 constants for network/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('fromSeed*', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('calculates privKey and chainCode for ' + f.master.fingerprint, function() {
|
2014-11-29 02:04:02 +01:00
|
|
|
var network = networks[f.network]
|
|
|
|
var hd = HDNode.fromSeedHex(f.master.seed, network)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2014-11-29 02:04:02 +01:00
|
|
|
assert.equal(hd.privKey.toWIF(network), f.master.wif)
|
2014-06-01 08:05:47 +02:00
|
|
|
assert.equal(hd.chainCode.toString('hex'), f.master.chainCode)
|
|
|
|
})
|
|
|
|
})
|
2014-07-11 08:33:39 +02:00
|
|
|
|
|
|
|
it('throws on low entropy seed', function() {
|
|
|
|
assert.throws(function() {
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
it('throws on too high entropy seed', function() {
|
|
|
|
assert.throws(function() {
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
describe('toBase58', function() {
|
2014-06-01 08:05:47 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('exports ' + f.master.base58 + ' (public) correctly', function() {
|
2014-11-29 02:04:02 +01:00
|
|
|
var network = networks[f.network]
|
|
|
|
var hd = HDNode.fromSeedHex(f.master.seed, network).neutered()
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2014-07-30 10:00:20 +02:00
|
|
|
assert.equal(hd.toBase58(), f.master.base58)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
})
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('exports ' + f.master.base58Priv + ' (private) correctly', function() {
|
2014-11-29 02:04:02 +01:00
|
|
|
var network = networks[f.network]
|
|
|
|
var hd = HDNode.fromSeedHex(f.master.seed, network)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2014-07-30 10:00:20 +02:00
|
|
|
assert.equal(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
|
|
|
|
2014-07-30 10:00:20 +02:00
|
|
|
// FIXME: remove in 2.x.y
|
2014-06-01 08:05:47 +02:00
|
|
|
it('fails when there is no private key', function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = HDNode.fromBase58(fixtures.valid[0].master.base58)
|
2014-05-31 07:24:03 +02:00
|
|
|
|
|
|
|
assert.throws(function() {
|
2014-03-31 05:47:47 +02:00
|
|
|
hd.toBase58(true)
|
2014-05-31 07:24:03 +02:00
|
|
|
}, /Missing private key/)
|
2014-01-16 08:03:09 +01:00
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2014-05-30 11:00:49 +02:00
|
|
|
describe('fromBase58', function() {
|
2014-06-01 08:05:47 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('imports ' + f.master.base58 + ' (public) correctly', function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = HDNode.fromBase58(f.master.base58)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
|
|
|
assert.equal(hd.toBase58(), f.master.base58)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('imports ' + f.master.base58Priv + ' (private) correctly', function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = HDNode.fromBase58(f.master.base58Priv)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2014-06-03 08:20:37 +02:00
|
|
|
assert.equal(hd.toBase58(), f.master.base58Priv)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-30 11:00:49 +02:00
|
|
|
fixtures.invalid.fromBase58.forEach(function(f) {
|
|
|
|
it('throws on ' + f.string, function() {
|
|
|
|
assert.throws(function() {
|
2014-11-29 02:12:24 +01:00
|
|
|
var network = networks[f.network]
|
|
|
|
|
|
|
|
HDNode.fromBase58(f.string, network)
|
2014-05-30 11:00:49 +02:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-01 08:39:07 +02:00
|
|
|
describe('fromBuffer/fromHex', function() {
|
2014-06-01 08:05:47 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('imports ' + f.master.hex + ' (public) correctly', function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = HDNode.fromHex(f.master.hex)
|
2014-05-31 07:32:31 +02:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
assert.equal(hd.toBuffer().toString('hex'), f.master.hex)
|
2014-05-31 07:32:31 +02:00
|
|
|
})
|
|
|
|
})
|
2014-03-11 15:41:20 +01:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('imports ' + f.master.hexPriv + ' (private) correctly', function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = HDNode.fromHex(f.master.hexPriv)
|
2014-03-11 15:41:20 +01:00
|
|
|
|
2014-06-03 08:20:37 +02:00
|
|
|
assert.equal(hd.toBuffer().toString('hex'), f.master.hexPriv)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-03-11 15:41:20 +01:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
fixtures.invalid.fromBuffer.forEach(function(f) {
|
2014-06-04 07:30:53 +02:00
|
|
|
it('throws on ' + f.hex, function() {
|
2014-06-01 08:05:47 +02:00
|
|
|
assert.throws(function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
HDNode.fromHex(f.hex)
|
2014-06-01 08:05:47 +02:00
|
|
|
}, new RegExp(f.exception))
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-01-16 08:03:09 +01:00
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2014-06-01 08:39:07 +02:00
|
|
|
describe('toBuffer/toHex', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('exports ' + f.master.hex + ' (public) correctly', function() {
|
2014-07-30 10:00:20 +02:00
|
|
|
var hd = HDNode.fromSeedHex(f.master.seed).neutered()
|
2014-06-01 08:39:07 +02:00
|
|
|
|
2014-07-30 10:00:20 +02:00
|
|
|
assert.equal(hd.toHex(), f.master.hex)
|
2014-06-01 08:39:07 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('exports ' + f.master.hexPriv + ' (private) correctly', function() {
|
2014-11-29 02:04:02 +01:00
|
|
|
var network = networks[f.network]
|
|
|
|
var hd = HDNode.fromSeedHex(f.master.seed, network)
|
2014-06-01 08:39:07 +02:00
|
|
|
|
2014-07-30 10:00:20 +02:00
|
|
|
assert.equal(hd.toHex(), f.master.hexPriv)
|
2014-06-01 08:39:07 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-07-30 10:00:20 +02:00
|
|
|
// FIXME: remove in 2.x.y
|
2014-06-01 08:39:07 +02:00
|
|
|
it('fails when there is no private key', function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = HDNode.fromHex(fixtures.valid[0].master.hex)
|
2014-06-01 08:39:07 +02:00
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
hd.toHex(true)
|
|
|
|
}, /Missing private key/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
describe('getIdentifier', function() {
|
|
|
|
var f = fixtures.valid[0]
|
2014-05-25 03:48:16 +02:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
it('returns the identifier for ' + f.master.fingerprint, function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = HDNode.fromBase58(f.master.base58)
|
2014-05-25 03:48:16 +02:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
assert.equal(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
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
describe('getFingerprint', function() {
|
|
|
|
var f = fixtures.valid[0]
|
2014-05-04 03:40:48 +02:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
it('returns the fingerprint for ' + f.master.fingerprint, function() {
|
2014-06-03 09:08:42 +02:00
|
|
|
var hd = HDNode.fromBase58(f.master.base58)
|
2014-05-04 04:13:25 +02:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
assert.equal(hd.getFingerprint().toString('hex'), f.master.fingerprint)
|
2014-05-04 03:40:48 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
describe('getAddress', function() {
|
2014-11-29 02:04:02 +01:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('returns ' + f.master.address + ' for ' + f.master.fingerprint, function() {
|
|
|
|
var hd = HDNode.fromBase58(f.master.base58)
|
2014-05-30 10:41:03 +02:00
|
|
|
|
2014-11-29 02:04:02 +01:00
|
|
|
assert.equal(hd.getAddress().toString(), f.master.address)
|
|
|
|
})
|
2014-05-30 10:41:03 +02:00
|
|
|
})
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
2014-05-30 10:41:03 +02:00
|
|
|
|
2014-07-28 17:34:46 +02:00
|
|
|
describe('neutered', function() {
|
|
|
|
var f = fixtures.valid[0]
|
|
|
|
|
|
|
|
it('strips all private information', function() {
|
|
|
|
var hd = HDNode.fromBase58(f.master.base58)
|
|
|
|
var hdn = hd.neutered()
|
|
|
|
|
|
|
|
assert.equal(hdn.privKey, undefined)
|
|
|
|
assert.equal(hdn.pubKey.toHex(), hd.pubKey.toHex())
|
|
|
|
assert.equal(hdn.chainCode, hd.chainCode)
|
|
|
|
assert.equal(hdn.depth, hd.depth)
|
|
|
|
assert.equal(hdn.index, hd.index)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
describe('derive', function() {
|
2014-11-29 02:04:02 +01:00
|
|
|
function verifyVector(hd, network, v, depth) {
|
|
|
|
assert.equal(hd.privKey.toWIF(network), v.wif)
|
2014-06-03 09:30:05 +02:00
|
|
|
assert.equal(hd.pubKey.toHex(), v.pubKey)
|
2014-06-01 08:05:47 +02:00
|
|
|
assert.equal(hd.chainCode.toString('hex'), v.chainCode)
|
|
|
|
assert.equal(hd.depth, depth || 0)
|
|
|
|
|
2014-06-03 08:12:36 +02:00
|
|
|
if (v.hardened) {
|
2014-06-03 09:08:42 +02:00
|
|
|
assert.equal(hd.index, v.m + HDNode.HIGHEST_BIT)
|
2014-06-01 08:05:47 +02:00
|
|
|
} else {
|
|
|
|
assert.equal(hd.index, v.m)
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 10:41:03 +02:00
|
|
|
|
2014-11-29 02:04:02 +01:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
var network = networks[f.network]
|
|
|
|
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
|
2014-06-01 08:05:47 +02: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-02-28 10:26:16 +01:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
} else {
|
|
|
|
hd = hd.derive(c.m)
|
|
|
|
}
|
2014-05-25 03:58:10 +02:00
|
|
|
|
2014-11-29 02:04:02 +01:00
|
|
|
verifyVector(hd, network, 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
|
|
|
|
2014-06-03 09:22:13 +02:00
|
|
|
it('works for Private -> public (neutered)', function() {
|
|
|
|
var f = fixtures.valid[1]
|
|
|
|
var c = f.children[0]
|
|
|
|
|
2014-07-28 17:34:56 +02:00
|
|
|
var master = HDNode.fromBase58(f.master.base58Priv)
|
|
|
|
var child = master.derive(c.m).neutered()
|
2014-06-03 09:22:13 +02:00
|
|
|
|
2014-07-28 17:34:56 +02:00
|
|
|
assert.equal(child.toBase58(), c.base58)
|
2014-06-03 09:22:13 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('works for Private -> public (neutered, hardened)', function() {
|
|
|
|
var f = fixtures.valid[0]
|
|
|
|
var c = f.children[0]
|
|
|
|
|
2014-07-28 17:34:56 +02:00
|
|
|
var master = HDNode.fromBase58(f.master.base58Priv)
|
|
|
|
var child = master.deriveHardened(c.m).neutered()
|
2014-06-03 09:22:13 +02:00
|
|
|
|
2014-07-28 17:34:56 +02:00
|
|
|
assert.equal(child.toBase58(), c.base58)
|
2014-06-03 09:22:13 +02: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]
|
|
|
|
|
2014-07-28 17:34:56 +02:00
|
|
|
var master = HDNode.fromBase58(f.master.base58)
|
|
|
|
var child = master.derive(c.m)
|
2014-06-03 08:13:20 +02:00
|
|
|
|
|
|
|
assert.equal(child.toBase58(), c.base58)
|
|
|
|
})
|
|
|
|
|
2014-06-03 09:22:13 +02:00
|
|
|
it('throws on Public -> public (hardened)', function() {
|
|
|
|
var f = fixtures.valid[0]
|
2014-06-03 08:13:20 +02:00
|
|
|
var c = f.children[0]
|
|
|
|
|
2014-07-28 17:34:56 +02:00
|
|
|
var master = HDNode.fromBase58(f.master.base58)
|
2014-06-03 08:13:20 +02: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
|
|
|
})
|