Complete first pass at asyncrhonizing keyring controller

feature/default_network_editable
Dan Finlay 8 years ago
parent 2efab79f5b
commit c77d355e98
  1. 84
      app/scripts/keyring-controller.js

@ -117,17 +117,21 @@ module.exports = class KeyringController extends EventEmitter {
const shouldMigrate = !!this.configManager.getWallet() && !this.configManager.getVault() const shouldMigrate = !!this.configManager.getWallet() && !this.configManager.getVault()
return this.idStoreMigrator.migratedVaultForPassword(password) return this.idStoreMigrator.migratedVaultForPassword(password)
.then((serialized) => { .then((serialized) => {
this.password = password
if (serialized && shouldMigrate) { if (serialized && shouldMigrate) {
this.password = password return this.restoreKeyring(serialized)
const keyring = this.restoreKeyring(serialized) .then((keyring) => {
this.keyrings.push(keyring) return keyring.getAccounts()
keyring.getAccounts() })
.then((accounts) => { .then((accounts) => {
this.configManager.setSelectedAccount(accounts[0]) this.configManager.setSelectedAccount(accounts[0])
return this.persistAllKeyrings() return this.persistAllKeyrings()
}) })
} else {
return Promise.resolve()
} }
return Promise.resolve()
}) })
} }
@ -216,9 +220,13 @@ module.exports = class KeyringController extends EventEmitter {
addNewAccount (keyRingNum = 0, cb) { addNewAccount (keyRingNum = 0, cb) {
const ring = this.keyrings[keyRingNum] const ring = this.keyrings[keyRingNum]
const accounts = ring.addAccounts(1) return ring.addAccounts(1)
this.setupAccounts(accounts) .then((accounts) => {
this.persistAllKeyrings() return this.setupAccounts(accounts)
})
.then(() => {
return this.persistAllKeyrings()
})
.then(() => { .then(() => {
cb() cb()
}) })
@ -228,9 +236,13 @@ module.exports = class KeyringController extends EventEmitter {
} }
setupAccounts (accounts) { setupAccounts (accounts) {
var arr = accounts || this.getAccounts() return this.getAccounts()
arr.forEach((account) => { .then((loadedAccounts) => {
this.getBalanceAndNickname(account) var arr = accounts || loadedAccounts
arr.forEach((account) => {
this.getBalanceAndNickname(account)
})
}) })
} }
@ -301,12 +313,13 @@ module.exports = class KeyringController extends EventEmitter {
const Keyring = this.getKeyringClassForType(type) const Keyring = this.getKeyringClassForType(type)
const keyring = new Keyring() const keyring = new Keyring()
keyring.deserialize(data) keyring.deserialize(data)
.then(() => {
const accounts = keyring.getAccounts() return keyring.getAccounts()
this.setupAccounts(accounts) })
.then((accounts) => {
this.keyrings.push(keyring) this.setupAccounts(accounts)
return keyring this.keyrings.push(keyring)
})
} }
getKeyringClassForType (type) { getKeyringClassForType (type) {
@ -529,17 +542,19 @@ module.exports = class KeyringController extends EventEmitter {
txParams.nonce = normalize(txParams.nonce) txParams.nonce = normalize(txParams.nonce)
let tx = new Transaction(txParams) let tx = new Transaction(txParams)
tx = keyring.signTransaction(address, tx) keyring.signTransaction(address, tx)
.then((tx) => {
// Add the tx hash to the persisted meta-tx object // Add the tx hash to the persisted meta-tx object
var txHash = ethUtil.bufferToHex(tx.hash()) var txHash = ethUtil.bufferToHex(tx.hash())
var metaTx = this.configManager.getTx(txParams.metamaskId) var metaTx = this.configManager.getTx(txParams.metamaskId)
metaTx.hash = txHash metaTx.hash = txHash
this.configManager.updateTx(metaTx) this.configManager.updateTx(metaTx)
// return raw serialized tx
var rawTx = ethUtil.bufferToHex(tx.serialize())
cb(null, rawTx)
})
// return raw serialized tx
var rawTx = ethUtil.bufferToHex(tx.serialize())
cb(null, rawTx)
} catch (e) { } catch (e) {
cb(e) cb(e)
} }
@ -549,8 +564,11 @@ module.exports = class KeyringController extends EventEmitter {
try { try {
const keyring = this.getKeyringForAccount(msgParams.from) const keyring = this.getKeyringForAccount(msgParams.from)
const address = normalize(msgParams.from) const address = normalize(msgParams.from)
const rawSig = keyring.signMessage(address, msgParams.data) return keyring.signMessage(address, msgParams.data)
cb(null, rawSig) .then((rawSig) => {
cb(null, rawSig)
return rawSig
})
} catch (e) { } catch (e) {
cb(e) cb(e)
} }
@ -581,10 +599,14 @@ module.exports = class KeyringController extends EventEmitter {
exportAccount (address, cb) { exportAccount (address, cb) {
try { try {
const keyring = this.getKeyringForAccount(address) const keyring = this.getKeyringForAccount(address)
const privateKey = keyring.exportAccount(normalize(address)) return keyring.exportAccount(normalize(address))
cb(null, privateKey) .then((privateKey) => {
cb(null, privateKey)
return privateKey
})
} catch (e) { } catch (e) {
cb(e) cb(e)
return Promise.reject(e)
} }
} }

Loading…
Cancel
Save