diff --git a/lib/hd/common.js b/lib/hd/common.js index 0f1cee3d..659a6f34 100644 --- a/lib/hd/common.js +++ b/lib/hd/common.js @@ -119,7 +119,7 @@ common.isMaster = function isMaster(key) { * @returns {Boolean} */ -common.isBIP44 = function isBIP44(key, account) { +common.isAccount = function isAccount(key, account) { if (account != null) { const index = (common.HARDENED | account) >>> 0; if (key.childIndex !== index) @@ -127,14 +127,3 @@ common.isBIP44 = function isBIP44(key, account) { } return key.depth === 3 && (key.childIndex & common.HARDENED) !== 0; }; - -/** - * Test whether the key is a BIP45 purpose key. - * @param {HDPrivateKey|HDPublicKey} key - * @returns {Boolean} - */ - -common.isBIP45 = function isBIP45(key) { - const index = (common.HARDENED | 45) >>> 0; - return key.depth === 1 && key.childIndex === index; -}; diff --git a/lib/hd/private.js b/lib/hd/private.js index 0867e56d..3fce4a74 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -257,31 +257,22 @@ HDPrivateKey.prototype.getID = function getID(index) { /** * Derive a BIP44 account key. + * @param {Number} purpose * @param {Number} account - * @param {Boolean?} bip48 * @returns {HDPrivateKey} * @throws Error if key is not a master key. */ -HDPrivateKey.prototype.deriveBIP44 = function deriveBIP44(account, bip48) { +HDPrivateKey.prototype.deriveAccount = function deriveAccount(purpose, account) { + assert(util.isU32(purpose), 'Purpose must be a number.'); assert(util.isU32(account), 'Account index must be a number.'); assert(this.isMaster(), 'Cannot derive account index.'); return this - .derive(bip48 ? 48 : 44, true) + .derive(purpose, true) .derive(this.network.keyPrefix.coinType, true) .derive(account, true); }; -/** - * Derive a BIP45 purpose key. - * @returns {HDPrivateKey} - */ - -HDPrivateKey.prototype.deriveBIP45 = function deriveBIP45() { - assert(this.isMaster(), 'Cannot derive purpose 45.'); - return this.derive(45, true); -}; - /** * Test whether the key is a master key. * @returns {Boolean} @@ -297,17 +288,8 @@ HDPrivateKey.prototype.isMaster = function isMaster() { * @returns {Boolean} */ -HDPrivateKey.prototype.isBIP44 = function isBIP44(account) { - return common.isBIP44(this, account); -}; - -/** - * Test whether the key is a BIP45 purpose key. - * @returns {Boolean} - */ - -HDPrivateKey.prototype.isBIP45 = function isBIP45() { - return common.isBIP45(this); +HDPrivateKey.prototype.isAccount = function isAccount(account) { + return common.isAccount(this, account); }; /** diff --git a/lib/hd/public.js b/lib/hd/public.js index 0cc59c2a..b8f1fc84 100644 --- a/lib/hd/public.js +++ b/lib/hd/public.js @@ -217,26 +217,16 @@ HDPublicKey.prototype.getID = function getID(index) { /** * Derive a BIP44 account key (does not derive, only ensures account key). * @method + * @param {Number} purpose * @param {Number} account - * @param {Boolean?} bip48 * @returns {HDPublicKey} * @throws Error if key is not already an account key. */ -HDPublicKey.prototype.deriveBIP44 = function deriveBIP44(account, bip48) { - assert(this.isBIP44(account), 'Cannot derive account index.'); - return this; -}; - -/** - * Derive a BIP45 purpose key (does not derive, only ensures account key). - * @method - * @returns {HDPublicKey} - * @throws Error if key is not already a purpose key. - */ - -HDPublicKey.prototype.deriveBIP45 = function deriveBIP45() { - assert(this.isBIP45(), 'Cannot derive purpose 45.'); +HDPublicKey.prototype.deriveAccount = function deriveAccount(purpose, account) { + assert(util.isU32(purpose)); + assert(util.isU32(account)); + assert(this.isAccount(account), 'Cannot derive account index.'); return this; }; @@ -257,18 +247,8 @@ HDPublicKey.prototype.isMaster = function isMaster() { * @returns {Boolean} */ -HDPublicKey.prototype.isBIP44 = function isBIP44(account) { - return common.isBIP44(this, account); -}; - -/** - * Test whether the key is a BIP45 purpose key. - * @method - * @returns {Boolean} - */ - -HDPublicKey.prototype.isBIP45 = function isBIP45() { - return common.isBIP45(this); +HDPublicKey.prototype.isAccount = function isAccount(account) { + return common.isAccount(this, account); }; /** diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 5670b2e0..024d14f9 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -283,7 +283,7 @@ Account.prototype.pushKey = function pushKey(key) { if (!HD.isPublic(key)) throw new Error('Must add HD keys to wallet.'); - if (!key.isBIP44()) + if (!key.isAccount()) throw new Error('Must add HD account keys to BIP44 wallet.'); if (this.type !== Account.types.MULTISIG) @@ -323,7 +323,7 @@ Account.prototype.spliceKey = function spliceKey(key) { if (!HD.isPublic(key)) throw new Error('Must add HD keys to wallet.'); - if (!key.isBIP44()) + if (!key.isAccount()) throw new Error('Must add HD account keys to BIP44 wallet.'); if (this.type !== Account.types.MULTISIG) @@ -538,7 +538,7 @@ Account.prototype.deriveKey = function deriveKey(branch, index, master) { let key; if (master && master.key && !this.watchOnly) { - key = master.key.deriveBIP44(this.accountIndex); + key = master.key.deriveAccount(44, this.accountIndex); key = key.derive(branch).derive(index); } else { key = this.accountKey.derive(branch).derive(index); diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 23f327b8..9f39edd3 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -694,7 +694,7 @@ Wallet.prototype._createAccount = async function _createAccount(options, passphr 'Network mismatch for watch only key.'); } else { assert(this.master.key); - key = this.master.key.deriveBIP44(this.accountDepth); + key = this.master.key.deriveAccount(44, this.accountDepth); key = key.toPublic(); } diff --git a/test/util/memwallet.js b/test/util/memwallet.js index a8e98b24..a2364df5 100644 --- a/test/util/memwallet.js +++ b/test/util/memwallet.js @@ -88,7 +88,7 @@ MemWallet.prototype.init = function init() { this.master = HD.PrivateKey.generate(); if (!this.key) - this.key = this.master.deriveBIP44(this.account); + this.key = this.master.deriveAccount(44, this.account); i = this.receiveDepth; while (i--) @@ -132,7 +132,7 @@ MemWallet.prototype.derivePath = function derivePath(path) { }; MemWallet.prototype.deriveKey = function deriveKey(branch, index) { - let key = this.master.deriveBIP44(this.account); + let key = this.master.deriveAccount(44, this.account); key = key.derive(branch).derive(index); const ring = new KeyRing({ network: this.network, diff --git a/test/wallet-test.js b/test/wallet-test.js index 1f7caf5a..9a7daaee 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -293,7 +293,7 @@ describe('Wallet', function() { }); const xpriv = HD.PrivateKey.generate(); - const key = xpriv.deriveBIP44(0).toPublic(); + const key = xpriv.deriveAccount(44, 0).toPublic(); await wallet.addSharedKey(key);