Skip to content

Commit

Permalink
fix tests and hashing to achieve parity between contracts and ens lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Whytecrowe committed Jun 21, 2023
1 parent bebea1a commit ba3ea3e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
23 changes: 13 additions & 10 deletions test/ZNSRegistrar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import {
deployZNS,
hashDomainLabel,
INVALID_TOKENID_ERC_ERR,
INVALID_TOKENID_ERC_ERR, normalizeName,
NOT_AUTHORIZED_REG_ERR,
NOT_NAME_OWNER_RAR_ERR, NOT_TOKEN_OWNER_RAR_ERR,
ONLY_NAME_OWNER_REG_ERR,
Expand Down Expand Up @@ -35,7 +35,7 @@ describe("ZNSRegistrar", () => {
let zns : ZNSContracts;
let zeroVault : SignerWithAddress;
let operator : SignerWithAddress;
const defaultDomain = "wilder";
const defaultDomain = normalizeName("wilder");

beforeEach(async () => {
[deployer, zeroVault, user, operator, governor, admin, randomUser] = await hre.ethers.getSigners();
Expand Down Expand Up @@ -158,20 +158,23 @@ describe("ZNSRegistrar", () => {
await expect(tx).to.be.revertedWith("ERC20: transfer amount exceeds balance");
});

it("Allows unicode characters in domain names", async () => {
const unicodeDomain = "œ柸þ€§ᆰ";
// eslint-disable-next-line max-len
it("Allows unicode characters in domain names and matches the hash of normalized string acquired from namehash library", async () => {
const unicodeDomainLabel = "œ柸þ€§ᆰ";

const tx = await defaultRegistration(user, zns, unicodeDomain);
const normalizedDomainLabel = normalizeName(unicodeDomainLabel);

const tx = await defaultRegistration(user, zns, normalizedDomainLabel);

const domainHash = await getDomainHashFromEvent(tx);
// validate that namehash lib works the same way as our contract hashing
// TODO: figure out why these do not match! For some reason contract returns
// a different domain from ensjs.
// const namehashRef = hashDomainLabel(unicodeDomain);
// expect(domainHash).to.eq(namehashRef);
// TODO: a security issue with namehash lib is the usage of non-ASCII characters
// this should be handled at the SDK/dApp level!
const namehashRef = hashDomainLabel(unicodeDomainLabel);
expect(domainHash).to.eq(namehashRef);
expect(await zns.registry.exists(domainHash)).to.be.true;

const expectedStaked = await getPrice(unicodeDomain, zns.priceOracle);
const expectedStaked = await getPrice(normalizedDomainLabel, zns.priceOracle);
const staked = await zns.treasury.stakedForDomain(domainHash);
expect(expectedStaked).to.eq(staked);
});
Expand Down
12 changes: 7 additions & 5 deletions test/helpers/hashing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
/* eslint-disable @typescript-eslint/no-var-requires */
const ensjs = require("@ensdomains/ensjs");
const namehash = require("eth-ens-namehash");


export const normalizeName = (name : string) => namehash.normalize(name);

/**
* The ens lib takes the inverse of our domain name format to
Expand All @@ -19,12 +23,10 @@ export const reverseInputName = (name : string) => {
export const hashSubdomainName = (name : string) => {
// ens namehash lib expects child.parent for hashing algorithm as opposed to our format: parent.child
const reversedInputName = reverseInputName(name);
const hashedName = ensjs.namehash(reversedInputName);

return hashedName;
return ensjs.namehash(reversedInputName);
};

/**
* Hashes last name label only.
*/
export const hashDomainLabel = (label : string) => ensjs.labelhash(label);
export const hashDomainLabel = (label : string) => ensjs.labelhash(label);

0 comments on commit ba3ea3e

Please sign in to comment.