aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/keyring-controller.js
blob: db7e5e61eac4512d7ea35664345b44a65b038ef1 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const EventEmitter = require('events').EventEmitter
const encryptor = require('./lib/encryptor')
const messageManager = require('./lib/message-manager')


module.exports = class KeyringController extends EventEmitter {

  constructor (opts) {
    super()
    this.configManager = opts.configManager
    this.ethStore = opts.ethStore
    this.keyChains = []
  }

  keyFromPassword(password, callback) {
    deriveKeyFromPassword(password, callback);
  }

  // Takes a pw and callback, returns a password-dervied key
  getKeyForPassword(password, callback) {
    let salt = this.configManager.getSalt()

    if (!salt) {
      salt = generateSalt(32)
      this.configManager.setSalt(salt)
    }

    var logN = 14
    var r = 8
    var dkLen = 32
    var interruptStep = 200

    var cb = function(derKey) {
      try {
        var ui8arr = (new Uint8Array(derKey))
        this.pwDerivedKey = ui8arr
        callback(null, ui8arr)
      } catch (err) {
        callback(err)
      }
    }

    scrypt(password, salt, logN, r, dkLen, interruptStep, cb, null)
  }

  getState() {
    return {
      isInitialized: !!this.key,
      isUnlocked: !!this.key,
      isConfirmed: true, // this.configManager.getConfirmed(),
      isEthConfirmed: this.configManager.getShouldntShowWarning(),
      unconfTxs: this.configManager.unconfirmedTxs(),
      transactions: this.configManager.getTxList(),
      unconfMsgs: messageManager.unconfirmedMsgs(),
      messages: messageManager.getMsgList(),
      selectedAddress: this.configManager.getSelectedAccount(),
      shapeShiftTxList: this.configManager.getShapeShiftTxList(),
      currentFiat: this.configManager.getCurrentFiat(),
      conversionRate: this.configManager.getConversionRate(),
      conversionDate: this.configManager.getConversionDate(),
    }
  }

  setStore(ethStore) {
    this.ethStore = ethStore
  }

  createNewVault(password, entropy, cb) {
    encryptor.keyFromPassword(password)
    .then((key) => {
      this.key = key
      return encryptor.encryptWithKey(key, {})
    })
    .then((encryptedString) =>  {
      this.configManager.setVault(encryptedString)
      cb(null, [])
    })
    .catch((err) => {
      cb(err)
    })
  }



  submitPassword(password, cb) {
    cb()
  }

  setSelectedAddress(address, cb) {
    this.selectedAddress = address
    cb(null, address)
  }

  approveTransaction(txId, cb) {
    cb()
  }

  cancelTransaction(txId, cb) {
    if (cb && typeof cb === 'function') {
      cb()
    }
  }

  signMessage(msgParams, cb) {
    cb()
  }

  cancelMessage(msgId, cb) {
    if (cb && typeof cb === 'function') {
      cb()
    }
  }

  setLocked(cb) {
    cb()
  }

  exportAccount(address, cb) {
    cb(null, '0xPrivateKey')
  }

  saveAccountLabel(account, label, cb) {
    cb(/* null, label */)
  }

  tryPassword(password, cb) {
    cb()
  }

}

function generateSalt (byteCount) {
  return bitcore.crypto.Random.getRandomBuffer(byteCount || 32).toString('base64')
}