|
|
@ -2,9 +2,10 @@ const EventEmitter = require('events').EventEmitter |
|
|
|
const hdkey = require('ethereumjs-wallet/hdkey') |
|
|
|
const hdkey = require('ethereumjs-wallet/hdkey') |
|
|
|
const bip39 = require('bip39') |
|
|
|
const bip39 = require('bip39') |
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
const type = 'HD Key Tree' |
|
|
|
|
|
|
|
const sigUtil = require('../lib/sig-util') |
|
|
|
const sigUtil = require('../lib/sig-util') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const type = 'HD Key Tree' |
|
|
|
|
|
|
|
|
|
|
|
const hdPathString = `m/44'/60'/0'/0` |
|
|
|
const hdPathString = `m/44'/60'/0'/0` |
|
|
|
|
|
|
|
|
|
|
|
module.exports = class HdKeyring extends EventEmitter { |
|
|
|
module.exports = class HdKeyring extends EventEmitter { |
|
|
@ -29,8 +30,8 @@ module.exports = class HdKeyring extends EventEmitter { |
|
|
|
this.initFromMnemonic(opts.mnemonic) |
|
|
|
this.initFromMnemonic(opts.mnemonic) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ('n' in opts) { |
|
|
|
if ('numberOfAccounts' in opts) { |
|
|
|
this.addAccounts(opts.n) |
|
|
|
this.addAccounts(opts.numberOfAccounts) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -44,7 +45,7 @@ module.exports = class HdKeyring extends EventEmitter { |
|
|
|
serialize () { |
|
|
|
serialize () { |
|
|
|
return { |
|
|
|
return { |
|
|
|
mnemonic: this.mnemonic, |
|
|
|
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') |
|
|
|
return wallet.getPrivateKey().toString('hex') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
addAccounts (n = 1) { |
|
|
|
addAccounts (numberOfAccounts = 1) { |
|
|
|
if (!this.root) { |
|
|
|
if (!this.root) { |
|
|
|
this.initFromMnemonic(bip39.generateMnemonic()) |
|
|
|
this.initFromMnemonic(bip39.generateMnemonic()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const oldLen = this.wallets.length |
|
|
|
const oldLen = this.wallets.length |
|
|
|
const newWallets = [] |
|
|
|
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 child = this.root.deriveChild(i) |
|
|
|
const wallet = child.getWallet() |
|
|
|
const wallet = child.getWallet() |
|
|
|
newWallets.push(wallet) |
|
|
|
newWallets.push(wallet) |
|
|
@ -94,11 +95,7 @@ module.exports = class HdKeyring extends EventEmitter { |
|
|
|
getWalletForAccount (account) { |
|
|
|
getWalletForAccount (account) { |
|
|
|
return this.wallets.find((w) => { |
|
|
|
return this.wallets.find((w) => { |
|
|
|
const address = w.getAddress().toString('hex') |
|
|
|
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()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|