Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Commit

Permalink
Fixed ENS namehash with leading and trailing dots (ethers-io#1605).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo authored and pull[bot] committed Jun 4, 2021
1 parent 9729201 commit 4f95818
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
12 changes: 8 additions & 4 deletions packages/hash/src.ts/namehash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ export function isValidName(name: string): boolean {
export function namehash(name: string): string {
/* istanbul ignore if */
if (typeof(name) !== "string") {
logger.throwArgumentError("invalid address - " + String(name), "name", name);
logger.throwArgumentError("invalid ENS name; not a string", "name", name);
}

let current = name;
let result: string | Uint8Array = Zeros;
while (name.length) {
const partition = name.match(Partition);
while (current.length) {
const partition = current.match(Partition);
if (partition == null || partition[2] === "") {
logger.throwArgumentError("invalid ENS address; missing component", "name", name);
}
const label = toUtf8Bytes(nameprep(partition[3]));
result = keccak256(concat([result, keccak256(label)]));

name = partition[2] || "";
current = partition[2] || "";
}

return hexlify(result);
Expand Down
39 changes: 36 additions & 3 deletions packages/tests/src.ts/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,44 @@ describe('Test Namehash', function() {
});
});

it("isValidName", function() {
assert.ok(ethers.utils.isValidName("ricmoo.eth"));
const goodNames = [
"ricmoo.eth",
"foo",
"foo.bar",
];

const badNames = [
".",
"..",
"ricmoo..eth",
"ricmoo...eth",
".foo",
"foo.",
];

// The empty string is not a valid name, but has the zero hash
// as its namehash, which may be used for recursive purposes
it("empty ENS name", function() {
assert.ok(!ethers.utils.isValidName(""));
assert.ok(!ethers.utils.isValidName("ricmoo..eth"));
});

goodNames.forEach((name) => {
it(`ENS namehash ok - ${ name }`, function() {
assert.ok(ethers.utils.isValidName(name));
ethers.utils.namehash(name);
});
});

badNames.forEach((name) => {
it(`ENS namehash fails - ${ name }`, function() {
assert.ok(!ethers.utils.isValidName(name));
assert.throws(() => {
const namehash = ethers.utils.namehash(name);
console.log(name, namehash);
}, (error: Error) => {
return !!error.message.match(/invalid ENS address/);
});
});
});
});

Expand Down

0 comments on commit 4f95818

Please sign in to comment.