aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/keyrings/hd.js
diff options
context:
space:
mode:
authorkumavis <kumavis@users.noreply.github.com>2017-01-17 07:02:24 +0800
committerGitHub <noreply@github.com>2017-01-17 07:02:24 +0800
commitee62a6a3913967a6840d28ca812b94bbfa7a5779 (patch)
treecac32de6cc520917db9621ce93f7f4ad9abd787e /app/scripts/keyrings/hd.js
parente8e06542e47b41668f196b6db54290490003845b (diff)
parent88fabdd2de78d0dd4be86b02c94a64d9748667fa (diff)
downloadtangerine-wallet-browser-ee62a6a3913967a6840d28ca812b94bbfa7a5779.tar.gz
tangerine-wallet-browser-ee62a6a3913967a6840d28ca812b94bbfa7a5779.tar.zst
tangerine-wallet-browser-ee62a6a3913967a6840d28ca812b94bbfa7a5779.zip
Merge pull request #1005 from MetaMask/dev
Merge dev into master!
Diffstat (limited to 'app/scripts/keyrings/hd.js')
-rw-r--r--app/scripts/keyrings/hd.js111
1 files changed, 111 insertions, 0 deletions
diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js
new file mode 100644
index 000000000..80b713b58
--- /dev/null
+++ b/app/scripts/keyrings/hd.js
@@ -0,0 +1,111 @@
+const EventEmitter = require('events').EventEmitter
+const hdkey = require('ethereumjs-wallet/hdkey')
+const bip39 = require('bip39')
+const ethUtil = require('ethereumjs-util')
+
+// *Internal Deps
+const sigUtil = require('../lib/sig-util')
+
+// Options:
+const hdPathString = `m/44'/60'/0'/0`
+const type = 'HD Key Tree'
+
+class HdKeyring extends EventEmitter {
+
+ /* PUBLIC METHODS */
+
+ constructor (opts = {}) {
+ super()
+ this.type = type
+ this.deserialize(opts)
+ }
+
+ serialize () {
+ return Promise.resolve({
+ mnemonic: this.mnemonic,
+ numberOfAccounts: this.wallets.length,
+ })
+ }
+
+ deserialize (opts = {}) {
+ this.opts = opts || {}
+ this.wallets = []
+ this.mnemonic = null
+ this.root = null
+
+ if (opts.mnemonic) {
+ this._initFromMnemonic(opts.mnemonic)
+ }
+
+ if (opts.numberOfAccounts) {
+ return this.addAccounts(opts.numberOfAccounts)
+ }
+
+ return Promise.resolve([])
+ }
+
+ addAccounts (numberOfAccounts = 1) {
+ if (!this.root) {
+ this._initFromMnemonic(bip39.generateMnemonic())
+ }
+
+ const oldLen = this.wallets.length
+ const newWallets = []
+ for (let i = oldLen; i < numberOfAccounts + oldLen; i++) {
+ const child = this.root.deriveChild(i)
+ const wallet = child.getWallet()
+ newWallets.push(wallet)
+ this.wallets.push(wallet)
+ }
+ const hexWallets = newWallets.map(w => w.getAddress().toString('hex'))
+ return Promise.resolve(hexWallets)
+ }
+
+ getAccounts () {
+ return Promise.resolve(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 Promise.resolve(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 Promise.resolve(rawMsgSig)
+ }
+
+ exportAccount (address) {
+ const wallet = this._getWalletForAccount(address)
+ return Promise.resolve(wallet.getPrivateKey().toString('hex'))
+ }
+
+
+ /* PRIVATE METHODS */
+
+ _initFromMnemonic (mnemonic) {
+ this.mnemonic = mnemonic
+ const seed = bip39.mnemonicToSeed(mnemonic)
+ this.hdWallet = hdkey.fromMasterSeed(seed)
+ this.root = this.hdWallet.derivePath(hdPathString)
+ }
+
+
+ _getWalletForAccount (account) {
+ return this.wallets.find((w) => {
+ const address = w.getAddress().toString('hex')
+ return ((address === account) || (sigUtil.normalize(address) === account))
+ })
+ }
+}
+
+HdKeyring.type = type
+module.exports = HdKeyring