ethers.js/packages/hash/src.ts/namehash.ts

60 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-10-19 04:45:32 +03:00
import { concat, hexlify } from "@ethersproject/bytes";
import { toUtf8Bytes } 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);
import { ens_normalize } from "./ens-normalize/lib";
2020-10-19 04:45:32 +03:00
const Zeros = new Uint8Array(32);
Zeros.fill(0);
const Partition = new RegExp("^((.*)\\.)?([^.]+)$");
export function isValidName(name: string): boolean {
try {
return ens_normalize(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") {
logger.throwArgumentError("invalid ENS name; not a string", "name", name);
2020-10-19 04:45:32 +03:00
}
let current = ens_normalize(name);
2020-10-19 04:45:32 +03:00
let result: string | Uint8Array = Zeros;
while (current.length) {
const partition = current.match(Partition);
if (partition == null || partition[2] === "") {
logger.throwArgumentError("invalid ENS address; missing component", "name", name);
}
const label = toUtf8Bytes(partition[3]);
2020-10-19 04:45:32 +03:00
result = keccak256(concat([result, keccak256(label)]));
current = partition[2] || "";
2020-10-19 04:45:32 +03:00
}
return hexlify(result);
}
export function dnsEncode(name: string): string {
name = ens_normalize(name)
return hexlify(concat(name.split(".").map((comp) => {
// DNS does not allow components over 63 bytes in length
if (toUtf8Bytes(comp).length > 63) {
throw new Error("invalid DNS encoded entry; length exceeds 63 bytes");
}
// We jam in an _ prefix to fill in with the length later
const bytes = toUtf8Bytes("_" + comp);
bytes[0] = bytes.length - 1;
return bytes;
}))) + "00";
}