From 3a99b23cb31a76dd26e9615fa5eb6c2773f50a7d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 18 Jun 2016 22:10:07 -0700 Subject: [PATCH] hd refactor. --- lib/bcoin/hd.js | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 6f629447..bd5c6c06 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -595,22 +595,11 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { */ HDPrivateKey.prototype.deriveAccount44 = function deriveAccount44(accountIndex) { - var coinType; - assert(utils.isNumber(accountIndex), 'Account index must be a number.'); - - if (this instanceof HDPublicKey) { - assert(this.isAccount44(accountIndex), 'Cannot derive account index.'); - return this; - } - assert(this.isMaster(), 'Cannot derive account index.'); - - coinType = this.network === 'main' ? 0 : 1; - return this .derive(44, true) - .derive(coinType, true) + .derive(this.network === 'main' ? 0 : 1, true) .derive(accountIndex, true); }; @@ -620,13 +609,7 @@ HDPrivateKey.prototype.deriveAccount44 = function deriveAccount44(accountIndex) */ HDPrivateKey.prototype.derivePurpose45 = function derivePurpose45() { - if (this instanceof HDPublicKey) { - assert(this.isPurpose45(), 'Cannot derive purpose 45.'); - return this; - } - assert(this.isMaster(), 'Cannot derive purpose 45.'); - return this.derive(45, true); }; @@ -1246,7 +1229,10 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { * @throws Error if key is not already an account key. */ -HDPublicKey.prototype.deriveAccount44 = HDPrivateKey.prototype.deriveAccount44; +HDPublicKey.prototype.deriveAccount44 = function deriveAccount44(accountIndex) { + assert(this.isAccount44(accountIndex), 'Cannot derive account index.'); + return this; +}; /** * Derive a BIP45 purpose key (does not derive, only ensures account key). @@ -1255,7 +1241,10 @@ HDPublicKey.prototype.deriveAccount44 = HDPrivateKey.prototype.deriveAccount44; * @throws Error if key is not already a purpose key. */ -HDPublicKey.prototype.derivePurpose45 = HDPrivateKey.prototype.derivePurpose45; +HDPublicKey.prototype.derivePurpose45 = function derivePurpose45() { + assert(this.isPurpose45(), 'Cannot derive purpose 45.'); + return this; +}; /** * Test whether the key is a master key.