Skip to content

Commit

Permalink
change chainId type to string, update types
Browse files Browse the repository at this point in the history
  • Loading branch information
rekmarks committed Sep 3, 2019
1 parent 9420e7b commit d57c54b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 47 deletions.
26 changes: 10 additions & 16 deletions src/third-party/EnsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -75,12 +64,12 @@ export class EnsController extends BaseController<BaseConfig, EnsState> {
* @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) {
Expand All @@ -99,8 +88,13 @@ export class EnsController extends BaseController<BaseConfig, EnsState> {
* @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}}`);
}

Expand Down
62 changes: 31 additions & 31 deletions tests/EnsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand All @@ -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: {} });
});
Expand Down

0 comments on commit d57c54b

Please sign in to comment.