mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
keyring - synchronous getState
This commit is contained in:
parent
8257853819
commit
c0d3db6a8c
@ -2,6 +2,7 @@ const ethUtil = require('ethereumjs-util')
|
|||||||
const BN = ethUtil.BN
|
const BN = ethUtil.BN
|
||||||
const bip39 = require('bip39')
|
const bip39 = require('bip39')
|
||||||
const EventEmitter = require('events').EventEmitter
|
const EventEmitter = require('events').EventEmitter
|
||||||
|
const extend = require('xtend')
|
||||||
const ObservableStore = require('obs-store')
|
const ObservableStore = require('obs-store')
|
||||||
const filter = require('promise-filter')
|
const filter = require('promise-filter')
|
||||||
const encryptor = require('browser-passworder')
|
const encryptor = require('browser-passworder')
|
||||||
@ -32,6 +33,7 @@ class KeyringController extends EventEmitter {
|
|||||||
super()
|
super()
|
||||||
const initState = opts.initState || {}
|
const initState = opts.initState || {}
|
||||||
this.store = new ObservableStore(initState)
|
this.store = new ObservableStore(initState)
|
||||||
|
this.memStore = new ObservableStore({})
|
||||||
this.configManager = opts.configManager
|
this.configManager = opts.configManager
|
||||||
this.ethStore = opts.ethStore
|
this.ethStore = opts.ethStore
|
||||||
this.encryptor = encryptor
|
this.encryptor = encryptor
|
||||||
@ -74,16 +76,14 @@ class KeyringController extends EventEmitter {
|
|||||||
// in this class, but will need to be Promisified when we move our
|
// in this class, but will need to be Promisified when we move our
|
||||||
// persistence to an async model.
|
// persistence to an async model.
|
||||||
getState () {
|
getState () {
|
||||||
return Promise.all(this.keyrings.map(this.displayForKeyring))
|
|
||||||
.then((displayKeyrings) => {
|
|
||||||
const state = this.store.getState()
|
const state = this.store.getState()
|
||||||
// old wallet
|
// old wallet
|
||||||
const wallet = this.configManager.getWallet()
|
const wallet = this.configManager.getWallet()
|
||||||
return {
|
const memState = this.memStore.getState()
|
||||||
|
return extend(memState, {
|
||||||
// computed
|
// computed
|
||||||
isInitialized: (!!wallet || !!state.vault),
|
isInitialized: (!!wallet || !!state.vault),
|
||||||
isUnlocked: (!!this.password),
|
isUnlocked: (!!this.password),
|
||||||
keyrings: displayKeyrings,
|
|
||||||
// hard coded
|
// hard coded
|
||||||
keyringTypes: this.keyringTypes.map(krt => krt.type),
|
keyringTypes: this.keyringTypes.map(krt => krt.type),
|
||||||
// memStore
|
// memStore
|
||||||
@ -97,7 +97,6 @@ class KeyringController extends EventEmitter {
|
|||||||
// messageManager
|
// messageManager
|
||||||
unconfMsgs: messageManager.unconfirmedMsgs(),
|
unconfMsgs: messageManager.unconfirmedMsgs(),
|
||||||
messages: messageManager.getMsgList(),
|
messages: messageManager.getMsgList(),
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,6 +163,7 @@ class KeyringController extends EventEmitter {
|
|||||||
setLocked () {
|
setLocked () {
|
||||||
this.password = null
|
this.password = null
|
||||||
this.keyrings = []
|
this.keyrings = []
|
||||||
|
this._updateMemStoreKeyrings()
|
||||||
return this.fullUpdate()
|
return this.fullUpdate()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -639,6 +639,13 @@ class KeyringController extends EventEmitter {
|
|||||||
this.identities = {}
|
this.identities = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_updateMemStoreKeyrings() {
|
||||||
|
Promise.all(this.keyrings.map(this.displayForKeyring))
|
||||||
|
.then((keyrings) => {
|
||||||
|
this.memStore.updateState({ keyrings })
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = KeyringController
|
module.exports = KeyringController
|
||||||
|
@ -169,14 +169,12 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
//
|
//
|
||||||
|
|
||||||
getState () {
|
getState () {
|
||||||
return this.keyringController.getState()
|
|
||||||
.then((keyringControllerState) => {
|
|
||||||
return extend(
|
return extend(
|
||||||
this.state,
|
this.state,
|
||||||
this.ethStore.getState(),
|
this.ethStore.getState(),
|
||||||
this.configManager.getConfig(),
|
this.configManager.getConfig(),
|
||||||
this.txManager.getState(),
|
this.txManager.getState(),
|
||||||
keyringControllerState,
|
this.keyringController.getState(),
|
||||||
this.preferencesController.store.getState(),
|
this.preferencesController.store.getState(),
|
||||||
this.noticeController.getState(),
|
this.noticeController.getState(),
|
||||||
{
|
{
|
||||||
@ -184,7 +182,6 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
lostAccounts: this.configManager.getLostAccounts(),
|
lostAccounts: this.configManager.getLostAccounts(),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -199,7 +196,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
// etc
|
// etc
|
||||||
getState: nodeify(this.getState.bind(this)),
|
getState: (cb) => cb(null, this.getState()),
|
||||||
setRpcTarget: this.setRpcTarget.bind(this),
|
setRpcTarget: this.setRpcTarget.bind(this),
|
||||||
setProviderType: this.setProviderType.bind(this),
|
setProviderType: this.setProviderType.bind(this),
|
||||||
useEtherscanProvider: this.useEtherscanProvider.bind(this),
|
useEtherscanProvider: this.useEtherscanProvider.bind(this),
|
||||||
@ -296,10 +293,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sendUpdate () {
|
sendUpdate () {
|
||||||
this.getState()
|
this.emit('update', this.getState())
|
||||||
.then((state) => {
|
|
||||||
this.emit('update', state)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -83,15 +83,10 @@ describe('IdentityStore to KeyringController migration', function() {
|
|||||||
describe('entering a password', function() {
|
describe('entering a password', function() {
|
||||||
it('should identify an old wallet as an initialized keyring', function(done) {
|
it('should identify an old wallet as an initialized keyring', function(done) {
|
||||||
keyringController.configManager.setWallet('something')
|
keyringController.configManager.setWallet('something')
|
||||||
keyringController.getState()
|
const state = keyringController.getState()
|
||||||
.then((state) => {
|
|
||||||
assert(state.isInitialized, 'old vault counted as initialized.')
|
assert(state.isInitialized, 'old vault counted as initialized.')
|
||||||
assert(!state.lostAccounts, 'no lost accounts')
|
assert(!state.lostAccounts, 'no lost accounts')
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
|
||||||
done(err)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user