From 9ba92dc4284816c51632e2214503c98925f2b18e Mon Sep 17 00:00:00 2001 From: Matthew Zipkin Date: Sun, 7 Oct 2018 09:45:11 -0700 Subject: [PATCH] move purpose definition to common --- lib/hd/private.js | 5 +---- lib/hd/public.js | 5 +---- lib/wallet/account.js | 50 +++++++++---------------------------------- lib/wallet/common.js | 36 +++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/lib/hd/private.js b/lib/hd/private.js index 935c41a725..c908fc6708 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -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'; diff --git a/lib/hd/public.js b/lib/hd/public.js index b11a1beccc..5b7275ead3 100644 --- a/lib/hd/public.js +++ b/lib/hd/public.js @@ -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'; diff --git a/lib/wallet/account.js b/lib/wallet/account.js index b730047a2e..9027f4e404 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -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') @@ -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); @@ -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); @@ -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; @@ -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} diff --git a/lib/wallet/common.js b/lib/wallet/common.js index 01a7c209a1..12a587fc31 100644 --- a/lib/wallet/common.js +++ b/lib/wallet/common.js @@ -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 +}