Skip to content

Commit

Permalink
migrate from eth-ens-namehash to ens-normalize lib (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanrikulu committed Apr 19, 2023
1 parent 2bc0ba7 commit 16eed14
Show file tree
Hide file tree
Showing 5 changed files with 669 additions and 38 deletions.
4 changes: 2 additions & 2 deletions mock/entry.mock.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { namehash } from '@ensdomains/ensjs/utils/normalise';
import { utils } from 'ethers';
import nock from 'nock';
import { Version } from '../src/base';
Expand All @@ -17,7 +18,6 @@ import {
WrappedDomainResponse,
} from './interface';

const namehash = require('@ensdomains/eth-ens-namehash'); // no types

const { SUBGRAPH_URL: subgraph_url } = getNetwork('goerli');
const SUBGRAPH_URL = new URL(subgraph_url);
Expand Down Expand Up @@ -46,7 +46,7 @@ export class MockEntry {
}: MockEntryBody) {
if (!name) throw Error('There must be a valid name.');
this.name = name;
this.namehash = namehash.hash(name);
this.namehash = namehash(name);

if (!registered) {
this.expect = 'No results found.';
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"node": ">=16.15"
},
"dependencies": {
"@adraffy/ens-normalize": "^1.9.0",
"@ensdomains/ens-avatar": "^0.2.4",
"@ensdomains/ens-validation": "^0.1.0",
"@ensdomains/eth-ens-namehash": "^2.0.15",
"@ensdomains/ensjs": "^3.0.0-alpha.61",
"@types/lodash": "^4.14.170",
"btoa": "^1.2.1",
"canvas": "^2.8.0",
Expand Down
19 changes: 8 additions & 11 deletions src/service/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {ens_normalize} from '@adraffy/ens-normalize';
import { Version } from '../base';
import {
CANVAS_FONT_PATH,
Expand All @@ -9,10 +10,7 @@ import { isASCII, findCharacterSet } from '../utils/characterSet';
import { getCodePointLength, getSegmentLength } from '../utils/charLength';

// no ts declaration files

const { createCanvas, registerFont } = require('canvas');
const namehash = require('@ensdomains/eth-ens-namehash');
const { validate } = require('@ensdomains/ens-validation');


try {
Expand Down Expand Up @@ -62,10 +60,9 @@ export class Metadata {
last_request_date
}: MetadataInit) {
const label = this.getLabel(name);
const is_valid = validate(name);
this.is_normalized = is_valid && this._checkNormalized(name);
this.is_normalized = this._checkNormalized(name);
this.name = this.formatName(name, tokenId);
this.description = this.formatDescription(name, description, is_valid);
this.description = this.formatDescription(name, description);
this.attributes = this.initializeAttributes(created_date, label);
this.url = this.is_normalized ? `https://app.ens.domains/name/${name}` : null;
this.last_request_date = last_request_date;
Expand All @@ -85,15 +82,15 @@ export class Metadata {
);
}

formatDescription(name: string, description?: string, is_valid?: boolean) {
formatDescription(name: string, description?: string) {
const baseDescription = description || `${this.name}, an ENS name.`;
const normalizedNote = !this.is_normalized ? ` (${name} is not in normalized form)` : '';
const asciiWarning = this.generateAsciiWarning(this.getLabel(name), is_valid);
const asciiWarning = this.generateAsciiWarning(this.getLabel(name));
return `${baseDescription}${normalizedNote}${asciiWarning}`;
}

generateAsciiWarning(label: string, is_valid?: boolean) {
if (!is_valid || !isASCII(label)) {
generateAsciiWarning(label: string) {
if (!isASCII(label)) {
return ' ⚠️ ATTENTION: This name contains non-ASCII characters as shown above. ' +
'Please be aware that there are characters that look identical or very ' +
'similar to English letters, especially characters from Cyrillic and Greek. ' +
Expand Down Expand Up @@ -256,7 +253,7 @@ export class Metadata {
private _checkNormalized(name: string) {
// this method can be used to filter many informal name types
try {
return name === namehash.normalize(name);
return name === ens_normalize(name);
} catch {
return false;
}
Expand Down
13 changes: 8 additions & 5 deletions src/utils/namehash.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { namehash } from '@ensdomains/ensjs/utils/normalise';
import { utils, BigNumber, ethers } from 'ethers';
import { Version } from '../base';
import { Version } from '../base';

const sha3 = require('js-sha3').keccak_256;
const namehash = require('@ensdomains/eth-ens-namehash');
const sha3 = require('js-sha3').keccak_256;

const eth0x =
'4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0';

export function constructEthNameHash(tokenId: string, version: Version): string {
export function constructEthNameHash(
tokenId: string,
version: Version
): string {
if (version > Version.v1) return tokenId;

const label0x = utils
Expand Down Expand Up @@ -37,6 +40,6 @@ export function getNamehash(nameOrNamehash: string) {
return nameOrNamehash;
}

const _lhexId = namehash.hash(nameOrNamehash);
const _lhexId = namehash(nameOrNamehash);
return _lhexId;
}
Loading

0 comments on commit 16eed14

Please sign in to comment.