Skip to content

Commit

Permalink
move purpose definition to common
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Oct 7, 2018
1 parent ee28a13 commit dc464f6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 59 deletions.
5 changes: 1 addition & 4 deletions lib/hd/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ class HDPrivateKey {
assert(Buffer.isBuffer(options.privateKey));

if (options.purpose) {
assert(
['x','y','z'].indexOf(options.purpose) !== -1,
'Bad purpose'
);
assert(common.purposes[options.purpose] !== undefined, 'Bad purpose');
this.purpose = options.purpose;
} else {
this.purpose = 'x';
Expand Down
5 changes: 1 addition & 4 deletions lib/hd/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ class HDPublicKey {
assert(Buffer.isBuffer(options.publicKey));

if (options.purpose) {
assert(
['x','y','z'].indexOf(options.purpose) !== -1,
'Bad purpose'
);
assert(common.purposes[options.purpose] !== undefined, 'Bad purpose');
this.purpose = options.purpose;
} else {
this.purpose = 'x';
Expand Down
50 changes: 10 additions & 40 deletions lib/wallet/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,10 @@ class Account {
}

if (options.purpose != null) {
assert(typeof options.purpose === 'string');
assert(
['x','y','z'].indexOf(options.purpose) !== -1,
'Bad purpose'
);
assert(common.purposes[options.purpose] !== undefined, 'Bad purpose');
this.purpose = options.purpose;
} else {
this.purpose = 'x';
}

if (this.purpose === 'y' || this.purpose === 'z')
Expand Down Expand Up @@ -481,19 +479,11 @@ class Account {
let key;
if (master && master.key && !this.watchOnly) {
const coinType = this.network.keyPrefix.coinType;

switch (this.purpose) {
case 'x':
key = master.key.deriveAccount(44, coinType, this.accountIndex);
break;
case 'y':
key = master.key.deriveAccount(49, coinType, this.accountIndex);
break;
case 'z':
key = master.key.deriveAccount(84, coinType, this.accountIndex);
break;
}

key = master.key.deriveAccount(
common.purposePath[this.purpose],
coinType,
this.accountIndex
);
key = key.derive(branch).derive(index);
} else {
key = this.accountKey.derive(branch).derive(index);
Expand Down Expand Up @@ -889,7 +879,7 @@ class Account {
if (this.witness)
flags |= 2;

const purposeBits = Account.purposes[this.purpose];
const purposeBits = common.purposes[this.purpose];
flags |= (purposeBits << 6);

bw.writeU8(flags);
Expand Down Expand Up @@ -921,7 +911,7 @@ class Account {
const flags = br.readU8();

const purposeBits = flags >> 6;
this.purpose = Account.purposesByVal[purposeBits];
this.purpose = common.purposesByVal[purposeBits];

this.initialized = (flags & 1) !== 0;
this.witness = (flags & 2) !== 0;
Expand Down Expand Up @@ -988,26 +978,6 @@ Account.typesByVal = [
'MULTISIG'
];

/**
* Account purposes.
* @enum {Number}
* @default
*/

Account.purposes = {
x: 0,
y: 1,
z: 2
};

/**
* Account purposes by value.
* @enum {Number}
* @default
*/

Account.purposesByVal = ['x', 'y', 'z'];

/**
* Default address lookahead.
* @const {Number}
Expand Down
36 changes: 36 additions & 0 deletions lib/wallet/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,39 @@ common.sortDeps = function sortDeps(txs) {

return result;
};

/**
* Purposes from extended key prefix bytes.
* @enum {Number}
* @default
*/

common.purposes = {
x: 0,
y: 1,
z: 2
};

/**
* Purposes by value.
* @enum {Number}
* @default
*/

common.purposesByVal = [
'x',
'y',
'z'
];

/**
* BIP44 "purpose" values by extended key prefix bytes
* @enum {Number}
* @default
*/

common.purposePath = {
x: 44,
y: 49,
z: 84
};
16 changes: 5 additions & 11 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,17 +591,11 @@ class Wallet extends EventEmitter {
} else {
assert(this.master.key);
const coinType = this.network.keyPrefix.coinType;
switch (this.purpose) {
case 'x':
key = this.master.key.deriveAccount(44, coinType, this.accountDepth);
break;
case 'y':
key = this.master.key.deriveAccount(49, coinType, this.accountDepth);
break;
case 'z':
key = this.master.key.deriveAccount(84, coinType, this.accountDepth);
break;
}
key = this.master.key.deriveAccount(
common.purposePath[this.purpose],
coinType,
this.accountDepth
);
key = key.toPublic();
}

Expand Down

0 comments on commit dc464f6

Please sign in to comment.