From a7758bab48e42e174e877e47be8d6243a91eecd1 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Sun, 28 Jul 2024 12:56:09 +0900 Subject: [PATCH] fix: handle error when the ens is not valid --- src/utils.spec.js | 18 +++++++++++++++++- src/utils.ts | 19 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/utils.spec.js b/src/utils.spec.js index f1f35941a..afeb14a83 100644 --- a/src/utils.spec.js +++ b/src/utils.spec.js @@ -5,7 +5,9 @@ import { validateSchema, getScores, getVp, - getFormattedAddress + getFormattedAddress, + getEnsOwner, + getEnsTextRecord } from './utils'; vi.mock('cross-fetch', async () => { @@ -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); + }); + }); }); diff --git a/src/utils.ts b/src/utils.ts index 2feda326e..4990db06b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 = [ @@ -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) {