hd: pass purpose number to bip44 derivation. drop bip45 support.
This commit is contained in:
parent
26f6fb5277
commit
3c2c8ea955
@ -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;
|
||||
};
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user