From c0d3db6a8ce2fe0522fa4e3cba1e01d10469280f Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 31 Jan 2017 20:02:38 -0800 Subject: [PATCH] keyring - synchronous getState --- app/scripts/keyring-controller.js | 55 ++++++++++++++++------------- app/scripts/metamask-controller.js | 38 +++++++++----------- test/unit/idStore-migration-test.js | 13 +++---- 3 files changed, 51 insertions(+), 55 deletions(-) diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js index f7a4e4010..3737d1b55 100644 --- a/app/scripts/keyring-controller.js +++ b/app/scripts/keyring-controller.js @@ -2,6 +2,7 @@ const ethUtil = require('ethereumjs-util') const BN = ethUtil.BN const bip39 = require('bip39') const EventEmitter = require('events').EventEmitter +const extend = require('xtend') const ObservableStore = require('obs-store') const filter = require('promise-filter') const encryptor = require('browser-passworder') @@ -32,6 +33,7 @@ class KeyringController extends EventEmitter { super() const initState = opts.initState || {} this.store = new ObservableStore(initState) + this.memStore = new ObservableStore({}) this.configManager = opts.configManager this.ethStore = opts.ethStore this.encryptor = encryptor @@ -74,30 +76,27 @@ class KeyringController extends EventEmitter { // in this class, but will need to be Promisified when we move our // persistence to an async model. getState () { - return Promise.all(this.keyrings.map(this.displayForKeyring)) - .then((displayKeyrings) => { - const state = this.store.getState() - // old wallet - const wallet = this.configManager.getWallet() - return { - // computed - isInitialized: (!!wallet || !!state.vault), - isUnlocked: (!!this.password), - keyrings: displayKeyrings, - // hard coded - keyringTypes: this.keyringTypes.map(krt => krt.type), - // memStore - identities: this.identities, - // configManager - seedWords: this.configManager.getSeedWords(), - isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), - currentFiat: this.configManager.getCurrentFiat(), - conversionRate: this.configManager.getConversionRate(), - conversionDate: this.configManager.getConversionDate(), - // messageManager - unconfMsgs: messageManager.unconfirmedMsgs(), - messages: messageManager.getMsgList(), - } + const state = this.store.getState() + // old wallet + const wallet = this.configManager.getWallet() + const memState = this.memStore.getState() + return extend(memState, { + // computed + isInitialized: (!!wallet || !!state.vault), + isUnlocked: (!!this.password), + // hard coded + keyringTypes: this.keyringTypes.map(krt => krt.type), + // memStore + identities: this.identities, + // configManager + seedWords: this.configManager.getSeedWords(), + isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), + currentFiat: this.configManager.getCurrentFiat(), + conversionRate: this.configManager.getConversionRate(), + conversionDate: this.configManager.getConversionDate(), + // messageManager + unconfMsgs: messageManager.unconfirmedMsgs(), + messages: messageManager.getMsgList(), }) } @@ -164,6 +163,7 @@ class KeyringController extends EventEmitter { setLocked () { this.password = null this.keyrings = [] + this._updateMemStoreKeyrings() return this.fullUpdate() } @@ -639,6 +639,13 @@ class KeyringController extends EventEmitter { this.identities = {} } + _updateMemStoreKeyrings() { + Promise.all(this.keyrings.map(this.displayForKeyring)) + .then((keyrings) => { + this.memStore.updateState({ keyrings }) + }) + } + } module.exports = KeyringController diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 2c379f8d9..17b5683a1 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -169,22 +169,19 @@ module.exports = class MetamaskController extends EventEmitter { // getState () { - return this.keyringController.getState() - .then((keyringControllerState) => { - return extend( - this.state, - this.ethStore.getState(), - this.configManager.getConfig(), - this.txManager.getState(), - keyringControllerState, - this.preferencesController.store.getState(), - this.noticeController.getState(), - { - shapeShiftTxList: this.configManager.getShapeShiftTxList(), - lostAccounts: this.configManager.getLostAccounts(), - } - ) - }) + return extend( + this.state, + this.ethStore.getState(), + this.configManager.getConfig(), + this.txManager.getState(), + this.keyringController.getState(), + this.preferencesController.store.getState(), + this.noticeController.getState(), + { + shapeShiftTxList: this.configManager.getShapeShiftTxList(), + lostAccounts: this.configManager.getLostAccounts(), + } + ) } // @@ -199,7 +196,7 @@ module.exports = class MetamaskController extends EventEmitter { return { // etc - getState: nodeify(this.getState.bind(this)), + getState: (cb) => cb(null, this.getState()), setRpcTarget: this.setRpcTarget.bind(this), setProviderType: this.setProviderType.bind(this), useEtherscanProvider: this.useEtherscanProvider.bind(this), @@ -295,11 +292,8 @@ module.exports = class MetamaskController extends EventEmitter { ) } - sendUpdate () { - this.getState() - .then((state) => { - this.emit('update', state) - }) + sendUpdate () { + this.emit('update', this.getState()) } // diff --git a/test/unit/idStore-migration-test.js b/test/unit/idStore-migration-test.js index 3aaf4bb94..1537e88ec 100644 --- a/test/unit/idStore-migration-test.js +++ b/test/unit/idStore-migration-test.js @@ -83,15 +83,10 @@ describe('IdentityStore to KeyringController migration', function() { describe('entering a password', function() { it('should identify an old wallet as an initialized keyring', function(done) { keyringController.configManager.setWallet('something') - keyringController.getState() - .then((state) => { - assert(state.isInitialized, 'old vault counted as initialized.') - assert(!state.lostAccounts, 'no lost accounts') - done() - }) - .catch((err) => { - done(err) - }) + const state = keyringController.getState() + assert(state.isInitialized, 'old vault counted as initialized.') + assert(!state.lostAccounts, 'no lost accounts') + done() }) }) })