Skip to content

Commit

Permalink
use chainId in EnsController
Browse files Browse the repository at this point in the history
  • Loading branch information
brad-decker committed Feb 23, 2021
1 parent 19ade66 commit cf6f617
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
10 changes: 7 additions & 3 deletions app/scripts/controllers/ens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
52 changes: 25 additions & 27 deletions test/unit/app/controllers/ens-controller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,33 @@ 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();
});
this.afterEach(() => {
sinon.reset();
});
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);
Expand All @@ -35,15 +43,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);
Expand All @@ -54,15 +60,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');
Expand All @@ -73,15 +77,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);
Expand All @@ -90,15 +92,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 {
Expand All @@ -111,9 +111,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'),
Expand All @@ -122,7 +119,8 @@ describe('EnsController', function () {
.withArgs('peaksignal.eth')
.returns(ZERO_X_ERROR_ADDRESS),
},
networkStore,
onNetworkDidChange,
getCurrentChainId,
});

try {
Expand Down

0 comments on commit cf6f617

Please sign in to comment.