mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
network controller - small refactor
This commit is contained in:
parent
77963e8417
commit
b88d11f86e
@ -1,5 +1,6 @@
|
|||||||
|
const assert = require('assert')
|
||||||
const EventEmitter = require('events')
|
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 ObservableStore = require('obs-store')
|
||||||
const ComposedStore = require('obs-store/lib/composed')
|
const ComposedStore = require('obs-store/lib/composed')
|
||||||
const extend = require('xtend')
|
const extend = require('xtend')
|
||||||
@ -18,13 +19,13 @@ module.exports = class NetworkController extends EventEmitter {
|
|||||||
this._proxy = createEventEmitterProxy()
|
this._proxy = createEventEmitterProxy()
|
||||||
|
|
||||||
this.on('networkDidChange', this.lookupNetwork)
|
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) {
|
initializeProvider (_providerParams) {
|
||||||
this._baseProviderParams = opts
|
this._baseProviderParams = _providerParams
|
||||||
const provider = providerContructor(opts)
|
const rpcUrl = this.getCurrentRpcAddress()
|
||||||
this._setProvider(provider)
|
this._configureStandardProvider({ rpcUrl })
|
||||||
this._proxy.on('block', this._logBlock.bind(this))
|
this._proxy.on('block', this._logBlock.bind(this))
|
||||||
this._proxy.on('error', this.verifyNetwork.bind(this))
|
this._proxy.on('error', this.verifyNetwork.bind(this))
|
||||||
this.ethQuery = new EthQuery(this._proxy)
|
this.ethQuery = new EthQuery(this._proxy)
|
||||||
@ -32,17 +33,8 @@ module.exports = class NetworkController extends EventEmitter {
|
|||||||
return this._proxy
|
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 () {
|
verifyNetwork () {
|
||||||
// Check network when restoring connectivity:
|
// Check network when restoring connectivity:
|
||||||
if (this.isNetworkLoading()) this.lookupNetwork()
|
if (this.isNetworkLoading()) this.lookupNetwork()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,10 +71,13 @@ module.exports = class NetworkController extends EventEmitter {
|
|||||||
return this.getRpcAddressForType(provider.type)
|
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
|
if (type === this.getProviderConfig().type) return
|
||||||
const rpcTarget = this.getRpcAddressForType(type)
|
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 () {
|
getProviderConfig () {
|
||||||
@ -94,6 +89,22 @@ module.exports = class NetworkController extends EventEmitter {
|
|||||||
return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC
|
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) {
|
_setProvider (provider) {
|
||||||
// collect old block tracker events
|
// collect old block tracker events
|
||||||
const oldProvider = this._provider
|
const oldProvider = this._provider
|
||||||
|
@ -217,13 +217,11 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
//
|
//
|
||||||
|
|
||||||
initializeProvider () {
|
initializeProvider () {
|
||||||
return this.networkController.initializeProvider({
|
const providerOpts = {
|
||||||
static: {
|
static: {
|
||||||
eth_syncing: false,
|
eth_syncing: false,
|
||||||
web3_clientVersion: `MetaMask/v${version}`,
|
web3_clientVersion: `MetaMask/v${version}`,
|
||||||
},
|
},
|
||||||
// rpc data source
|
|
||||||
rpcUrl: this.networkController.getCurrentRpcAddress(),
|
|
||||||
originHttpHeaderKey: 'X-Metamask-Origin',
|
originHttpHeaderKey: 'X-Metamask-Origin',
|
||||||
// account mgmt
|
// account mgmt
|
||||||
getAccounts: (cb) => {
|
getAccounts: (cb) => {
|
||||||
@ -243,7 +241,9 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
processMessage: this.newUnsignedMessage.bind(this),
|
processMessage: this.newUnsignedMessage.bind(this),
|
||||||
// personal_sign msg signing
|
// personal_sign msg signing
|
||||||
processPersonalMessage: this.newUnsignedPersonalMessage.bind(this),
|
processPersonalMessage: this.newUnsignedPersonalMessage.bind(this),
|
||||||
})
|
}
|
||||||
|
const providerProxy = this.networkController.initializeProvider(providerOpts)
|
||||||
|
return providerProxy
|
||||||
}
|
}
|
||||||
|
|
||||||
initPublicConfigStore () {
|
initPublicConfigStore () {
|
||||||
@ -312,13 +312,14 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
const txController = this.txController
|
const txController = this.txController
|
||||||
const noticeController = this.noticeController
|
const noticeController = this.noticeController
|
||||||
const addressBookController = this.addressBookController
|
const addressBookController = this.addressBookController
|
||||||
|
const networkController = this.networkController
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// etc
|
// etc
|
||||||
getState: (cb) => cb(null, this.getState()),
|
getState: (cb) => cb(null, this.getState()),
|
||||||
setProviderType: this.networkController.setProviderType.bind(this.networkController),
|
|
||||||
setCurrentCurrency: this.setCurrentCurrency.bind(this),
|
setCurrentCurrency: this.setCurrentCurrency.bind(this),
|
||||||
markAccountsFound: this.markAccountsFound.bind(this),
|
markAccountsFound: this.markAccountsFound.bind(this),
|
||||||
|
|
||||||
// coinbase
|
// coinbase
|
||||||
buyEth: this.buyEth.bind(this),
|
buyEth: this.buyEth.bind(this),
|
||||||
// shapeshift
|
// shapeshift
|
||||||
@ -333,12 +334,15 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
// vault management
|
// vault management
|
||||||
submitPassword: this.submitPassword.bind(this),
|
submitPassword: this.submitPassword.bind(this),
|
||||||
|
|
||||||
|
// network management
|
||||||
|
setProviderType: nodeify(networkController.setProviderType, networkController),
|
||||||
|
setDefaultRpc: nodeify(this.setDefaultRpc, this),
|
||||||
|
setCustomRpc: nodeify(this.setCustomRpc, this),
|
||||||
|
|
||||||
// PreferencesController
|
// PreferencesController
|
||||||
setSelectedAddress: nodeify(preferencesController.setSelectedAddress, preferencesController),
|
setSelectedAddress: nodeify(preferencesController.setSelectedAddress, preferencesController),
|
||||||
addToken: nodeify(preferencesController.addToken, preferencesController),
|
addToken: nodeify(preferencesController.addToken, preferencesController),
|
||||||
setCurrentAccountTab: nodeify(preferencesController.setCurrentAccountTab, preferencesController),
|
setCurrentAccountTab: nodeify(preferencesController.setCurrentAccountTab, preferencesController),
|
||||||
setDefaultRpc: nodeify(this.setDefaultRpc, this),
|
|
||||||
setCustomRpc: nodeify(this.setCustomRpc, this),
|
|
||||||
|
|
||||||
// AddressController
|
// AddressController
|
||||||
setAddressBook: nodeify(addressBookController.setAddressBook, addressBookController),
|
setAddressBook: nodeify(addressBookController.setAddressBook, addressBookController),
|
||||||
@ -689,19 +693,19 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
createShapeShiftTx (depositAddress, depositType) {
|
createShapeShiftTx (depositAddress, depositType) {
|
||||||
this.shapeshiftController.createShapeShiftTx(depositAddress, depositType)
|
this.shapeshiftController.createShapeShiftTx(depositAddress, depositType)
|
||||||
}
|
}
|
||||||
// network
|
|
||||||
|
|
||||||
setDefaultRpc () {
|
// network
|
||||||
this.networkController.setRpcTarget('http://localhost:8545')
|
|
||||||
return Promise.resolve('http://localhost:8545')
|
async setDefaultRpc () {
|
||||||
|
const localhost = 'http://localhost:8545'
|
||||||
|
this.networkController.setRpcTarget(localhost)
|
||||||
|
return localhost
|
||||||
}
|
}
|
||||||
|
|
||||||
setCustomRpc (rpcTarget, rpcList) {
|
async setCustomRpc (rpcTarget, rpcList) {
|
||||||
this.networkController.setRpcTarget(rpcTarget)
|
this.networkController.setRpcTarget(rpcTarget)
|
||||||
|
await this.preferencesController.updateFrequentRpcList(rpcTarget)
|
||||||
return this.preferencesController.updateFrequentRpcList(rpcTarget)
|
return rpcTarget
|
||||||
.then(() => {
|
|
||||||
return Promise.resolve(rpcTarget)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user