aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/keyrings/hd.js
blob: b98f291874f73593a2cd06c64cad6317cc283aeb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const EventEmitter = require('events').EventEmitter
const hdkey = require('ethereumjs-wallet/hdkey')
const bip39 = require('bip39')
const ethUtil = require('ethereumjs-util')
const sigUtil = require('../lib/sig-util')

const type = 'HD Key Tree'

const hdPathString = `m/44'/60'/0'/0`

module.exports = class HdKeyring extends EventEmitter {

  static type () {
    return type
  }

  constructor (opts = {}) {
    super()
    this.type = type
    this.deserialize(opts)
  }

  deserialize (opts = {}) {
    this.opts = opts || {}
    this.wallets = []
    this.mnemonic = null
    this.root = null

    if ('mnemonic' in opts) {
      this.initFromMnemonic(opts.mnemonic)
    }

    if ('numberOfAccounts' in opts) {
      this.addAccounts(opts.numberOfAccounts)
    }
  }

  initFromMnemonic (mnemonic) {
    this.mnemonic = mnemonic
    const seed = bip39.mnemonicToSeed(mnemonic)
    this.hdWallet = hdkey.fromMasterSeed(seed)
    this.root = this.hdWallet.derivePath(hdPathString)
  }

  serialize () {
    return {
      mnemonic: this.mnemonic,
      numberOfAccounts: this.wallets.length,
    }
  }

  exportAccount (address) {
    const wallet = this.getWalletForAccount(address)
    return wallet.getPrivateKey().toString('hex')
  }

  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)
    }
    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) => {
      const address = w.getAddress().toString('hex')
      return ((address === account) || (sigUtil.normalize(address) === account))
    })
  }
}