diff options
-rw-r--r-- | app/scripts/controllers/network.js | 47 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 38 |
2 files changed, 50 insertions, 35 deletions
diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js index 2a17cdae8..9e05afe75 100644 --- a/app/scripts/controllers/network.js +++ b/app/scripts/controllers/network.js @@ -1,5 +1,6 @@ +const assert = require('assert') const EventEmitter = require('events') -const MetaMaskProvider = require('web3-provider-engine/zero.js') +const createMetamaskProvider = require('web3-provider-engine/zero.js') const ObservableStore = require('obs-store') const ComposedStore = require('obs-store/lib/composed') const extend = require('xtend') @@ -18,13 +19,13 @@ module.exports = class NetworkController extends EventEmitter { this._proxy = createEventEmitterProxy() this.on('networkDidChange', this.lookupNetwork) - this.providerStore.subscribe((state) => this.switchNetwork({ rpcUrl: state.rpcTarget })) + this.providerStore.subscribe((state) => this._switchNetwork({ rpcUrl: state.rpcTarget })) } - initializeProvider (opts, providerContructor = MetaMaskProvider) { - this._baseProviderParams = opts - const provider = providerContructor(opts) - this._setProvider(provider) + initializeProvider (_providerParams) { + this._baseProviderParams = _providerParams + const rpcUrl = this.getCurrentRpcAddress() + this._configureStandardProvider({ rpcUrl }) this._proxy.on('block', this._logBlock.bind(this)) this._proxy.on('error', this.verifyNetwork.bind(this)) this.ethQuery = new EthQuery(this._proxy) @@ -32,17 +33,8 @@ module.exports = class NetworkController extends EventEmitter { return this._proxy } - switchNetwork (opts) { - this.setNetworkState('loading') - const providerParams = extend(this._baseProviderParams, opts) - this._baseProviderParams = providerParams - const provider = MetaMaskProvider(providerParams) - this._setProvider(provider) - this.emit('networkDidChange') - } - verifyNetwork () { - // Check network when restoring connectivity: + // Check network when restoring connectivity: if (this.isNetworkLoading()) this.lookupNetwork() } @@ -79,10 +71,13 @@ module.exports = class NetworkController extends EventEmitter { return this.getRpcAddressForType(provider.type) } - setProviderType (type) { + async setProviderType (type) { + assert(type !== 'rpc', `NetworkController.setProviderType - cannot connect by type "rpc"`) + // skip if type already matches if (type === this.getProviderConfig().type) return const rpcTarget = this.getRpcAddressForType(type) - this.providerStore.updateState({type, rpcTarget}) + assert(rpcTarget, `NetworkController - unknown rpc address for type "${type}"`) + this.providerStore.updateState({ type, rpcTarget }) } getProviderConfig () { @@ -94,6 +89,22 @@ module.exports = class NetworkController extends EventEmitter { return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC } + // + // Private + // + + _switchNetwork (providerParams) { + this.setNetworkState('loading') + this._configureStandardProvider(providerParams) + this.emit('networkDidChange') + } + + _configureStandardProvider(_providerParams) { + const providerParams = extend(this._baseProviderParams, _providerParams) + const provider = createMetamaskProvider(providerParams) + this._setProvider(provider) + } + _setProvider (provider) { // collect old block tracker events const oldProvider = this._provider diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index b28f2738a..fa6f1a245 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -217,13 +217,11 @@ module.exports = class MetamaskController extends EventEmitter { // initializeProvider () { - return this.networkController.initializeProvider({ + const providerOpts = { static: { eth_syncing: false, web3_clientVersion: `MetaMask/v${version}`, }, - // rpc data source - rpcUrl: this.networkController.getCurrentRpcAddress(), originHttpHeaderKey: 'X-Metamask-Origin', // account mgmt getAccounts: (cb) => { @@ -243,7 +241,9 @@ module.exports = class MetamaskController extends EventEmitter { processMessage: this.newUnsignedMessage.bind(this), // personal_sign msg signing processPersonalMessage: this.newUnsignedPersonalMessage.bind(this), - }) + } + const providerProxy = this.networkController.initializeProvider(providerOpts) + return providerProxy } initPublicConfigStore () { @@ -312,13 +312,14 @@ module.exports = class MetamaskController extends EventEmitter { const txController = this.txController const noticeController = this.noticeController const addressBookController = this.addressBookController + const networkController = this.networkController return { // etc getState: (cb) => cb(null, this.getState()), - setProviderType: this.networkController.setProviderType.bind(this.networkController), setCurrentCurrency: this.setCurrentCurrency.bind(this), markAccountsFound: this.markAccountsFound.bind(this), + // coinbase buyEth: this.buyEth.bind(this), // shapeshift @@ -333,12 +334,15 @@ module.exports = class MetamaskController extends EventEmitter { // vault management submitPassword: this.submitPassword.bind(this), + // network management + setProviderType: nodeify(networkController.setProviderType, networkController), + setDefaultRpc: nodeify(this.setDefaultRpc, this), + setCustomRpc: nodeify(this.setCustomRpc, this), + // PreferencesController setSelectedAddress: nodeify(preferencesController.setSelectedAddress, preferencesController), addToken: nodeify(preferencesController.addToken, preferencesController), setCurrentAccountTab: nodeify(preferencesController.setCurrentAccountTab, preferencesController), - setDefaultRpc: nodeify(this.setDefaultRpc, this), - setCustomRpc: nodeify(this.setCustomRpc, this), // AddressController setAddressBook: nodeify(addressBookController.setAddressBook, addressBookController), @@ -689,19 +693,19 @@ module.exports = class MetamaskController extends EventEmitter { createShapeShiftTx (depositAddress, depositType) { this.shapeshiftController.createShapeShiftTx(depositAddress, depositType) } -// network - setDefaultRpc () { - this.networkController.setRpcTarget('http://localhost:8545') - return Promise.resolve('http://localhost:8545') + // network + + async setDefaultRpc () { + const localhost = 'http://localhost:8545' + this.networkController.setRpcTarget(localhost) + return localhost } - setCustomRpc (rpcTarget, rpcList) { + async setCustomRpc (rpcTarget, rpcList) { this.networkController.setRpcTarget(rpcTarget) - - return this.preferencesController.updateFrequentRpcList(rpcTarget) - .then(() => { - return Promise.resolve(rpcTarget) - }) + await this.preferencesController.updateFrequentRpcList(rpcTarget) + return rpcTarget } + } |