aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/keyring-controller.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/scripts/keyring-controller.js')
-rw-r--r--app/scripts/keyring-controller.js111
1 files changed, 103 insertions, 8 deletions
diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js
index d25dddba1..86d61b22b 100644
--- a/app/scripts/keyring-controller.js
+++ b/app/scripts/keyring-controller.js
@@ -1,7 +1,13 @@
const EventEmitter = require('events').EventEmitter
const encryptor = require('./lib/encryptor')
const messageManager = require('./lib/message-manager')
+const ethUtil = require('ethereumjs-util')
+// Keyrings:
+const SimpleKeyring = require('./keyrings/simple')
+const keyringTypes = [
+ SimpleKeyring,
+]
module.exports = class KeyringController extends EventEmitter {
@@ -9,14 +15,15 @@ module.exports = class KeyringController extends EventEmitter {
super()
this.configManager = opts.configManager
this.ethStore = opts.ethStore
- this.keyChains = []
+ this.keyrings = []
+ this.identities = {} // Essentially a nickname hash
}
getState() {
return {
isInitialized: !!this.configManager.getVault(),
isUnlocked: !!this.key,
- isConfirmed: true, // this.configManager.getConfirmed(),
+ isConfirmed: true, // AUDIT this.configManager.getConfirmed(),
isEthConfirmed: this.configManager.getShouldntShowWarning(),
unconfTxs: this.configManager.unconfirmedTxs(),
transactions: this.configManager.getTxList(),
@@ -27,6 +34,8 @@ module.exports = class KeyringController extends EventEmitter {
currentFiat: this.configManager.getCurrentFiat(),
conversionRate: this.configManager.getConversionRate(),
conversionDate: this.configManager.getConversionDate(),
+ keyringTypes: keyringTypes.map((krt) => krt.type()),
+ identities: this.identities,
}
}
@@ -39,7 +48,7 @@ module.exports = class KeyringController extends EventEmitter {
this.configManager.setSalt(salt)
this.loadKey(password)
.then((key) => {
- return encryptor.encryptWithKey(key, {})
+ return encryptor.encryptWithKey(key, [])
})
.then((encryptedString) => {
this.configManager.setVault(encryptedString)
@@ -53,6 +62,10 @@ module.exports = class KeyringController extends EventEmitter {
submitPassword(password, cb) {
this.loadKey(password)
.then((key) => {
+ return this.unlockKeyrings(key)
+ })
+ .then((keyrings) => {
+ this.keyrings = keyrings
cb(null, this.getState())
})
.catch((err) => {
@@ -69,8 +82,94 @@ module.exports = class KeyringController extends EventEmitter {
})
}
+ addNewKeyring(type, opts, cb) {
+ const i = this.getAccounts().length
+ const Keyring = this.getKeyringClassForType(type)
+ const keyring = new Keyring(opts)
+ const accounts = keyring.addAccounts(1)
+
+ accounts.forEach((account) => {
+ this.createBalanceAndNickname(account, i)
+ })
+
+ this.persistAllKeyrings()
+ .then(() => {
+ cb(this.getState())
+ })
+ .catch((reason) => {
+ cb(reason)
+ })
+ }
+
+ // Takes an account address and an iterator representing
+ // the current number of nicknamed accounts.
+ createBalanceAndNickname(account, i) {
+ this.ethStore.addAccount(ethUtil.addHexPrefix(account))
+ const oldNickname = this.configManager.nicknameForWallet(account)
+ const nickname = oldNickname || `Account ${++i}`
+ this.identities[account] = {
+ address: account,
+ nickname,
+ }
+ this.saveAccountLabel(account, nickname)
+ }
+
+ saveAccountLabel (account, label, cb) {
+ const configManager = this.configManager
+ configManager.setNicknameForWallet(account, label)
+ if (cb) {
+ cb(null, label)
+ }
+ }
+
+ persistAllKeyrings() {
+ const serialized = this.keyrings.map(k => k.serialize())
+ return encryptor.encryptWithKey(this.key, serialized)
+ .then((encryptedString) => {
+ this.configManager.setVault(encryptedString)
+ return true
+ })
+ .catch((reason) => {
+ console.error('Failed to persist keyrings.', reason)
+ })
+ }
+
+ unlockKeyrings(key) {
+ const encryptedVault = this.configManager.getVault()
+ return encryptor.decryptWithKey(key, encryptedVault)
+ .then((vault) => {
+ this.keyrings = vault.map(this.restoreKeyring)
+ return this.keyrings
+ })
+ }
+
+ restoreKeyring(serialized) {
+ const { type } = serialized
+ const Keyring = this.getKeyringClassForType(type)
+ const keyring = new Keyring(serialized)
+ return keyring
+ }
+
+ getKeyringClassForType(type) {
+ const Keyring = keyringTypes.reduce((res, kr) => {
+ if (kr.type() === type) {
+ return kr
+ } else {
+ return res
+ }
+ })
+ return Keyring
+ }
+
+ getAccounts() {
+ return this.keyrings.map(kr => kr.getAccounts())
+ .reduce((res, arr) => {
+ return res.concat(arr)
+ }, [])
+ }
+
setSelectedAddress(address, cb) {
- this.selectedAddress = address
+ this.configManager.setSelectedAccount(address)
cb(null, address)
}
@@ -102,10 +201,6 @@ module.exports = class KeyringController extends EventEmitter {
cb(null, '0xPrivateKey')
}
- saveAccountLabel(account, label, cb) {
- cb(/* null, label */)
- }
-
tryPassword(password, cb) {
cb()
}