From 55d56f77cf42a9c4e80768fd7e4a9bb6f0485606 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 20 Oct 2016 16:44:31 -0700 Subject: Began adding first basic keyring --- app/scripts/keyrings/simple.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 app/scripts/keyrings/simple.js (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/simple.js b/app/scripts/keyrings/simple.js new file mode 100644 index 000000000..3eda9b8f9 --- /dev/null +++ b/app/scripts/keyrings/simple.js @@ -0,0 +1,41 @@ +const EventEmitter = require('events').EventEmitter +const Wallet = require('ethereumjs-wallet') +const type = 'Simple Key Pair' + +module.exports = class SimpleKeyring extends EventEmitter { + + static type() { + return type + } + + constructor(opts) { + super() + this.type = type + this.opts = opts || {} + const walletData = this.opts.wallets || [] + this.wallets = walletData.map((w) => { + return Wallet.fromPrivateKey(w) + }) + } + + serialize() { + return { + type, + wallets: this.wallets.map(w => w.getPrivateKey()), + } + } + + addAccounts(n = 1) { + var newWallets = [] + for (var i = 0; i < n; i++) { + newWallets.push(Wallet.generate()) + } + this.wallets.concat(newWallets) + return newWallets.map(w => w.getAddress()) + } + + getAccounts() { + return this.wallets.map(w => w.getAddress()) + } + +} -- cgit v1.2.3 From 957b7a72b55be864320a346108673d02448caefd Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 20 Oct 2016 17:24:03 -0700 Subject: Improved simple account generation --- app/scripts/keyrings/simple.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/simple.js b/app/scripts/keyrings/simple.js index 3eda9b8f9..59d4691c6 100644 --- a/app/scripts/keyrings/simple.js +++ b/app/scripts/keyrings/simple.js @@ -12,17 +12,19 @@ module.exports = class SimpleKeyring extends EventEmitter { super() this.type = type this.opts = opts || {} - const walletData = this.opts.wallets || [] - this.wallets = walletData.map((w) => { - return Wallet.fromPrivateKey(w) - }) + this.wallets = [] } serialize() { - return { - type, - wallets: this.wallets.map(w => w.getPrivateKey()), - } + return this.wallets.map(w => w.getPrivateKey().toString('hex')) + } + + deserialize(wallets = []) { + this.wallets = wallets.map((w) => { + var b = new Buffer(w, 'hex') + const wallet = Wallet.fromPrivateKey(b) + return wallet + }) } addAccounts(n = 1) { @@ -30,12 +32,12 @@ module.exports = class SimpleKeyring extends EventEmitter { for (var i = 0; i < n; i++) { newWallets.push(Wallet.generate()) } - this.wallets.concat(newWallets) - return newWallets.map(w => w.getAddress()) + this.wallets = this.wallets.concat(newWallets) + return newWallets.map(w => w.getAddress().toString('hex')) } getAccounts() { - return this.wallets.map(w => w.getAddress()) + return this.wallets.map(w => w.getAddress().toString('hex')) } } -- cgit v1.2.3 From 9560ae93ee66cd9466c95c98b6853c2062f21235 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 20 Oct 2016 19:01:04 -0700 Subject: Added tx and msg signing to keychain & controller --- app/scripts/keyrings/simple.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/simple.js b/app/scripts/keyrings/simple.js index 59d4691c6..6c5af1884 100644 --- a/app/scripts/keyrings/simple.js +++ b/app/scripts/keyrings/simple.js @@ -1,5 +1,6 @@ const EventEmitter = require('events').EventEmitter const Wallet = require('ethereumjs-wallet') +const ethUtil = require('ethereumjs-util') const type = 'Simple Key Pair' module.exports = class SimpleKeyring extends EventEmitter { @@ -40,4 +41,44 @@ module.exports = class SimpleKeyring extends EventEmitter { return this.wallets.map(w => w.getAddress().toString('hex')) } + // tx is an instance of the ethereumjs-transaction class. + signTransaction(address, tx) { + const wallet = this.getWalletForAccount(address) + var privKey = wallet.getPrivateKey() + tx.sign(privKey) + return tx + } + + // For eth_sign, we need to sign transactions: + signMessage(withAccount, data) { + const wallet = this.getWalletForAccount(withAccount) + const message = ethUtil.removeHexPrefix(data) + var privKey = wallet.getPrivateKey() + var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey) + var rawMsgSig = ethUtil.bufferToHex(concatSig(msgSig.v, msgSig.r, msgSig.s)) + return rawMsgSig + } + + getWalletForAccount(account) { + return this.wallets.find(w => w.getAddress().toString('hex') === account) + } + +} + +function concatSig (v, r, s) { + const rSig = ethUtil.fromSigned(r) + const sSig = ethUtil.fromSigned(s) + const vSig = ethUtil.bufferToInt(v) + const rStr = padWithZeroes(ethUtil.toUnsigned(rSig).toString('hex'), 64) + const sStr = padWithZeroes(ethUtil.toUnsigned(sSig).toString('hex'), 64) + const vStr = ethUtil.stripHexPrefix(ethUtil.intToHex(vSig)) + return ethUtil.addHexPrefix(rStr.concat(sStr, vStr)).toString('hex') +} + +function padWithZeroes (number, length) { + var myString = '' + number + while (myString.length < length) { + myString = '0' + myString + } + return myString } -- cgit v1.2.3 From c3e1c5c57f2062155626647e239c2a760f3e4b8a Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 21 Oct 2016 11:10:36 -0700 Subject: Added SimpleKeyring tests --- app/scripts/keyrings/simple.js | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/simple.js b/app/scripts/keyrings/simple.js index 6c5af1884..9e832f274 100644 --- a/app/scripts/keyrings/simple.js +++ b/app/scripts/keyrings/simple.js @@ -2,6 +2,7 @@ const EventEmitter = require('events').EventEmitter const Wallet = require('ethereumjs-wallet') const ethUtil = require('ethereumjs-util') const type = 'Simple Key Pair' +const sigUtil = require('../lib/sig-util') module.exports = class SimpleKeyring extends EventEmitter { @@ -55,7 +56,7 @@ module.exports = class SimpleKeyring extends EventEmitter { const message = ethUtil.removeHexPrefix(data) var privKey = wallet.getPrivateKey() var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey) - var rawMsgSig = ethUtil.bufferToHex(concatSig(msgSig.v, msgSig.r, msgSig.s)) + var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s)) return rawMsgSig } @@ -65,20 +66,3 @@ module.exports = class SimpleKeyring extends EventEmitter { } -function concatSig (v, r, s) { - const rSig = ethUtil.fromSigned(r) - const sSig = ethUtil.fromSigned(s) - const vSig = ethUtil.bufferToInt(v) - const rStr = padWithZeroes(ethUtil.toUnsigned(rSig).toString('hex'), 64) - const sStr = padWithZeroes(ethUtil.toUnsigned(sSig).toString('hex'), 64) - const vStr = ethUtil.stripHexPrefix(ethUtil.intToHex(vSig)) - return ethUtil.addHexPrefix(rStr.concat(sStr, vStr)).toString('hex') -} - -function padWithZeroes (number, length) { - var myString = '' + number - while (myString.length < length) { - myString = '0' + myString - } - return myString -} -- cgit v1.2.3 From f02e02bef845fb4b55dfa315c29da1e0afe9e68c Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 27 Oct 2016 16:50:01 -0700 Subject: Add hd wallet keyring --- app/scripts/keyrings/hd.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 app/scripts/keyrings/hd.js (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js new file mode 100644 index 000000000..a852c9d29 --- /dev/null +++ b/app/scripts/keyrings/hd.js @@ -0,0 +1,78 @@ +const EventEmitter = require('events').EventEmitter +const hdkey = require('ethereumjs-wallet/hdkey') +const bip39 = require('bip39') +const ethUtil = require('ethereumjs-util') +const type = 'HD Key Tree' +const sigUtil = require('../lib/sig-util') + +module.exports = class SimpleKeyring extends EventEmitter { + + static type() { + return type + } + + constructor(opts) { + super() + this.type = type + this.opts = opts || {} + this.wallets = [] + this.mnemonic = null + } + + deserialize({ mnemonic, n }) { + this.initFromMnemonic(mnemonic || bip39.generateMnemonic()) + this.addAccounts(n) + } + + initFromMnemonic(mnemonic) { + const seed = bip39.mnemonicToSeed(mnemonic) + this.mnemonic = mnemonic + this.hdWallet = hdkey.fromMasterSeed(seed) + this.seed = bip39.mnemonicToSeedHex(seed) + } + + serialize() { + return { + mnemonic: this.mnemonic, + n: this.wallets.length, + } + } + + addAccounts(n = 1) { + const newWallets = [] + for (let i = 0; i < n; i++) { + const wallet = this.hdWallet.getWallet() + newWallets.push(wallet) + this.wallets.push(wallet) + } + return newWallets.map(w => w.getAddress().toString('hex')) + } + + getAccounts() { + return this.wallets.map(w => w.getAddress().toString('hex')) + } + + // tx is an instance of the ethereumjs-transaction class. + signTransaction(address, tx) { + const wallet = this.getWalletForAccount(address) + var privKey = wallet.getPrivateKey() + tx.sign(privKey) + return tx + } + + // For eth_sign, we need to sign transactions: + signMessage(withAccount, data) { + const wallet = this.getWalletForAccount(withAccount) + const message = ethUtil.removeHexPrefix(data) + var privKey = wallet.getPrivateKey() + var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey) + var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s)) + return rawMsgSig + } + + getWalletForAccount(account) { + return this.wallets.find(w => w.getAddress().toString('hex') === account) + } + +} + -- cgit v1.2.3 From 2690d1acfd3042f468cb2cd7ccc2dccd63c20cc9 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 27 Oct 2016 17:23:26 -0700 Subject: Added hd wallet tests --- app/scripts/keyrings/hd.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index a852c9d29..61df8f28e 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -5,7 +5,9 @@ const ethUtil = require('ethereumjs-util') const type = 'HD Key Tree' const sigUtil = require('../lib/sig-util') -module.exports = class SimpleKeyring extends EventEmitter { +const hdPathString = `m/44'/60'/0'/0` + +module.exports = class HdKeyring extends EventEmitter { static type() { return type @@ -28,7 +30,7 @@ module.exports = class SimpleKeyring extends EventEmitter { const seed = bip39.mnemonicToSeed(mnemonic) this.mnemonic = mnemonic this.hdWallet = hdkey.fromMasterSeed(seed) - this.seed = bip39.mnemonicToSeedHex(seed) + this.root = this.hdWallet.derivePath(hdPathString) } serialize() { @@ -39,9 +41,15 @@ module.exports = class SimpleKeyring extends EventEmitter { } addAccounts(n = 1) { + if (!this.root) { + this.initFromMnemonic(bip39.generateMnemonic()) + } + + const oldLen = this.wallets.length const newWallets = [] - for (let i = 0; i < n; i++) { - const wallet = this.hdWallet.getWallet() + for (let i = oldLen; i < n + oldLen; i++) { + const child = this.root.deriveChild(i) + const wallet = child.getWallet() newWallets.push(wallet) this.wallets.push(wallet) } -- cgit v1.2.3 From db356a181a3fde4ad528c699f6da517053171866 Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Tue, 1 Nov 2016 11:25:38 -0700 Subject: Made progress on parity for MultiVault - Deleted some unused items - Renamed files and paths to match with new locations. - Modified keyring controller logic to separate concerns. - Fix account naming issues. - Enable creation of new vault with default HD keyring. - Formatting issues. --- app/scripts/keyrings/hd.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index 61df8f28e..69b8d25bc 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -17,13 +17,22 @@ module.exports = class HdKeyring extends EventEmitter { super() this.type = type this.opts = opts || {} + this.deserialize(opts) + } + + deserialize(opts) { this.wallets = [] this.mnemonic = null - } + this.root = null + + if ('mnemonic' in opts) { + this.initFromMnemonic(opts.mnemonic) + } + + if ('n' in opts) { + this.addAccounts(opts.n) + } - deserialize({ mnemonic, n }) { - this.initFromMnemonic(mnemonic || bip39.generateMnemonic()) - this.addAccounts(n) } initFromMnemonic(mnemonic) { @@ -83,4 +92,3 @@ module.exports = class HdKeyring extends EventEmitter { } } - -- cgit v1.2.3 From b5f6ef8c013f2f742546a04e148bac99fbc4691c Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 1 Nov 2016 17:00:17 -0700 Subject: Fixed bugs related to clearing caches when restoring to a new vault --- app/scripts/keyrings/hd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index 69b8d25bc..90052d9e7 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -13,7 +13,7 @@ module.exports = class HdKeyring extends EventEmitter { return type } - constructor(opts) { + constructor(opts = {}) { super() this.type = type this.opts = opts || {} -- cgit v1.2.3 From 4cf1b606e46fa735263b5e1fade5910b572335e3 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 2 Nov 2016 15:04:50 -0700 Subject: Fix handling of migrating old vault style Now old vaults are recognized as an "Initialized" MetaMask instance. Upon logging in, when fetching the initial password-derived key, if there is no new-style vault, but there is an old style vault, it is migrated to the new format before proceeding through the usual unlocking steps. --- app/scripts/keyrings/hd.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index 90052d9e7..d0ebee419 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -16,11 +16,11 @@ module.exports = class HdKeyring extends EventEmitter { constructor(opts = {}) { super() this.type = type - this.opts = opts || {} this.deserialize(opts) } - deserialize(opts) { + deserialize(opts = {}) { + this.opts = opts || {} this.wallets = [] this.mnemonic = null this.root = null @@ -32,12 +32,11 @@ module.exports = class HdKeyring extends EventEmitter { if ('n' in opts) { this.addAccounts(opts.n) } - } initFromMnemonic(mnemonic) { - const seed = bip39.mnemonicToSeed(mnemonic) this.mnemonic = mnemonic + const seed = bip39.mnemonicToSeed(mnemonic) this.hdWallet = hdkey.fromMasterSeed(seed) this.root = this.hdWallet.derivePath(hdPathString) } -- cgit v1.2.3 From 2afc06287dfd1a87bd247234c9a04b92a8394cac Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Thu, 3 Nov 2016 15:40:23 -0700 Subject: Implement private key exporting. --- app/scripts/keyrings/hd.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index d0ebee419..4bfc56c15 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -48,6 +48,11 @@ module.exports = class HdKeyring extends EventEmitter { } } + exportAccount(address) { + const wallet = this.getWalletForAccount(address) + return wallet.getPrivateKey().toString('hex') + } + addAccounts(n = 1) { if (!this.root) { this.initFromMnemonic(bip39.generateMnemonic()) @@ -87,7 +92,16 @@ module.exports = class HdKeyring extends EventEmitter { } getWalletForAccount(account) { - return this.wallets.find(w => w.getAddress().toString('hex') === account) + return this.wallets.find((w) => { + const address = w.getAddress().toString('hex') + return ((address === account) || (normalize(address) === account)) + }) } + + +} + +function normalize(address) { + return ethUtil.addHexPrefix(address.toLowerCase()) } -- cgit v1.2.3 From 23263bec7d5100accd61f7648fd9355fc95e2bb7 Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Fri, 11 Nov 2016 10:26:12 -0800 Subject: Linting to the max. --- app/scripts/keyrings/hd.js | 27 ++++++++++++--------------- app/scripts/keyrings/simple.js | 19 +++++++++---------- 2 files changed, 21 insertions(+), 25 deletions(-) (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index 4bfc56c15..6df78555d 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -9,17 +9,17 @@ const hdPathString = `m/44'/60'/0'/0` module.exports = class HdKeyring extends EventEmitter { - static type() { + static type () { return type } - constructor(opts = {}) { + constructor (opts = {}) { super() this.type = type this.deserialize(opts) } - deserialize(opts = {}) { + deserialize (opts = {}) { this.opts = opts || {} this.wallets = [] this.mnemonic = null @@ -34,26 +34,26 @@ module.exports = class HdKeyring extends EventEmitter { } } - initFromMnemonic(mnemonic) { + initFromMnemonic (mnemonic) { this.mnemonic = mnemonic const seed = bip39.mnemonicToSeed(mnemonic) this.hdWallet = hdkey.fromMasterSeed(seed) this.root = this.hdWallet.derivePath(hdPathString) } - serialize() { + serialize () { return { mnemonic: this.mnemonic, n: this.wallets.length, } } - exportAccount(address) { + exportAccount (address) { const wallet = this.getWalletForAccount(address) return wallet.getPrivateKey().toString('hex') } - addAccounts(n = 1) { + addAccounts (n = 1) { if (!this.root) { this.initFromMnemonic(bip39.generateMnemonic()) } @@ -69,12 +69,12 @@ module.exports = class HdKeyring extends EventEmitter { return newWallets.map(w => w.getAddress().toString('hex')) } - getAccounts() { + getAccounts () { return this.wallets.map(w => w.getAddress().toString('hex')) } // tx is an instance of the ethereumjs-transaction class. - signTransaction(address, tx) { + signTransaction (address, tx) { const wallet = this.getWalletForAccount(address) var privKey = wallet.getPrivateKey() tx.sign(privKey) @@ -82,7 +82,7 @@ module.exports = class HdKeyring extends EventEmitter { } // For eth_sign, we need to sign transactions: - signMessage(withAccount, data) { + signMessage (withAccount, data) { const wallet = this.getWalletForAccount(withAccount) const message = ethUtil.removeHexPrefix(data) var privKey = wallet.getPrivateKey() @@ -91,17 +91,14 @@ module.exports = class HdKeyring extends EventEmitter { return rawMsgSig } - getWalletForAccount(account) { + getWalletForAccount (account) { return this.wallets.find((w) => { const address = w.getAddress().toString('hex') return ((address === account) || (normalize(address) === account)) }) } - - - } -function normalize(address) { +function normalize (address) { return ethUtil.addHexPrefix(address.toLowerCase()) } diff --git a/app/scripts/keyrings/simple.js b/app/scripts/keyrings/simple.js index 9e832f274..ee743bc03 100644 --- a/app/scripts/keyrings/simple.js +++ b/app/scripts/keyrings/simple.js @@ -6,22 +6,22 @@ const sigUtil = require('../lib/sig-util') module.exports = class SimpleKeyring extends EventEmitter { - static type() { + static type () { return type } - constructor(opts) { + constructor (opts) { super() this.type = type this.opts = opts || {} this.wallets = [] } - serialize() { + serialize () { return this.wallets.map(w => w.getPrivateKey().toString('hex')) } - deserialize(wallets = []) { + deserialize (wallets = []) { this.wallets = wallets.map((w) => { var b = new Buffer(w, 'hex') const wallet = Wallet.fromPrivateKey(b) @@ -29,7 +29,7 @@ module.exports = class SimpleKeyring extends EventEmitter { }) } - addAccounts(n = 1) { + addAccounts (n = 1) { var newWallets = [] for (var i = 0; i < n; i++) { newWallets.push(Wallet.generate()) @@ -38,12 +38,12 @@ module.exports = class SimpleKeyring extends EventEmitter { return newWallets.map(w => w.getAddress().toString('hex')) } - getAccounts() { + getAccounts () { return this.wallets.map(w => w.getAddress().toString('hex')) } // tx is an instance of the ethereumjs-transaction class. - signTransaction(address, tx) { + signTransaction (address, tx) { const wallet = this.getWalletForAccount(address) var privKey = wallet.getPrivateKey() tx.sign(privKey) @@ -51,7 +51,7 @@ module.exports = class SimpleKeyring extends EventEmitter { } // For eth_sign, we need to sign transactions: - signMessage(withAccount, data) { + signMessage (withAccount, data) { const wallet = this.getWalletForAccount(withAccount) const message = ethUtil.removeHexPrefix(data) var privKey = wallet.getPrivateKey() @@ -60,9 +60,8 @@ module.exports = class SimpleKeyring extends EventEmitter { return rawMsgSig } - getWalletForAccount(account) { + getWalletForAccount (account) { return this.wallets.find(w => w.getAddress().toString('hex') === account) } } - -- cgit v1.2.3 From bfeaae69f2a0c845d8ebfa907a90049f53ba3aea Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Fri, 11 Nov 2016 15:40:12 -0800 Subject: Clarify functions names. Package normalize inside util file. Fix require headers. --- app/scripts/keyrings/hd.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'app/scripts/keyrings') diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index 6df78555d..b98f29187 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -2,9 +2,10 @@ const EventEmitter = require('events').EventEmitter const hdkey = require('ethereumjs-wallet/hdkey') const bip39 = require('bip39') const ethUtil = require('ethereumjs-util') -const type = 'HD Key Tree' const sigUtil = require('../lib/sig-util') +const type = 'HD Key Tree' + const hdPathString = `m/44'/60'/0'/0` module.exports = class HdKeyring extends EventEmitter { @@ -29,8 +30,8 @@ module.exports = class HdKeyring extends EventEmitter { this.initFromMnemonic(opts.mnemonic) } - if ('n' in opts) { - this.addAccounts(opts.n) + if ('numberOfAccounts' in opts) { + this.addAccounts(opts.numberOfAccounts) } } @@ -44,7 +45,7 @@ module.exports = class HdKeyring extends EventEmitter { serialize () { return { mnemonic: this.mnemonic, - n: this.wallets.length, + numberOfAccounts: this.wallets.length, } } @@ -53,14 +54,14 @@ module.exports = class HdKeyring extends EventEmitter { return wallet.getPrivateKey().toString('hex') } - addAccounts (n = 1) { + addAccounts (numberOfAccounts = 1) { if (!this.root) { this.initFromMnemonic(bip39.generateMnemonic()) } const oldLen = this.wallets.length const newWallets = [] - for (let i = oldLen; i < n + oldLen; i++) { + for (let i = oldLen; i < numberOfAccounts + oldLen; i++) { const child = this.root.deriveChild(i) const wallet = child.getWallet() newWallets.push(wallet) @@ -94,11 +95,7 @@ module.exports = class HdKeyring extends EventEmitter { getWalletForAccount (account) { return this.wallets.find((w) => { const address = w.getAddress().toString('hex') - return ((address === account) || (normalize(address) === account)) + return ((address === account) || (sigUtil.normalize(address) === account)) }) } } - -function normalize (address) { - return ethUtil.addHexPrefix(address.toLowerCase()) -} -- cgit v1.2.3