From e1eefb1c972df76352ce3a7ea3bdb1d77d85e1d0 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Thu, 25 Feb 2016 19:03:39 +0300 Subject: [PATCH 01/12] Fix PrivateKey.toBuffer --- lib/privatekey.js | 2 +- test/privatekey.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index 9697bbfba..636eb2d52 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -336,7 +336,7 @@ PrivateKey.prototype.toBigNumber = function(){ * @returns {Buffer} A buffer of the private key */ PrivateKey.prototype.toBuffer = function(){ - return this.bn.toBuffer(); + return this.bn.toBuffer({ size: 32 }); }; /** diff --git a/test/privatekey.js b/test/privatekey.js index 6df029934..ffbaaf893 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -330,6 +330,13 @@ describe('PrivateKey', function() { var fromBuffer = PrivateKey.fromBuffer(toBuffer.toBuffer()); fromBuffer.toString().should.equal(privkey.toString()); }); + + it('should return buffer with length equal 32', function() { + var bn = BN.fromBuffer(buf.slice(0, 31)); + var privkey = new PrivateKey(bn, 'livenet'); + var expected = Buffer.concat([ new Buffer([0]), buf.slice(0, 31) ]); + privkey.toBuffer().toString('hex').should.equal(expected.toString('hex')); + }); }); describe('#toBigNumber', function() { From 89b469796d592432d8a4c878244179c2e14857d5 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 12 Sep 2016 19:01:01 -0400 Subject: [PATCH 02/12] Add note to README for 1.0.0 staging/development --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4e4456a7f..b3606e883 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +**Note**: This is a development/staging branch for 1.0.0 + Bitcore Library ======= From 5f1bcb0ff646e17f674c228d1bac7b698876b3e0 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 12 Sep 2016 20:05:47 -0400 Subject: [PATCH 03/12] Remove Node.js 0.10, 0.12 and iojs 2.0.0 from travis ci --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 202aa51e9..b3e4d403c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,6 @@ language: node_js sudo: false node_js: -- '0.10' -- '0.12' -- '2.0.0' - '4' before_install: - npm install -g bower From 5a7069d581647322ebbadb5376886a87d8335557 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 12 Sep 2016 18:14:30 -0400 Subject: [PATCH 04/12] Fix implementation of hd derivation to be bip32 compliant https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions --- lib/hdkeycache.js | 8 +++---- lib/hdprivatekey.js | 51 ++++++++++++++++++++++++++++++++++++--------- test/hdkeycache.js | 10 ++++----- test/hdkeys.js | 32 ++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 19 deletions(-) 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..8d5ed1b35 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -139,6 +139,9 @@ HDPrivateKey._getDerivationIndexes = function(path) { * derived. If the second argument is truthy, the hardened version will be * derived. See the example usage for clarification. * + * WARNING: The `nonCompliant` option should NOT be used, except for older implementation + * that used a derivation strategy that used a non-zero padded private key. + * * @example * ```javascript * var parent = new HDPrivateKey('xprv...'); @@ -149,18 +152,38 @@ HDPrivateKey._getDerivationIndexes = function(path) { * * @param {string|number} arg * @param {boolean?} hardened + * @param {boolean?} nonCompliant - This should not be used and only for backwards compatibility + * */ -HDPrivateKey.prototype.derive = function(arg, hardened) { +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) { +/** + * 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.deriveNonCompliant = function(arg, hardened) { + var derived = this.derive(arg, hardened, true); + return derived; +}; + +HDPrivateKey.prototype._deriveWithNumber = function(index, hardened, nonCompliant) { /* jshint maxstatements: 20 */ /* jshint maxcomplexity: 10 */ if (!HDPrivateKey.isValidPath(index, hardened)) { @@ -172,15 +195,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]); } @@ -202,18 +233,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/test/hdkeycache.js b/test/hdkeycache.js index db1f4fc18..692c01d3e 100644 --- a/test/hdkeycache.js +++ b/test/hdkeycache.js @@ -25,22 +25,22 @@ 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); + 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); + expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey); var child4 = child3.derive(0); - expect(cache._cache[master.xprivkey + '/0/false']).to.equal(undefined); + 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); + expect(cache._cache['0' + 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); + 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); diff --git a/test/hdkeys.js b/test/hdkeys.js index d4bd6bda7..377e18b52 100644 --- a/test/hdkeys.js +++ b/test/hdkeys.js @@ -221,6 +221,38 @@ describe('BIP32 compliance', function() { .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.derive("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 that the previously implemented non-compliant to BIP32 + 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.deriveNonCompliant("m/44'/0'/0'/0/0'"); + derived.privateKey.toString().should.equal('4811a079bab267bfdca855b3bddff20231ff7044e648514fa099158472df2836'); + }); + describe('seed', function() { it('should initialize a new BIP32 correctly from test vector 1 seed', function() { From f42bbd3fa98f71e55e133f84cbe3bfc87a7b7e1c Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 13 Sep 2016 18:59:16 -0400 Subject: [PATCH 05/12] Handle edge case that invalid private key is derived --- lib/hdprivatekey.js | 5 +++++ test/hdkeys.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 8d5ed1b35..33ec36327 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -225,6 +225,11 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened, nonComplian 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, diff --git a/test/hdkeys.js b/test/hdkeys.js index 377e18b52..d57a54b5b 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; @@ -253,6 +254,42 @@ describe('BIP32 compliance', function() { derived.privateKey.toString().should.equal('4811a079bab267bfdca855b3bddff20231ff7044e648514fa099158472df2836'); }); + 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.derive("m/44'"); + derived.privateKey.toString().should.equal('b15bce3608d607ee3a49069197732c656bca942ee59f3e29b4d56914c1de6825'); + bitcore.PrivateKey.isValid.callCount.should.equal(2); + }); + }); + describe('seed', function() { it('should initialize a new BIP32 correctly from test vector 1 seed', function() { From 1c8fe1eda948c281982bbb08b5263d2541940a0e Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 13 Sep 2016 19:25:10 -0400 Subject: [PATCH 06/12] Handle invalid public key derivation rather than throw error --- lib/hdpublickey.js | 7 ++++++- test/hdkeys.js | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index 198947d06..04aad48d9 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -136,7 +136,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/test/hdkeys.js b/test/hdkeys.js index d57a54b5b..edbe8468a 100644 --- a/test/hdkeys.js +++ b/test/hdkeys.js @@ -288,6 +288,27 @@ describe('BIP32 compliance', function() { 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.derive("m/44"); + key._deriveWithNumber.callCount.should.equal(2); + key.publicKey.toString().should.equal('029e58b241790284ef56502667b15157b3fc58c567f044ddc35653860f9455d099'); + }); }); describe('seed', function() { From d0e86271e4f7bf39b8efb0dcd60ce465a013e9e6 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 16 Sep 2016 10:13:43 -0400 Subject: [PATCH 07/12] Add toBufferNoPadding method to private key --- lib/privatekey.js | 9 +++++++++ test/privatekey.js | 14 ++++++++++++++ 2 files changed, 23 insertions(+) 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/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'); From 6e311fe753d16287433c19fcb39460042073a4aa Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 1 Dec 2016 10:53:13 -0300 Subject: [PATCH 08/12] defaults to nonCompliant key derivation --- lib/hdprivatekey.js | 75 +++++++++++++++++++++++++++++++++++++++++++-- lib/hdpublickey.js | 26 +++++++++++----- test/hdkeycache.js | 24 +++++++-------- test/hdkeys.js | 70 +++++++++++++++++++++--------------------- test/hdpublickey.js | 20 ++++++------ 5 files changed, 147 insertions(+), 68 deletions(-) diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 33ec36327..6df343d7e 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -127,6 +127,7 @@ HDPrivateKey._getDerivationIndexes = function(path) { return _.any(indexes, isNaN) ? null : indexes; }; + /** * Get a derived child based on a string or number. * @@ -156,6 +157,41 @@ HDPrivateKey._getDerivationIndexes = function(path) { * */ HDPrivateKey.prototype.derive = function(arg, hardened, nonCompliant) { + + // Defaults to the nonCompliant derivation, for backwards compatibility + if (_.isUndefined(nonCompliant)) + nonCompliant = true; + + if (_.isNumber(arg)) { + return this._deriveWithNumber(arg, hardened, nonCompliant); + } else if (_.isString(arg)) { + return this._deriveFromString(arg, nonCompliant); + } else { + throw new hdErrors.InvalidDerivationArgument(arg); + } +}; + + + +/** + * + * WARNING: This method should not be called directly. Please use: `getChild` or `getNonCompliantChild'. + * + * Internal method to get a derived child based on a string or number. + * + * @param {string|number} arg + * @param {boolean?} hardened + * @param {boolean?} nonCompliant - This should not be used and only for backwards compatibility + * + */ +HDPrivateKey.prototype.derive = function(arg, hardened, nonCompliant) { + + // Defaults to the nonCompliant derivation, for backwards compatibility + if (_.isUndefined(nonCompliant)) { + console.log('Warning: Using no BIP32 Compliant key derivation. If this is on purpose, use `getNonCompliantChild` to avoid this warning, otherwise use the `getChild`') + nonCompliant = true; + } + if (_.isNumber(arg)) { return this._deriveWithNumber(arg, hardened, nonCompliant); } else if (_.isString(arg)) { @@ -165,9 +201,40 @@ HDPrivateKey.prototype.derive = function(arg, hardened, nonCompliant) { } }; + +/** + * Get a derived child based on a string or number. + * + * If the first argument is a string, it's parsed as the full path of + * derivation. Valid values for this argument include "m" (which returns the + * same private key), "m/0/1/40/2'/1000", where the ' quote means a hardened + * derivation. + * + * If the first argument is a number, the child with that index will be + * derived. If the second argument is truthy, the hardened version will be + * derived. See the example usage for clarification. + * + * @example + * ```javascript + * var parent = new HDPrivateKey('xprv...'); + * var child_0_1_2h = parent.getChild(0).getChild(1).getChild(2, true); + * var copy_of_child_0_1_2h = parent.getChild("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.getChild = 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. + * `getChild` 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 @@ -178,7 +245,7 @@ HDPrivateKey.prototype.derive = function(arg, hardened, nonCompliant) { * @param {string|number} arg * @param {boolean?} hardened */ -HDPrivateKey.prototype.deriveNonCompliant = function(arg, hardened) { +HDPrivateKey.prototype.getNonCompliantChild = function(arg, hardened) { var derived = this.derive(arg, hardened, true); return derived; }; @@ -209,7 +276,9 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened, nonComplian 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}); + 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 { diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index 04aad48d9..c321c387d 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -86,6 +86,22 @@ HDPublicKey.isValidPath = function(arg) { return false; }; + +/** + * Internal method to get a derivated child based on a string or number. + * + */ + +HDPublicKey.prototype.derive = function(arg, hardened) { + if (_.isNumber(arg)) { + return this._deriveWithNumber(arg, hardened); + } else if (_.isString(arg)) { + return this._deriveFromString(arg); + } else { + throw new hdErrors.InvalidDerivationArgument(arg); + } +}; + /** * Get a derivated child based on a string or number. * @@ -108,14 +124,8 @@ HDPublicKey.isValidPath = function(arg) { * * @param {string|number} arg */ -HDPublicKey.prototype.derive = function(arg, hardened) { - if (_.isNumber(arg)) { - return this._deriveWithNumber(arg, hardened); - } else if (_.isString(arg)) { - return this._deriveFromString(arg); - } else { - throw new hdErrors.InvalidDerivationArgument(arg); - } +HDPublicKey.prototype.getChild = function(arg, hardened) { + return this.derive(arg, hardened); }; HDPublicKey.prototype._deriveWithNumber = function(index, hardened) { diff --git a/test/hdkeycache.js b/test/hdkeycache.js index 692c01d3e..8eeaf47ee 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); + var child = master.getChild(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); + var child1 = master.getChild(0); + var child2 = child1.getChild(0); + var child3 = child2.getChild(0); expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey); - var child4 = child3.derive(0); + var child4 = child3.getChild(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); + var child1 = master.getChild(0); + var child2 = master.getChild(0).getChild(0); expect(cache._cache['0' + master.xprivkey + '/0/false'].xprivkey).to.equal(child1.xprivkey); - var child1_copy = master.derive(0); + var child1_copy = master.getChild(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.getChild(0); + var child2 = child1.getChild(0); + var child3 = child2.getChild(0); + var child4 = child3.getChild(0); expect(_.size(cache._cache)).to.equal(3); }); }); diff --git a/test/hdkeys.js b/test/hdkeys.js index edbe8468a..d4c841c2e 100644 --- a/test/hdkeys.js +++ b/test/hdkeys.js @@ -81,69 +81,69 @@ 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).getChild("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).getChild("m/0'") .xpubkey.should.equal(vector1_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).getChild("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).getChild("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).getChild("m/0'").hdPublicKey.getChild("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.getChild("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).getChild("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).getChild("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).getChild("m/0'/1/2'").hdPublicKey; + derived.getChild("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).getChild("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).getChild("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).getChild("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).getChild("m/0'/1/2'/2").hdPublicKey; + derived.getChild("m/1000000000").xpubkey.should.equal(vector1_m0h12h21000000000_public); }); it('should initialize test vector 2 from the extended public key', function() { @@ -159,66 +159,66 @@ 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).getChild(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).getChild(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.getChild(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).getChild("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).getChild("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).getChild("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).getChild("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).getChild("m/0/2147483647'").hdPublicKey; + derived.getChild(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).getChild("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).getChild("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).getChild("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).getChild("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") + .getChild("m/0/2147483647'/1/2147483646'").hdPublicKey; + derivedPublic.getChild("m/2") .xpubkey.should.equal(vector2_m02147483647h12147483646h2_public); }); @@ -234,7 +234,7 @@ describe('BIP32 compliance', function() { privateKey: privateKeyBuffer, chainCode: chainCodeBuffer }); - var derived = key.derive("m/44'/0'/0'/0/0'"); + var derived = key.getChild("m/44'/0'/0'/0/0'"); derived.privateKey.toString().should.equal('3348069561d2a0fb925e74bf198762acc47dce7db27372257d2d959a9e6f8aeb'); }); @@ -250,7 +250,7 @@ describe('BIP32 compliance', function() { privateKey: privateKeyBuffer, chainCode: chainCodeBuffer }); - var derived = key.deriveNonCompliant("m/44'/0'/0'/0/0'"); + var derived = key.getNonCompliantChild("m/44'/0'/0'/0/0'"); derived.privateKey.toString().should.equal('4811a079bab267bfdca855b3bddff20231ff7044e648514fa099158472df2836'); }); @@ -284,11 +284,11 @@ describe('BIP32 compliance', function() { privateKey: privateKeyBuffer, chainCode: chainCodeBuffer }); - var derived = key.derive("m/44'"); + var derived = key.getChild("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() { + it('will handle edge case that a getChild public key is invalid', function() { var publicKeyBuffer = new Buffer('029e58b241790284ef56502667b15157b3fc58c567f044ddc35653860f9455d099', 'hex'); var chainCodeBuffer = new Buffer('39816057bba9d952fe87fe998b7fd4d690a1bb58c2ff69141469e4d1dffb4b91', 'hex'); var key = new HDPublicKey({ @@ -305,7 +305,7 @@ describe('BIP32 compliance', function() { throw new Error('Point cannot be equal to Infinity'); }; sandbox.spy(key, '_deriveWithNumber'); - var derived = key.derive("m/44"); + var derived = key.getChild("m/44"); key._deriveWithNumber.callCount.should.equal(2); key.publicKey.toString().should.equal('029e58b241790284ef56502667b15157b3fc58c567f044ddc35653860f9455d099'); }); diff --git a/test/hdpublickey.js b/test/hdpublickey.js index f006d737b..89c38697f 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.getChild(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.getChild(0).getChild(1).getChild(200000); + var derived2 = pubkey.getChild('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).getChild(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).getChild({}); }, hdErrors.InvalidDerivationArgument); }); it('needs first argument for derivation', function() { expectFail(function() { - return new HDPublicKey(xpubkey).derive('s'); + return new HDPublicKey(xpubkey).getChild('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).getChild(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).getChild(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.getChild(0); + var derived2 = pubkey.getChild(0); derived1.should.equal(derived2); }); }); From 7083da2497bbff910e91ba457accaa74affe3bad Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 1 Dec 2016 12:57:07 -0300 Subject: [PATCH 09/12] update docs to `getChild` --- docs/hierarchical.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/hierarchical.md b/docs/hierarchical.md index dc35571e5..792fe0213 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.getChild("m/0'"); +var derivedByNumber = hdPrivateKey.getChild(1).getChild(2, true); +var derivedByArgument = hdPrivateKey.getChild("m/1/2'"); assert(derivedByNumber.xprivkey === derivedByArgument.xprivkey); var address = derived.privateKey.toAddress(); @@ -39,5 +39,5 @@ try { } var address = new Address(hdPublicKey.publicKey, Networks.livenet); -var derivedAddress = new Address(hdPublicKey.derive(100).publicKey, Networks.testnet); +var derivedAddress = new Address(hdPublicKey.getChild(100).publicKey, Networks.testnet); ``` From 218cf3192d06e90fd352bed6ef11ad14a1aee575 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 2 Dec 2016 08:51:03 -0300 Subject: [PATCH 10/12] better warn message --- lib/hdprivatekey.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 6df343d7e..637039ba3 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -188,7 +188,7 @@ HDPrivateKey.prototype.derive = function(arg, hardened, nonCompliant) { // Defaults to the nonCompliant derivation, for backwards compatibility if (_.isUndefined(nonCompliant)) { - console.log('Warning: Using no BIP32 Compliant key derivation. If this is on purpose, use `getNonCompliantChild` to avoid this warning, otherwise use the `getChild`') + console.log('WARN: .derive will be removed in next Bitcore-lib releases. Use `getChild` for BIP32 Compliant key derivation and `getNonCompliantChild` for backwards compatibility key derivation. Details at https://github.com/bitpay/bitcore-lib/pull/115') nonCompliant = true; } From 40ab42daa70bd9c13fefb27c32f8e791430a2f44 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 2 Dec 2016 16:47:13 -0300 Subject: [PATCH 11/12] rm warning --- lib/hdprivatekey.js | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 637039ba3..ae54cdf5f 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -172,36 +172,6 @@ HDPrivateKey.prototype.derive = function(arg, hardened, nonCompliant) { }; - -/** - * - * WARNING: This method should not be called directly. Please use: `getChild` or `getNonCompliantChild'. - * - * Internal method to get a derived child based on a string or number. - * - * @param {string|number} arg - * @param {boolean?} hardened - * @param {boolean?} nonCompliant - This should not be used and only for backwards compatibility - * - */ -HDPrivateKey.prototype.derive = function(arg, hardened, nonCompliant) { - - // Defaults to the nonCompliant derivation, for backwards compatibility - if (_.isUndefined(nonCompliant)) { - console.log('WARN: .derive will be removed in next Bitcore-lib releases. Use `getChild` for BIP32 Compliant key derivation and `getNonCompliantChild` for backwards compatibility key derivation. Details at https://github.com/bitpay/bitcore-lib/pull/115') - nonCompliant = true; - } - - if (_.isNumber(arg)) { - return this._deriveWithNumber(arg, hardened, nonCompliant); - } else if (_.isString(arg)) { - return this._deriveFromString(arg, nonCompliant); - } else { - throw new hdErrors.InvalidDerivationArgument(arg); - } -}; - - /** * Get a derived child based on a string or number. * From 183a09c8d7069b681217ce625c28977e640bdab3 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 2 Dec 2016 16:49:36 -0300 Subject: [PATCH 12/12] update doc --- lib/hdprivatekey.js | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index ae54cdf5f..2abd7ec96 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -129,31 +129,8 @@ HDPrivateKey._getDerivationIndexes = function(path) { /** - * Get a derived child based on a string or number. - * - * If the first argument is a string, it's parsed as the full path of - * derivation. Valid values for this argument include "m" (which returns the - * same private key), "m/0/1/40/2'/1000", where the ' quote means a hardened - * derivation. - * - * If the first argument is a number, the child with that index will be - * derived. If the second argument is truthy, the hardened version will be - * derived. See the example usage for clarification. - * - * WARNING: The `nonCompliant` option should NOT be used, except for older implementation - * that used a derivation strategy that used a non-zero padded private key. * - * @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'"); - * assert(child_0_1_2h.xprivkey === copy_of_child_0_1_2h); - * ``` - * - * @param {string|number} arg - * @param {boolean?} hardened - * @param {boolean?} nonCompliant - This should not be used and only for backwards compatibility + * Get a derived child based on a string or number. This method should not be called directly. Please use: `getChild` or `getNonCompliantChild'. * */ HDPrivateKey.prototype.derive = function(arg, hardened, nonCompliant) { @@ -172,6 +149,9 @@ HDPrivateKey.prototype.derive = function(arg, hardened, nonCompliant) { }; + + + /** * Get a derived child based on a string or number. *