From 0a1918f71a0a5768d7501e3ee799a3ed2e6d9f49 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 12:42:24 -0800 Subject: preferences - introduce preferences controller --- app/scripts/lib/controllers/preferences.js | 30 ++++++++++++++++++++++++++++++ app/scripts/metamask-controller.js | 9 +++++++++ 2 files changed, 39 insertions(+) create mode 100644 app/scripts/lib/controllers/preferences.js (limited to 'app/scripts') diff --git a/app/scripts/lib/controllers/preferences.js b/app/scripts/lib/controllers/preferences.js new file mode 100644 index 000000000..e338f5b5e --- /dev/null +++ b/app/scripts/lib/controllers/preferences.js @@ -0,0 +1,30 @@ +const ObservableStore = require('obs-store') +const normalizeAddress = require('../sig-util').normalize + +class PreferencesController { + + constructor (opts = {}) { + const initState = opts.initState || {} + this.store = new ObservableStore(initState) + } + + // + // PUBLIC METHODS + // + + setSelectedAddress(_address) { + const address = normalizeAddress(_address) + this.store.updateState({ selectedAddress: address }) + } + + getSelectedAddress(_address) { + return this.store.getState().selectedAddress + } + + // + // PRIVATE METHODS + // + +} + +module.exports = PreferencesController diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index b9a94b0c9..c0673dda4 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -11,6 +11,7 @@ const streamIntoProvider = require('web3-stream-provider/handler') const MetaMaskProvider = require('web3-provider-engine/zero.js') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const KeyringController = require('./keyring-controller') +const PreferencesController = require('./lib/controllers/preferences') const NoticeController = require('./notice-controller') const messageManager = require('./lib/message-manager') const TxManager = require('./transaction-manager') @@ -40,6 +41,11 @@ module.exports = class MetamaskController extends EventEmitter { }) this.configManager.updateConversionRate() + // preferences controller + this.prefencesController = new PreferencesController({ + initState: initState.PrefencesController, + }) + // rpc provider this.provider = this.initializeProvider(opts) this.provider.on('block', this.logBlock.bind(this)) @@ -101,6 +107,9 @@ module.exports = class MetamaskController extends EventEmitter { this.keyringController.store.subscribe((state) => { this.store.updateState({ KeyringController: state }) }) + this.prefencesController.store.subscribe((state) => { + this.store.updateState({ PrefencesController: state }) + }) } // -- cgit v1.2.3 From 4dafb09db20a8c7050a431e70eb773e2ac62d6c8 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 13:01:07 -0800 Subject: metamask - move selectedAccount set/get to preferencesController --- app/scripts/keyring-controller.js | 24 ------------------------ app/scripts/metamask-controller.js | 21 ++++++++++++--------- 2 files changed, 12 insertions(+), 33 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js index 40cc975f9..3e329cb3f 100644 --- a/app/scripts/keyring-controller.js +++ b/app/scripts/keyring-controller.js @@ -88,8 +88,6 @@ class KeyringController extends EventEmitter { keyringTypes: this.keyringTypes.map(krt => krt.type), // memStore identities: this.identities, - // diskStore - selectedAccount: state.selectedAccount, // configManager seedWords: this.configManager.getSeedWords(), isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), @@ -231,28 +229,6 @@ class KeyringController extends EventEmitter { .then(this.fullUpdate.bind(this)) } - // Set Selected Account - // @string address - // - // returns Promise( @string address ) - // - // Sets the state's `selectedAccount` value - // to the specified address. - setSelectedAccount (account) { - var address = normalizeAddress(account) - this.store.updateState({ selectedAccount: address }) - return this.fullUpdate() - } - - // Get Selected Account - // - // returns String - // - // Gets the state's `selectedAccount` value - getSelectedAccount () { - return this.store.getState().selectedAccount - } - // Save Account Label // @string account // @string label diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index c0673dda4..2f9e37cee 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -42,8 +42,8 @@ module.exports = class MetamaskController extends EventEmitter { this.configManager.updateConversionRate() // preferences controller - this.prefencesController = new PreferencesController({ - initState: initState.PrefencesController, + this.preferencesController = new PreferencesController({ + initState: initState.PreferencesController, }) // rpc provider @@ -71,7 +71,7 @@ module.exports = class MetamaskController extends EventEmitter { txList: this.configManager.getTxList(), txHistoryLimit: 40, setTxList: this.configManager.setTxList.bind(this.configManager), - getSelectedAccount: this.keyringController.getSelectedAccount.bind(this.keyringController), + getSelectedAccount: this.preferencesController.getSelectedAccount.bind(this.preferencesController), getGasMultiplier: this.configManager.getGasMultiplier.bind(this.configManager), getNetwork: this.getStateNetwork.bind(this), signTransaction: this.keyringController.signTransaction.bind(this.keyringController), @@ -107,8 +107,8 @@ module.exports = class MetamaskController extends EventEmitter { this.keyringController.store.subscribe((state) => { this.store.updateState({ KeyringController: state }) }) - this.prefencesController.store.subscribe((state) => { - this.store.updateState({ PrefencesController: state }) + this.preferencesController.store.subscribe((state) => { + this.store.updateState({ PreferencesController: state }) }) } @@ -125,7 +125,7 @@ module.exports = class MetamaskController extends EventEmitter { rpcUrl: this.configManager.getCurrentRpcAddress(), // account mgmt getAccounts: (cb) => { - let selectedAccount = this.keyringController.getSelectedAccount() + let selectedAccount = this.preferencesController.getSelectedAccount() let result = selectedAccount ? [selectedAccount] : [] cb(null, result) }, @@ -191,6 +191,7 @@ module.exports = class MetamaskController extends EventEmitter { getApi () { const keyringController = this.keyringController + const preferencesController = this.preferencesController const txManager = this.txManager const noticeController = this.noticeController @@ -221,12 +222,14 @@ module.exports = class MetamaskController extends EventEmitter { // vault management submitPassword: this.submitPassword.bind(this), + // PreferencesController + setSelectedAccount: nodeify(preferencesController.setSelectedAccount).bind(preferencesController), + // KeyringController setLocked: nodeify(keyringController.setLocked).bind(keyringController), createNewVaultAndKeychain: nodeify(keyringController.createNewVaultAndKeychain).bind(keyringController), createNewVaultAndRestore: nodeify(keyringController.createNewVaultAndRestore).bind(keyringController), addNewKeyring: nodeify(keyringController.addNewKeyring).bind(keyringController), - setSelectedAccount: nodeify(keyringController.setSelectedAccount).bind(keyringController), saveAccountLabel: nodeify(keyringController.saveAccountLabel).bind(keyringController), exportAccount: nodeify(keyringController.exportAccount).bind(keyringController), @@ -339,7 +342,7 @@ module.exports = class MetamaskController extends EventEmitter { // ensuring they are only ever available in the background process. clearSeedWordCache (cb) { this.configManager.setSeedWords(null) - cb(null, this.keyringController.getSelectedAccount()) + cb(null, this.preferencesController.getSelectedAccount()) } importAccountWithStrategy (strategy, args, cb) { @@ -348,7 +351,7 @@ module.exports = class MetamaskController extends EventEmitter { return this.keyringController.addNewKeyring('Simple Key Pair', [ privateKey ]) }) .then(keyring => keyring.getAccounts()) - .then((accounts) => this.keyringController.setSelectedAccount(accounts[0])) + .then((accounts) => this.preferencesController.setSelectedAccount(accounts[0])) .then(() => { cb(null, this.keyringController.fullUpdate()) }) .catch((reason) => { cb(reason) }) } -- cgit v1.2.3 From 7a0acb23337772d3204ed037cf320e02f1e5232a Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 13:01:54 -0800 Subject: migration #6 - move selectedAccount from keyringController to preferencesController --- app/scripts/migrations/006.js | 41 +++++++++++++++++++++++++++++++++++++++++ app/scripts/migrations/index.js | 1 + 2 files changed, 42 insertions(+) create mode 100644 app/scripts/migrations/006.js (limited to 'app/scripts') diff --git a/app/scripts/migrations/006.js b/app/scripts/migrations/006.js new file mode 100644 index 000000000..b2fcbae15 --- /dev/null +++ b/app/scripts/migrations/006.js @@ -0,0 +1,41 @@ +const version = 6 + +/* + +This migration moves KeyringController.selectedAddress to PreferencesController.selectedAddress + +*/ + +const extend = require('xtend') + +module.exports = { + version, + + migrate: function (versionedData) { + versionedData.meta.version = version + try { + const state = versionedData.data + const newState = migrateState(state) + versionedData.data = newState + } catch (err) { + console.warn(`MetaMask Migration #${version}` + err.stack) + } + return Promise.resolve(versionedData) + }, +} + +function migrateState (state) { + const config = state.config + + // add new state + const newState = extend(state, { + PreferencesController: { + selectedAccount: config.selectedAccount, + }, + }) + + // rm old state + delete newState.KeyringController.selectedAccount + + return newState +} diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js index a7ce745e7..17c191448 100644 --- a/app/scripts/migrations/index.js +++ b/app/scripts/migrations/index.js @@ -16,4 +16,5 @@ module.exports = [ require('./003'), require('./004'), require('./005'), + require('./006'), ] -- cgit v1.2.3 From b1de2cdefa2341eeab8d0269a1b14b349fdd5d16 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 13:09:46 -0800 Subject: metamask - rename getSelectedAccount to getSelectedAddress --- app/scripts/metamask-controller.js | 6 +++--- app/scripts/transaction-manager.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 2f9e37cee..64deae426 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -71,7 +71,7 @@ module.exports = class MetamaskController extends EventEmitter { txList: this.configManager.getTxList(), txHistoryLimit: 40, setTxList: this.configManager.setTxList.bind(this.configManager), - getSelectedAccount: this.preferencesController.getSelectedAccount.bind(this.preferencesController), + getSelectedAddress: this.preferencesController.getSelectedAddress.bind(this.preferencesController), getGasMultiplier: this.configManager.getGasMultiplier.bind(this.configManager), getNetwork: this.getStateNetwork.bind(this), signTransaction: this.keyringController.signTransaction.bind(this.keyringController), @@ -125,7 +125,7 @@ module.exports = class MetamaskController extends EventEmitter { rpcUrl: this.configManager.getCurrentRpcAddress(), // account mgmt getAccounts: (cb) => { - let selectedAccount = this.preferencesController.getSelectedAccount() + let selectedAccount = this.preferencesController.getSelectedAddress() let result = selectedAccount ? [selectedAccount] : [] cb(null, result) }, @@ -342,7 +342,7 @@ module.exports = class MetamaskController extends EventEmitter { // ensuring they are only ever available in the background process. clearSeedWordCache (cb) { this.configManager.setSeedWords(null) - cb(null, this.preferencesController.getSelectedAccount()) + cb(null, this.preferencesController.getSelectedAddress()) } importAccountWithStrategy (strategy, args, cb) { diff --git a/app/scripts/transaction-manager.js b/app/scripts/transaction-manager.js index 6d0121afd..c7d4686f3 100644 --- a/app/scripts/transaction-manager.js +++ b/app/scripts/transaction-manager.js @@ -13,7 +13,7 @@ module.exports = class TransactionManager extends EventEmitter { this.txList = opts.txList || [] this._setTxList = opts.setTxList this.txHistoryLimit = opts.txHistoryLimit - this.getSelectedAccount = opts.getSelectedAccount + this.getSelectedAddress = opts.getSelectedAddress this.provider = opts.provider this.blockTracker = opts.blockTracker this.txProviderUtils = new TxProviderUtil(this.provider) @@ -25,7 +25,7 @@ module.exports = class TransactionManager extends EventEmitter { } getState () { - var selectedAccount = this.getSelectedAccount() + var selectedAccount = this.getSelectedAddress() return { transactions: this.getTxList(), unconfTxs: this.getUnapprovedTxList(), -- cgit v1.2.3 From f9b31fe2c3a43339a4519761e4c8b2f3813e85e0 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 15:08:31 -0800 Subject: rename selectedAccount to selectedAddress --- app/scripts/keyring-controller.js | 4 +--- app/scripts/lib/controllers/preferences.js | 7 +++++-- app/scripts/metamask-controller.js | 10 ++++++---- app/scripts/migrations/006.js | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js index 3e329cb3f..f7a4e4010 100644 --- a/app/scripts/keyring-controller.js +++ b/app/scripts/keyring-controller.js @@ -150,7 +150,7 @@ class KeyringController extends EventEmitter { const firstAccount = accounts[0] if (!firstAccount) throw new Error('KeyringController - First Account not found.') const hexAccount = normalizeAddress(firstAccount) - this.setSelectedAccount(hexAccount) + this.emit('newAccount', hexAccount) return this.setupAccounts(accounts) }) .then(this.persistAllKeyrings.bind(this, password)) @@ -391,7 +391,6 @@ class KeyringController extends EventEmitter { const firstAccount = accounts[0] if (!firstAccount) throw new Error('KeyringController - No account found on keychain.') const hexAccount = normalizeAddress(firstAccount) - this.setSelectedAccount(hexAccount) this.emit('newAccount', hexAccount) return this.setupAccounts(accounts) }) @@ -638,7 +637,6 @@ class KeyringController extends EventEmitter { this.keyrings = [] this.identities = {} - this.setSelectedAccount() } } diff --git a/app/scripts/lib/controllers/preferences.js b/app/scripts/lib/controllers/preferences.js index e338f5b5e..dc9464c4e 100644 --- a/app/scripts/lib/controllers/preferences.js +++ b/app/scripts/lib/controllers/preferences.js @@ -13,8 +13,11 @@ class PreferencesController { // setSelectedAddress(_address) { - const address = normalizeAddress(_address) - this.store.updateState({ selectedAddress: address }) + return new Promise((resolve, reject) => { + const address = normalizeAddress(_address) + this.store.updateState({ selectedAddress: address }) + resolve() + }) } getSelectedAddress(_address) { diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 64deae426..e081188ce 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -62,8 +62,9 @@ module.exports = class MetamaskController extends EventEmitter { configManager: this.configManager, getNetwork: this.getStateNetwork.bind(this), }) - this.keyringController.on('newAccount', (account) => { - autoFaucet(account) + this.keyringController.on('newAccount', (address) => { + this.preferencesController.setSelectedAddress(address) + autoFaucet(address) }) // tx mgmt @@ -176,6 +177,7 @@ module.exports = class MetamaskController extends EventEmitter { this.configManager.getConfig(), this.txManager.getState(), keyringControllerState, + this.preferencesController.store.getState(), this.noticeController.getState(), { shapeShiftTxList: this.configManager.getShapeShiftTxList(), @@ -223,7 +225,7 @@ module.exports = class MetamaskController extends EventEmitter { submitPassword: this.submitPassword.bind(this), // PreferencesController - setSelectedAccount: nodeify(preferencesController.setSelectedAccount).bind(preferencesController), + setSelectedAddress: nodeify(preferencesController.setSelectedAddress).bind(preferencesController), // KeyringController setLocked: nodeify(keyringController.setLocked).bind(keyringController), @@ -351,7 +353,7 @@ module.exports = class MetamaskController extends EventEmitter { return this.keyringController.addNewKeyring('Simple Key Pair', [ privateKey ]) }) .then(keyring => keyring.getAccounts()) - .then((accounts) => this.preferencesController.setSelectedAccount(accounts[0])) + .then((accounts) => this.preferencesController.setSelectedAddress(accounts[0])) .then(() => { cb(null, this.keyringController.fullUpdate()) }) .catch((reason) => { cb(reason) }) } diff --git a/app/scripts/migrations/006.js b/app/scripts/migrations/006.js index b2fcbae15..677e89bf7 100644 --- a/app/scripts/migrations/006.js +++ b/app/scripts/migrations/006.js @@ -30,7 +30,7 @@ function migrateState (state) { // add new state const newState = extend(state, { PreferencesController: { - selectedAccount: config.selectedAccount, + selectedAddress: config.selectedAccount, }, }) -- cgit v1.2.3 From 2fa60cfcbfdeb43599007f0dd26fd44a4beec8f1 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 15:25:12 -0800 Subject: continue rename selectedAccount to selectedAddress --- app/scripts/inpage.js | 4 ++-- app/scripts/lib/inpage-provider.js | 10 +++++----- app/scripts/metamask-controller.js | 8 ++++---- app/scripts/transaction-manager.js | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 42332d92e..599604a82 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -50,9 +50,9 @@ reloadStream.once('data', triggerReload) // }) // endOfStream(pingStream, triggerReload) -// set web3 defaultAcount +// set web3 defaultAccount inpageProvider.publicConfigStore.subscribe(function (state) { - web3.eth.defaultAccount = state.selectedAccount + web3.eth.defaultAccount = state.PreferencesController.selectedAddress }) // diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js index 066916b4d..faecac137 100644 --- a/app/scripts/lib/inpage-provider.js +++ b/app/scripts/lib/inpage-provider.js @@ -63,20 +63,20 @@ function MetamaskInpageProvider (connectionStream) { MetamaskInpageProvider.prototype.send = function (payload) { const self = this - let selectedAccount + let selectedAddress let result = null switch (payload.method) { case 'eth_accounts': // read from localStorage - selectedAccount = self.publicConfigStore.getState().selectedAccount - result = selectedAccount ? [selectedAccount] : [] + selectedAddress = self.publicConfigStore.getState().selectedAddress + result = selectedAddress ? [selectedAddress] : [] break case 'eth_coinbase': // read from localStorage - selectedAccount = self.publicConfigStore.getState().selectedAccount - result = selectedAccount || '0x0000000000000000000000000000000000000000' + selectedAddress = self.publicConfigStore.getState().selectedAddress + result = selectedAddress break case 'eth_uninstallFilter': diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index e081188ce..fc7df5137 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -126,8 +126,8 @@ module.exports = class MetamaskController extends EventEmitter { rpcUrl: this.configManager.getCurrentRpcAddress(), // account mgmt getAccounts: (cb) => { - let selectedAccount = this.preferencesController.getSelectedAddress() - let result = selectedAccount ? [selectedAccount] : [] + let selectedAddress = this.preferencesController.getSelectedAddress() + let result = selectedAddress ? [selectedAddress] : [] cb(null, result) }, // tx signing @@ -154,9 +154,9 @@ module.exports = class MetamaskController extends EventEmitter { ) function selectPublicState(state) { - const result = { selectedAccount: undefined } + const result = { selectedAddress: undefined } try { - result.selectedAccount = state.KeyringController.selectedAccount + result.selectedAddress = state.KeyringController.selectedAddress } catch (_) {} return result } diff --git a/app/scripts/transaction-manager.js b/app/scripts/transaction-manager.js index c7d4686f3..d69dab1d7 100644 --- a/app/scripts/transaction-manager.js +++ b/app/scripts/transaction-manager.js @@ -25,11 +25,11 @@ module.exports = class TransactionManager extends EventEmitter { } getState () { - var selectedAccount = this.getSelectedAddress() + var selectedAddress = this.getSelectedAddress() return { transactions: this.getTxList(), unconfTxs: this.getUnapprovedTxList(), - selectedAccountTxList: this.getFilteredTxList({metamaskNetworkId: this.getNetwork(), from: selectedAccount}), + selectedAddressTxList: this.getFilteredTxList({metamaskNetworkId: this.getNetwork(), from: selectedAddress}), } } -- cgit v1.2.3 From 7ccaa263d265edc2f987dc66af0caf5fedb42296 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 15:38:32 -0800 Subject: inpage - set web3.eth.defaultAccount correctly --- app/scripts/inpage.js | 2 +- app/scripts/metamask-controller.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 599604a82..419f78cd6 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -52,7 +52,7 @@ reloadStream.once('data', triggerReload) // set web3 defaultAccount inpageProvider.publicConfigStore.subscribe(function (state) { - web3.eth.defaultAccount = state.PreferencesController.selectedAddress + web3.eth.defaultAccount = state.selectedAddress }) // diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index fc7df5137..2c379f8d9 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -156,7 +156,7 @@ module.exports = class MetamaskController extends EventEmitter { function selectPublicState(state) { const result = { selectedAddress: undefined } try { - result.selectedAddress = state.KeyringController.selectedAddress + result.selectedAddress = state.PreferencesController.selectedAddress } catch (_) {} return result } -- cgit v1.2.3 From 0b9d37c6c8e714eb26bec5156e984a6b0cd268ad Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 15:53:21 -0800 Subject: migration #6 - fix selectedAddress data source --- app/scripts/migrations/006.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/migrations/006.js b/app/scripts/migrations/006.js index 677e89bf7..950c4deb8 100644 --- a/app/scripts/migrations/006.js +++ b/app/scripts/migrations/006.js @@ -25,12 +25,12 @@ module.exports = { } function migrateState (state) { - const config = state.config + const keyringSubstate = state.KeyringController // add new state const newState = extend(state, { PreferencesController: { - selectedAddress: config.selectedAccount, + selectedAddress: keyringSubstate.selectedAccount, }, }) -- cgit v1.2.3