1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 11:01:41 +01:00
metamask-extension/app/scripts/controllers/network/network.js

191 lines
5.8 KiB
JavaScript
Raw Normal View History

2017-09-30 01:09:38 +02:00
const assert = require('assert')
const EventEmitter = require('events')
const createMetamaskProvider = require('web3-provider-engine/zero.js')
const SubproviderFromProvider = require('web3-provider-engine/subproviders/provider.js')
const createInfuraProvider = require('eth-json-rpc-infura/src/createProvider')
const ObservableStore = require('obs-store')
2017-05-23 08:12:28 +02:00
const ComposedStore = require('obs-store/lib/composed')
const extend = require('xtend')
const EthQuery = require('eth-query')
2018-04-12 23:17:36 +02:00
const createEventEmitterProxy = require('../../lib/events-proxy.js')
const log = require('loglevel')
2018-04-12 23:17:36 +02:00
const {
ROPSTEN,
RINKEBY,
KOVAN,
MAINNET,
LOCALHOST,
2018-04-12 23:17:36 +02:00
} = require('./enums')
const LOCALHOST_RPC_URL = 'http://localhost:8545'
2018-04-12 23:17:36 +02:00
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET]
const env = process.env.METAMASK_ENV
const METAMASK_DEBUG = process.env.METAMASK_DEBUG
const testMode = (METAMASK_DEBUG || env === 'test')
const defaultProviderConfig = {
type: testMode ? RINKEBY : MAINNET,
}
module.exports = class NetworkController extends EventEmitter {
constructor (opts = {}) {
super()
// parse options
const providerConfig = opts.provider || defaultProviderConfig
// create stores
this.providerStore = new ObservableStore(providerConfig)
2017-09-27 23:10:58 +02:00
this.networkStore = new ObservableStore('loading')
2017-05-23 08:12:28 +02:00
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore })
// create event emitter proxy
this._proxy = createEventEmitterProxy()
2017-05-23 07:56:10 +02:00
2017-05-23 08:12:28 +02:00
this.on('networkDidChange', this.lookupNetwork)
2017-05-23 07:56:10 +02:00
}
2017-09-30 01:09:38 +02:00
initializeProvider (_providerParams) {
this._baseProviderParams = _providerParams
const { type, rpcTarget } = this.providerStore.getState()
this._configureProvider({ type, rpcTarget })
this._proxy.on('block', this._logBlock.bind(this))
this._proxy.on('error', this.verifyNetwork.bind(this))
this.ethQuery = new EthQuery(this._proxy)
this.lookupNetwork()
return this._proxy
}
2017-05-23 07:56:10 +02:00
verifyNetwork () {
2017-09-30 01:09:38 +02:00
// Check network when restoring connectivity:
if (this.isNetworkLoading()) this.lookupNetwork()
}
getNetworkState () {
2017-05-23 08:12:28 +02:00
return this.networkStore.getState()
}
setNetworkState (network) {
2017-05-23 08:12:28 +02:00
return this.networkStore.putState(network)
}
isNetworkLoading () {
return this.getNetworkState() === 'loading'
}
2017-05-23 08:12:28 +02:00
lookupNetwork () {
// Prevent firing when provider is not defined.
if (!this.ethQuery || !this.ethQuery.sendAsync) {
2017-11-28 00:59:32 +01:00
return log.warn('NetworkController - lookupNetwork aborted due to missing ethQuery')
}
this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => {
if (err) return this.setNetworkState('loading')
log.info('web3.getNetwork returned ' + network)
this.setNetworkState(network)
})
}
setRpcTarget (rpcTarget) {
const providerConfig = {
2017-05-23 08:12:28 +02:00
type: 'rpc',
rpcTarget,
}
this.providerConfig = providerConfig
}
async setProviderType (type) {
assert.notEqual(type, 'rpc', `NetworkController - cannot call "setProviderType" with type 'rpc'. use "setRpcTarget"`)
assert(INFURA_PROVIDER_TYPES.includes(type) || type === LOCALHOST, `NetworkController - Unknown rpc type "${type}"`)
const providerConfig = { type }
this.providerConfig = providerConfig
}
resetConnection () {
this.providerConfig = this.getProviderConfig()
}
set providerConfig (providerConfig) {
this.providerStore.updateState(providerConfig)
this._switchNetwork(providerConfig)
}
2017-05-23 08:12:28 +02:00
getProviderConfig () {
return this.providerStore.getState()
}
2017-09-30 01:09:38 +02:00
//
// Private
//
_switchNetwork (opts) {
2017-09-30 01:09:38 +02:00
this.setNetworkState('loading')
this._configureProvider(opts)
2017-09-30 01:09:38 +02:00
this.emit('networkDidChange')
}
_configureProvider (opts) {
const { type, rpcTarget } = opts
// infura type-based endpoints
const isInfura = INFURA_PROVIDER_TYPES.includes(type)
if (isInfura) {
this._configureInfuraProvider(opts)
// other type-based rpc endpoints
} else if (type === LOCALHOST) {
this._configureStandardProvider({ rpcUrl: LOCALHOST_RPC_URL })
// url-based rpc endpoints
2018-07-03 00:49:33 +02:00
} else if (type === 'rpc') {
this._configureStandardProvider({ rpcUrl: rpcTarget })
} else {
throw new Error(`NetworkController - _configureProvider - unknown type "${type}"`)
}
}
_configureInfuraProvider ({ type }) {
log.info('_configureInfuraProvider', type)
const infuraProvider = createInfuraProvider({ network: type })
const infuraSubprovider = new SubproviderFromProvider(infuraProvider)
const providerParams = extend(this._baseProviderParams, {
engineParams: {
pollingInterval: 8000,
blockTrackerProvider: infuraProvider,
},
dataSubprovider: infuraSubprovider,
})
const provider = createMetamaskProvider(providerParams)
this._setProvider(provider)
}
_configureStandardProvider ({ rpcUrl }) {
const providerParams = extend(this._baseProviderParams, {
rpcUrl,
engineParams: {
pollingInterval: 8000,
},
})
const provider = createMetamaskProvider(providerParams)
this._setProvider(provider)
}
_setProvider (provider) {
// collect old block tracker events
const oldProvider = this._provider
let blockTrackerHandlers
if (oldProvider) {
// capture old block handlers
blockTrackerHandlers = oldProvider._blockTracker.proxyEventHandlers
// tear down
oldProvider.removeAllListeners()
oldProvider.stop()
}
// override block tracler
provider._blockTracker = createEventEmitterProxy(provider._blockTracker, blockTrackerHandlers)
// set as new provider
this._provider = provider
this._proxy.setTarget(provider)
}
2017-05-23 07:56:10 +02:00
_logBlock (block) {
log.info(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`)
this.verifyNetwork()
}
}