Skip to content

Commit

Permalink
Merge extension ens controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Salah-eddineS committed Apr 18, 2023
1 parent 1970dc1 commit 42346e8
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 8 deletions.
6 changes: 6 additions & 0 deletions packages/controller-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,9 @@ export enum ApprovalType {
Unlock = 'unlock',
ConnectAccounts = 'connect_accounts',
}

export const NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP = {
[NetworksChainId.goerli]: NetworkType.goerli,
[NetworksChainId.sepolia]: NetworkType.sepolia,
[NetworksChainId.mainnet]: NetworkType.mainnet,
} as const;
6 changes: 5 additions & 1 deletion packages/ens-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@ethersproject/providers": "^5.7.0",
"@metamask/base-controller": "workspace:^",
"@metamask/controller-utils": "workspace:^"
"@metamask/controller-utils": "workspace:^",
"ethereum-ens-network-map": "^1.0.2",
"punycode": "^2.1.1"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.1.0",
"@types/jest": "^26.0.22",
"deepmerge": "^4.2.2",
"jest": "^26.4.2",
"sinon": "^9.2.4",
"ts-jest": "^26.5.2",
"typedoc": "^0.22.15",
"typedoc-plugin-missing-exports": "^0.22.6",
Expand Down
148 changes: 142 additions & 6 deletions packages/ens-controller/src/EnsController.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ControllerMessenger } from '@metamask/base-controller';
import { toChecksumHexAddress } from '@metamask/controller-utils';
import sinon from 'sinon';
import { EnsController } from './EnsController';

const address1 = '0x32Be343B94f860124dC4fEe278FDCBD38C102D88';
Expand All @@ -26,12 +27,60 @@ function getMessenger() {
}

describe('EnsController', () => {
afterEach(function () {
sinon.restore();
});

it('should set default state', () => {
const messenger = getMessenger();
const controller = new EnsController({
messenger,
});
expect(controller.state).toStrictEqual({ ensEntries: {} });
expect(controller.state).toStrictEqual({
ensEntries: {},
ensResolutionsByAddress: {},
});
});

it('should construct the controller given a provider, a network and a onNetworkStateChange', async function () {
const messenger = getMessenger();
const ens = new EnsController({
messenger,
provider: sinon.fake(),
onNetworkStateChange: (listener) => {
listener({
network: '1',
providerConfig: {
chainId: '1',
},
});
},
});

expect(ens.ethProvider).toBeDefined();
});

it('should return a null eth provider when not given a provider or onNetworkStateChange', async function () {
expect(
new EnsController({
messenger: getMessenger(),
onNetworkStateChange: (listener) => {
listener({
network: '1',
providerConfig: {
chainId: '1',
},
});
},
}).ethProvider,
).toBeNull();

expect(
new EnsController({
messenger: getMessenger(),
provider: sinon.fake(),
}).ethProvider,
).toBeNull();
});

it('should add a new ENS entry and return true', () => {
Expand All @@ -50,6 +99,7 @@ describe('EnsController', () => {
},
},
},
ensResolutionsByAddress: {},
});
});

Expand All @@ -69,6 +119,7 @@ describe('EnsController', () => {
},
},
},
ensResolutionsByAddress: {},
});
});

Expand All @@ -89,6 +140,7 @@ describe('EnsController', () => {
},
},
},
ensResolutionsByAddress: {},
});
});

Expand All @@ -109,6 +161,7 @@ describe('EnsController', () => {
},
},
},
ensResolutionsByAddress: {},
});
});

Expand All @@ -129,6 +182,7 @@ describe('EnsController', () => {
},
},
},
ensResolutionsByAddress: {},
});
});

Expand All @@ -149,6 +203,7 @@ describe('EnsController', () => {
},
},
},
ensResolutionsByAddress: {},
});
});

Expand Down Expand Up @@ -183,6 +238,7 @@ describe('EnsController', () => {
},
},
},
ensResolutionsByAddress: {},
});
});

