Skip to content

Commit

Permalink
fix: handle error when the ens is not valid
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Jul 28, 2024
1 parent 04b881d commit a7758ba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
validateSchema,
getScores,
getVp,
getFormattedAddress
getFormattedAddress,
getEnsOwner,
getEnsTextRecord
} from './utils';

vi.mock('cross-fetch', async () => {
Expand Down Expand Up @@ -611,4 +613,18 @@ describe('utils', () => {
expect(result).not.toBe(true);
});
});

describe('getEnsOwner', () => {
test('should return null when the ENS is not valid', () => {
// special hidden characters after the k
expect(getEnsOwner('elonmusk‍‍.eth')).resolves.toBe(null);
});
});

describe('getEnsTextRecord', () => {
test('should return null when the ENS is not valid', () => {
// special hidden characters after the k
expect(getEnsTextRecord('elonmusk‍‍.eth')).resolves.toBe(null);
});
});
});
19 changes: 17 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,14 @@ export async function getEnsTextRecord(
...multicallOptions
} = options;

const ensHash = namehash(ensNormalize(ens));
let ensHash: string;

try {
ensHash = namehash(ensNormalize(ens));
} catch (e: any) {
return null;
}

const provider = getProvider(network, { broviderUrl });

const calls = [
Expand Down Expand Up @@ -592,9 +599,17 @@ export async function getEnsOwner(
['function owner(bytes32) view returns (address)'],
provider
);

let ensHash: string;

try {
ensHash = namehash(ensNormalize(ens));
} catch (e: any) {
return null;
}

const ensNameWrapper =
options.ensNameWrapper || networks[network].ensNameWrapper;
const ensHash = namehash(ensNormalize(ens));
let owner = await ensRegistry.owner(ensHash);
// If owner is the ENSNameWrapper contract, resolve the owner of the name
if (owner === ensNameWrapper) {
Expand Down

0 comments on commit a7758ba

Please sign in to comment.