hd/wallet: add bip48 arg to prepare for bip48 support. see #238.
This commit is contained in:
parent
75adfe5ec0
commit
9219e23d8c
@ -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;
|
||||
};
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user