diff --git a/docs/hierarchical.md b/docs/hierarchical.md index dc35571e5..bac5e37c6 100644 --- a/docs/hierarchical.md +++ b/docs/hierarchical.md @@ -15,9 +15,9 @@ var HDPrivateKey = bitcore.HDPrivateKey; var hdPrivateKey = new HDPrivateKey(); var retrieved = new HDPrivateKey('xpriv...'); -var derived = hdPrivateKey.derive("m/0'"); -var derivedByNumber = hdPrivateKey.derive(1).derive(2, true); -var derivedByArgument = hdPrivateKey.derive("m/1/2'"); +var derived = hdPrivateKey.deriveChild("m/0'"); +var derivedByNumber = hdPrivateKey.deriveChild(1).deriveChild(2, true); +var derivedByArgument = hdPrivateKey.deriveChild("m/1/2'"); assert(derivedByNumber.xprivkey === derivedByArgument.xprivkey); var address = derived.privateKey.toAddress(); @@ -39,5 +39,11 @@ try { } var address = new Address(hdPublicKey.publicKey, Networks.livenet); -var derivedAddress = new Address(hdPublicKey.derive(100).publicKey, Networks.testnet); +var derivedAddress = new Address(hdPublicKey.deriveChild(100).publicKey, Networks.testnet); ``` + +## Upgrading from v0.13.x and previous to v1.0.0 + +There was a bug that was discovered with derivation that would incorrectly calculate the child key against the [BIP32 specification](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki). The method `derive` has been deprecated and replaced with `deriveChild` and `deriveNonCompliantChild`. As the names indicate `deriveNonCompliantChild` will derive using the non-BIP32 derivation and is the equivalent of `derive` in v0.13 and previous versions. The `deriveNonCompliantChild` method should not be used unless you're upgrading and need to maintain compatibility with the old derivation. + +The bug only affected hardened derivations using an extended private key, and did not affect public key derivation. It also did not affect every derivation and would happen 1 in 256 times where where the private key for the extended private key had a leading zero *(e.g. any private key less than or equal to '0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')*. The leading zero was not included in serialization before hashing to derive a child key, as it should have been. diff --git a/lib/hdkeycache.js b/lib/hdkeycache.js index 721d9da48..606e00c1a 100644 --- a/lib/hdkeycache.js +++ b/lib/hdkeycache.js @@ -8,17 +8,17 @@ module.exports = { _usedIndex: {}, _CACHE_SIZE: 5000, - get: function(xkey, number, hardened) { + get: function(xkey, number, hardened, nonCompliant) { hardened = !!hardened; - var key = xkey + '/' + number + '/' + hardened; + var key = (nonCompliant ? '1' : '0') + xkey + '/' + number + '/' + hardened; if (this._cache[key]) { this._cacheHit(key); return this._cache[key]; } }, - set: function(xkey, number, hardened, derived) { + set: function(xkey, number, hardened, derived, nonCompliant) { hardened = !!hardened; - var key = xkey + '/' + number + '/' + hardened; + var key = (nonCompliant ? '1' : '0') + xkey + '/' + number + '/' + hardened; this._cache[key] = derived; this._cacheHit(key); }, diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 3826e0b40..e90d86da9 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -127,6 +127,11 @@ HDPrivateKey._getDerivationIndexes = function(path) { return _.any(indexes, isNaN) ? null : indexes; }; +HDPrivateKey.prototype.derive = function() { + throw new Error('Method has been deprecated, please use deriveChild or deriveNonCompliantChild' + + 'and see documentation for more information'); +}; + /** * Get a derived child based on a string or number. * @@ -142,25 +147,48 @@ HDPrivateKey._getDerivationIndexes = function(path) { * @example * ```javascript * var parent = new HDPrivateKey('xprv...'); - * var child_0_1_2h = parent.derive(0).derive(1).derive(2, true); - * var copy_of_child_0_1_2h = parent.derive("m/0/1/2'"); + * var child_0_1_2h = parent.deriveChild(0).deriveChild(1).deriveChild(2, true); + * var copy_of_child_0_1_2h = parent.deriveChild("m/0/1/2'"); * assert(child_0_1_2h.xprivkey === copy_of_child_0_1_2h); * ``` * * @param {string|number} arg * @param {boolean?} hardened */ -HDPrivateKey.prototype.derive = function(arg, hardened) { +HDPrivateKey.prototype.deriveChild = function(arg, hardened) { + var derived = this._derive(arg, hardened, false); + return derived; +}; + +/** + * WARNING: If this is a new implementation you should NOT use this method, you should be using + * `derive` instead. + * + * This method is explicitly for use and compatibility with an implementation that + * was not compliant with BIP32 regarding the derivation algorithm. The private key + * must be 32 bytes hashing, and this implementation will use the non-zero padded + * serialization of a private key, such that it's still possible to derive the privateKey + * to recover those funds. + * + * @param {string|number} arg + * @param {boolean?} hardened + */ +HDPrivateKey.prototype.deriveNonCompliantChild = function(arg, hardened) { + var derived = this._derive(arg, hardened, true); + return derived; +}; + +HDPrivateKey.prototype._derive = function(arg, hardened, nonCompliant) { if (_.isNumber(arg)) { - return this._deriveWithNumber(arg, hardened); + return this._deriveWithNumber(arg, hardened, nonCompliant); } else if (_.isString(arg)) { - return this._deriveFromString(arg); + return this._deriveFromString(arg, nonCompliant); } else { throw new hdErrors.InvalidDerivationArgument(arg); } }; -HDPrivateKey.prototype._deriveWithNumber = function(index, hardened) { +HDPrivateKey.prototype._deriveWithNumber = function(index, hardened, nonCompliant) { /* jshint maxstatements: 20 */ /* jshint maxcomplexity: 10 */ if (!HDPrivateKey.isValidPath(index, hardened)) { @@ -172,15 +200,23 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened) { index += HDPrivateKey.Hardened; } - var cached = HDKeyCache.get(this.xprivkey, index, hardened); + var cached = HDKeyCache.get(this.xprivkey, index, hardened, nonCompliant); if (cached) { return cached; } var indexBuffer = BufferUtil.integerAsBuffer(index); var data; - if (hardened) { - data = BufferUtil.concat([new buffer.Buffer([0]), this.privateKey.toBuffer(), indexBuffer]); + if (hardened && nonCompliant) { + // The private key serialization in this case will not be exactly 32 bytes and can be + // any value less, and the value is not zero-padded. + var nonZeroPadded = this.privateKey.bn.toBuffer(); + data = BufferUtil.concat([new buffer.Buffer([0]), nonZeroPadded, indexBuffer]); + } else if (hardened) { + // This will use a 32 byte zero padded serialization of the private key + var privateKeyBuffer = this.privateKey.bn.toBuffer({size: 32}); + assert(privateKeyBuffer.length === 32, 'length of private key buffer is expected to be 32 bytes'); + data = BufferUtil.concat([new buffer.Buffer([0]), privateKeyBuffer, indexBuffer]); } else { data = BufferUtil.concat([this.publicKey.toBuffer(), indexBuffer]); } @@ -194,6 +230,11 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened) { size: 32 }); + if (!PrivateKey.isValid(privateKey)) { + // Index at this point is already hardened, we can pass null as the hardened arg + return this._deriveWithNumber(index + 1, null, nonCompliant); + } + var derived = new HDPrivateKey({ network: this.network, depth: this.depth + 1, @@ -202,18 +243,18 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened) { chainCode: chainCode, privateKey: privateKey }); - HDKeyCache.set(this.xprivkey, index, hardened, derived); + HDKeyCache.set(this.xprivkey, index, hardened, derived, nonCompliant); return derived; }; -HDPrivateKey.prototype._deriveFromString = function(path) { +HDPrivateKey.prototype._deriveFromString = function(path, nonCompliant) { if (!HDPrivateKey.isValidPath(path)) { throw new hdErrors.InvalidPath(path); } var indexes = HDPrivateKey._getDerivationIndexes(path); var derived = indexes.reduce(function(prev, index) { - return prev._deriveWithNumber(index); + return prev._deriveWithNumber(index, null, nonCompliant); }, this); return derived; diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index 198947d06..d28a82bf5 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -86,6 +86,11 @@ HDPublicKey.isValidPath = function(arg) { return false; }; +HDPublicKey.prototype.derive = function() { + throw new Error('Method has been deprecated, please use deriveChild instead' + + 'and see documentation for more information'); +}; + /** * Get a derivated child based on a string or number. * @@ -101,14 +106,14 @@ HDPublicKey.isValidPath = function(arg) { * @example * ```javascript * var parent = new HDPublicKey('xpub...'); - * var child_0_1_2 = parent.derive(0).derive(1).derive(2); - * var copy_of_child_0_1_2 = parent.derive("m/0/1/2"); + * var child_0_1_2 = parent.deriveChild(0).deriveChild(1).deriveChild(2); + * var copy_of_child_0_1_2 = parent.deriveChild("m/0/1/2"); * assert(child_0_1_2.xprivkey === copy_of_child_0_1_2); * ``` * * @param {string|number} arg */ -HDPublicKey.prototype.derive = function(arg, hardened) { +HDPublicKey.prototype.deriveChild = function(arg, hardened) { if (_.isNumber(arg)) { return this._deriveWithNumber(arg, hardened); } else if (_.isString(arg)) { @@ -136,7 +141,12 @@ HDPublicKey.prototype._deriveWithNumber = function(index, hardened) { var leftPart = BN.fromBuffer(hash.slice(0, 32), {size: 32}); var chainCode = hash.slice(32, 64); - var publicKey = PublicKey.fromPoint(Point.getG().mul(leftPart).add(this.publicKey.point)); + var publicKey; + try { + publicKey = PublicKey.fromPoint(Point.getG().mul(leftPart).add(this.publicKey.point)); + } catch (e) { + return this._deriveWithNumber(index + 1); + } var derived = new HDPublicKey({ network: this.network, diff --git a/lib/privatekey.js b/lib/privatekey.js index 636eb2d52..440b0e5e1 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -339,6 +339,15 @@ PrivateKey.prototype.toBuffer = function(){ return this.bn.toBuffer({ size: 32 }); }; +/** + * Will return the private key as a BN buffer without leading zero padding + * + * @returns {Buffer} A buffer of the private key + */ +PrivateKey.prototype.toBufferNoPadding = function() { + return this.bn.toBuffer(); +}; + /** * Will return the corresponding public key * diff --git a/test/hdkeycache.js b/test/hdkeycache.js index db1f4fc18..35a51678a 100644 --- a/test/hdkeycache.js +++ b/test/hdkeycache.js @@ -24,29 +24,29 @@ describe('HDKey cache', function() { }); it('saves a derived key', function() { - var child = master.derive(0); - expect(cache._cache[master.xprivkey + '/0/false'].xprivkey).to.equal(child.xprivkey); + var child = master.deriveChild(0); + expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child.xprivkey); }); it('starts erasing unused keys', function() { - var child1 = master.derive(0); - var child2 = child1.derive(0); - var child3 = child2.derive(0); - expect(cache._cache[master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey); - var child4 = child3.derive(0); - expect(cache._cache[master.xprivkey + '/0/false']).to.equal(undefined); + var child1 = master.deriveChild(0); + var child2 = child1.deriveChild(0); + var child3 = child2.deriveChild(0); + expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey); + var child4 = child3.deriveChild(0); + expect(cache._cache['0' + master.xprivkey + '/0/false']).to.equal(undefined); }); it('avoids erasing keys that get cache hits ("hot keys")', function() { - var child1 = master.derive(0); - var child2 = master.derive(0).derive(0); - expect(cache._cache[master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey); - var child1_copy = master.derive(0); - expect(cache._cache[master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey); + var child1 = master.deriveChild(0); + var child2 = master.deriveChild(0).deriveChild(0); + expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey); + var child1_copy = master.deriveChild(0); + expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey); }); it('keeps the size of the cache small', function() { - var child1 = master.derive(0); - var child2 = child1.derive(0); - var child3 = child2.derive(0); - var child4 = child3.derive(0); + var child1 = master.deriveChild(0); + var child2 = child1.deriveChild(0); + var child3 = child2.deriveChild(0); + var child4 = child3.deriveChild(0); expect(_.size(cache._cache)).to.equal(3); }); }); diff --git a/test/hdkeys.js b/test/hdkeys.js index d4bd6bda7..1744a5442 100644 --- a/test/hdkeys.js +++ b/test/hdkeys.js @@ -13,6 +13,7 @@ var _ = require('lodash'); var should = require('chai').should(); var expect = require('chai').expect; +var sinon = require('sinon'); var bitcore = require('..'); var Networks = bitcore.Networks; var HDPrivateKey = bitcore.HDPrivateKey; @@ -80,69 +81,79 @@ describe('BIP32 compliance', function() { }); it("should get m/0' ext. private key from test vector 1", function() { - var privateKey = new HDPrivateKey(vector1_m_private).derive("m/0'"); + var privateKey = new HDPrivateKey(vector1_m_private).deriveChild("m/0'"); privateKey.xprivkey.should.equal(vector1_m0h_private); }); it("should get m/0' ext. public key from test vector 1", function() { - HDPrivateKey(vector1_m_private).derive("m/0'") + HDPrivateKey(vector1_m_private).deriveChild("m/0'") .xpubkey.should.equal(vector1_m0h_public); }); + it("should get m/0' ext. private key from test vector 3", function() { + var privateKey = new HDPrivateKey(vector3_m_private).deriveChild("m/0'"); + privateKey.xprivkey.should.equal(vector3_m0h_private); + }); + + it("should get m/0' ext. public key from test vector 3", function() { + HDPrivateKey(vector3_m_private).deriveChild("m/0'") + .xpubkey.should.equal(vector3_m0h_public); + }); + it("should get m/0'/1 ext. private key from test vector 1", function() { - HDPrivateKey(vector1_m_private).derive("m/0'/1") + HDPrivateKey(vector1_m_private).deriveChild("m/0'/1") .xprivkey.should.equal(vector1_m0h1_private); }); it("should get m/0'/1 ext. public key from test vector 1", function() { - HDPrivateKey(vector1_m_private).derive("m/0'/1") + HDPrivateKey(vector1_m_private).deriveChild("m/0'/1") .xpubkey.should.equal(vector1_m0h1_public); }); it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function() { - var derivedPublic = HDPrivateKey(vector1_m_private).derive("m/0'").hdPublicKey.derive("m/1"); + var derivedPublic = HDPrivateKey(vector1_m_private).deriveChild("m/0'").hdPublicKey.deriveChild("m/1"); derivedPublic.xpubkey.should.equal(vector1_m0h1_public); }); it("should get m/0'/1/2' ext. private key from test vector 1", function() { var privateKey = new HDPrivateKey(vector1_m_private); - var derived = privateKey.derive("m/0'/1/2'"); + var derived = privateKey.deriveChild("m/0'/1/2'"); derived.xprivkey.should.equal(vector1_m0h12h_private); }); it("should get m/0'/1/2' ext. public key from test vector 1", function() { - HDPrivateKey(vector1_m_private).derive("m/0'/1/2'") + HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'") .xpubkey.should.equal(vector1_m0h12h_public); }); it("should get m/0'/1/2'/2 ext. private key from test vector 1", function() { - HDPrivateKey(vector1_m_private).derive("m/0'/1/2'/2") + HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'/2") .xprivkey.should.equal(vector1_m0h12h2_private); }); it("should get m/0'/1/2'/2 ext. public key from m/0'/1/2' public key from test vector 1", function() { - var derived = HDPrivateKey(vector1_m_private).derive("m/0'/1/2'").hdPublicKey; - derived.derive("m/2").xpubkey.should.equal(vector1_m0h12h2_public); + var derived = HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'").hdPublicKey; + derived.deriveChild("m/2").xpubkey.should.equal(vector1_m0h12h2_public); }); it("should get m/0'/1/2h/2 ext. public key from test vector 1", function() { - HDPrivateKey(vector1_m_private).derive("m/0'/1/2'/2") + HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'/2") .xpubkey.should.equal(vector1_m0h12h2_public); }); it("should get m/0'/1/2h/2/1000000000 ext. private key from test vector 1", function() { - HDPrivateKey(vector1_m_private).derive("m/0'/1/2'/2/1000000000") + HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'/2/1000000000") .xprivkey.should.equal(vector1_m0h12h21000000000_private); }); it("should get m/0'/1/2h/2/1000000000 ext. public key from test vector 1", function() { - HDPrivateKey(vector1_m_private).derive("m/0'/1/2'/2/1000000000") + HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'/2/1000000000") .xpubkey.should.equal(vector1_m0h12h21000000000_public); }); it("should get m/0'/1/2'/2/1000000000 ext. public key from m/0'/1/2'/2 public key from test vector 1", function() { - var derived = HDPrivateKey(vector1_m_private).derive("m/0'/1/2'/2").hdPublicKey; - derived.derive("m/1000000000").xpubkey.should.equal(vector1_m0h12h21000000000_public); + var derived = HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'/2").hdPublicKey; + derived.deriveChild("m/1000000000").xpubkey.should.equal(vector1_m0h12h21000000000_public); }); it('should initialize test vector 2 from the extended public key', function() { @@ -158,69 +169,174 @@ describe('BIP32 compliance', function() { }); it("should get m/0 ext. private key from test vector 2", function() { - HDPrivateKey(vector2_m_private).derive(0).xprivkey.should.equal(vector2_m0_private); + HDPrivateKey(vector2_m_private).deriveChild(0).xprivkey.should.equal(vector2_m0_private); }); it("should get m/0 ext. public key from test vector 2", function() { - HDPrivateKey(vector2_m_private).derive(0).xpubkey.should.equal(vector2_m0_public); + HDPrivateKey(vector2_m_private).deriveChild(0).xpubkey.should.equal(vector2_m0_public); }); it("should get m/0 ext. public key from m public key from test vector 2", function() { - HDPrivateKey(vector2_m_private).hdPublicKey.derive(0).xpubkey.should.equal(vector2_m0_public); + HDPrivateKey(vector2_m_private).hdPublicKey.deriveChild(0).xpubkey.should.equal(vector2_m0_public); }); it("should get m/0/2147483647h ext. private key from test vector 2", function() { - HDPrivateKey(vector2_m_private).derive("m/0/2147483647'") + HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'") .xprivkey.should.equal(vector2_m02147483647h_private); }); it("should get m/0/2147483647h ext. public key from test vector 2", function() { - HDPrivateKey(vector2_m_private).derive("m/0/2147483647'") + HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'") .xpubkey.should.equal(vector2_m02147483647h_public); }); it("should get m/0/2147483647h/1 ext. private key from test vector 2", function() { - HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1") + HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1") .xprivkey.should.equal(vector2_m02147483647h1_private); }); it("should get m/0/2147483647h/1 ext. public key from test vector 2", function() { - HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1") + HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1") .xpubkey.should.equal(vector2_m02147483647h1_public); }); it("should get m/0/2147483647h/1 ext. public key from m/0/2147483647h public key from test vector 2", function() { - var derived = HDPrivateKey(vector2_m_private).derive("m/0/2147483647'").hdPublicKey; - derived.derive(1).xpubkey.should.equal(vector2_m02147483647h1_public); + var derived = HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'").hdPublicKey; + derived.deriveChild(1).xpubkey.should.equal(vector2_m02147483647h1_public); }); it("should get m/0/2147483647h/1/2147483646h ext. private key from test vector 2", function() { - HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1/2147483646'") + HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1/2147483646'") .xprivkey.should.equal(vector2_m02147483647h12147483646h_private); }); it("should get m/0/2147483647h/1/2147483646h ext. public key from test vector 2", function() { - HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1/2147483646'") + HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1/2147483646'") .xpubkey.should.equal(vector2_m02147483647h12147483646h_public); }); it("should get m/0/2147483647h/1/2147483646h/2 ext. private key from test vector 2", function() { - HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1/2147483646'/2") + HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1/2147483646'/2") .xprivkey.should.equal(vector2_m02147483647h12147483646h2_private); }); it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from test vector 2", function() { - HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1/2147483646'/2") + HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1/2147483646'/2") .xpubkey.should.equal(vector2_m02147483647h12147483646h2_public); }); it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from m/0/2147483647h/2147483646h public key from test vector 2", function() { var derivedPublic = HDPrivateKey(vector2_m_private) - .derive("m/0/2147483647'/1/2147483646'").hdPublicKey; - derivedPublic.derive("m/2") + .deriveChild("m/0/2147483647'/1/2147483646'").hdPublicKey; + derivedPublic.deriveChild("m/2") .xpubkey.should.equal(vector2_m02147483647h12147483646h2_public); }); + it('should use full 32 bytes for private key data that is hashed (as per bip32)', function() { + // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki + var privateKeyBuffer = new Buffer('00000055378cf5fafb56c711c674143f9b0ee82ab0ba2924f19b64f5ae7cdbfd', 'hex'); + var chainCodeBuffer = new Buffer('9c8a5c863e5941f3d99453e6ba66b328bb17cf0b8dec89ed4fc5ace397a1c089', 'hex'); + var key = HDPrivateKey.fromObject({ + network: 'testnet', + depth: 0, + parentFingerPrint: 0, + childIndex: 0, + privateKey: privateKeyBuffer, + chainCode: chainCodeBuffer + }); + var derived = key.deriveChild("m/44'/0'/0'/0/0'"); + derived.privateKey.toString().should.equal('3348069561d2a0fb925e74bf198762acc47dce7db27372257d2d959a9e6f8aeb'); + }); + + it('should NOT use full 32 bytes for private key data that is hashed with nonCompliant flag', function() { + // This is to test compatibility with the previous `derive` method that was non-compliant to BIP32 + // and was affected by a leading zero bug. + var privateKeyBuffer = new Buffer('00000055378cf5fafb56c711c674143f9b0ee82ab0ba2924f19b64f5ae7cdbfd', 'hex'); + var chainCodeBuffer = new Buffer('9c8a5c863e5941f3d99453e6ba66b328bb17cf0b8dec89ed4fc5ace397a1c089', 'hex'); + var key = HDPrivateKey.fromObject({ + network: 'testnet', + depth: 0, + parentFingerPrint: 0, + childIndex: 0, + privateKey: privateKeyBuffer, + chainCode: chainCodeBuffer + }); + var derived = key.deriveNonCompliantChild("m/44'/0'/0'/0/0'"); + derived.privateKey.toString().should.equal('4811a079bab267bfdca855b3bddff20231ff7044e648514fa099158472df2836'); + }); + + it('hdprivatekey will throw deprecation message', function() { + expect(function() { + var key = new HDPrivateKey(); + key.derive(); + }).to.throw('Method has been deprecated'); + }); + + it('hdpublickey will throw deprecation message', function() { + expect(function() { + var key = new HDPrivateKey(); + var pubkey = key.hdPublicKey; + pubkey.derive(); + }).to.throw('Method has been deprecated'); + }); + + describe('edge cases', function() { + var sandbox = sinon.sandbox.create(); + afterEach(function() { + sandbox.restore(); + }); + it('will handle edge case that derived private key is invalid', function() { + var invalid = new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex'); + var privateKeyBuffer = new Buffer('5f72914c48581fc7ddeb944a9616389200a9560177d24f458258e5b04527bcd1', 'hex'); + var chainCodeBuffer = new Buffer('39816057bba9d952fe87fe998b7fd4d690a1bb58c2ff69141469e4d1dffb4b91', 'hex'); + var unstubbed = bitcore.crypto.BN.prototype.toBuffer; + var count = 0; + var stub = sandbox.stub(bitcore.crypto.BN.prototype, 'toBuffer', function(args) { + // On the fourth call to the function give back an invalid private key + // otherwise use the normal behavior. + count++; + if (count === 4) { + return invalid; + } + var ret = unstubbed.apply(this, arguments); + return ret; + }); + sandbox.spy(bitcore.PrivateKey, 'isValid'); + var key = HDPrivateKey.fromObject({ + network: 'testnet', + depth: 0, + parentFingerPrint: 0, + childIndex: 0, + privateKey: privateKeyBuffer, + chainCode: chainCodeBuffer + }); + var derived = key.deriveChild("m/44'"); + derived.privateKey.toString().should.equal('b15bce3608d607ee3a49069197732c656bca942ee59f3e29b4d56914c1de6825'); + bitcore.PrivateKey.isValid.callCount.should.equal(2); + }); + it('will handle edge case that a derive public key is invalid', function() { + var publicKeyBuffer = new Buffer('029e58b241790284ef56502667b15157b3fc58c567f044ddc35653860f9455d099', 'hex'); + var chainCodeBuffer = new Buffer('39816057bba9d952fe87fe998b7fd4d690a1bb58c2ff69141469e4d1dffb4b91', 'hex'); + var key = new HDPublicKey({ + network: 'testnet', + depth: 0, + parentFingerPrint: 0, + childIndex: 0, + chainCode: chainCodeBuffer, + publicKey: publicKeyBuffer + }); + var unstubbed = bitcore.PublicKey.fromPoint; + bitcore.PublicKey.fromPoint = function() { + bitcore.PublicKey.fromPoint = unstubbed; + throw new Error('Point cannot be equal to Infinity'); + }; + sandbox.spy(key, '_deriveWithNumber'); + var derived = key.deriveChild("m/44"); + key._deriveWithNumber.callCount.should.equal(2); + key.publicKey.toString().should.equal('029e58b241790284ef56502667b15157b3fc58c567f044ddc35653860f9455d099'); + }); + }); + describe('seed', function() { it('should initialize a new BIP32 correctly from test vector 1 seed', function() { @@ -234,6 +350,13 @@ describe('BIP32 compliance', function() { seededKey.xprivkey.should.equal(vector2_m_private); seededKey.xpubkey.should.equal(vector2_m_public); }); + + it('should initialize a new BIP32 correctly from test vector 3 seed', function() { + var seededKey = HDPrivateKey.fromSeed(vector3_master, Networks.livenet); + seededKey.xprivkey.should.equal(vector3_m_private); + seededKey.xpubkey.should.equal(vector3_m_public); + }); + }); }); @@ -264,3 +387,10 @@ var vector2_m02147483647h12147483646h_public = 'xpub6ERApfZwUNrhLCkDtcHTcxd75Rbz var vector2_m02147483647h12147483646h_private = 'xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc'; var vector2_m02147483647h12147483646h2_public = 'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt'; var vector2_m02147483647h12147483646h2_private = 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j'; + +// leading zero test vectors +var vector3_master = '4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be'; +var vector3_m_public = 'xpub661MyMwAqRbcEZVB4dScxMAdx6d4nFc9nvyvH3v4gJL378CSRZiYmhRoP7mBy6gSPSCYk6SzXPTf3ND1cZAceL7SfJ1Z3GC8vBgp2epUt13'; +var vector3_m_private = 'xprv9s21ZrQH143K25QhxbucbDDuQ4naNntJRi4KUfWT7xo4EKsHt2QJDu7KXp1A3u7Bi1j8ph3EGsZ9Xvz9dGuVrtHHs7pXeTzjuxBrCmmhgC6'; +var vector3_m0h_public = 'xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y'; +var vector3_m0h_private = 'xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L'; diff --git a/test/hdprivatekey.js b/test/hdprivatekey.js index 6b63e6279..2e99591f4 100644 --- a/test/hdprivatekey.js +++ b/test/hdprivatekey.js @@ -30,7 +30,7 @@ describe('HDPrivate key interface', function() { var expectDerivationFail = function(argument, error) { return expectFail(function() { var privateKey = new HDPrivateKey(xprivkey); - privateKey.derive(argument); + privateKey.deriveChild(argument); }, error); }; @@ -123,14 +123,14 @@ describe('HDPrivate key interface', function() { it('allows derivation of hardened keys by passing a very big number', function() { var privateKey = new HDPrivateKey(xprivkey); - var derivedByNumber = privateKey.derive(0x80000000); - var derivedByArgument = privateKey.derive(0, true); + var derivedByNumber = privateKey.deriveChild(0x80000000); + var derivedByArgument = privateKey.deriveChild(0, true); derivedByNumber.xprivkey.should.equal(derivedByArgument.xprivkey); }); it('returns itself with \'m\' parameter', function() { var privateKey = new HDPrivateKey(xprivkey); - privateKey.should.equal(privateKey.derive('m')); + privateKey.should.equal(privateKey.deriveChild('m')); }); it('returns InvalidArgument if invalid data is given to getSerializedError', function() { @@ -203,8 +203,8 @@ describe('HDPrivate key interface', function() { it('shouldn\'t matter if derivations are made with strings or numbers', function() { var privateKey = new HDPrivateKey(xprivkey); - var derivedByString = privateKey.derive('m/0\'/1/2\''); - var derivedByNumber = privateKey.derive(0, true).derive(1).derive(2, true); + var derivedByString = privateKey.deriveChild('m/0\'/1/2\''); + var derivedByNumber = privateKey.deriveChild(0, true).deriveChild(1).deriveChild(2, true); derivedByNumber.xprivkey.should.equal(derivedByString.xprivkey); }); diff --git a/test/hdpublickey.js b/test/hdpublickey.js index f006d737b..19f86b304 100644 --- a/test/hdpublickey.js +++ b/test/hdpublickey.js @@ -32,7 +32,7 @@ describe('HDPublicKey interface', function() { var expectDerivationFail = function(argument, error) { (function() { var pubkey = new HDPublicKey(xpubkey); - pubkey.derive(argument); + pubkey.deriveChild(argument); }).should.throw(error); }; @@ -188,15 +188,15 @@ describe('HDPublicKey interface', function() { describe('derivation', function() { it('derivation is the same whether deriving with number or string', function() { var pubkey = new HDPublicKey(xpubkey); - var derived1 = pubkey.derive(0).derive(1).derive(200000); - var derived2 = pubkey.derive('m/0/1/200000'); + var derived1 = pubkey.deriveChild(0).deriveChild(1).deriveChild(200000); + var derived2 = pubkey.deriveChild('m/0/1/200000'); derived1.xpubkey.should.equal(derived_0_1_200000); derived2.xpubkey.should.equal(derived_0_1_200000); }); it('allows special parameters m, M', function() { var expectDerivationSuccess = function(argument) { - new HDPublicKey(xpubkey).derive(argument).xpubkey.should.equal(xpubkey); + new HDPublicKey(xpubkey).deriveChild(argument).xpubkey.should.equal(xpubkey); }; expectDerivationSuccess('m'); expectDerivationSuccess('M'); @@ -204,13 +204,13 @@ describe('HDPublicKey interface', function() { it('doesn\'t allow object arguments for derivation', function() { expectFail(function() { - return new HDPublicKey(xpubkey).derive({}); + return new HDPublicKey(xpubkey).deriveChild({}); }, hdErrors.InvalidDerivationArgument); }); it('needs first argument for derivation', function() { expectFail(function() { - return new HDPublicKey(xpubkey).derive('s'); + return new HDPublicKey(xpubkey).deriveChild('s'); }, hdErrors.InvalidPath); }); @@ -224,13 +224,13 @@ describe('HDPublicKey interface', function() { it('can\'t derive hardened keys', function() { expectFail(function() { - return new HDPublicKey(xpubkey).derive(HDPublicKey.Hardened); + return new HDPublicKey(xpubkey).deriveChild(HDPublicKey.Hardened); }, hdErrors.InvalidIndexCantDeriveHardened); }); it('can\'t derive hardened keys via second argument', function() { expectFail(function() { - return new HDPublicKey(xpubkey).derive(5, true); + return new HDPublicKey(xpubkey).deriveChild(5, true); }, hdErrors.InvalidIndexCantDeriveHardened); }); @@ -274,8 +274,8 @@ describe('HDPublicKey interface', function() { it('should use the cache', function() { var pubkey = new HDPublicKey(xpubkey); - var derived1 = pubkey.derive(0); - var derived2 = pubkey.derive(0); + var derived1 = pubkey.deriveChild(0); + var derived2 = pubkey.deriveChild(0); derived1.should.equal(derived2); }); }); diff --git a/test/privatekey.js b/test/privatekey.js index ffbaaf893..90f9aeaa4 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -331,6 +331,20 @@ describe('PrivateKey', function() { fromBuffer.toString().should.equal(privkey.toString()); }); + it('will output a 32 byte buffer', function() { + var bn = BN.fromBuffer(new Buffer('9b5a0e8fee1835e21170ce1431f9b6f19b487e67748ed70d8a4462bc031915', 'hex')); + var privkey = new PrivateKey(bn); + var buffer = privkey.toBuffer(); + buffer.length.should.equal(32); + }); + + it('will output a 31 byte buffer', function() { + var bn = BN.fromBuffer(new Buffer('9b5a0e8fee1835e21170ce1431f9b6f19b487e67748ed70d8a4462bc031915', 'hex')); + var privkey = new PrivateKey(bn); + var buffer = privkey.toBufferNoPadding(); + buffer.length.should.equal(31); + }); + it('should return buffer with length equal 32', function() { var bn = BN.fromBuffer(buf.slice(0, 31)); var privkey = new PrivateKey(bn, 'livenet');