From 4e79e730b8da2c687bf141cd4d4704791d923909 Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Tue, 19 May 2020 10:30:23 -0700 Subject: [PATCH 1/4] cleanup network enums; fix infuraClient chainId --- .../controllers/incoming-transactions.js | 18 ++----- .../controllers/network/createInfuraClient.js | 33 +++--------- app/scripts/controllers/network/enums.js | 54 ++++++++++++++++--- app/scripts/controllers/network/network.js | 29 ++++++++-- app/scripts/controllers/network/util.js | 31 +---------- app/scripts/lib/account-tracker.js | 12 ++--- .../network/network-controller-test.js | 15 ++++-- .../recipient-blacklist-checker-test.js | 4 +- .../network-display.component.js | 26 ++++----- 9 files changed, 119 insertions(+), 103 deletions(-) diff --git a/app/scripts/controllers/incoming-transactions.js b/app/scripts/controllers/incoming-transactions.js index aaf74f59da90..7e8aa87d0b12 100644 --- a/app/scripts/controllers/incoming-transactions.js +++ b/app/scripts/controllers/incoming-transactions.js @@ -6,25 +6,14 @@ import { bnToHex } from '../lib/util' import fetchWithTimeout from '../lib/fetch-with-timeout' import { - MAINNET_CODE, - ROPSTEN_CODE, - RINKEBY_CODE, - KOVAN_CODE, - GOERLI_CODE, ROPSTEN, RINKEBY, KOVAN, GOERLI, MAINNET, + NETWORK_TYPE_TO_ID_MAP, } from './network/enums' -const networkTypeToIdMap = { - [ROPSTEN]: String(ROPSTEN_CODE), - [RINKEBY]: String(RINKEBY_CODE), - [KOVAN]: String(KOVAN_CODE), - [GOERLI]: String(GOERLI_CODE), - [MAINNET]: String(MAINNET_CODE), -} const fetch = fetchWithTimeout({ timeout: 30000, }) @@ -185,10 +174,9 @@ export default class IncomingTransactionsController { async _fetchTxs (address, fromBlock, networkType) { let etherscanSubdomain = 'api' - const currentNetworkID = networkTypeToIdMap[networkType] - const supportedNetworkTypes = [ROPSTEN, RINKEBY, KOVAN, GOERLI, MAINNET] + const currentNetworkID = NETWORK_TYPE_TO_ID_MAP[networkType]?.networkId - if (supportedNetworkTypes.indexOf(networkType) === -1) { + if (!currentNetworkID) { return {} } diff --git a/app/scripts/controllers/network/createInfuraClient.js b/app/scripts/controllers/network/createInfuraClient.js index 9f16b12978b0..1ed367dfc6ba 100644 --- a/app/scripts/controllers/network/createInfuraClient.js +++ b/app/scripts/controllers/network/createInfuraClient.js @@ -9,6 +9,8 @@ import providerFromMiddleware from 'eth-json-rpc-middleware/providerFromMiddlewa import createInfuraMiddleware from 'eth-json-rpc-infura' import BlockTracker from 'eth-block-tracker' +import { NETWORK_TYPE_TO_ID_MAP } from './enums' + export default function createInfuraClient ({ network }) { const infuraMiddleware = createInfuraMiddleware({ network, maxAttempts: 5, source: 'metamask' }) const infuraProvider = providerFromMiddleware(infuraMiddleware) @@ -27,36 +29,15 @@ export default function createInfuraClient ({ network }) { } function createNetworkAndChainIdMiddleware ({ network }) { - let chainId - let netId - switch (network) { - case 'mainnet': - netId = '1' - chainId = '0x01' - break - case 'ropsten': - netId = '3' - chainId = '0x03' - break - case 'rinkeby': - netId = '4' - chainId = '0x04' - break - case 'kovan': - netId = '42' - chainId = '0x2a' - break - case 'goerli': - netId = '5' - chainId = '0x05' - break - default: - throw new Error(`createInfuraClient - unknown network "${network}"`) + if (!NETWORK_TYPE_TO_ID_MAP[network]) { + throw new Error(`createInfuraClient - unknown network "${network}"`) } + const { chainId, networkId } = NETWORK_TYPE_TO_ID_MAP[network] + return createScaffoldMiddleware({ eth_chainId: chainId, - net_version: netId, + net_version: networkId.toString(), }) } diff --git a/app/scripts/controllers/network/enums.js b/app/scripts/controllers/network/enums.js index 697cfb8fcfcd..e2a7deab3db5 100644 --- a/app/scripts/controllers/network/enums.js +++ b/app/scripts/controllers/network/enums.js @@ -2,17 +2,59 @@ export const ROPSTEN = 'ropsten' export const RINKEBY = 'rinkeby' export const KOVAN = 'kovan' export const MAINNET = 'mainnet' -export const LOCALHOST = 'localhost' export const GOERLI = 'goerli' +export const LOCALHOST = 'localhost' -export const MAINNET_CODE = 1 -export const ROPSTEN_CODE = 3 -export const RINKEBY_CODE = 4 -export const KOVAN_CODE = 42 -export const GOERLI_CODE = 5 +export const MAINNET_NETWORK_ID = 1 +export const ROPSTEN_NETWORK_ID = 3 +export const RINKEBY_NETWORK_ID = 4 +export const GOERLI_NETWORK_ID = 5 +export const KOVAN_NETWORK_ID = 42 + +export const MAINNET_CHAIN_ID = '0x1' +export const ROPSTEN_CHAIN_ID = '0x3' +export const RINKEBY_CHAIN_ID = '0x4' +export const GOERLI_CHAIN_ID = '0x5' +export const KOVAN_CHAIN_ID = '0x42' export const ROPSTEN_DISPLAY_NAME = 'Ropsten' export const RINKEBY_DISPLAY_NAME = 'Rinkeby' export const KOVAN_DISPLAY_NAME = 'Kovan' export const MAINNET_DISPLAY_NAME = 'Main Ethereum Network' export const GOERLI_DISPLAY_NAME = 'Goerli' + +export const INFURA_PROVIDER_TYPES = [ + ROPSTEN, + RINKEBY, + KOVAN, + MAINNET, + GOERLI, +] + +export const NETWORK_TYPE_TO_ID_MAP = { + [ROPSTEN]: { networkId: ROPSTEN_NETWORK_ID, chainId: ROPSTEN_CHAIN_ID }, + [RINKEBY]: { networkId: RINKEBY_NETWORK_ID, chainId: RINKEBY_CHAIN_ID }, + [KOVAN]: { networkId: KOVAN_NETWORK_ID, chainId: KOVAN_CHAIN_ID }, + [GOERLI]: { networkId: GOERLI_NETWORK_ID, chainId: GOERLI_CHAIN_ID }, + [MAINNET]: { networkId: MAINNET_NETWORK_ID, chainId: MAINNET_CHAIN_ID }, +} + +export const NETWORK_TO_NAME_MAP = { + [ROPSTEN]: ROPSTEN_DISPLAY_NAME, + [RINKEBY]: RINKEBY_DISPLAY_NAME, + [KOVAN]: KOVAN_DISPLAY_NAME, + [MAINNET]: MAINNET_DISPLAY_NAME, + [GOERLI]: GOERLI_DISPLAY_NAME, + + [ROPSTEN_NETWORK_ID]: ROPSTEN_DISPLAY_NAME, + [RINKEBY_NETWORK_ID]: RINKEBY_DISPLAY_NAME, + [KOVAN_NETWORK_ID]: KOVAN_DISPLAY_NAME, + [GOERLI_NETWORK_ID]: GOERLI_DISPLAY_NAME, + [MAINNET_NETWORK_ID]: MAINNET_DISPLAY_NAME, + + [ROPSTEN_CHAIN_ID]: ROPSTEN_DISPLAY_NAME, + [RINKEBY_CHAIN_ID]: RINKEBY_DISPLAY_NAME, + [KOVAN_CHAIN_ID]: KOVAN_DISPLAY_NAME, + [GOERLI_CHAIN_ID]: GOERLI_DISPLAY_NAME, + [MAINNET_CHAIN_ID]: MAINNET_DISPLAY_NAME, +} diff --git a/app/scripts/controllers/network/network.js b/app/scripts/controllers/network/network.js index de6deb863255..747ef449eac7 100644 --- a/app/scripts/controllers/network/network.js +++ b/app/scripts/controllers/network/network.js @@ -14,9 +14,13 @@ import { createSwappableProxy, createEventEmitterProxy } from 'swappable-obj-pro const networks = { networkList: {} } -import { ROPSTEN, RINKEBY, KOVAN, MAINNET, LOCALHOST, GOERLI } from './enums' - -const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET, GOERLI] +import { + RINKEBY, + MAINNET, + LOCALHOST, + INFURA_PROVIDER_TYPES, + NETWORK_TYPE_TO_ID_MAP, +} from './enums' const env = process.env.METAMASK_ENV const METAMASK_DEBUG = process.env.METAMASK_DEBUG @@ -167,6 +171,25 @@ export default class NetworkController extends EventEmitter { this.emit('networkDidChange', opts.type) } + _getNetworkDidChangeInfo (opts) { + + const { type } = opts + let { chainId } = opts + let networkId + + const isInfura = INFURA_PROVIDER_TYPES.includes(type) + if (isInfura) { + networkId = NETWORK_TYPE_TO_ID_MAP[type].networkId + chainId = NETWORK_TYPE_TO_ID_MAP[type].chainId + } + + return { + type, + chainId, + networkId, + } + } + _configureProvider (opts) { const { type, rpcTarget, chainId, ticker, nickname } = opts // infura type-based endpoints diff --git a/app/scripts/controllers/network/util.js b/app/scripts/controllers/network/util.js index d4243a851d28..bea71d66632d 100644 --- a/app/scripts/controllers/network/util.js +++ b/app/scripts/controllers/network/util.js @@ -1,33 +1,6 @@ -import { - ROPSTEN, - RINKEBY, - KOVAN, - MAINNET, - GOERLI, - ROPSTEN_CODE, - RINKEBY_CODE, - KOVAN_CODE, - GOERLI_CODE, - ROPSTEN_DISPLAY_NAME, - RINKEBY_DISPLAY_NAME, - KOVAN_DISPLAY_NAME, - MAINNET_DISPLAY_NAME, - GOERLI_DISPLAY_NAME, -} from './enums' +import { NETWORK_TO_NAME_MAP } from './enums' -const networkToNameMap = { - [ROPSTEN]: ROPSTEN_DISPLAY_NAME, - [RINKEBY]: RINKEBY_DISPLAY_NAME, - [KOVAN]: KOVAN_DISPLAY_NAME, - [MAINNET]: MAINNET_DISPLAY_NAME, - [GOERLI]: GOERLI_DISPLAY_NAME, - [ROPSTEN_CODE]: ROPSTEN_DISPLAY_NAME, - [RINKEBY_CODE]: RINKEBY_DISPLAY_NAME, - [KOVAN_CODE]: KOVAN_DISPLAY_NAME, - [GOERLI_CODE]: GOERLI_DISPLAY_NAME, -} - -export const getNetworkDisplayName = (key) => networkToNameMap[key] +export const getNetworkDisplayName = (key) => NETWORK_TO_NAME_MAP[key] export function formatTxMetaForRpcResult (txMeta) { return { diff --git a/app/scripts/lib/account-tracker.js b/app/scripts/lib/account-tracker.js index 306265ec12e1..d1ab1e583544 100644 --- a/app/scripts/lib/account-tracker.js +++ b/app/scripts/lib/account-tracker.js @@ -15,7 +15,7 @@ import pify from 'pify' import Web3 from 'web3' import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi' import { bnToHex } from './util' -import { MAINNET_CODE, RINKEBY_CODE, ROPSTEN_CODE, KOVAN_CODE } from '../controllers/network/enums' +import { MAINNET_NETWORK_ID, RINKEBY_NETWORK_ID, ROPSTEN_NETWORK_ID, KOVAN_NETWORK_ID } from '../controllers/network/enums' import { SINGLE_CALL_BALANCES_ADDRESS, @@ -188,22 +188,22 @@ export default class AccountTracker { async _updateAccounts () { const accounts = this.store.getState().accounts const addresses = Object.keys(accounts) - const currentNetwork = parseInt(this.network.getNetworkState()) + const currentNetwork = this.network.getNetworkState() switch (currentNetwork) { - case MAINNET_CODE: + case MAINNET_NETWORK_ID: await this._updateAccountsViaBalanceChecker(addresses, SINGLE_CALL_BALANCES_ADDRESS) break - case RINKEBY_CODE: + case RINKEBY_NETWORK_ID: await this._updateAccountsViaBalanceChecker(addresses, SINGLE_CALL_BALANCES_ADDRESS_RINKEBY) break - case ROPSTEN_CODE: + case ROPSTEN_NETWORK_ID: await this._updateAccountsViaBalanceChecker(addresses, SINGLE_CALL_BALANCES_ADDRESS_ROPSTEN) break - case KOVAN_CODE: + case KOVAN_NETWORK_ID: await this._updateAccountsViaBalanceChecker(addresses, SINGLE_CALL_BALANCES_ADDRESS_KOVAN) break diff --git a/test/unit/app/controllers/network/network-controller-test.js b/test/unit/app/controllers/network/network-controller-test.js index 239204fed446..e081204be12b 100644 --- a/test/unit/app/controllers/network/network-controller-test.js +++ b/test/unit/app/controllers/network/network-controller-test.js @@ -67,13 +67,22 @@ describe('NetworkController', function () { it('getNetworkDisplayName should return the correct network name', function () { const tests = [ { - input: 3, + input: '3', expected: 'Ropsten', }, { - input: 4, + input: '4', expected: 'Rinkeby', }, { - input: 42, + input: '42', + expected: 'Kovan', + }, { + input: '0x3', + expected: 'Ropsten', + }, { + input: '0x4', + expected: 'Rinkeby', + }, { + input: '0x42', expected: 'Kovan', }, { input: 'ropsten', diff --git a/test/unit/app/controllers/transactions/recipient-blacklist-checker-test.js b/test/unit/app/controllers/transactions/recipient-blacklist-checker-test.js index 0f0a37074f21..baab1ca00742 100644 --- a/test/unit/app/controllers/transactions/recipient-blacklist-checker-test.js +++ b/test/unit/app/controllers/transactions/recipient-blacklist-checker-test.js @@ -1,6 +1,6 @@ import { strict as assert } from 'assert' import { throwIfAccountIsBlacklisted } from '../../../../../app/scripts/controllers/transactions/lib/recipient-blacklist-checker' -import { ROPSTEN_CODE, RINKEBY_CODE, KOVAN_CODE, GOERLI_CODE } from '../../../../../app/scripts/controllers/network/enums' +import { ROPSTEN_NETWORK_ID, RINKEBY_NETWORK_ID, KOVAN_NETWORK_ID, GOERLI_NETWORK_ID } from '../../../../../app/scripts/controllers/network/enums' describe('Recipient Blacklist Checker', function () { describe('#throwIfAccountIsBlacklisted', function () { @@ -19,7 +19,7 @@ describe('Recipient Blacklist Checker', function () { ] it('does not fail on test networks', function () { - const networks = [ROPSTEN_CODE, RINKEBY_CODE, KOVAN_CODE, GOERLI_CODE] + const networks = [ROPSTEN_NETWORK_ID, RINKEBY_NETWORK_ID, KOVAN_NETWORK_ID, GOERLI_NETWORK_ID] for (const networkId of networks) { for (const account of publicAccounts) { assert.doesNotThrow(() => throwIfAccountIsBlacklisted(networkId, account)) diff --git a/ui/app/components/app/network-display/network-display.component.js b/ui/app/components/app/network-display/network-display.component.js index 1ecec3179d94..2dbe96adf2be 100644 --- a/ui/app/components/app/network-display/network-display.component.js +++ b/ui/app/components/app/network-display/network-display.component.js @@ -2,19 +2,19 @@ import React, { Component } from 'react' import PropTypes from 'prop-types' import classnames from 'classnames' import { - MAINNET_CODE, - ROPSTEN_CODE, - RINKEBY_CODE, - KOVAN_CODE, - GOERLI_CODE, + MAINNET_NETWORK_ID, + ROPSTEN_NETWORK_ID, + RINKEBY_NETWORK_ID, + KOVAN_NETWORK_ID, + GOERLI_NETWORK_ID, } from '../../../../../app/scripts/controllers/network/enums' -const networkToClassHash = { - [MAINNET_CODE]: 'mainnet', - [ROPSTEN_CODE]: 'ropsten', - [RINKEBY_CODE]: 'rinkeby', - [GOERLI_CODE]: 'goerli', - [KOVAN_CODE]: 'kovan', +const networkIdToTypeMap = { + [MAINNET_NETWORK_ID]: 'mainnet', + [ROPSTEN_NETWORK_ID]: 'ropsten', + [RINKEBY_NETWORK_ID]: 'rinkeby', + [GOERLI_NETWORK_ID]: 'goerli', + [KOVAN_NETWORK_ID]: 'kovan', } export default class NetworkDisplay extends Component { @@ -34,7 +34,7 @@ export default class NetworkDisplay extends Component { renderNetworkIcon () { const { network } = this.props - const networkClass = networkToClassHash[network] + const networkClass = networkIdToTypeMap[network] return networkClass ?
@@ -51,7 +51,7 @@ export default class NetworkDisplay extends Component { render () { const { colored, network, provider: { type, nickname } } = this.props - const networkClass = networkToClassHash[network] + const networkClass = networkIdToTypeMap[network] return (
Date: Tue, 19 May 2020 11:04:09 -0700 Subject: [PATCH 2/4] revert infura client changes --- .../controllers/network/createInfuraClient.js | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/app/scripts/controllers/network/createInfuraClient.js b/app/scripts/controllers/network/createInfuraClient.js index 1ed367dfc6ba..9f16b12978b0 100644 --- a/app/scripts/controllers/network/createInfuraClient.js +++ b/app/scripts/controllers/network/createInfuraClient.js @@ -9,8 +9,6 @@ import providerFromMiddleware from 'eth-json-rpc-middleware/providerFromMiddlewa import createInfuraMiddleware from 'eth-json-rpc-infura' import BlockTracker from 'eth-block-tracker' -import { NETWORK_TYPE_TO_ID_MAP } from './enums' - export default function createInfuraClient ({ network }) { const infuraMiddleware = createInfuraMiddleware({ network, maxAttempts: 5, source: 'metamask' }) const infuraProvider = providerFromMiddleware(infuraMiddleware) @@ -29,15 +27,36 @@ export default function createInfuraClient ({ network }) { } function createNetworkAndChainIdMiddleware ({ network }) { + let chainId + let netId - if (!NETWORK_TYPE_TO_ID_MAP[network]) { - throw new Error(`createInfuraClient - unknown network "${network}"`) + switch (network) { + case 'mainnet': + netId = '1' + chainId = '0x01' + break + case 'ropsten': + netId = '3' + chainId = '0x03' + break + case 'rinkeby': + netId = '4' + chainId = '0x04' + break + case 'kovan': + netId = '42' + chainId = '0x2a' + break + case 'goerli': + netId = '5' + chainId = '0x05' + break + default: + throw new Error(`createInfuraClient - unknown network "${network}"`) } - const { chainId, networkId } = NETWORK_TYPE_TO_ID_MAP[network] - return createScaffoldMiddleware({ eth_chainId: chainId, - net_version: networkId.toString(), + net_version: netId, }) } From 6d4a4e7a6b8c2e39f5bb446a57c8b81c74b79d2b Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Tue, 19 May 2020 11:24:02 -0700 Subject: [PATCH 3/4] consolidate chain id enums --- app/scripts/controllers/network/network.js | 19 ------------------- app/scripts/lib/enums.js | 11 ----------- app/scripts/lib/select-chain-id.js | 2 +- 3 files changed, 1 insertion(+), 31 deletions(-) diff --git a/app/scripts/controllers/network/network.js b/app/scripts/controllers/network/network.js index 747ef449eac7..60505f529024 100644 --- a/app/scripts/controllers/network/network.js +++ b/app/scripts/controllers/network/network.js @@ -171,25 +171,6 @@ export default class NetworkController extends EventEmitter { this.emit('networkDidChange', opts.type) } - _getNetworkDidChangeInfo (opts) { - - const { type } = opts - let { chainId } = opts - let networkId - - const isInfura = INFURA_PROVIDER_TYPES.includes(type) - if (isInfura) { - networkId = NETWORK_TYPE_TO_ID_MAP[type].networkId - chainId = NETWORK_TYPE_TO_ID_MAP[type].chainId - } - - return { - type, - chainId, - networkId, - } - } - _configureProvider (opts) { const { type, rpcTarget, chainId, ticker, nickname } = opts // infura type-based endpoints diff --git a/app/scripts/lib/enums.js b/app/scripts/lib/enums.js index 50e3611fae7a..50a8ccf68e42 100644 --- a/app/scripts/lib/enums.js +++ b/app/scripts/lib/enums.js @@ -9,12 +9,6 @@ const PLATFORM_EDGE = 'Edge' const PLATFORM_FIREFOX = 'Firefox' const PLATFORM_OPERA = 'Opera' -const MAINNET_CHAIN_ID = '0x1' -const ROPSTEN_CHAIN_ID = '0x3' -const RINKEBY_CHAIN_ID = '0x4' -const KOVAN_CHAIN_ID = '0x2a' -const GOERLI_CHAIN_ID = '0x5' - export { ENVIRONMENT_TYPE_POPUP, ENVIRONMENT_TYPE_NOTIFICATION, @@ -25,9 +19,4 @@ export { PLATFORM_EDGE, PLATFORM_FIREFOX, PLATFORM_OPERA, - MAINNET_CHAIN_ID, - ROPSTEN_CHAIN_ID, - RINKEBY_CHAIN_ID, - KOVAN_CHAIN_ID, - GOERLI_CHAIN_ID, } diff --git a/app/scripts/lib/select-chain-id.js b/app/scripts/lib/select-chain-id.js index 2672ab2c79a4..ed501b83799e 100644 --- a/app/scripts/lib/select-chain-id.js +++ b/app/scripts/lib/select-chain-id.js @@ -4,7 +4,7 @@ import { RINKEBY_CHAIN_ID, KOVAN_CHAIN_ID, GOERLI_CHAIN_ID, -} from './enums' +} from '../controllers/network/enums' const standardNetworkId = { '1': MAINNET_CHAIN_ID, From da71e94d7b349a19cde46b9bd97a51ac427256bb Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Tue, 19 May 2020 12:50:02 -0700 Subject: [PATCH 4/4] fixup! consolidate chain id enums --- app/scripts/controllers/network/network.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app/scripts/controllers/network/network.js b/app/scripts/controllers/network/network.js index 60505f529024..6d12e4268fa9 100644 --- a/app/scripts/controllers/network/network.js +++ b/app/scripts/controllers/network/network.js @@ -19,7 +19,6 @@ import { MAINNET, LOCALHOST, INFURA_PROVIDER_TYPES, - NETWORK_TYPE_TO_ID_MAP, } from './enums' const env = process.env.METAMASK_ENV