Asynced keyrings and started on controller

feature/default_network_editable
Dan Finlay 8 years ago
parent ec8b0148f0
commit 2efab79f5b
  1. 44
      app/scripts/keyring-controller.js
  2. 17
      app/scripts/keyrings/hd.js
  3. 18
      app/scripts/keyrings/simple.js

@ -121,10 +121,13 @@ module.exports = class KeyringController extends EventEmitter {
this.password = password this.password = password
const keyring = this.restoreKeyring(serialized) const keyring = this.restoreKeyring(serialized)
this.keyrings.push(keyring) this.keyrings.push(keyring)
this.configManager.setSelectedAccount(keyring.getAccounts()[0]) keyring.getAccounts()
return this.persistAllKeyrings() .then((accounts) => {
this.configManager.setSelectedAccount(accounts[0])
return this.persistAllKeyrings()
})
} }
return return Promise.resolve()
}) })
} }
@ -165,13 +168,18 @@ module.exports = class KeyringController extends EventEmitter {
placeSeedWords (cb) { placeSeedWords (cb) {
const firstKeyring = this.keyrings[0] const firstKeyring = this.keyrings[0]
const seedWords = firstKeyring.serialize().mnemonic firstKeyring.serialize()
this.configManager.setSeedWords(seedWords) .then((serialized) => {
if (cb) { const seedWords = serialized.mnemonic
cb() this.configManager.setSeedWords(seedWords)
}
this.emit('update') if (cb) {
cb()
}
this.emit('update')
})
} }
submitPassword (password, cb) { submitPassword (password, cb) {
@ -259,13 +267,19 @@ module.exports = class KeyringController extends EventEmitter {
} }
persistAllKeyrings () { persistAllKeyrings () {
const serialized = this.keyrings.map((keyring) => { Promise.all(this.keyrings.map((keyring) => {
return { return Promise.all([keyring.type, keyring.serialize()])
type: keyring.type, .then((serializedKeyringArray) => {
data: keyring.serialize(), // Label the output values on each serialized Keyring:
} return {
type: serializedKeyringArray[0],
data: serializedKeyringArray[1],
}
})
}))
.then((serializedKeyrings) => {
return this.encryptor.encrypt(this.password, serializedKeyrings)
}) })
return this.encryptor.encrypt(this.password, serialized)
.then((encryptedString) => { .then((encryptedString) => {
this.configManager.setVault(encryptedString) this.configManager.setVault(encryptedString)
return true return true

@ -21,10 +21,10 @@ class HdKeyring extends EventEmitter {
} }
serialize () { serialize () {
return { return Promise.resolve({
mnemonic: this.mnemonic, mnemonic: this.mnemonic,
numberOfAccounts: this.wallets.length, numberOfAccounts: this.wallets.length,
} })
} }
deserialize (opts = {}) { deserialize (opts = {}) {
@ -40,6 +40,8 @@ class HdKeyring extends EventEmitter {
if ('numberOfAccounts' in opts) { if ('numberOfAccounts' in opts) {
this.addAccounts(opts.numberOfAccounts) this.addAccounts(opts.numberOfAccounts)
} }
return Promise.resolve()
} }
addAccounts (numberOfAccounts = 1) { addAccounts (numberOfAccounts = 1) {
@ -55,11 +57,12 @@ class HdKeyring extends EventEmitter {
newWallets.push(wallet) newWallets.push(wallet)
this.wallets.push(wallet) this.wallets.push(wallet)
} }
return newWallets.map(w => w.getAddress().toString('hex')) const hexWallets = newWallets.map(w => w.getAddress().toString('hex'))
return Promise.resolve(hexWallets)
} }
getAccounts () { getAccounts () {
return this.wallets.map(w => w.getAddress().toString('hex')) return Promise.resolve(this.wallets.map(w => w.getAddress().toString('hex')))
} }
// tx is an instance of the ethereumjs-transaction class. // tx is an instance of the ethereumjs-transaction class.
@ -67,7 +70,7 @@ class HdKeyring extends EventEmitter {
const wallet = this._getWalletForAccount(address) const wallet = this._getWalletForAccount(address)
var privKey = wallet.getPrivateKey() var privKey = wallet.getPrivateKey()
tx.sign(privKey) tx.sign(privKey)
return tx return Promise.resolve(tx)
} }
// For eth_sign, we need to sign transactions: // For eth_sign, we need to sign transactions:
@ -77,12 +80,12 @@ class HdKeyring extends EventEmitter {
var privKey = wallet.getPrivateKey() var privKey = wallet.getPrivateKey()
var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey) var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey)
var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s)) var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s))
return rawMsgSig return Promise.resolve(rawMsgSig)
} }
exportAccount (address) { exportAccount (address) {
const wallet = this._getWalletForAccount(address) const wallet = this._getWalletForAccount(address)
return wallet.getPrivateKey().toString('hex') return Promise.resolve(wallet.getPrivateKey().toString('hex'))
} }

@ -8,10 +8,6 @@ class SimpleKeyring extends EventEmitter {
/* PUBLIC METHODS */ /* PUBLIC METHODS */
static type () {
return type
}
constructor (opts) { constructor (opts) {
super() super()
this.type = type this.type = type
@ -20,7 +16,7 @@ class SimpleKeyring extends EventEmitter {
} }
serialize () { serialize () {
return this.wallets.map(w => w.getPrivateKey().toString('hex')) return Promise.resolve(this.wallets.map(w => w.getPrivateKey().toString('hex')))
} }
deserialize (wallets = []) { deserialize (wallets = []) {
@ -29,6 +25,7 @@ class SimpleKeyring extends EventEmitter {
const wallet = Wallet.fromPrivateKey(b) const wallet = Wallet.fromPrivateKey(b)
return wallet return wallet
}) })
return Promise.resolve()
} }
addAccounts (n = 1) { addAccounts (n = 1) {
@ -37,11 +34,12 @@ class SimpleKeyring extends EventEmitter {
newWallets.push(Wallet.generate()) newWallets.push(Wallet.generate())
} }
this.wallets = this.wallets.concat(newWallets) this.wallets = this.wallets.concat(newWallets)
return newWallets.map(w => w.getAddress().toString('hex')) const hexWallets = newWallets.map(w => w.getAddress().toString('hex'))
return Promise.resolve(hexWallets)
} }
getAccounts () { getAccounts () {
return this.wallets.map(w => w.getAddress().toString('hex')) return Promise.resolve(this.wallets.map(w => w.getAddress().toString('hex')))
} }
// tx is an instance of the ethereumjs-transaction class. // tx is an instance of the ethereumjs-transaction class.
@ -49,7 +47,7 @@ class SimpleKeyring extends EventEmitter {
const wallet = this._getWalletForAccount(address) const wallet = this._getWalletForAccount(address)
var privKey = wallet.getPrivateKey() var privKey = wallet.getPrivateKey()
tx.sign(privKey) tx.sign(privKey)
return tx return Promise.resolve(tx)
} }
// For eth_sign, we need to sign transactions: // For eth_sign, we need to sign transactions:
@ -59,12 +57,12 @@ class SimpleKeyring extends EventEmitter {
var privKey = wallet.getPrivateKey() var privKey = wallet.getPrivateKey()
var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey) var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey)
var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s)) var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s))
return rawMsgSig return Promise.resolve(rawMsgSig)
} }
exportAccount (address) { exportAccount (address) {
const wallet = this._getWalletForAccount(address) const wallet = this._getWalletForAccount(address)
return wallet.getPrivateKey().toString('hex') return Promise.resolve(wallet.getPrivateKey().toString('hex'))
} }

Loading…
Cancel
Save