Expand Down Expand Up @@ -227,7 +283,10 @@ describe('EnsController', () => {
}).toThrow(
'Invalid ENS entry: { chainId:a, ensName:foobarb.eth, address:0x32Be343B94f860124dC4fEe278FDCBD38C102D88}',
);
expect(controller.state).toStrictEqual({ ensEntries: {} });
expect(controller.state).toStrictEqual({
ensEntries: {},
ensResolutionsByAddress: {},
});
});

it('should throw on attempt to set invalid ENS entry: ENS name', () => {
Expand All @@ -238,7 +297,10 @@ describe('EnsController', () => {
expect(() => {
controller.set('1', 'foo.eth', address1);
}).toThrow('Invalid ENS name: foo.eth');
expect(controller.state).toStrictEqual({ ensEntries: {} });
expect(controller.state).toStrictEqual({
ensEntries: {},
ensResolutionsByAddress: {},
});
});

it('should throw on attempt to set invalid ENS entry: address', () => {
Expand All @@ -251,7 +313,10 @@ describe('EnsController', () => {
}).toThrow(
'Invalid ENS entry: { chainId:1, ensName:foobarb.eth, address:foo}',
);
expect(controller.state).toStrictEqual({ ensEntries: {} });
expect(controller.state).toStrictEqual({
ensEntries: {},
ensResolutionsByAddress: {},
});
});

it('should remove an ENS entry and return true', () => {
Expand All @@ -261,7 +326,10 @@ describe('EnsController', () => {
});
expect(controller.set('1', name1, address1)).toStrictEqual(true);
expect(controller.delete('1', name1)).toStrictEqual(true);
expect(controller.state).toStrictEqual({ ensEntries: {} });
expect(controller.state).toStrictEqual({
ensEntries: {},
ensResolutionsByAddress: {},
});
});

it('should return false if an ENS entry was NOT deleted', () => {
Expand All @@ -282,6 +350,7 @@ describe('EnsController', () => {
},
},
},
ensResolutionsByAddress: {},
});
});

Expand Down Expand Up @@ -311,6 +380,7 @@ describe('EnsController', () => {
},
},
},
ensResolutionsByAddress: {},
});
});

Expand All @@ -323,6 +393,72 @@ describe('EnsController', () => {
expect(controller.set('1', name2, address2)).toStrictEqual(true);
expect(controller.set('2', name1, address1)).toStrictEqual(true);
controller.clear();
expect(controller.state).toStrictEqual({ ensEntries: {} });
expect(controller.state).toStrictEqual({
ensEntries: {},
ensResolutionsByAddress: {},
});
});

describe('reverseResolveName', () => {
it('should through "Provider has not been initialized."', async () => {
const messenger = getMessenger();
const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5';
const ens = new EnsController({
messenger,
});

await expect(() => ens.reverseResolveAddress(address)).rejects.toThrow(
'Provider has not been initialized.',
);
});

it('should only resolve an ENS name once', async () => {
const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5';
const messenger = getMessenger();
const ens = new EnsController({
messenger,
provider: sinon.fake(),
onNetworkStateChange: (listener) => {
listener({
network: '1',
providerConfig: {
chainId: '1',
},
});
},
});
const reverse = sinon.stub().withArgs(address).returns('peaksignal.eth');
const lookup = sinon.stub().withArgs('peaksignal.eth').returns(address);
ens.reverse = reverse;
ens.lookup = lookup;

expect(await ens.reverseResolveAddress(address)).toStrictEqual(
'peaksignal.eth',
);
expect(await ens.reverseResolveAddress(address)).toStrictEqual(
'peaksignal.eth',
);
expect(lookup.calledOnce).toBe(true);
expect(reverse.calledOnce).toBe(true);
});

it('should fail if the name is registered to a different address than the reverse-resolved', async () => {
const address = '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5';
const messenger = getMessenger();
const ens = new EnsController({
messenger,
provider: sinon.fake(),
onNetworkStateChange: (listener) => {
listener({
network: '1',
providerConfig: {
chainId: '1',
},
});
},
});

expect(await ens.reverseResolveAddress(address)).toBeUndefined();
});
});
});
Loading

0 comments on commit 42346e8

Please sign in to comment.