diff --git a/lib/hd/common.js b/lib/hd/common.js index d686332e..a8c9f180 100644 --- a/lib/hd/common.js +++ b/lib/hd/common.js @@ -117,13 +117,13 @@ common.isMaster = function isMaster(key) { /** * Test whether the key is (most likely) a BIP44 account key. * @param {HDPrivateKey|HDPublicKey} key - * @param {Number?} accountIndex + * @param {Number?} account * @returns {Boolean} */ -common.isAccount44 = function isAccount44(key, accountIndex) { - if (accountIndex != null) { - if (key.childIndex !== common.HARDENED + accountIndex) +common.isBIP44 = function isBIP44(key, account) { + if (account != null) { + if (key.childIndex !== common.HARDENED + account) return false; } return key.depth === 3 && key.childIndex >= common.HARDENED; @@ -135,6 +135,6 @@ common.isAccount44 = function isAccount44(key, accountIndex) { * @returns {Boolean} */ -common.isPurpose45 = function isPurpose45(key) { +common.isBIP45 = function isBIP45(key) { return key.depth === 1 && key.childIndex === common.HARDENED + 45; }; diff --git a/lib/hd/private.js b/lib/hd/private.js index 99b4afda..ccddb884 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -252,18 +252,19 @@ HDPrivateKey.prototype.getID = function getID(index) { /** * Derive a BIP44 account key. - * @param {Number} accountIndex + * @param {Number} account + * @param {Boolean?} bip48 * @returns {HDPrivateKey} * @throws Error if key is not a master key. */ -HDPrivateKey.prototype.deriveAccount44 = function deriveAccount44(accountIndex) { - assert(util.isNumber(accountIndex), 'Account index must be a number.'); +HDPrivateKey.prototype.deriveBIP44 = function deriveBIP44(account, bip48) { + assert(util.isNumber(account), 'Account index must be a number.'); assert(this.isMaster(), 'Cannot derive account index.'); return this - .derive(44, true) + .derive(bip48 ? 48 : 44, true) .derive(this.network.keyPrefix.coinType, true) - .derive(accountIndex, true); + .derive(account, true); }; /** @@ -271,7 +272,7 @@ HDPrivateKey.prototype.deriveAccount44 = function deriveAccount44(accountIndex) * @returns {HDPrivateKey} */ -HDPrivateKey.prototype.derivePurpose45 = function derivePurpose45() { +HDPrivateKey.prototype.deriveBIP45 = function deriveBIP45() { assert(this.isMaster(), 'Cannot derive purpose 45.'); return this.derive(45, true); }; @@ -287,12 +288,12 @@ HDPrivateKey.prototype.isMaster = function isMaster() { /** * Test whether the key is (most likely) a BIP44 account key. - * @param {Number?} accountIndex + * @param {Number?} account * @returns {Boolean} */ -HDPrivateKey.prototype.isAccount44 = function isAccount44(accountIndex) { - return common.isAccount44(this, accountIndex); +HDPrivateKey.prototype.isBIP44 = function isBIP44(account) { + return common.isBIP44(this, account); }; /** @@ -300,8 +301,8 @@ HDPrivateKey.prototype.isAccount44 = function isAccount44(accountIndex) { * @returns {Boolean} */ -HDPrivateKey.prototype.isPurpose45 = function isPurpose45() { - return common.isPurpose45(this); +HDPrivateKey.prototype.isBIP45 = function isBIP45() { + return common.isBIP45(this); }; /** diff --git a/lib/hd/public.js b/lib/hd/public.js index 58aed56d..c9ea6490 100644 --- a/lib/hd/public.js +++ b/lib/hd/public.js @@ -218,13 +218,14 @@ HDPublicKey.prototype.getID = function getID(index) { /** * Derive a BIP44 account key (does not derive, only ensures account key). * @method - * @param {Number} accountIndex + * @param {Number} account + * @param {Boolean?} bip48 * @returns {HDPublicKey} * @throws Error if key is not already an account key. */ -HDPublicKey.prototype.deriveAccount44 = function deriveAccount44(accountIndex) { - assert(this.isAccount44(accountIndex), 'Cannot derive account index.'); +HDPublicKey.prototype.deriveBIP44 = function deriveBIP44(account, bip48) { + assert(this.isBIP44(account), 'Cannot derive account index.'); return this; }; @@ -235,8 +236,8 @@ HDPublicKey.prototype.deriveAccount44 = function deriveAccount44(accountIndex) { * @throws Error if key is not already a purpose key. */ -HDPublicKey.prototype.derivePurpose45 = function derivePurpose45() { - assert(this.isPurpose45(), 'Cannot derive purpose 45.'); +HDPublicKey.prototype.deriveBIP45 = function deriveBIP45() { + assert(this.isBIP45(), 'Cannot derive purpose 45.'); return this; }; @@ -253,12 +254,12 @@ HDPublicKey.prototype.isMaster = function isMaster() { /** * Test whether the key is (most likely) a BIP44 account key. * @method - * @param {Number?} accountIndex + * @param {Number?} account * @returns {Boolean} */ -HDPublicKey.prototype.isAccount44 = function isAccount44(accountIndex) { - return common.isAccount44(this, accountIndex); +HDPublicKey.prototype.isBIP44 = function isBIP44(account) { + return common.isBIP44(this, account); }; /** @@ -267,8 +268,8 @@ HDPublicKey.prototype.isAccount44 = function isAccount44(accountIndex) { * @returns {Boolean} */ -HDPublicKey.prototype.isPurpose45 = function isPurpose45() { - return common.isPurpose45(this); +HDPublicKey.prototype.isBIP45 = function isBIP45() { + return common.isBIP45(this); }; /** diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 24cc5e66..013aad34 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -285,7 +285,7 @@ Account.prototype.pushKey = function pushKey(key) { if (!HD.isPublic(key)) throw new Error('Must add HD keys to wallet.'); - if (!key.isAccount44()) + if (!key.isBIP44()) throw new Error('Must add HD account keys to BIP44 wallet.'); if (this.type !== Account.types.MULTISIG) @@ -325,7 +325,7 @@ Account.prototype.spliceKey = function spliceKey(key) { if (!HD.isPublic(key)) throw new Error('Must add HD keys to wallet.'); - if (!key.isAccount44()) + if (!key.isBIP44()) throw new Error('Must add HD account keys to BIP44 wallet.'); if (this.type !== Account.types.MULTISIG) @@ -542,7 +542,7 @@ Account.prototype.deriveKey = function deriveKey(branch, index, master) { assert(typeof branch === 'number'); if (master && master.key && !this.watchOnly) { - key = master.key.deriveAccount44(this.accountIndex); + key = master.key.deriveBIP44(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 39279bda..6282d2d7 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -705,7 +705,7 @@ Wallet.prototype._createAccount = async function createAccount(options, passphra 'Network mismatch for watch only key.'); } else { assert(this.master.key); - key = this.master.key.deriveAccount44(this.accountDepth); + key = this.master.key.deriveBIP44(this.accountDepth); key = key.toPublic(); } diff --git a/test/util/memwallet.js b/test/util/memwallet.js index dcb42f6c..939f4663 100644 --- a/test/util/memwallet.js +++ b/test/util/memwallet.js @@ -89,7 +89,7 @@ MemWallet.prototype.init = function init() { this.master = HD.PrivateKey.generate(); if (!this.key) - this.key = this.master.deriveAccount44(this.account); + this.key = this.master.deriveBIP44(this.account); i = this.receiveDepth; while (i--) @@ -133,7 +133,7 @@ MemWallet.prototype.derivePath = function derivePath(path) { }; MemWallet.prototype.deriveKey = function deriveKey(branch, index) { - let key = this.master.deriveAccount44(this.account); + let key = this.master.deriveBIP44(this.account); key = key.derive(branch).derive(index); key = new KeyRing({ network: this.network, diff --git a/test/wallet-test.js b/test/wallet-test.js index 0e7be4da..6f6d7a5a 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -151,7 +151,7 @@ describe('Wallet', function() { n: 2 }); - k = HD.generate().deriveAccount44(0).toPublic(); + k = HD.generate().deriveBIP44(0).toPublic(); await w.addSharedKey(k);