2020-10-19 06:19:16 +03:00
|
|
|
import { concat, hexlify } from "@ethersproject/bytes";
|
|
|
|
import { nameprep, toUtf8Bytes } from "@ethersproject/strings";
|
|
|
|
import { keccak256 } from "@ethersproject/keccak256";
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
const Zeros = new Uint8Array(32);
|
|
|
|
Zeros.fill(0);
|
|
|
|
const Partition = new RegExp("^((.*)\\.)?([^.]+)$");
|
|
|
|
export function isValidName(name) {
|
|
|
|
try {
|
|
|
|
const comps = name.split(".");
|
|
|
|
for (let i = 0; i < comps.length; i++) {
|
|
|
|
if (nameprep(comps[i]).length === 0) {
|
|
|
|
throw new Error("empty");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
export function namehash(name) {
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (typeof (name) !== "string") {
|
2021-06-01 02:06:24 +03:00
|
|
|
logger.throwArgumentError("invalid ENS name; not a string", "name", name);
|
2020-10-19 06:19:16 +03:00
|
|
|
}
|
2021-06-01 02:06:24 +03:00
|
|
|
let current = name;
|
2020-10-19 06:19:16 +03:00
|
|
|
let result = Zeros;
|
2021-06-01 02:06:24 +03:00
|
|
|
while (current.length) {
|
|
|
|
const partition = current.match(Partition);
|
|
|
|
if (partition == null || partition[2] === "") {
|
|
|
|
logger.throwArgumentError("invalid ENS address; missing component", "name", name);
|
|
|
|
}
|
2020-10-19 06:19:16 +03:00
|
|
|
const label = toUtf8Bytes(nameprep(partition[3]));
|
|
|
|
result = keccak256(concat([result, keccak256(label)]));
|
2021-06-01 02:06:24 +03:00
|
|
|
current = partition[2] || "";
|
2020-10-19 06:19:16 +03:00
|
|
|
}
|
|
|
|
return hexlify(result);
|
|
|
|
}
|
2022-03-09 10:56:08 +03:00
|
|
|
export function dnsEncode(name) {
|
|
|
|
return hexlify(concat(name.split(".").map((comp) => {
|
|
|
|
// We jam in an _ prefix to fill in with the length later
|
|
|
|
// Note: Nameprep throws if the component is over 63 bytes
|
|
|
|
const bytes = toUtf8Bytes("_" + nameprep(comp));
|
|
|
|
bytes[0] = bytes.length - 1;
|
|
|
|
return bytes;
|
|
|
|
}))) + "00";
|
|
|
|
}
|
2020-10-19 06:19:16 +03:00
|
|
|
//# sourceMappingURL=namehash.js.map
|