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-06-01 08:05:47 +02:00
|
|
|
var sec = require('../src/sec')
|
|
|
|
var ecparams = sec("secp256k1")
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
var BigInteger = require('bigi')
|
2014-05-13 09:55:53 +02:00
|
|
|
var HDWallet = require('../src/hdwallet')
|
2014-04-16 22:18:31 +02:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
var fixtures = require('./fixtures/hdwallet.json')
|
2014-01-16 08:03:09 +01:00
|
|
|
|
|
|
|
describe('HDWallet', function() {
|
2014-06-01 08:05:47 +02:00
|
|
|
describe('Constructor', function() {
|
|
|
|
var D = BigInteger.ONE
|
|
|
|
var Q = ecparams.getG().multiply(D)
|
|
|
|
var chainCode = new Buffer(32)
|
|
|
|
chainCode.fill(1)
|
|
|
|
|
|
|
|
it('calculates the publicKey from a BigInteger', function() {
|
|
|
|
var hd = new HDWallet(D, chainCode)
|
|
|
|
|
|
|
|
assert(hd.pub.Q.equals(Q))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('only uses compressed points', function() {
|
|
|
|
var hd = new HDWallet(Q, chainCode)
|
|
|
|
var hdP = new HDWallet(D, chainCode)
|
|
|
|
|
|
|
|
assert.strictEqual(hd.pub.compressed, true)
|
|
|
|
assert.strictEqual(hdP.pub.compressed, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('has a default depth/index of 0', function() {
|
|
|
|
var hd = new HDWallet(Q, chainCode)
|
|
|
|
|
|
|
|
assert.strictEqual(hd.depth, 0)
|
|
|
|
assert.strictEqual(hd.index, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('defaults to the bitcoin network', function() {
|
|
|
|
var hd = new HDWallet(Q, chainCode)
|
|
|
|
|
|
|
|
assert.equal(hd.network, networks.bitcoin)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('supports alternative networks', function() {
|
|
|
|
var hd = new HDWallet(Q, chainCode, networks.testnet)
|
|
|
|
|
|
|
|
assert.equal(hd.network, networks.testnet)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws an exception when an unknown network is given', function() {
|
|
|
|
assert.throws(function() {
|
|
|
|
new HDWallet(D, chainCode, {})
|
|
|
|
}, /Unknown BIP32 constants for network/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('fromSeed*', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('calculates privKey and chainCode for ' + f.master.fingerprint, function() {
|
|
|
|
var hd = HDWallet.fromSeedHex(f.master.seed)
|
|
|
|
|
|
|
|
assert.equal(hd.priv.toWIF(), f.master.wif)
|
|
|
|
assert.equal(hd.chainCode.toString('hex'), f.master.chainCode)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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() {
|
|
|
|
var hd = HDWallet.fromSeedHex(f.master.seed)
|
|
|
|
|
|
|
|
assert.equal(hd.toBase58(), f.master.base58)
|
|
|
|
})
|
|
|
|
})
|
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() {
|
|
|
|
var hd = HDWallet.fromSeedHex(f.master.seed)
|
|
|
|
|
|
|
|
assert.equal(hd.toBase58(true), f.master.base58Priv)
|
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-01-16 08:03:09 +01:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
it('fails when there is no private key', function() {
|
|
|
|
var hd = HDWallet.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() {
|
|
|
|
var hd = HDWallet.fromBase58(f.master.base58)
|
|
|
|
|
|
|
|
assert.equal(hd.toBase58(), f.master.base58)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('imports ' + f.master.base58Priv + ' (private) correctly', function() {
|
|
|
|
var hd = HDWallet.fromBase58(f.master.base58Priv)
|
|
|
|
|
|
|
|
assert.equal(hd.toBase58(true), f.master.base58Priv)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-30 11:00:49 +02:00
|
|
|
fixtures.invalid.fromBase58.forEach(function(f) {
|
|
|
|
it('throws on ' + f.string, function() {
|
|
|
|
assert.throws(function() {
|
|
|
|
HDWallet.fromBase58(f.string)
|
|
|
|
}, 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-01 08:39:07 +02:00
|
|
|
var hd = HDWallet.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-01 08:39:07 +02:00
|
|
|
var hd = HDWallet.fromHex(f.master.hexPriv)
|
2014-03-11 15:41:20 +01:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
assert.equal(hd.toBuffer(true).toString('hex'), f.master.hexPriv)
|
|
|
|
})
|
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) {
|
|
|
|
it('throws on ' + f.string, function() {
|
|
|
|
assert.throws(function() {
|
2014-06-01 08:39:07 +02:00
|
|
|
HDWallet.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() {
|
|
|
|
var hd = HDWallet.fromSeedHex(f.master.seed)
|
|
|
|
|
|
|
|
assert.equal(hd.toHex(), f.master.hex)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('exports ' + f.master.hexPriv + ' (private) correctly', function() {
|
|
|
|
var hd = HDWallet.fromSeedHex(f.master.seed)
|
|
|
|
|
|
|
|
assert.equal(hd.toHex(true), f.master.hexPriv)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('fails when there is no private key', function() {
|
|
|
|
var hd = HDWallet.fromHex(fixtures.valid[0].master.hex)
|
|
|
|
|
|
|
|
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() {
|
|
|
|
var hd = HDWallet.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() {
|
|
|
|
var hd = HDWallet.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() {
|
|
|
|
var f = fixtures.valid[0]
|
|
|
|
|
|
|
|
it('returns the Address (pubKeyHash) for ' + f.master.fingerprint, function() {
|
|
|
|
var hd = HDWallet.fromBase58(f.master.base58)
|
2014-05-30 10:41:03 +02:00
|
|
|
|
2014-06-01 08:05:47 +02: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
|
|
|
it('supports alternative networks', function() {
|
|
|
|
var hd = HDWallet.fromBase58(f.master.base58)
|
|
|
|
hd.network = networks.testnet
|
2014-05-30 10:41:03 +02:00
|
|
|
|
2014-06-01 08:05:47 +02:00
|
|
|
assert.equal(hd.getAddress().version, networks.testnet.pubKeyHash)
|
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-06-01 08:05:47 +02:00
|
|
|
describe('derive', function() {
|
|
|
|
function verifyVector(hd, v, depth) {
|
|
|
|
assert.equal(hd.priv.toWIF(), v.wif)
|
|
|
|
assert.equal(hd.pub.toHex(), v.pubKey)
|
|
|
|
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) {
|
|
|
|
assert.equal(hd.index, v.m + HDWallet.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-06-01 08:05:47 +02:00
|
|
|
fixtures.valid.forEach(function(f, j) {
|
|
|
|
var hd = HDWallet.fromSeedHex(f.master.seed)
|
2014-02-28 10:26:16 +01:00
|
|
|
|
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) {
|
|
|
|
hd = hd.derivePrivate(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-06-01 08:05:47 +02:00
|
|
|
verifyVector(hd, c, i + 1)
|
|
|
|
})
|
|
|
|
})
|
2014-05-25 03:58:10 +02:00
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-01-16 08:03:09 +01:00
|
|
|
})
|