2020-10-19 04:45:32 +03:00
|
|
|
import { concat, hexlify } from "@ethersproject/bytes";
|
2022-08-16 05:45:19 +03:00
|
|
|
import { toUtf8Bytes, toUtf8String } from "@ethersproject/strings";
|
2020-10-19 04:45:32 +03:00
|
|
|
import { keccak256 } from "@ethersproject/keccak256";
|
|
|
|
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2022-08-14 23:01:06 +03:00
|
|
|
import { ens_normalize } from "./ens-normalize/lib";
|
|
|
|
|
2020-10-19 04:45:32 +03:00
|
|
|
const Zeros = new Uint8Array(32);
|
|
|
|
Zeros.fill(0);
|
|
|
|
|
2022-08-16 05:45:19 +03:00
|
|
|
function checkComponent(comp: Uint8Array): Uint8Array {
|
|
|
|
if (comp.length === 0) { throw new Error("invalid ENS name; empty component"); }
|
|
|
|
let nonUnder = false;
|
|
|
|
let last = -1;
|
|
|
|
for (let i = 0; i < comp.length; i++) {
|
|
|
|
const c = comp[i];
|
|
|
|
|
|
|
|
// An underscore (i.e. "_"); only allows at the beginning
|
|
|
|
if (c === 0x5f) {
|
|
|
|
if (nonUnder) { throw new Error("invalid ENS name; non-prefix underscore"); }
|
|
|
|
} else {
|
|
|
|
// A hyphen (i.e. "-"); only allows a single in a row
|
|
|
|
if (c === 0x2d && last === c) {
|
|
|
|
throw new Error("invalid ENS name; double-hyphen");
|
|
|
|
}
|
|
|
|
nonUnder = true;
|
|
|
|
}
|
|
|
|
last = c;
|
|
|
|
}
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensNameSplit(name: string): Array<Uint8Array> {
|
|
|
|
const bytes = toUtf8Bytes(ens_normalize(name));
|
|
|
|
const comps: Array<Uint8Array> = [ ];
|
|
|
|
|
|
|
|
if (name.length === 0) { return comps; }
|
|
|
|
|
|
|
|
let last = 0;
|
|
|
|
for (let i = 0; i < bytes.length; i++) {
|
|
|
|
const d = bytes[i];
|
|
|
|
|
|
|
|
// A separator (i.e. "."); copy this component
|
|
|
|
if (d === 0x2e) {
|
|
|
|
comps.push(checkComponent(bytes.slice(last, i)));
|
|
|
|
last = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There was a stray separator at the end of the name
|
|
|
|
if (last >= bytes.length) { throw new Error("invalid ENS name; empty component"); }
|
|
|
|
|
|
|
|
comps.push(checkComponent(bytes.slice(last)));
|
|
|
|
return comps;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ensNormalize(name: string): string {
|
|
|
|
return ensNameSplit(name).map((comp) => toUtf8String(comp)).join(".");
|
|
|
|
}
|
2020-10-19 04:45:32 +03:00
|
|
|
|
|
|
|
export function isValidName(name: string): boolean {
|
|
|
|
try {
|
2022-08-16 05:45:19 +03:00
|
|
|
return (ensNameSplit(name).length !== 0);
|
2020-10-19 04:45:32 +03:00
|
|
|
} catch (error) { }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function namehash(name: string): string {
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (typeof(name) !== "string") {
|
2021-06-01 01:16:30 +03:00
|
|
|
logger.throwArgumentError("invalid ENS name; not a string", "name", name);
|
2020-10-19 04:45:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let result: string | Uint8Array = Zeros;
|
|
|
|
|
2022-08-16 05:45:19 +03:00
|
|
|
const comps = ensNameSplit(name);
|
|
|
|
while (comps.length) {
|
|
|
|
result = keccak256(concat([result, keccak256(comps.pop())]));
|
2020-10-19 04:45:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return hexlify(result);
|
|
|
|
}
|
|
|
|
|
2022-02-19 02:15:03 +03:00
|
|
|
export function dnsEncode(name: string): string {
|
2022-08-16 05:45:19 +03:00
|
|
|
return hexlify(concat(ensNameSplit(name).map((comp) => {
|
2022-08-14 23:01:06 +03:00
|
|
|
// DNS does not allow components over 63 bytes in length
|
2022-08-16 05:45:19 +03:00
|
|
|
if (comp.length > 63) {
|
2022-08-14 23:01:06 +03:00
|
|
|
throw new Error("invalid DNS encoded entry; length exceeds 63 bytes");
|
|
|
|
}
|
|
|
|
|
2022-08-16 05:45:19 +03:00
|
|
|
const bytes = new Uint8Array(comp.length + 1);
|
|
|
|
bytes.set(comp, 1);
|
2022-02-19 02:15:03 +03:00
|
|
|
bytes[0] = bytes.length - 1;
|
|
|
|
return bytes;
|
2022-08-16 05:45:19 +03:00
|
|
|
|
2022-02-19 02:15:03 +03:00
|
|
|
}))) + "00";
|
|
|
|
}
|
2022-08-16 05:45:19 +03:00
|
|
|
|