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({
getCurrentNetwork: controller.getCurrentNetwork,
getCurrentChainId: controller.networkController.getCurrentChainId.bind(
controller.networkController,
),
getIpfsGateway: controller.preferencesController.getIpfsGateway.bind(
controller.preferencesController,
),

View File

@ -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,

View File

@ -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

View File

@ -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({

View File

@ -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 {

View File

@ -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),