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}
|
* @returns {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
common.isBIP44 = function isBIP44(key, account) {
|
common.isAccount = function isAccount(key, account) {
|
||||||
if (account != null) {
|
if (account != null) {
|
||||||
const index = (common.HARDENED | account) >>> 0;
|
const index = (common.HARDENED | account) >>> 0;
|
||||||
if (key.childIndex !== index)
|
if (key.childIndex !== index)
|
||||||
@ -127,14 +127,3 @@ common.isBIP44 = function isBIP44(key, account) {
|
|||||||
}
|
}
|
||||||
return key.depth === 3 && (key.childIndex & common.HARDENED) !== 0;
|
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.
|
* Derive a BIP44 account key.
|
||||||
|
* @param {Number} purpose
|
||||||
* @param {Number} account
|
* @param {Number} account
|
||||||
* @param {Boolean?} bip48
|
|
||||||
* @returns {HDPrivateKey}
|
* @returns {HDPrivateKey}
|
||||||
* @throws Error if key is not a master key.
|
* @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(util.isU32(account), 'Account index must be a number.');
|
||||||
assert(this.isMaster(), 'Cannot derive account index.');
|
assert(this.isMaster(), 'Cannot derive account index.');
|
||||||
return this
|
return this
|
||||||
.derive(bip48 ? 48 : 44, true)
|
.derive(purpose, true)
|
||||||
.derive(this.network.keyPrefix.coinType, true)
|
.derive(this.network.keyPrefix.coinType, true)
|
||||||
.derive(account, 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.
|
* Test whether the key is a master key.
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
@ -297,17 +288,8 @@ HDPrivateKey.prototype.isMaster = function isMaster() {
|
|||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HDPrivateKey.prototype.isBIP44 = function isBIP44(account) {
|
HDPrivateKey.prototype.isAccount = function isAccount(account) {
|
||||||
return common.isBIP44(this, account);
|
return common.isAccount(this, account);
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test whether the key is a BIP45 purpose key.
|
|
||||||
* @returns {Boolean}
|
|
||||||
*/
|
|
||||||
|
|
||||||
HDPrivateKey.prototype.isBIP45 = function isBIP45() {
|
|
||||||
return common.isBIP45(this);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -217,26 +217,16 @@ HDPublicKey.prototype.getID = function getID(index) {
|
|||||||
/**
|
/**
|
||||||
* Derive a BIP44 account key (does not derive, only ensures account key).
|
* Derive a BIP44 account key (does not derive, only ensures account key).
|
||||||
* @method
|
* @method
|
||||||
|
* @param {Number} purpose
|
||||||
* @param {Number} account
|
* @param {Number} account
|
||||||
* @param {Boolean?} bip48
|
|
||||||
* @returns {HDPublicKey}
|
* @returns {HDPublicKey}
|
||||||
* @throws Error if key is not already an account key.
|
* @throws Error if key is not already an account key.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HDPublicKey.prototype.deriveBIP44 = function deriveBIP44(account, bip48) {
|
HDPublicKey.prototype.deriveAccount = function deriveAccount(purpose, account) {
|
||||||
assert(this.isBIP44(account), 'Cannot derive account index.');
|
assert(util.isU32(purpose));
|
||||||
return this;
|
assert(util.isU32(account));
|
||||||
};
|
assert(this.isAccount(account), 'Cannot derive account index.');
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.');
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -257,18 +247,8 @@ HDPublicKey.prototype.isMaster = function isMaster() {
|
|||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HDPublicKey.prototype.isBIP44 = function isBIP44(account) {
|
HDPublicKey.prototype.isAccount = function isAccount(account) {
|
||||||
return common.isBIP44(this, account);
|
return common.isAccount(this, account);
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test whether the key is a BIP45 purpose key.
|
|
||||||
* @method
|
|
||||||
* @returns {Boolean}
|
|
||||||
*/
|
|
||||||
|
|
||||||
HDPublicKey.prototype.isBIP45 = function isBIP45() {
|
|
||||||
return common.isBIP45(this);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -283,7 +283,7 @@ Account.prototype.pushKey = function pushKey(key) {
|
|||||||
if (!HD.isPublic(key))
|
if (!HD.isPublic(key))
|
||||||
throw new Error('Must add HD keys to wallet.');
|
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.');
|
throw new Error('Must add HD account keys to BIP44 wallet.');
|
||||||
|
|
||||||
if (this.type !== Account.types.MULTISIG)
|
if (this.type !== Account.types.MULTISIG)
|
||||||
@ -323,7 +323,7 @@ Account.prototype.spliceKey = function spliceKey(key) {
|
|||||||
if (!HD.isPublic(key))
|
if (!HD.isPublic(key))
|
||||||
throw new Error('Must add HD keys to wallet.');
|
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.');
|
throw new Error('Must add HD account keys to BIP44 wallet.');
|
||||||
|
|
||||||
if (this.type !== Account.types.MULTISIG)
|
if (this.type !== Account.types.MULTISIG)
|
||||||
@ -538,7 +538,7 @@ Account.prototype.deriveKey = function deriveKey(branch, index, master) {
|
|||||||
|
|
||||||
let key;
|
let key;
|
||||||
if (master && master.key && !this.watchOnly) {
|
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);
|
key = key.derive(branch).derive(index);
|
||||||
} else {
|
} else {
|
||||||
key = this.accountKey.derive(branch).derive(index);
|
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.');
|
'Network mismatch for watch only key.');
|
||||||
} else {
|
} else {
|
||||||
assert(this.master.key);
|
assert(this.master.key);
|
||||||
key = this.master.key.deriveBIP44(this.accountDepth);
|
key = this.master.key.deriveAccount(44, this.accountDepth);
|
||||||
key = key.toPublic();
|
key = key.toPublic();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -88,7 +88,7 @@ MemWallet.prototype.init = function init() {
|
|||||||
this.master = HD.PrivateKey.generate();
|
this.master = HD.PrivateKey.generate();
|
||||||
|
|
||||||
if (!this.key)
|
if (!this.key)
|
||||||
this.key = this.master.deriveBIP44(this.account);
|
this.key = this.master.deriveAccount(44, this.account);
|
||||||
|
|
||||||
i = this.receiveDepth;
|
i = this.receiveDepth;
|
||||||
while (i--)
|
while (i--)
|
||||||
@ -132,7 +132,7 @@ MemWallet.prototype.derivePath = function derivePath(path) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
MemWallet.prototype.deriveKey = function deriveKey(branch, index) {
|
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);
|
key = key.derive(branch).derive(index);
|
||||||
const ring = new KeyRing({
|
const ring = new KeyRing({
|
||||||
network: this.network,
|
network: this.network,
|
||||||
|
|||||||
@ -293,7 +293,7 @@ describe('Wallet', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const xpriv = HD.PrivateKey.generate();
|
const xpriv = HD.PrivateKey.generate();
|
||||||
const key = xpriv.deriveBIP44(0).toPublic();
|
const key = xpriv.deriveAccount(44, 0).toPublic();
|
||||||
|
|
||||||
await wallet.addSharedKey(key);
|
await wallet.addSharedKey(key);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user