diff --git a/test/ZNSRegistrar.test.ts b/test/ZNSRegistrar.test.ts index af4950e76..fe5be88f0 100644 --- a/test/ZNSRegistrar.test.ts +++ b/test/ZNSRegistrar.test.ts @@ -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, @@ -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(); @@ -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); }); diff --git a/test/helpers/hashing.ts b/test/helpers/hashing.ts index 2e894dc8f..bdf8b72d3 100644 --- a/test/helpers/hashing.ts +++ b/test/helpers/hashing.ts @@ -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 @@ -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); \ No newline at end of file +export const hashDomainLabel = (label : string) => ensjs.labelhash(label);