1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

use chain id for enabling ENS IPFS resolution (#10507)

This commit is contained in:
Brad Decker 2021-02-25 05:40:57 -06:00 committed by GitHub
parent 3181eca351
commit 15d78b8158
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 37 deletions

View File

@ -243,7 +243,9 @@ function setupController(initState, initLangCode) {
}); });
setupEnsIpfsResolver({ setupEnsIpfsResolver({
getCurrentNetwork: controller.getCurrentNetwork, getCurrentChainId: controller.networkController.getCurrentChainId.bind(
controller.networkController,
),
getIpfsGateway: controller.preferencesController.getIpfsGateway.bind( getIpfsGateway: controller.preferencesController.getIpfsGateway.bind(
controller.preferencesController, controller.preferencesController,
), ),

View File

@ -2,20 +2,22 @@ import punycode from 'punycode/punycode';
import ethUtil from 'ethereumjs-util'; import ethUtil from 'ethereumjs-util';
import { ObservableStore } from '@metamask/obs-store'; import { ObservableStore } from '@metamask/obs-store';
import log from 'loglevel'; import log from 'loglevel';
import { CHAIN_ID_TO_NETWORK_ID_MAP } from '../../../../shared/constants/network';
import Ens from './ens'; import Ens from './ens';
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
const ZERO_X_ERROR_ADDRESS = '0x'; const ZERO_X_ERROR_ADDRESS = '0x';
export default class EnsController { export default class EnsController {
constructor({ ens, provider, networkStore } = {}) { constructor({ ens, provider, onNetworkDidChange, getCurrentChainId } = {}) {
const initState = { const initState = {
ensResolutionsByAddress: {}, ensResolutionsByAddress: {},
}; };
this._ens = ens; this._ens = ens;
if (!this._ens) { if (!this._ens) {
const network = networkStore.getState(); const chainId = getCurrentChainId();
const network = CHAIN_ID_TO_NETWORK_ID_MAP[chainId];
if (Ens.getNetworkEnsSupport(network)) { if (Ens.getNetworkEnsSupport(network)) {
this._ens = new Ens({ this._ens = new Ens({
network, network,
@ -25,8 +27,10 @@ export default class EnsController {
} }
this.store = new ObservableStore(initState); this.store = new ObservableStore(initState);
networkStore.subscribe((network) => { onNetworkDidChange(() => {
this.store.putState(initState); this.store.putState(initState);
const chainId = getCurrentChainId();
const network = CHAIN_ID_TO_NETWORK_ID_MAP[chainId];
if (Ens.getNetworkEnsSupport(network)) { if (Ens.getNetworkEnsSupport(network)) {
this._ens = new Ens({ this._ens = new Ens({
network, network,

View File

@ -8,7 +8,7 @@ const supportedTopLevelDomains = ['eth'];
export default function setupEnsIpfsResolver({ export default function setupEnsIpfsResolver({
provider, provider,
getCurrentNetwork, getCurrentChainId,
getIpfsGateway, getIpfsGateway,
}) { }) {
// install listener // install listener
@ -30,7 +30,7 @@ export default function setupEnsIpfsResolver({
const { tabId, url } = details; const { tabId, url } = details;
// ignore requests that are not associated with tabs // ignore requests that are not associated with tabs
// only attempt ENS resolution on mainnet // only attempt ENS resolution on mainnet
if (tabId === -1 || getCurrentNetwork() !== '1') { if (tabId === -1 || getCurrentChainId() !== '0x1') {
return; return;
} }
// parse ens name // parse ens name

View File

@ -178,7 +178,13 @@ export default class MetamaskController extends EventEmitter {
this.ensController = new EnsController({ this.ensController = new EnsController({
provider: this.provider, 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({ this.incomingTransactionsController = new IncomingTransactionsController({

View File

@ -1,31 +1,38 @@
import assert from 'assert'; import assert from 'assert';
import sinon from 'sinon'; import sinon from 'sinon';
import { ObservableStore } from '@metamask/obs-store';
import EnsController from '../../../../app/scripts/controllers/ens'; import EnsController from '../../../../app/scripts/controllers/ens';
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
const ZERO_X_ERROR_ADDRESS = '0x'; const ZERO_X_ERROR_ADDRESS = '0x';
describe('EnsController', function () { 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 () { describe('#constructor', function () {
it('should construct the controller given a provider and a network', async 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({ const ens = new EnsController({
provider: {}, provider: {},
networkStore, getCurrentChainId,
onNetworkDidChange,
}); });
assert.ok(ens._ens); assert.ok(ens._ens);
}); });
it('should construct the controller given an existing ENS instance', async function () { it('should construct the controller given an existing ENS instance', async function () {
const networkStore = {
subscribe: sinon.spy(),
};
const ens = new EnsController({ const ens = new EnsController({
ens: {}, ens: {},
networkStore, getCurrentChainId,
onNetworkDidChange,
}); });
assert.ok(ens._ens); assert.ok(ens._ens);
@ -35,15 +42,13 @@ describe('EnsController', function () {
describe('#reverseResolveName', function () { describe('#reverseResolveName', function () {
it('should resolve to an ENS name', async function () { it('should resolve to an ENS name', async function () {
const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'; const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5';
const networkStore = {
subscribe: sinon.spy(),
};
const ens = new EnsController({ const ens = new EnsController({
ens: { ens: {
reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'), reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'),
lookup: sinon.stub().withArgs('peaksignal.eth').returns(address), lookup: sinon.stub().withArgs('peaksignal.eth').returns(address),
}, },
networkStore, onNetworkDidChange,
getCurrentChainId,
}); });
const name = await ens.reverseResolveAddress(address); const name = await ens.reverseResolveAddress(address);
@ -54,15 +59,13 @@ describe('EnsController', function () {
const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'; const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5';
const reverse = sinon.stub().withArgs(address).returns('peaksignal.eth'); const reverse = sinon.stub().withArgs(address).returns('peaksignal.eth');
const lookup = sinon.stub().withArgs('peaksignal.eth').returns(address); const lookup = sinon.stub().withArgs('peaksignal.eth').returns(address);
const networkStore = {
subscribe: sinon.spy(),
};
const ens = new EnsController({ const ens = new EnsController({
ens: { ens: {
reverse, reverse,
lookup, lookup,
}, },
networkStore, getCurrentChainId,
onNetworkDidChange,
}); });
assert.equal(await ens.reverseResolveAddress(address), 'peaksignal.eth'); 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 () { it('should fail if the name is registered to a different address than the reverse-resolved', async function () {
const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'; const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5';
const networkStore = {
subscribe: sinon.spy(),
};
const ens = new EnsController({ const ens = new EnsController({
ens: { ens: {
reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'), reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'),
lookup: sinon.stub().withArgs('peaksignal.eth').returns('0xfoo'), lookup: sinon.stub().withArgs('peaksignal.eth').returns('0xfoo'),
}, },
networkStore, onNetworkDidChange,
getCurrentChainId,
}); });
const name = await ens.reverseResolveAddress(address); 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 () { it('should throw an error when the lookup resolves to the zero address', async function () {
const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'; const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5';
const networkStore = {
subscribe: sinon.spy(),
};
const ens = new EnsController({ const ens = new EnsController({
ens: { ens: {
reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'), reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'),
lookup: sinon.stub().withArgs('peaksignal.eth').returns(ZERO_ADDRESS), lookup: sinon.stub().withArgs('peaksignal.eth').returns(ZERO_ADDRESS),
}, },
networkStore, getCurrentChainId,
onNetworkDidChange,
}); });
try { try {
@ -111,9 +110,6 @@ describe('EnsController', function () {
it('should throw an error the lookup resolves to the zero x address', async function () { it('should throw an error the lookup resolves to the zero x address', async function () {
const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'; const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5';
const networkStore = {
subscribe: sinon.spy(),
};
const ens = new EnsController({ const ens = new EnsController({
ens: { ens: {
reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'), reverse: sinon.stub().withArgs(address).returns('peaksignal.eth'),
@ -122,7 +118,8 @@ describe('EnsController', function () {
.withArgs('peaksignal.eth') .withArgs('peaksignal.eth')
.returns(ZERO_X_ERROR_ADDRESS), .returns(ZERO_X_ERROR_ADDRESS),
}, },
networkStore, onNetworkDidChange,
getCurrentChainId,
}); });
try { try {

View File

@ -1,16 +1,18 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { CHAIN_ID_TO_NETWORK_ID_MAP } from '../../../../../../shared/constants/network';
import { import {
getCurrentNetwork,
getSendTo, getSendTo,
getSendToNickname, getSendToNickname,
getAddressBookEntry, getAddressBookEntry,
getCurrentChainId,
} from '../../../../selectors'; } from '../../../../selectors';
import EnsInput from './ens-input.component'; import EnsInput from './ens-input.component';
export default connect((state) => { export default connect((state) => {
const selectedAddress = getSendTo(state); const selectedAddress = getSendTo(state);
const chainId = getCurrentChainId(state);
return { return {
network: getCurrentNetwork(state), network: CHAIN_ID_TO_NETWORK_ID_MAP[chainId],
selectedAddress, selectedAddress,
selectedName: getSendToNickname(state), selectedName: getSendToNickname(state),
contact: getAddressBookEntry(state, selectedAddress), contact: getAddressBookEntry(state, selectedAddress),