Skip to content

Commit

Permalink
fix: handle error when the ens is not valid (#1041)
Browse files Browse the repository at this point in the history
* fix: handle error when the ens is not valid

* v0.12.3

---------

Co-authored-by: Chaitanya <[email protected]>
  • Loading branch information
wa0x6e and ChaituVR authored Jul 29, 2024
1 parent 04b881d commit 2157c96
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snapshot-labs/snapshot.js",
"version": "0.12.2",
"version": "0.12.3",
"repository": "snapshot-labs/snapshot.js",
"license": "MIT",
"main": "dist/snapshot.cjs.js",
Expand Down
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 2157c96

Please sign in to comment.