diff --git a/app/scripts/lib/enums.js b/app/scripts/lib/enums.js index 32c0947a3..296b4cca6 100644 --- a/app/scripts/lib/enums.js +++ b/app/scripts/lib/enums.js @@ -9,6 +9,12 @@ 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' + module.exports = { ENVIRONMENT_TYPE_POPUP, ENVIRONMENT_TYPE_NOTIFICATION, @@ -19,4 +25,9 @@ module.exports = { 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 new file mode 100644 index 000000000..3171c9840 --- /dev/null +++ b/app/scripts/lib/select-chain-id.js @@ -0,0 +1,22 @@ +const { + MAINNET_CHAIN_ID, + ROPSTEN_CHAIN_ID, + RINKEBY_CHAIN_ID, + KOVAN_CHAIN_ID, + GOERLI_CHAIN_ID, +} = require('./enums') + +const standardNetworkId = { + '1': MAINNET_CHAIN_ID, + '3': ROPSTEN_CHAIN_ID, + '4': RINKEBY_CHAIN_ID, + '42': KOVAN_CHAIN_ID, + '5': GOERLI_CHAIN_ID, +} + +function selectChainId (metamaskState) { + const { network, provider: { chaindId } } = metamaskState + return standardNetworkId[network] || `0x${parseInt(chaindId, 10).toString(16)}` +} + +module.exports = selectChainId diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 28537a8eb..37c3340f4 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -41,6 +41,7 @@ const ProviderApprovalController = require('./controllers/provider-approval') const nodeify = require('./lib/nodeify') const accountImporter = require('./account-import-strategies') const getBuyEthUrl = require('./lib/buy-eth-url') +const selectChainId = require('./lib/select-chain-id') const {Mutex} = require('await-semaphore') const {version} = require('../manifest.json') const {BN} = require('ethereumjs-util') @@ -360,7 +361,7 @@ module.exports = class MetamaskController extends EventEmitter { publicConfigStore.putState(publicState) } - function selectPublicState ({ isUnlocked, selectedAddress, network, completedOnboarding }) { + function selectPublicState ({ isUnlocked, selectedAddress, network, completedOnboarding, provider }) { const isEnabled = checkIsEnabled() const isReady = isUnlocked && isEnabled const result = { @@ -369,6 +370,7 @@ module.exports = class MetamaskController extends EventEmitter { selectedAddress: isReady ? selectedAddress : undefined, networkVersion: network, onboardingcomplete: completedOnboarding, + chainId: selectChainId({ network, provider }), } return result } diff --git a/package.json b/package.json index 4436e3e1d..8b7f1edc3 100644 --- a/package.json +++ b/package.json @@ -114,7 +114,7 @@ "lodash.shuffle": "^4.2.0", "loglevel": "^1.4.1", "luxon": "^1.8.2", - "metamask-inpage-provider": "^2.0.3", + "metamask-inpage-provider": "^2.1.0", "metamask-logo": "^2.1.4", "mkdirp": "^0.5.1", "multihashes": "^0.4.12", diff --git a/test/e2e/contract-test/contract.js b/test/e2e/contract-test/contract.js index 9fe5c21b1..38db041c8 100644 --- a/test/e2e/contract-test/contract.js +++ b/test/e2e/contract-test/contract.js @@ -1,3 +1,5 @@ +/*global ethereum*/ + /* The `piggybankContract` is compiled from: @@ -164,4 +166,22 @@ web3.currentProvider.enable().then(() => { }) }) + + ethereum.autoRefreshOnNetworkChange = false + + const networkDiv = document.getElementById('network') + const chainIdDiv = document.getElementById('chainId') + const accountsDiv = document.getElementById('accounts') + + ethereum.on('networkChanged', (networkId) => { + networkDiv.innerHTML = networkId + }) + + ethereum.on('chainIdChanged', (chainId) => { + chainIdDiv.innerHTML = chainId + }) + + ethereum.on('accountsChanged', (accounts) => { + accountsDiv.innerHTML = accounts + }) }) diff --git a/test/e2e/contract-test/index.html b/test/e2e/contract-test/index.html index 6e134dc36..03792de76 100644 --- a/test/e2e/contract-test/index.html +++ b/test/e2e/contract-test/index.html @@ -31,6 +31,12 @@ +