2021-02-04 19:15:23 +01:00
|
|
|
import assert from 'assert';
|
|
|
|
import EventEmitter from 'events';
|
|
|
|
import { ComposedStore, ObservableStore } from '@metamask/obs-store';
|
|
|
|
import { JsonRpcEngine } from 'json-rpc-engine';
|
|
|
|
import providerFromEngine from 'eth-json-rpc-middleware/providerFromEngine';
|
|
|
|
import log from 'loglevel';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
createSwappableProxy,
|
|
|
|
createEventEmitterProxy,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from 'swappable-obj-proxy';
|
|
|
|
import EthQuery from 'eth-query';
|
2020-05-20 17:57:45 +02:00
|
|
|
import {
|
|
|
|
RINKEBY,
|
|
|
|
MAINNET,
|
|
|
|
INFURA_PROVIDER_TYPES,
|
2021-01-20 17:49:05 +01:00
|
|
|
NETWORK_TYPE_RPC,
|
2020-10-06 19:57:02 +02:00
|
|
|
NETWORK_TYPE_TO_ID_MAP,
|
2020-12-07 20:40:42 +01:00
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
RINKEBY_CHAIN_ID,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../../shared/constants/network';
|
2021-01-21 00:37:18 +01:00
|
|
|
import {
|
|
|
|
isPrefixedFormattedHexString,
|
|
|
|
isSafeChainId,
|
2021-02-12 16:25:58 +01:00
|
|
|
} from '../../../../shared/modules/network.utils';
|
2021-02-04 19:15:23 +01:00
|
|
|
import createMetamaskMiddleware from './createMetamaskMiddleware';
|
|
|
|
import createInfuraClient from './createInfuraClient';
|
|
|
|
import createJsonRpcClient from './createJsonRpcClient';
|
2017-05-18 23:54:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const env = process.env.METAMASK_ENV;
|
2019-03-28 17:04:31 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let defaultProviderConfigOpts;
|
2019-03-28 17:04:31 +01:00
|
|
|
if (process.env.IN_TEST === 'true') {
|
2020-10-12 21:05:40 +02:00
|
|
|
defaultProviderConfigOpts = {
|
2021-01-20 17:49:05 +01:00
|
|
|
type: NETWORK_TYPE_RPC,
|
2020-10-12 21:05:40 +02:00
|
|
|
rpcUrl: 'http://localhost:8545',
|
|
|
|
chainId: '0x539',
|
|
|
|
nickname: 'Localhost 8545',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-08 18:22:14 +02:00
|
|
|
} else if (process.env.METAMASK_DEBUG || env === 'test') {
|
2021-02-04 19:15:23 +01:00
|
|
|
defaultProviderConfigOpts = { type: RINKEBY, chainId: RINKEBY_CHAIN_ID };
|
2019-03-28 17:04:31 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
defaultProviderConfigOpts = { type: MAINNET, chainId: MAINNET_CHAIN_ID };
|
2019-03-28 17:04:31 +01:00
|
|
|
}
|
2018-05-03 03:03:59 +02:00
|
|
|
|
|
|
|
const defaultProviderConfig = {
|
2018-10-26 10:26:43 +02:00
|
|
|
ticker: 'ETH',
|
2020-10-12 21:05:40 +02:00
|
|
|
...defaultProviderConfigOpts,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-10-26 10:26:43 +02:00
|
|
|
|
2021-02-09 00:22:30 +01:00
|
|
|
export const NETWORK_EVENTS = {
|
|
|
|
// Fired after the actively selected network is changed
|
|
|
|
NETWORK_DID_CHANGE: 'networkDidChange',
|
|
|
|
// Fired when the actively selected network *will* change
|
|
|
|
NETWORK_WILL_CHANGE: 'networkWillChange',
|
|
|
|
};
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default class NetworkController extends EventEmitter {
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(opts = {}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
super();
|
2017-12-22 19:43:02 +01:00
|
|
|
|
2018-05-03 03:03:59 +02:00
|
|
|
// create stores
|
2020-10-06 19:57:02 +02:00
|
|
|
this.providerStore = new ObservableStore(
|
|
|
|
opts.provider || { ...defaultProviderConfig },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-01-07 00:31:11 +01:00
|
|
|
this.previousProviderStore = new ObservableStore(
|
|
|
|
this.providerStore.getState(),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
this.networkStore = new ObservableStore('loading');
|
2020-10-06 19:57:02 +02:00
|
|
|
this.store = new ComposedStore({
|
|
|
|
provider: this.providerStore,
|
2021-01-07 00:31:11 +01:00
|
|
|
previousProviderStore: this.previousProviderStore,
|
2020-10-06 19:57:02 +02:00
|
|
|
network: this.networkStore,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2018-05-17 05:46:34 +02:00
|
|
|
// provider and block tracker
|
2021-02-04 19:15:23 +01:00
|
|
|
this._provider = null;
|
|
|
|
this._blockTracker = null;
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2018-05-17 05:46:34 +02:00
|
|
|
// provider and block tracker proxies - because the network changes
|
2021-02-04 19:15:23 +01:00
|
|
|
this._providerProxy = null;
|
|
|
|
this._blockTrackerProxy = null;
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-02-09 00:22:30 +01:00
|
|
|
this.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, this.lookupNetwork);
|
2017-05-23 07:56:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 18:16:00 +02:00
|
|
|
/**
|
|
|
|
* Sets the Infura project ID
|
|
|
|
*
|
|
|
|
* @param {string} projectId - The Infura project ID
|
|
|
|
* @throws {Error} if the project ID is not a valid string
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setInfuraProjectId(projectId) {
|
2020-09-10 18:16:00 +02:00
|
|
|
if (!projectId || typeof projectId !== 'string') {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error('Invalid Infura project ID');
|
2020-09-10 18:16:00 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this._infuraProjectId = projectId;
|
2020-09-10 18:16:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
initializeProvider(providerParams) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._baseProviderParams = providerParams;
|
|
|
|
const { type, rpcUrl, chainId } = this.getProviderConfig();
|
|
|
|
this._configureProvider({ type, rpcUrl, chainId });
|
|
|
|
this.lookupNetwork();
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// return the proxies so the references will always be good
|
2020-11-03 00:41:28 +01:00
|
|
|
getProviderAndBlockTracker() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const provider = this._providerProxy;
|
|
|
|
const blockTracker = this._blockTrackerProxy;
|
|
|
|
return { provider, blockTracker };
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
2017-05-23 07:56:10 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
verifyNetwork() {
|
2017-09-30 01:09:38 +02:00
|
|
|
// Check network when restoring connectivity:
|
2019-11-20 01:03:20 +01:00
|
|
|
if (this.isNetworkLoading()) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.lookupNetwork();
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
getNetworkState() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.networkStore.getState();
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
setNetworkState(network) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.networkStore.putState(network);
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
isNetworkLoading() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.getNetworkState() === 'loading';
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
lookupNetwork() {
|
2017-10-23 07:24:50 +02:00
|
|
|
// Prevent firing when provider is not defined.
|
2018-05-17 05:46:34 +02:00
|
|
|
if (!this._provider) {
|
2020-11-03 00:41:28 +01:00
|
|
|
log.warn(
|
|
|
|
'NetworkController - lookupNetwork aborted due to missing provider',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return;
|
2017-10-23 07:24:50 +02:00
|
|
|
}
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const chainId = this.getCurrentChainId();
|
2020-10-06 19:57:02 +02:00
|
|
|
if (!chainId) {
|
2020-11-03 00:41:28 +01:00
|
|
|
log.warn(
|
|
|
|
'NetworkController - lookupNetwork aborted due to missing chainId',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
this.setNetworkState('loading');
|
|
|
|
return;
|
2020-10-06 19:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ping the RPC endpoint so we can confirm that it works
|
2021-02-04 19:15:23 +01:00
|
|
|
const ethQuery = new EthQuery(this._provider);
|
|
|
|
const initialNetwork = this.getNetworkState();
|
2020-10-06 22:18:24 +02:00
|
|
|
ethQuery.sendAsync({ method: 'net_version' }, (err, networkVersion) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const currentNetwork = this.getNetworkState();
|
2018-12-06 20:42:57 +01:00
|
|
|
if (initialNetwork === currentNetwork) {
|
|
|
|
if (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setNetworkState('loading');
|
|
|
|
return;
|
2018-12-06 20:42:57 +01:00
|
|
|
}
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setNetworkState(networkVersion);
|
2018-12-06 20:42:57 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
getCurrentChainId() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { type, chainId: configChainId } = this.getProviderConfig();
|
|
|
|
return NETWORK_TYPE_TO_ID_MAP[type]?.chainId || configChainId;
|
2020-10-06 22:18:24 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
setRpcTarget(rpcUrl, chainId, ticker = 'ETH', nickname = '', rpcPrefs) {
|
2021-01-21 00:37:18 +01:00
|
|
|
assert.ok(
|
|
|
|
isPrefixedFormattedHexString(chainId),
|
|
|
|
`Invalid chain ID "${chainId}": invalid hex string.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-01-21 00:37:18 +01:00
|
|
|
assert.ok(
|
|
|
|
isSafeChainId(parseInt(chainId, 16)),
|
|
|
|
`Invalid chain ID "${chainId}": numerical value greater than max safe value.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
this.setProviderConfig({
|
2021-01-20 17:49:05 +01:00
|
|
|
type: NETWORK_TYPE_RPC,
|
2020-10-06 19:57:02 +02:00
|
|
|
rpcUrl,
|
2018-10-26 10:26:43 +02:00
|
|
|
chainId,
|
|
|
|
ticker,
|
|
|
|
nickname,
|
2019-05-09 19:27:14 +02:00
|
|
|
rpcPrefs,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-01 02:59:53 +02:00
|
|
|
}
|
2017-12-22 19:43:02 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async setProviderType(type, rpcUrl = '', ticker = 'ETH', nickname = '') {
|
2021-01-21 00:37:18 +01:00
|
|
|
assert.notStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
type,
|
2021-01-20 17:49:05 +01:00
|
|
|
NETWORK_TYPE_RPC,
|
|
|
|
`NetworkController - cannot call "setProviderType" with type "${NETWORK_TYPE_RPC}". Use "setRpcTarget"`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-01-21 00:37:18 +01:00
|
|
|
assert.ok(
|
2020-11-03 00:41:28 +01:00
|
|
|
INFURA_PROVIDER_TYPES.includes(type),
|
2021-01-21 00:37:18 +01:00
|
|
|
`Unknown Infura provider type "${type}".`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const { chainId } = NETWORK_TYPE_TO_ID_MAP[type];
|
|
|
|
this.setProviderConfig({ type, rpcUrl, chainId, ticker, nickname });
|
2018-06-07 01:46:39 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
resetConnection() {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setProviderConfig(this.getProviderConfig());
|
2018-06-07 01:46:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 19:57:02 +02:00
|
|
|
/**
|
|
|
|
* Sets the provider config and switches the network.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setProviderConfig(config) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.previousProviderStore.updateState(this.getProviderConfig());
|
|
|
|
this.providerStore.updateState(config);
|
|
|
|
this._switchNetwork(config);
|
2021-01-07 00:31:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rollbackToPreviousProvider() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const config = this.previousProviderStore.getState();
|
|
|
|
this.providerStore.updateState(config);
|
|
|
|
this._switchNetwork(config);
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
getProviderConfig() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.providerStore.getState();
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
getNetworkIdentifier() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const provider = this.providerStore.getState();
|
|
|
|
return provider.type === NETWORK_TYPE_RPC ? provider.rpcUrl : provider.type;
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
|
|
|
|
2017-09-30 01:09:38 +02:00
|
|
|
//
|
|
|
|
// Private
|
|
|
|
//
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_switchNetwork(opts) {
|
2021-02-09 00:22:30 +01:00
|
|
|
this.emit(NETWORK_EVENTS.NETWORK_WILL_CHANGE);
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setNetworkState('loading');
|
|
|
|
this._configureProvider(opts);
|
2021-02-09 00:22:30 +01:00
|
|
|
this.emit(NETWORK_EVENTS.NETWORK_DID_CHANGE, opts.type);
|
2017-09-30 01:09:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_configureProvider({ type, rpcUrl, chainId }) {
|
2018-05-01 02:59:53 +02:00
|
|
|
// infura type-based endpoints
|
2021-02-04 19:15:23 +01:00
|
|
|
const isInfura = INFURA_PROVIDER_TYPES.includes(type);
|
2018-05-01 02:59:53 +02:00
|
|
|
if (isInfura) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._configureInfuraProvider(type, this._infuraProjectId);
|
2020-11-03 00:41:28 +01:00
|
|
|
// url-based rpc endpoints
|
2021-01-20 17:49:05 +01:00
|
|
|
} else if (type === NETWORK_TYPE_RPC) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._configureStandardProvider(rpcUrl, chainId);
|
2017-12-14 03:57:27 +01:00
|
|
|
} else {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`NetworkController - _configureProvider - unknown type "${type}"`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2017-12-14 03:57:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_configureInfuraProvider(type, projectId) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.info('NetworkController - configureInfuraProvider', type);
|
2019-09-23 11:54:41 +02:00
|
|
|
const networkClient = createInfuraClient({
|
|
|
|
network: type,
|
2020-09-10 18:16:00 +02:00
|
|
|
projectId,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
this._setNetworkClient(networkClient);
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_configureStandardProvider(rpcUrl, chainId) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.info('NetworkController - configureStandardProvider', rpcUrl);
|
|
|
|
const networkClient = createJsonRpcClient({ rpcUrl, chainId });
|
|
|
|
this._setNetworkClient(networkClient);
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_setNetworkClient({ networkMiddleware, blockTracker }) {
|
|
|
|
const metamaskMiddleware = createMetamaskMiddleware(
|
|
|
|
this._baseProviderParams,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const engine = new JsonRpcEngine();
|
|
|
|
engine.push(metamaskMiddleware);
|
|
|
|
engine.push(networkMiddleware);
|
|
|
|
const provider = providerFromEngine(engine);
|
|
|
|
this._setProviderAndBlockTracker({ provider, blockTracker });
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_setProviderAndBlockTracker({ provider, blockTracker }) {
|
2018-05-17 05:46:34 +02:00
|
|
|
// update or intialize proxies
|
|
|
|
if (this._providerProxy) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._providerProxy.setTarget(provider);
|
2018-05-17 05:46:34 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._providerProxy = createSwappableProxy(provider);
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
|
|
|
if (this._blockTrackerProxy) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._blockTrackerProxy.setTarget(blockTracker);
|
2018-05-17 05:46:34 +02:00
|
|
|
} else {
|
2020-11-03 00:41:28 +01:00
|
|
|
this._blockTrackerProxy = createEventEmitterProxy(blockTracker, {
|
|
|
|
eventFilter: 'skipInternal',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2017-09-27 23:44:54 +02:00
|
|
|
}
|
2018-05-17 05:46:34 +02:00
|
|
|
// set new provider and blockTracker
|
2021-02-04 19:15:23 +01:00
|
|
|
this._provider = provider;
|
|
|
|
this._blockTracker = blockTracker;
|
2017-09-27 23:44:54 +02:00
|
|
|
}
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|