diff --git a/src/third-party/EnsController.ts b/src/third-party/EnsController.ts index f7d8fa14227..be0b17971fd 100644 --- a/src/third-party/EnsController.ts +++ b/src/third-party/EnsController.ts @@ -11,7 +11,7 @@ import { isValidAddress, toChecksumAddress } from 'ethereumjs-util'; * @property address - Hex address with the ENS name */ export interface EnsEntry { - chainId: number; + chainId: string; ensName: string; address: string; } @@ -27,17 +27,6 @@ export interface EnsState extends BaseState { ensEntries: { [chainId: string]: { [ensName: string]: EnsEntry } }; } -/** - * @type EnsEntriesState - * - * ENS controller state - * - * @property ensEntries - Object of ENS entry objects - */ -interface EnsEntriesState { - [chainId: string]: { [ensName: string]: EnsEntry }; -} - /** * Controller that manages a list ENS names and their resolved addresses * by chainId @@ -75,12 +64,12 @@ export class EnsController extends BaseController { * @param chainId - Parent chain of the ENS entry to delete * @param ensName - Name of the ENS entry to delete */ - delete(chainId: number, ensName: string) { + delete(chainId: string, ensName: string) { if (!this.state.ensEntries[chainId] || !this.state.ensEntries[chainId][ensName]) { return false; } - const ensEntries: EnsEntriesState = Object.assign({}, this.state.ensEntries); + const ensEntries = Object.assign({}, this.state.ensEntries); delete ensEntries[chainId][ensName]; if (Object.keys(ensEntries[chainId]).length === 0) { @@ -99,8 +88,13 @@ export class EnsController extends BaseController { * @param address - Associated address to add or update * @returns - Boolean indicating whether the entry was set */ - set(chainId: number, ensName: string, address: string): boolean { - if (!Number.isInteger(chainId) || !ensName || typeof ensName !== 'string' || !isValidAddress(address)) { + set(chainId: string, ensName: string, address: string): boolean { + if ( + !Number.isInteger(Number.parseInt(chainId, 10)) || + !ensName || + typeof ensName !== 'string' || + !isValidAddress(address) + ) { throw new Error(`Invalid ENS entry: { chainId:${chainId}, ensName:${ensName}, address:${address}}`); } diff --git a/tests/EnsController.test.ts b/tests/EnsController.test.ts index f8f54cab6c9..e8e95547f63 100644 --- a/tests/EnsController.test.ts +++ b/tests/EnsController.test.ts @@ -20,13 +20,13 @@ describe('EnsController', () => { it('should add a new ENS entry and return true', () => { const controller = new EnsController(); - expect(controller.set(1, name1, address1)).toBeTruthy(); + expect(controller.set('1', name1, address1)).toBeTruthy(); expect(controller.state).toEqual({ ensEntries: { 1: { [name1]: { address: address1Checksum, - chainId: 1, + chainId: '1', ensName: name1 } } @@ -36,14 +36,14 @@ describe('EnsController', () => { it('should update an ENS entry and return true', () => { const controller = new EnsController(); - expect(controller.set(1, name1, address1)).toBeTruthy(); - expect(controller.set(1, name1, address2)).toBeTruthy(); + expect(controller.set('1', name1, address1)).toBeTruthy(); + expect(controller.set('1', name1, address2)).toBeTruthy(); expect(controller.state).toEqual({ ensEntries: { 1: { [name1]: { address: address2Checksum, - chainId: 1, + chainId: '1', ensName: name1 } } @@ -53,14 +53,14 @@ describe('EnsController', () => { it('should not update an ENS entry if the address is the same and return false', () => { const controller = new EnsController(); - expect(controller.set(1, name1, address1)).toBeTruthy(); - expect(controller.set(1, name1, address1)).toBeFalsy(); + expect(controller.set('1', name1, address1)).toBeTruthy(); + expect(controller.set('1', name1, address1)).toBeFalsy(); expect(controller.state).toEqual({ ensEntries: { 1: { [name1]: { address: address1Checksum, - chainId: 1, + chainId: '1', ensName: name1 } } @@ -70,28 +70,28 @@ describe('EnsController', () => { it('should add multiple ENS entries and update without side effects', () => { const controller = new EnsController(); - expect(controller.set(1, name1, address1)).toBeTruthy(); - expect(controller.set(1, name2, address2)).toBeTruthy(); - expect(controller.set(2, name1, address1)).toBeTruthy(); - expect(controller.set(1, name1, address3)).toBeTruthy(); + expect(controller.set('1', name1, address1)).toBeTruthy(); + expect(controller.set('1', name2, address2)).toBeTruthy(); + expect(controller.set('2', name1, address1)).toBeTruthy(); + expect(controller.set('1', name1, address3)).toBeTruthy(); expect(controller.state).toEqual({ ensEntries: { 1: { [name1]: { address: address3Checksum, - chainId: 1, + chainId: '1', ensName: name1 }, [name2]: { address: address2Checksum, - chainId: 1, + chainId: '1', ensName: name2 } }, 2: { [name1]: { address: address1Checksum, - chainId: 2, + chainId: '2', ensName: name1 } } @@ -102,29 +102,29 @@ describe('EnsController', () => { it('should throw on attempt to set invalid ENS entry', () => { const controller = new EnsController(); expect(() => { - controller.set(1, '1337', 'foo'); + controller.set('1', '1337', 'foo'); }).toThrowError(); expect(controller.state).toEqual({ ensEntries: {} }); }); it('should remove an ENS entry and return true', () => { const controller = new EnsController(); - expect(controller.set(1, name1, address1)).toBeTruthy(); - expect(controller.delete(1, name1)).toBeTruthy(); + expect(controller.set('1', name1, address1)).toBeTruthy(); + expect(controller.delete('1', name1)).toBeTruthy(); expect(controller.state).toEqual({ ensEntries: {} }); }); it('should return false if an ENS entry was NOT deleted', () => { const controller = new EnsController(); - controller.set(1, name1, address1); - expect(controller.delete(1, 'bar')).toBeFalsy(); - expect(controller.delete(2, 'bar')).toBeFalsy(); + controller.set('1', name1, address1); + expect(controller.delete('1', 'bar')).toBeFalsy(); + expect(controller.delete('2', 'bar')).toBeFalsy(); expect(controller.state).toEqual({ ensEntries: { 1: { [name1]: { address: address1Checksum, - chainId: 1, + chainId: '1', ensName: name1 } } @@ -134,23 +134,23 @@ describe('EnsController', () => { it('should add multiple ENS entries and remove without side effects', () => { const controller = new EnsController(); - expect(controller.set(1, name1, address1)).toBeTruthy(); - expect(controller.set(1, name2, address2)).toBeTruthy(); - expect(controller.set(2, name1, address1)).toBeTruthy(); - expect(controller.delete(1, name1)).toBeTruthy(); + expect(controller.set('1', name1, address1)).toBeTruthy(); + expect(controller.set('1', name2, address2)).toBeTruthy(); + expect(controller.set('2', name1, address1)).toBeTruthy(); + expect(controller.delete('1', name1)).toBeTruthy(); expect(controller.state).toEqual({ ensEntries: { 1: { [name2]: { address: address2Checksum, - chainId: 1, + chainId: '1', ensName: name2 } }, 2: { [name1]: { address: address1Checksum, - chainId: 2, + chainId: '2', ensName: name1 } } @@ -160,9 +160,9 @@ describe('EnsController', () => { it('should clear all ENS entries', () => { const controller = new EnsController(); - expect(controller.set(1, name1, address1)).toBeTruthy(); - expect(controller.set(1, name2, address2)).toBeTruthy(); - expect(controller.set(2, name1, address1)).toBeTruthy(); + expect(controller.set('1', name1, address1)).toBeTruthy(); + expect(controller.set('1', name2, address2)).toBeTruthy(); + expect(controller.set('2', name1, address1)).toBeTruthy(); controller.clear(); expect(controller.state).toEqual({ ensEntries: {} }); });