diff options
author | Frankie <frankie.diamond@gmail.com> | 2017-02-02 04:03:42 +0800 |
---|---|---|
committer | Frankie <frankie.diamond@gmail.com> | 2017-02-02 04:03:42 +0800 |
commit | ce0c3ed03c20eb0b7438cb9add7cbce490d7de64 (patch) | |
tree | 7ac9686e82b938097c698b3b68ddc1e1b55598c8 /app/scripts | |
parent | e2e8a7cca06f7e6f41f417d86710227b0b5428d6 (diff) | |
parent | 82578538198de74955d7bc5cfd3c9a02d1b69a6d (diff) | |
download | tangerine-wallet-browser-ce0c3ed03c20eb0b7438cb9add7cbce490d7de64.tar tangerine-wallet-browser-ce0c3ed03c20eb0b7438cb9add7cbce490d7de64.tar.gz tangerine-wallet-browser-ce0c3ed03c20eb0b7438cb9add7cbce490d7de64.tar.bz2 tangerine-wallet-browser-ce0c3ed03c20eb0b7438cb9add7cbce490d7de64.tar.lz tangerine-wallet-browser-ce0c3ed03c20eb0b7438cb9add7cbce490d7de64.tar.xz tangerine-wallet-browser-ce0c3ed03c20eb0b7438cb9add7cbce490d7de64.tar.zst tangerine-wallet-browser-ce0c3ed03c20eb0b7438cb9add7cbce490d7de64.zip |
Merge branch 'dev' into messageManagerCleanUp
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/inpage.js | 4 | ||||
-rw-r--r-- | app/scripts/keyring-controller.js | 28 | ||||
-rw-r--r-- | app/scripts/lib/controllers/preferences.js | 33 | ||||
-rw-r--r-- | app/scripts/lib/inpage-provider.js | 10 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 34 | ||||
-rw-r--r-- | app/scripts/migrations/006.js | 41 | ||||
-rw-r--r-- | app/scripts/migrations/index.js | 1 | ||||
-rw-r--r-- | app/scripts/transaction-manager.js | 6 |
8 files changed, 110 insertions, 47 deletions
diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 42332d92e..419f78cd6 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.selectedAddress }) // diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js index 6b8260a28..111f679c8 100644 --- a/app/scripts/keyring-controller.js +++ b/app/scripts/keyring-controller.js @@ -80,8 +80,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(), @@ -141,7 +139,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)) @@ -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 @@ -348,7 +324,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) }) @@ -595,7 +570,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 new file mode 100644 index 000000000..dc9464c4e --- /dev/null +++ b/app/scripts/lib/controllers/preferences.js @@ -0,0 +1,33 @@ +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) { + return new Promise((resolve, reject) => { + const address = normalizeAddress(_address) + this.store.updateState({ selectedAddress: address }) + resolve() + }) + } + + getSelectedAddress(_address) { + return this.store.getState().selectedAddress + } + + // + // PRIVATE METHODS + // + +} + +module.exports = PreferencesController 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 38358f04b..84ac2130e 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.preferencesController = new PreferencesController({ + initState: initState.PreferencesController, + }) + // rpc provider this.provider = this.initializeProvider(opts) this.provider.on('block', this.logBlock.bind(this)) @@ -56,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 @@ -65,7 +72,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), + 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), @@ -102,6 +109,9 @@ module.exports = class MetamaskController extends EventEmitter { this.keyringController.store.subscribe((state) => { this.store.updateState({ KeyringController: state }) }) + this.preferencesController.store.subscribe((state) => { + this.store.updateState({ PreferencesController: state }) + }) } // @@ -117,8 +127,8 @@ module.exports = class MetamaskController extends EventEmitter { rpcUrl: this.configManager.getCurrentRpcAddress(), // account mgmt getAccounts: (cb) => { - let selectedAccount = this.keyringController.getSelectedAccount() - let result = selectedAccount ? [selectedAccount] : [] + let selectedAddress = this.preferencesController.getSelectedAddress() + let result = selectedAddress ? [selectedAddress] : [] cb(null, result) }, // tx signing @@ -141,9 +151,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.PreferencesController.selectedAddress } catch (_) {} return result } @@ -165,6 +175,7 @@ module.exports = class MetamaskController extends EventEmitter { this.txManager.getState(), this.messageManager.getState(), keyringControllerState, + this.preferencesController.store.getState(), this.noticeController.getState(), { shapeShiftTxList: this.configManager.getShapeShiftTxList(), @@ -180,6 +191,7 @@ module.exports = class MetamaskController extends EventEmitter { getApi () { const keyringController = this.keyringController + const preferencesController = this.preferencesController const txManager = this.txManager const messageManager = this.messageManager const noticeController = this.noticeController @@ -211,12 +223,14 @@ module.exports = class MetamaskController extends EventEmitter { // vault management submitPassword: this.submitPassword.bind(this), + // PreferencesController + setSelectedAddress: nodeify(preferencesController.setSelectedAddress).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), @@ -329,7 +343,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.getSelectedAddress()) } importAccountWithStrategy (strategy, args, cb) { @@ -338,7 +352,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.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 new file mode 100644 index 000000000..950c4deb8 --- /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 keyringSubstate = state.KeyringController + + // add new state + const newState = extend(state, { + PreferencesController: { + selectedAddress: keyringSubstate.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'), ] diff --git a/app/scripts/transaction-manager.js b/app/scripts/transaction-manager.js index 153b8bc28..6fecdba39 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,11 +25,11 @@ module.exports = class TransactionManager extends EventEmitter { } getState () { - var selectedAccount = this.getSelectedAccount() + var selectedAddress = this.getSelectedAddress() return { transactions: this.getTxList(), unapprovedTxs: this.getUnapprovedTxList(), - selectedAccountTxList: this.getFilteredTxList({metamaskNetworkId: this.getNetwork(), from: selectedAccount}), + selectedAddressTxList: this.getFilteredTxList({metamaskNetworkId: this.getNetwork(), from: selectedAddress}), } } |