Fix more async usage of KeyringController

feature/default_network_editable
Dan Finlay 8 years ago
parent 230a0ab876
commit d9dc2eac63
  1. 57
      app/scripts/keyring-controller.js

@ -98,9 +98,11 @@ module.exports = class KeyringController extends EventEmitter {
this.addNewKeyring('HD Key Tree', { this.addNewKeyring('HD Key Tree', {
mnemonic: seed, mnemonic: seed,
numberOfAccounts: 1, numberOfAccounts: 1,
}, (err) => { }).then(() => {
const firstKeyring = this.keyrings[0] const firstKeyring = this.keyrings[0]
const accounts = firstKeyring.getAccounts() return firstKeyring.getAccounts()
})
.then((accounts) => {
const firstAccount = accounts[0] const firstAccount = accounts[0]
const hexAccount = normalize(firstAccount) const hexAccount = normalize(firstAccount)
this.configManager.setSelectedAccount(hexAccount) this.configManager.setSelectedAccount(hexAccount)
@ -108,7 +110,7 @@ module.exports = class KeyringController extends EventEmitter {
this.persistAllKeyrings() this.persistAllKeyrings()
.then(() => { .then(() => {
this.emit('update') this.emit('update')
cb(err) cb(err, this.getState())
}) })
}) })
}) })
@ -129,7 +131,6 @@ module.exports = class KeyringController extends EventEmitter {
this.configManager.setSelectedAccount(accounts[0]) this.configManager.setSelectedAccount(accounts[0])
return this.persistAllKeyrings() return this.persistAllKeyrings()
}) })
} else { } else {
return Promise.resolve() return Promise.resolve()
} }
@ -154,15 +155,17 @@ module.exports = class KeyringController extends EventEmitter {
this.clearKeyrings() this.clearKeyrings()
this.addNewKeyring('HD Key Tree', {numberOfAccounts: 1}, (err) => { this.addNewKeyring('HD Key Tree', {numberOfAccounts: 1}, (err) => {
if (err) return cb(err) if (err) return cb(err)
const accounts = this.keyrings[0].getAccounts() this.keyrings[0].getAccounts()
const firstAccount = accounts[0] .then((accounts) => {
const hexAccount = normalize(firstAccount) const firstAccount = accounts[0]
this.configManager.setSelectedAccount(firstAccount) const hexAccount = normalize(firstAccount)
this.configManager.setSelectedAccount(firstAccount)
this.placeSeedWords() this.placeSeedWords()
this.emit('newAccount', hexAccount) this.emit('newAccount', hexAccount)
this.setupAccounts(accounts) this.setupAccounts(accounts)
this.persistAllKeyrings() return this.persistAllKeyrings()
})
.then(() => { .then(() => {
cb() cb()
}) })
@ -176,7 +179,6 @@ module.exports = class KeyringController extends EventEmitter {
const firstKeyring = this.keyrings[0] const firstKeyring = this.keyrings[0]
firstKeyring.serialize() firstKeyring.serialize()
.then((serialized) => { .then((serialized) => {
const seedWords = serialized.mnemonic const seedWords = serialized.mnemonic
this.configManager.setSeedWords(seedWords) this.configManager.setSeedWords(seedWords)
@ -207,16 +209,23 @@ module.exports = class KeyringController extends EventEmitter {
addNewKeyring (type, opts, cb) { addNewKeyring (type, opts, cb) {
const Keyring = this.getKeyringClassForType(type) const Keyring = this.getKeyringClassForType(type)
const keyring = new Keyring(opts) const keyring = new Keyring(opts)
const accounts = keyring.getAccounts() return keyring.getAccounts()
.then((accounts) => {
this.keyrings.push(keyring) this.keyrings.push(keyring)
this.setupAccounts(accounts) return this.setupAccounts(accounts)
this.persistAllKeyrings() }).then(() => {
.then(() => { return this.persistAllKeyrings()
cb() }).then(() => {
if (cb) {
cb(null, keyring)
}
return keyring
}) })
.catch((reason) => { .catch((reason) => {
cb(reason) if (cb) {
cb(reason)
}
return reason
}) })
} }
@ -240,8 +249,7 @@ module.exports = class KeyringController extends EventEmitter {
setupAccounts (accounts) { setupAccounts (accounts) {
return this.getAccounts() return this.getAccounts()
.then((loadedAccounts) => { .then((loadedAccounts) => {
var arr = accounts || loadedAccounts const arr = accounts || loadedAccounts
arr.forEach((account) => { arr.forEach((account) => {
this.getBalanceAndNickname(account) this.getBalanceAndNickname(account)
}) })
@ -544,7 +552,7 @@ module.exports = class KeyringController extends EventEmitter {
txParams.gasLimit = normalize(txParams.gasLimit || txParams.gas) txParams.gasLimit = normalize(txParams.gasLimit || txParams.gas)
txParams.nonce = normalize(txParams.nonce) txParams.nonce = normalize(txParams.nonce)
let tx = new Transaction(txParams) const tx = new Transaction(txParams)
keyring.signTransaction(address, tx) keyring.signTransaction(address, tx)
.then((tx) => { .then((tx) => {
// Add the tx hash to the persisted meta-tx object // Add the tx hash to the persisted meta-tx object
@ -557,7 +565,6 @@ module.exports = class KeyringController extends EventEmitter {
var rawTx = ethUtil.bufferToHex(tx.serialize()) var rawTx = ethUtil.bufferToHex(tx.serialize())
cb(null, rawTx) cb(null, rawTx)
}) })
} catch (e) { } catch (e) {
cb(e) cb(e)
} }

Loading…
Cancel
Save