|
|
@ -14,52 +14,40 @@ const { |
|
|
|
RINKEBY, |
|
|
|
RINKEBY, |
|
|
|
KOVAN, |
|
|
|
KOVAN, |
|
|
|
MAINNET, |
|
|
|
MAINNET, |
|
|
|
OLD_UI_NETWORK_TYPE, |
|
|
|
LOCALHOST, |
|
|
|
DEFAULT_NETWORK, |
|
|
|
|
|
|
|
} = require('./enums') |
|
|
|
} = require('./enums') |
|
|
|
const { getNetworkEndpoints } = require('./util') |
|
|
|
const LOCALHOST_RPC_URL = 'http://localhost:8545' |
|
|
|
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET] |
|
|
|
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 { |
|
|
|
module.exports = class NetworkController extends EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
constructor (config) { |
|
|
|
constructor (opts = {}) { |
|
|
|
super() |
|
|
|
super() |
|
|
|
|
|
|
|
|
|
|
|
this._networkEndpointVersion = OLD_UI_NETWORK_TYPE |
|
|
|
// parse options
|
|
|
|
this._networkEndpoints = getNetworkEndpoints(OLD_UI_NETWORK_TYPE) |
|
|
|
const providerConfig = opts.provider || defaultProviderConfig |
|
|
|
this._defaultRpc = this._networkEndpoints[DEFAULT_NETWORK] |
|
|
|
// create stores
|
|
|
|
|
|
|
|
this.providerStore = new ObservableStore(providerConfig) |
|
|
|
config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider) |
|
|
|
|
|
|
|
this.networkStore = new ObservableStore('loading') |
|
|
|
this.networkStore = new ObservableStore('loading') |
|
|
|
this.providerStore = new ObservableStore(config.provider) |
|
|
|
|
|
|
|
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore }) |
|
|
|
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore }) |
|
|
|
|
|
|
|
// create event emitter proxy
|
|
|
|
this._proxy = createEventEmitterProxy() |
|
|
|
this._proxy = createEventEmitterProxy() |
|
|
|
|
|
|
|
|
|
|
|
this.on('networkDidChange', this.lookupNetwork) |
|
|
|
this.on('networkDidChange', this.lookupNetwork) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async setNetworkEndpoints (version) { |
|
|
|
|
|
|
|
if (version === this._networkEndpointVersion) { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._networkEndpointVersion = version |
|
|
|
|
|
|
|
this._networkEndpoints = getNetworkEndpoints(version) |
|
|
|
|
|
|
|
this._defaultRpc = this._networkEndpoints[DEFAULT_NETWORK] |
|
|
|
|
|
|
|
const { type } = this.getProviderConfig() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this.setProviderType(type, true) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initializeProvider (_providerParams) { |
|
|
|
initializeProvider (_providerParams) { |
|
|
|
this._baseProviderParams = _providerParams |
|
|
|
this._baseProviderParams = _providerParams |
|
|
|
const { type, rpcTarget } = this.providerStore.getState() |
|
|
|
const { type, rpcTarget } = this.providerStore.getState() |
|
|
|
// map rpcTarget to rpcUrl
|
|
|
|
this._configureProvider({ type, rpcTarget }) |
|
|
|
const opts = { |
|
|
|
|
|
|
|
type, |
|
|
|
|
|
|
|
rpcUrl: rpcTarget, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this._configureProvider(opts) |
|
|
|
|
|
|
|
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) |
|
|
@ -96,45 +84,27 @@ module.exports = class NetworkController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setRpcTarget (rpcUrl) { |
|
|
|
setRpcTarget (rpcTarget) { |
|
|
|
this.providerStore.updateState({ |
|
|
|
const providerConfig = { |
|
|
|
type: 'rpc', |
|
|
|
type: 'rpc', |
|
|
|
rpcTarget: rpcUrl, |
|
|
|
rpcTarget, |
|
|
|
}) |
|
|
|
|
|
|
|
this._switchNetwork({ rpcUrl }) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getCurrentRpcAddress () { |
|
|
|
|
|
|
|
const provider = this.getProviderConfig() |
|
|
|
|
|
|
|
if (!provider) return null |
|
|
|
|
|
|
|
return this.getRpcAddressForType(provider.type) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
this.providerStore.updateState(providerConfig) |
|
|
|
async setProviderType (type, forceUpdate = false) { |
|
|
|
this._switchNetwork(providerConfig) |
|
|
|
assert(type !== 'rpc', `NetworkController.setProviderType - cannot connect by type "rpc"`) |
|
|
|
|
|
|
|
// skip if type already matches
|
|
|
|
|
|
|
|
if (type === this.getProviderConfig().type && !forceUpdate) { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const rpcTarget = this.getRpcAddressForType(type) |
|
|
|
async setProviderType (type) { |
|
|
|
assert(rpcTarget, `NetworkController - unknown rpc address for type "${type}"`) |
|
|
|
assert.notEqual(type, 'rpc', `NetworkController - cannot call "setProviderType" with type 'rpc'. use "setRpcTarget"`) |
|
|
|
this.providerStore.updateState({ type, rpcTarget }) |
|
|
|
assert(INFURA_PROVIDER_TYPES.includes(type) || type === LOCALHOST, `NetworkController - Unknown rpc type "${type}"`) |
|
|
|
this._switchNetwork({ type }) |
|
|
|
const providerConfig = { type } |
|
|
|
|
|
|
|
this.providerStore.updateState(providerConfig) |
|
|
|
|
|
|
|
this._switchNetwork(providerConfig) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getProviderConfig () { |
|
|
|
getProviderConfig () { |
|
|
|
return this.providerStore.getState() |
|
|
|
return this.providerStore.getState() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getRpcAddressForType (type, provider = this.getProviderConfig()) { |
|
|
|
|
|
|
|
if (this._networkEndpoints[type]) { |
|
|
|
|
|
|
|
return this._networkEndpoints[type] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return provider && provider.rpcTarget ? provider.rpcTarget : this._defaultRpc |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Private
|
|
|
|
// Private
|
|
|
|
//
|
|
|
|
//
|
|
|
@ -146,32 +116,27 @@ module.exports = class NetworkController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_configureProvider (opts) { |
|
|
|
_configureProvider (opts) { |
|
|
|
// type-based rpc endpoints
|
|
|
|
const { type, rpcTarget } = opts |
|
|
|
const { type } = opts |
|
|
|
// infura type-based endpoints
|
|
|
|
if (type) { |
|
|
|
|
|
|
|
// type-based infura rpc endpoints
|
|
|
|
|
|
|
|
const isInfura = INFURA_PROVIDER_TYPES.includes(type) |
|
|
|
const isInfura = INFURA_PROVIDER_TYPES.includes(type) |
|
|
|
opts.rpcUrl = this.getRpcAddressForType(type) |
|
|
|
|
|
|
|
if (isInfura) { |
|
|
|
if (isInfura) { |
|
|
|
this._configureInfuraProvider(opts) |
|
|
|
this._configureInfuraProvider(opts) |
|
|
|
// other type-based rpc endpoints
|
|
|
|
// other type-based rpc endpoints
|
|
|
|
} else { |
|
|
|
} else if (type === LOCALHOST) { |
|
|
|
this._configureStandardProvider(opts) |
|
|
|
this._configureStandardProvider({ rpcUrl: LOCALHOST_RPC_URL }) |
|
|
|
} |
|
|
|
|
|
|
|
// url-based rpc endpoints
|
|
|
|
// url-based rpc endpoints
|
|
|
|
|
|
|
|
} else if (type === 'rpc'){ |
|
|
|
|
|
|
|
this._configureStandardProvider({ rpcUrl: rpcTarget }) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
this._configureStandardProvider(opts) |
|
|
|
throw new Error(`NetworkController - _configureProvider - unknown type "${type}"`) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_configureInfuraProvider (opts) { |
|
|
|
_configureInfuraProvider ({ type }) { |
|
|
|
log.info('_configureInfuraProvider', opts) |
|
|
|
log.info('_configureInfuraProvider', type) |
|
|
|
const infuraProvider = createInfuraProvider({ |
|
|
|
const infuraProvider = createInfuraProvider({ network: type }) |
|
|
|
network: opts.type, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
const infuraSubprovider = new SubproviderFromProvider(infuraProvider) |
|
|
|
const infuraSubprovider = new SubproviderFromProvider(infuraProvider) |
|
|
|
const providerParams = extend(this._baseProviderParams, { |
|
|
|
const providerParams = extend(this._baseProviderParams, { |
|
|
|
rpcUrl: opts.rpcUrl, |
|
|
|
|
|
|
|
engineParams: { |
|
|
|
engineParams: { |
|
|
|
pollingInterval: 8000, |
|
|
|
pollingInterval: 8000, |
|
|
|
blockTrackerProvider: infuraProvider, |
|
|
|
blockTrackerProvider: infuraProvider, |
|
|
|