nifty-wallet/app/scripts/keyring-controller.js

121 lines
2.7 KiB
JavaScript
Raw Normal View History

const EventEmitter = require('events').EventEmitter
2016-10-19 14:55:08 -07:00
const encryptor = require('./lib/encryptor')
const messageManager = require('./lib/message-manager')
2016-10-11 15:09:22 -07:00
module.exports = class KeyringController extends EventEmitter {
2016-10-11 15:09:22 -07:00
constructor (opts) {
super()
2016-10-11 15:09:22 -07:00
this.configManager = opts.configManager
this.ethStore = opts.ethStore
2016-10-11 15:09:22 -07:00
this.keyChains = []
}
getState() {
2016-10-19 14:55:08 -07:00
return {
2016-10-20 10:28:45 -07:00
isInitialized: !!this.configManager.getVault(),
2016-10-19 14:55:08 -07:00
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) {
2016-10-20 11:33:18 -07:00
const salt = generateNewSalt()
this.configManager.setSalt(salt)
2016-10-20 10:28:45 -07:00
this.loadKey(password)
2016-10-19 14:55:08 -07:00
.then((key) => {
return encryptor.encryptWithKey(key, {})
})
2016-10-20 11:33:18 -07:00
.then((encryptedString) => {
2016-10-19 14:55:08 -07:00
this.configManager.setVault(encryptedString)
cb(null, this.getState())
2016-10-19 14:55:08 -07:00
})
.catch((err) => {
cb(err)
})
}
submitPassword(password, cb) {
2016-10-20 10:28:45 -07:00
this.loadKey(password)
.then((key) => {
cb(null, this.getState())
2016-10-20 10:28:45 -07:00
})
.catch((err) => {
cb(err)
})
}
loadKey(password) {
2016-10-20 11:33:18 -07:00
const salt = this.configManager.getSalt()
return encryptor.keyFromPassword(password + salt)
2016-10-20 10:28:45 -07:00
.then((key) => {
this.key = key
return key
})
}
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()
}
2016-10-11 15:09:22 -07:00
}
function generateSalt (byteCount) {
2016-10-20 11:33:18 -07:00
var view = new Uint8Array(32)
global.crypto.getRandomValues(view)
var b64encoded = btoa(String.fromCharCode.apply(null, view))
return b64encoded
2016-10-11 15:09:22 -07:00
}