1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Clarify functions names. Package normalize inside util file. Fix require headers.

This commit is contained in:
Kevin Serrano 2016-11-11 15:40:12 -08:00
parent 7012747fb5
commit bfeaae69f2
No known key found for this signature in database
GPG Key ID: 7CC862A58D2889B4

View File

@ -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())
}