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-09-12 07:10:44 +02:00
|
|
|
var ecdsa = require('../src/ecdsa')
|
2018-03-20 03:19:39 +01:00
|
|
|
var mockme = require('./mockme')
|
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')
|
2015-09-12 07:10:44 +02:00
|
|
|
var curve = ecdsa.__curve
|
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])
|
|
|
|
}
|
|
|
|
|
2016-10-21 03:47:54 +02:00
|
|
|
var validAll = []
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
function addNetwork (n) {
|
|
|
|
n.network = f.network
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
validAll = validAll.concat(addNetwork(f.master), f.children.map(addNetwork))
|
|
|
|
})
|
|
|
|
|
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)
|
2017-04-19 09:39:16 +02:00
|
|
|
chainCode = Buffer.alloc(32, 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 () {
|
2017-04-19 09:39:16 +02:00
|
|
|
new HDNode(keyPair, Buffer.alloc(20))
|
2016-09-30 08:25:13 +02:00
|
|
|
}, /Expected property "1" of type Buffer\(Length: 32\), got Buffer\(Length: 20\)/)
|
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
|
|
|
|
2018-03-20 03:19:39 +01:00
|
|
|
it('throws if IL is not within interval [1, n - 1] | IL === 0', mockme(function () {
|
|
|
|
this.mock(BigInteger, 'fromBuffer', function () {
|
|
|
|
return BigInteger.ZERO
|
|
|
|
}, 1)
|
2015-09-12 07:10:44 +02:00
|
|
|
|
|
|
|
assert.throws(function () {
|
|
|
|
HDNode.fromSeedHex('ffffffffffffffffffffffffffffffff')
|
|
|
|
}, /Private key must be greater than 0/)
|
|
|
|
}))
|
|
|
|
|
2018-03-20 03:19:39 +01:00
|
|
|
it('throws if IL is not within interval [1, n - 1] | IL === n', mockme(function () {
|
|
|
|
this.mock(BigInteger, 'fromBuffer', function () {
|
|
|
|
return curve.n
|
|
|
|
}, 1)
|
2015-09-12 07:10:44 +02:00
|
|
|
|
|
|
|
assert.throws(function () {
|
|
|
|
HDNode.fromSeedHex('ffffffffffffffffffffffffffffffff')
|
|
|
|
}, /Private key must be less than the curve order/)
|
|
|
|
}))
|
|
|
|
|
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-09-21 08:34:25 +02:00
|
|
|
describe('ECPair wrappers', function () {
|
|
|
|
var keyPair, hd, hash
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
keyPair = ECPair.makeRandom()
|
2017-04-19 09:39:16 +02:00
|
|
|
hash = Buffer.alloc(32)
|
2015-09-21 08:34:25 +02:00
|
|
|
|
2017-04-19 09:39:16 +02:00
|
|
|
var chainCode = Buffer.alloc(32)
|
2015-09-21 08:34:25 +02:00
|
|
|
hd = new HDNode(keyPair, chainCode)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getAddress', function () {
|
2018-03-20 03:19:39 +01:00
|
|
|
it('wraps keyPair.getAddress', mockme(function () {
|
|
|
|
this.mock(hd.keyPair, 'getAddress', function () {
|
|
|
|
return 'foo'
|
|
|
|
}, 1)
|
2015-09-21 08:34:25 +02:00
|
|
|
|
2018-03-20 03:19:39 +01:00
|
|
|
assert.strictEqual(hd.getAddress(), 'foo')
|
2015-09-21 08:34:25 +02:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2015-09-21 09:37:21 +02:00
|
|
|
describe('getNetwork', function () {
|
2018-03-20 03:19:39 +01:00
|
|
|
it('wraps keyPair.getNetwork', mockme(function () {
|
|
|
|
this.mock(hd.keyPair, 'getNetwork', function () {
|
|
|
|
return 'foo'
|
|
|
|
}, 1)
|
2015-09-21 09:37:21 +02:00
|
|
|
|
2018-03-20 03:19:39 +01:00
|
|
|
assert.strictEqual(hd.getNetwork(), 'foo')
|
2015-09-21 09:37:21 +02:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2015-09-21 08:44:53 +02:00
|
|
|
describe('getPublicKeyBuffer', function () {
|
2018-03-20 03:19:39 +01:00
|
|
|
it('wraps keyPair.getPublicKeyBuffer', mockme(function () {
|
|
|
|
this.mock(hd.keyPair, 'getPublicKeyBuffer', function () {
|
|
|
|
return 'foo'
|
|
|
|
}, 1)
|
2015-09-21 08:44:53 +02:00
|
|
|
|
2018-03-20 03:19:39 +01:00
|
|
|
assert.strictEqual(hd.getPublicKeyBuffer(), 'foo')
|
2015-09-21 08:44:53 +02:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2015-09-21 08:34:25 +02:00
|
|
|
describe('sign', function () {
|
2018-03-20 03:19:39 +01:00
|
|
|
it('wraps keyPair.sign', mockme(function () {
|
|
|
|
this.mock(hd.keyPair, 'sign', function (h) {
|
|
|
|
assert.strictEqual(hash, h)
|
|
|
|
return 'foo'
|
|
|
|
}, 1)
|
2015-09-21 08:34:25 +02:00
|
|
|
|
2018-03-20 03:19:39 +01:00
|
|
|
assert.strictEqual(hd.sign(hash), 'foo')
|
2015-09-21 08:34:25 +02:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('verify', function () {
|
|
|
|
var signature
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
signature = hd.sign(hash)
|
|
|
|
})
|
|
|
|
|
2018-03-20 03:19:39 +01:00
|
|
|
it('wraps keyPair.verify', mockme(function () {
|
|
|
|
this.mock(hd.keyPair, 'verify', function (h, s) {
|
|
|
|
assert.strictEqual(hash, h)
|
|
|
|
assert.strictEqual(signature, s)
|
|
|
|
return 'foo'
|
|
|
|
}, 1)
|
2015-09-21 08:34:25 +02:00
|
|
|
|
2018-03-20 03:19:39 +01:00
|
|
|
assert.strictEqual(hd.verify(hash, signature), 'foo')
|
2015-09-21 08:34:25 +02:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-10-21 03:47:54 +02:00
|
|
|
describe('fromBase58 / toBase58', function () {
|
|
|
|
validAll.forEach(function (f) {
|
|
|
|
it('exports ' + f.base58 + ' (public) correctly', function () {
|
|
|
|
var hd = HDNode.fromBase58(f.base58, NETWORKS_LIST)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2016-10-21 03:47:54 +02:00
|
|
|
assert.strictEqual(hd.toBase58(), f.base58)
|
|
|
|
assert.throws(function () { hd.keyPair.toWIF() }, /Missing private key/)
|
2014-06-01 08:05:47 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-10-21 03:47:54 +02:00
|
|
|
validAll.forEach(function (f) {
|
|
|
|
it('exports ' + f.base58Priv + ' (private) correctly', function () {
|
|
|
|
var hd = HDNode.fromBase58(f.base58Priv, NETWORKS_LIST)
|
2014-06-01 08:05:47 +02:00
|
|
|
|
2016-10-21 03:47:54 +02:00
|
|
|
assert.strictEqual(hd.toBase58(), f.base58Priv)
|
|
|
|
assert.strictEqual(hd.keyPair.toWIF(), f.wif)
|
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 () {
|
2016-02-25 03:41:35 +01:00
|
|
|
var networks = f.network ? NETWORKS[f.network] : NETWORKS_LIST
|
2014-11-29 02:12:24 +01:00
|
|
|
|
2016-02-25 03:41:35 +01:00
|
|
|
HDNode.fromBase58(f.string, networks)
|
2014-05-30 11:00:49 +02:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('getIdentifier', function () {
|
2016-10-21 03:47:54 +02:00
|
|
|
validAll.forEach(function (f) {
|
|
|
|
it('returns the identifier for ' + f.fingerprint, function () {
|
|
|
|
var hd = HDNode.fromBase58(f.base58, NETWORKS_LIST)
|
2014-05-25 03:48:16 +02:00
|
|
|
|
2016-10-21 03:47:54 +02:00
|
|
|
assert.strictEqual(hd.getIdentifier().toString('hex'), f.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 () {
|
2016-10-21 03:47:54 +02:00
|
|
|
validAll.forEach(function (f) {
|
|
|
|
it('returns the fingerprint for ' + f.fingerprint, function () {
|
|
|
|
var hd = HDNode.fromBase58(f.base58, NETWORKS_LIST)
|
2014-05-04 04:13:25 +02:00
|
|
|
|
2016-10-21 03:47:54 +02:00
|
|
|
assert.strictEqual(hd.getFingerprint().toString('hex'), f.fingerprint)
|
|
|
|
})
|
2014-05-04 03:40:48 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-10-21 03:47:54 +02:00
|
|
|
describe('neutered / isNeutered', function () {
|
|
|
|
validAll.forEach(function (f) {
|
|
|
|
it('drops the private key for ' + f.fingerprint, function () {
|
|
|
|
var hd = HDNode.fromBase58(f.base58Priv, NETWORKS_LIST)
|
|
|
|
var hdn = hd.neutered()
|
|
|
|
|
|
|
|
assert.notEqual(hdn.keyPair, hd.keyPair)
|
|
|
|
assert.throws(function () { hdn.keyPair.toWIF() }, /Missing private key/)
|
|
|
|
assert.strictEqual(hdn.toBase58(), f.base58)
|
|
|
|
assert.strictEqual(hdn.chainCode, hd.chainCode)
|
|
|
|
assert.strictEqual(hdn.depth, f.depth >>> 0)
|
|
|
|
assert.strictEqual(hdn.index, f.index >>> 0)
|
|
|
|
assert.strictEqual(hdn.isNeutered(), true)
|
|
|
|
|
|
|
|
// does not modify the original
|
|
|
|
assert.strictEqual(hd.toBase58(), f.base58Priv)
|
|
|
|
assert.strictEqual(hd.isNeutered(), false)
|
|
|
|
})
|
2014-07-28 17:34:46 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('derive', function () {
|
2016-10-21 04:32:18 +02:00
|
|
|
function verifyVector (hd, v) {
|
2016-10-21 04:25:25 +02:00
|
|
|
if (hd.isNeutered()) {
|
|
|
|
assert.strictEqual(hd.toBase58(), v.base58)
|
|
|
|
} else {
|
|
|
|
assert.strictEqual(hd.neutered().toBase58(), v.base58)
|
|
|
|
assert.strictEqual(hd.toBase58(), v.base58Priv)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual(hd.getFingerprint().toString('hex'), v.fingerprint)
|
|
|
|
assert.strictEqual(hd.getIdentifier().toString('hex'), v.identifier)
|
|
|
|
assert.strictEqual(hd.getAddress(), v.address)
|
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)
|
2016-10-21 04:32:18 +02:00
|
|
|
assert.strictEqual(hd.depth, v.depth >>> 0)
|
2016-10-21 03:47:54 +02:00
|
|
|
assert.strictEqual(hd.index, v.index >>> 0)
|
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)
|
2016-02-06 02:02:39 +01:00
|
|
|
var master = hd
|
2014-02-28 10:26:16 +01:00
|
|
|
|
2016-02-06 02:02:39 +01:00
|
|
|
// testing deriving path from master
|
|
|
|
f.children.forEach(function (c) {
|
2016-10-21 04:39:28 +02:00
|
|
|
it(c.path + ' from ' + f.master.fingerprint + ' by path', function () {
|
|
|
|
var child = master.derivePath(c.path)
|
|
|
|
var childNoM = master.derivePath(c.path.slice(2)) // no m/ on path
|
2016-02-06 02:02:39 +01:00
|
|
|
|
2016-10-21 04:32:18 +02:00
|
|
|
verifyVector(child, c)
|
2016-10-21 04:34:26 +02:00
|
|
|
verifyVector(childNoM, c)
|
2016-02-06 02:02:39 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-02-12 03:17:56 +01:00
|
|
|
// testing deriving path from children
|
|
|
|
f.children.forEach(function (c, i) {
|
2016-10-21 04:39:28 +02:00
|
|
|
var cn = master.derivePath(c.path)
|
2016-02-06 02:02:39 +01:00
|
|
|
|
2016-02-12 03:17:56 +01:00
|
|
|
f.children.slice(i + 1).forEach(function (cc) {
|
2016-10-21 04:39:28 +02:00
|
|
|
it(cc.path + ' from ' + c.fingerprint + ' by path', function () {
|
|
|
|
var ipath = cc.path.slice(2).split('/').slice(i + 1).join('/')
|
|
|
|
var child = cn.derivePath(ipath)
|
2016-10-21 04:32:18 +02:00
|
|
|
verifyVector(child, cc)
|
2016-02-06 02:02:39 +01:00
|
|
|
|
2016-02-12 03:17:56 +01:00
|
|
|
assert.throws(function () {
|
2016-10-21 04:39:28 +02:00
|
|
|
cn.derivePath('m/' + ipath)
|
2016-02-12 03:17:56 +01:00
|
|
|
}, /Not a master node/)
|
|
|
|
})
|
2016-02-06 02:02:39 +01:00
|
|
|
})
|
|
|
|
})
|
2016-10-21 04:25:25 +02:00
|
|
|
|
|
|
|
// FIXME: test data is only testing Private -> private for now
|
2017-01-06 03:47:41 +01:00
|
|
|
f.children.forEach(function (c) {
|
2016-10-21 04:25:25 +02:00
|
|
|
if (c.m === undefined) return
|
|
|
|
|
2016-10-21 04:39:28 +02:00
|
|
|
it(c.path + ' from ' + f.master.fingerprint, function () {
|
2016-10-21 04:25:25 +02:00
|
|
|
if (c.hardened) {
|
|
|
|
hd = hd.deriveHardened(c.m)
|
|
|
|
} else {
|
|
|
|
hd = hd.derive(c.m)
|
|
|
|
}
|
|
|
|
|
2016-10-21 04:32:18 +02:00
|
|
|
verifyVector(hd, c)
|
2016-10-21 04:25:25 +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
|
|
|
|
2016-10-21 03:47:54 +02:00
|
|
|
assert.strictEqual(c.base58, child.toBase58())
|
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
|
|
|
|
2016-10-21 03:47:54 +02:00
|
|
|
assert.strictEqual(c.base58, child.toBase58())
|
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/)
|
|
|
|
})
|
2016-02-06 17:12:13 +01:00
|
|
|
|
2016-02-14 16:12:16 +01:00
|
|
|
it('throws on wrong types', function () {
|
2016-02-06 17:12:13 +01:00
|
|
|
var f = fixtures.valid[0]
|
|
|
|
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
|
|
|
|
|
2016-02-14 16:12:16 +01:00
|
|
|
fixtures.invalid.derive.forEach(function (fx) {
|
|
|
|
assert.throws(function () {
|
|
|
|
master.derive(fx)
|
|
|
|
}, /Expected UInt32/)
|
|
|
|
})
|
2016-02-06 17:12:13 +01:00
|
|
|
|
2016-02-14 16:12:16 +01:00
|
|
|
fixtures.invalid.deriveHardened.forEach(function (fx) {
|
|
|
|
assert.throws(function () {
|
|
|
|
master.deriveHardened(fx)
|
|
|
|
}, /Expected UInt31/)
|
|
|
|
})
|
2016-02-06 17:12:13 +01:00
|
|
|
|
2016-02-14 16:12:16 +01:00
|
|
|
fixtures.invalid.derivePath.forEach(function (fx) {
|
|
|
|
assert.throws(function () {
|
|
|
|
master.derivePath(fx)
|
2016-10-10 04:34:40 +02:00
|
|
|
}, /Expected BIP32 derivation path/)
|
2016-02-14 16:12:16 +01:00
|
|
|
})
|
2016-02-06 17:12:13 +01:00
|
|
|
})
|
2016-10-20 05:06:39 +02:00
|
|
|
|
|
|
|
it('works when private key has leading zeros', function () {
|
|
|
|
var key = 'xprv9s21ZrQH143K3ckY9DgU79uMTJkQRLdbCCVDh81SnxTgPzLLGax6uHeBULTtaEtcAvKjXfT7ZWtHzKjTpujMkUd9dDb8msDeAfnJxrgAYhr'
|
|
|
|
var hdkey = HDNode.fromBase58(key)
|
|
|
|
assert.strictEqual(hdkey.keyPair.d.toBuffer(32).toString('hex'), '00000055378cf5fafb56c711c674143f9b0ee82ab0ba2924f19b64f5ae7cdbfd')
|
|
|
|
var child = hdkey.derivePath('m/44\'/0\'/0\'/0/0\'')
|
|
|
|
assert.strictEqual(child.keyPair.d.toBuffer().toString('hex'), '3348069561d2a0fb925e74bf198762acc47dce7db27372257d2d959a9e6f8aeb')
|
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-01-16 08:03:09 +01:00
|
|
|
})
|