diff --git a/app/scripts/background.js b/app/scripts/background.js index c9155c858..44bcab288 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -243,7 +243,9 @@ function setupController(initState, initLangCode) { }); setupEnsIpfsResolver({ - getCurrentNetwork: controller.getCurrentNetwork, + getCurrentChainId: controller.networkController.getCurrentChainId.bind( + controller.networkController, + ), getIpfsGateway: controller.preferencesController.getIpfsGateway.bind( controller.preferencesController, ), diff --git a/app/scripts/controllers/ens/index.js b/app/scripts/controllers/ens/index.js index 05d531103..015c3ee0a 100644 --- a/app/scripts/controllers/ens/index.js +++ b/app/scripts/controllers/ens/index.js @@ -2,20 +2,22 @@ import punycode from 'punycode/punycode'; import ethUtil from 'ethereumjs-util'; import { ObservableStore } from '@metamask/obs-store'; import log from 'loglevel'; +import { CHAIN_ID_TO_NETWORK_ID_MAP } from '../../../../shared/constants/network'; import Ens from './ens'; const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; const ZERO_X_ERROR_ADDRESS = '0x'; export default class EnsController { - constructor({ ens, provider, networkStore } = {}) { + constructor({ ens, provider, onNetworkDidChange, getCurrentChainId } = {}) { const initState = { ensResolutionsByAddress: {}, }; this._ens = ens; if (!this._ens) { - const network = networkStore.getState(); + const chainId = getCurrentChainId(); + const network = CHAIN_ID_TO_NETWORK_ID_MAP[chainId]; if (Ens.getNetworkEnsSupport(network)) { this._ens = new Ens({ network, @@ -25,8 +27,10 @@ export default class EnsController { } this.store = new ObservableStore(initState); - networkStore.subscribe((network) => { + onNetworkDidChange(() => { this.store.putState(initState); + const chainId = getCurrentChainId(); + const network = CHAIN_ID_TO_NETWORK_ID_MAP[chainId]; if (Ens.getNetworkEnsSupport(network)) { this._ens = new Ens({ network, diff --git a/app/scripts/lib/ens-ipfs/setup.js b/app/scripts/lib/ens-ipfs/setup.js index 51b740301..8c30de975 100644 --- a/app/scripts/lib/ens-ipfs/setup.js +++ b/app/scripts/lib/ens-ipfs/setup.js @@ -8,7 +8,7 @@ const supportedTopLevelDomains = ['eth']; export default function setupEnsIpfsResolver({ provider, - getCurrentNetwork, + getCurrentChainId, getIpfsGateway, }) { // install listener @@ -30,7 +30,7 @@ export default function setupEnsIpfsResolver({ const { tabId, url } = details; // ignore requests that are not associated with tabs // only attempt ENS resolution on mainnet - if (tabId === -1 || getCurrentNetwork() !== '1') { + if (tabId === -1 || getCurrentChainId() !== '0x1') { return; } // parse ens name diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 1ac569760..a71ad26d2 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -178,7 +178,13 @@ export default class MetamaskController extends EventEmitter { this.ensController = new EnsController({ provider: this.provider, - networkStore: this.networkController.networkStore, + getCurrentChainId: this.networkController.getCurrentChainId.bind( + this.networkController, + ), + onNetworkDidChange: this.networkController.on.bind( + this.networkController, + NETWORK_EVENTS.NETWORK_DID_CHANGE, + ), }); this.incomingTransactionsController = new IncomingTransactionsController({ diff --git a/test/unit/app/controllers/ens-controller-test.js b/test/unit/app/controllers/ens-controller-test.js index c210fa3f8..d21a723e1 100644 --- a/test/unit/app/controllers/ens-controller-test.js +++ b/test/unit/app/controllers/ens-controller-test.js @@ -1,31 +1,38 @@ import assert from 'assert'; import sinon from 'sinon'; -import { ObservableStore } from '@metamask/obs-store'; import EnsController from '../../../../app/scripts/controllers/ens'; const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; const ZERO_X_ERROR_ADDRESS = '0x'; describe('EnsController', function () { + let currentChainId; + let getCurrentChainId; + let onNetworkDidChange; + beforeEach(function () { + currentChainId = '0x3'; + getCurrentChainId = () => currentChainId; + onNetworkDidChange = sinon.spy(); + }); + afterEach(function () { + sinon.restore(); + }); describe('#constructor', function () { it('should construct the controller given a provider and a network', async function () { - const currentNetworkId = '3'; - const networkStore = new ObservableStore(currentNetworkId); const ens = new EnsController({ provider: {}, - networkStore, + getCurrentChainId, + onNetworkDidChange, }); assert.ok(ens._ens); }); it('should construct the controller given an existing ENS instance', async function () { - const networkStore = { - subscribe: sinon.spy(), - }; const ens = new EnsController({ ens: {}, - networkStore, + getCurrentChainId, + onNetworkDidChange, }); assert.ok(ens._ens); @@ -35,15 +42,13 @@ describe('EnsController', function () { describe('#reverseResolveName', function () { it('should resolve to an ENS name', async function () { const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'; - const networkStore = { - subscribe: sinon.spy(), - }; const ens = new EnsController({ ens: { reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'), lookup: sinon.stub().withArgs('peaksignal.eth').returns(address), }, - networkStore, + onNetworkDidChange, + getCurrentChainId, }); const name = await ens.reverseResolveAddress(address); @@ -54,15 +59,13 @@ describe('EnsController', function () { const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'; const reverse = sinon.stub().withArgs(address).returns('peaksignal.eth'); const lookup = sinon.stub().withArgs('peaksignal.eth').returns(address); - const networkStore = { - subscribe: sinon.spy(), - }; const ens = new EnsController({ ens: { reverse, lookup, }, - networkStore, + getCurrentChainId, + onNetworkDidChange, }); assert.equal(await ens.reverseResolveAddress(address), 'peaksignal.eth'); @@ -73,15 +76,13 @@ describe('EnsController', function () { it('should fail if the name is registered to a different address than the reverse-resolved', async function () { const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'; - const networkStore = { - subscribe: sinon.spy(), - }; const ens = new EnsController({ ens: { reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'), lookup: sinon.stub().withArgs('peaksignal.eth').returns('0xfoo'), }, - networkStore, + onNetworkDidChange, + getCurrentChainId, }); const name = await ens.reverseResolveAddress(address); @@ -90,15 +91,13 @@ describe('EnsController', function () { it('should throw an error when the lookup resolves to the zero address', async function () { const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'; - const networkStore = { - subscribe: sinon.spy(), - }; const ens = new EnsController({ ens: { reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'), lookup: sinon.stub().withArgs('peaksignal.eth').returns(ZERO_ADDRESS), }, - networkStore, + getCurrentChainId, + onNetworkDidChange, }); try { @@ -111,9 +110,6 @@ describe('EnsController', function () { it('should throw an error the lookup resolves to the zero x address', async function () { const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'; - const networkStore = { - subscribe: sinon.spy(), - }; const ens = new EnsController({ ens: { reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'), @@ -122,7 +118,8 @@ describe('EnsController', function () { .withArgs('peaksignal.eth') .returns(ZERO_X_ERROR_ADDRESS), }, - networkStore, + onNetworkDidChange, + getCurrentChainId, }); try { diff --git a/ui/app/pages/send/send-content/add-recipient/ens-input.container.js b/ui/app/pages/send/send-content/add-recipient/ens-input.container.js index f8ad890bc..42e75e501 100644 --- a/ui/app/pages/send/send-content/add-recipient/ens-input.container.js +++ b/ui/app/pages/send/send-content/add-recipient/ens-input.container.js @@ -1,16 +1,18 @@ import { connect } from 'react-redux'; +import { CHAIN_ID_TO_NETWORK_ID_MAP } from '../../../../../../shared/constants/network'; import { - getCurrentNetwork, getSendTo, getSendToNickname, getAddressBookEntry, + getCurrentChainId, } from '../../../../selectors'; import EnsInput from './ens-input.component'; export default connect((state) => { const selectedAddress = getSendTo(state); + const chainId = getCurrentChainId(state); return { - network: getCurrentNetwork(state), + network: CHAIN_ID_TO_NETWORK_ID_MAP[chainId], selectedAddress, selectedName: getSendToNickname(state), contact: getAddressBookEntry(state, selectedAddress),