aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/keyrings
diff options
context:
space:
mode:
authorKevin Serrano <kevgagser@gmail.com>2016-10-29 07:19:14 +0800
committerKevin Serrano <kevgagser@gmail.com>2016-10-29 07:19:14 +0800
commit9b4f3825e78659148ea2c651c2622b3fb01c41b5 (patch)
tree44d5a0838a6670db7b32ef893c7e1b3fce5dab47 /app/scripts/keyrings
parent6eb6968037aeacd9c876912634ca5f4b5287957b (diff)
parent331d9c91ee42f766fe56e2ebef1d1ad283d6c438 (diff)
downloadtangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar
tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar.gz
tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar.bz2
tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar.lz
tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar.xz
tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar.zst
tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.zip
Merge branch 'i328-MultiVault' of github.com:MetaMask/metamask-plugin into i328-MultiVault
Diffstat (limited to 'app/scripts/keyrings')
-rw-r--r--app/scripts/keyrings/hd.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js
new file mode 100644
index 000000000..61df8f28e
--- /dev/null
+++ b/app/scripts/keyrings/hd.js
@@ -0,0 +1,86 @@
+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 hdPathString = `m/44'/60'/0'/0`
+
+module.exports = class HdKeyring 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.root = this.hdWallet.derivePath(hdPathString)
+ }
+
+ serialize() {
+ return {
+ mnemonic: this.mnemonic,
+ n: this.wallets.length,
+ }
+ }
+
+ addAccounts(n = 1) {
+ if (!this.root) {
+ this.initFromMnemonic(bip39.generateMnemonic())
+ }
+
+ const oldLen = this.wallets.length
+ const newWallets = []
+ 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)
+ }
+ 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)
+ }
+
+}
+