ethers.js/lib.commonjs/hash/namehash.js

91 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2022-09-05 16:57:11 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dnsEncode = exports.namehash = exports.isValidName = exports.ensNormalize = void 0;
2022-09-15 22:58:45 -04:00
const index_js_1 = require("../crypto/index.js");
const index_js_2 = require("../utils/index.js");
2023-02-16 08:19:59 -05:00
const ens_normalize_1 = require("@adraffy/ens-normalize");
2022-09-05 16:57:11 -04:00
const Zeros = new Uint8Array(32);
Zeros.fill(0);
function checkComponent(comp) {
2023-01-26 23:36:26 -05:00
(0, index_js_2.assertArgument)(comp.length !== 0, "invalid ENS name; empty component", "comp", comp);
2022-09-05 16:57:11 -04:00
return comp;
}
function ensNameSplit(name) {
2023-01-28 01:53:29 -05:00
const bytes = (0, index_js_2.toUtf8Bytes)(ensNormalize(name));
2022-09-05 16:57:11 -04:00
const comps = [];
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
2023-01-26 23:36:26 -05:00
(0, index_js_2.assertArgument)(last < bytes.length, "invalid ENS name; empty component", "name", name);
2022-09-05 16:57:11 -04:00
comps.push(checkComponent(bytes.slice(last)));
return comps;
}
2023-01-28 01:53:29 -05:00
/**
* Returns the ENS %%name%% normalized.
*/
2022-09-05 16:57:11 -04:00
function ensNormalize(name) {
2023-01-28 01:53:29 -05:00
try {
2023-10-10 22:41:46 -04:00
if (name.length === 0) {
throw new Error("empty label");
}
2023-02-16 08:19:59 -05:00
return (0, ens_normalize_1.ens_normalize)(name);
2023-01-28 01:53:29 -05:00
}
catch (error) {
(0, index_js_2.assertArgument)(false, `invalid ENS name (${error.message})`, "name", name);
}
2022-09-05 16:57:11 -04:00
}
exports.ensNormalize = ensNormalize;
2023-01-28 01:53:29 -05:00
/**
* Returns ``true`` if %%name%% is a valid ENS name.
*/
2022-09-05 16:57:11 -04:00
function isValidName(name) {
try {
return (ensNameSplit(name).length !== 0);
}
catch (error) { }
return false;
}
exports.isValidName = isValidName;
2023-01-28 01:53:29 -05:00
/**
* Returns the [[link-namehash]] for %%name%%.
*/
2022-09-05 16:57:11 -04:00
function namehash(name) {
2022-11-09 02:57:02 -05:00
(0, index_js_2.assertArgument)(typeof (name) === "string", "invalid ENS name; not a string", "name", name);
2023-10-10 22:41:46 -04:00
(0, index_js_2.assertArgument)(name.length, `invalid ENS name (empty label)`, "name", name);
2022-09-05 16:57:11 -04:00
let result = Zeros;
const comps = ensNameSplit(name);
while (comps.length) {
2022-09-15 22:58:45 -04:00
result = (0, index_js_1.keccak256)((0, index_js_2.concat)([result, (0, index_js_1.keccak256)((comps.pop()))]));
2022-09-05 16:57:11 -04:00
}
2022-09-15 22:58:45 -04:00
return (0, index_js_2.hexlify)(result);
2022-09-05 16:57:11 -04:00
}
exports.namehash = namehash;
2023-01-28 01:53:29 -05:00
/**
* Returns the DNS encoded %%name%%.
*
* This is used for various parts of ENS name resolution, such
* as the wildcard resolution.
*/
2024-02-02 03:25:03 -05:00
function dnsEncode(name, _maxLength) {
const length = (_maxLength != null) ? _maxLength : 63;
(0, index_js_2.assertArgument)(length <= 255, "DNS encoded label cannot exceed 255", "length", length);
2022-09-15 22:58:45 -04:00
return (0, index_js_2.hexlify)((0, index_js_2.concat)(ensNameSplit(name).map((comp) => {
2024-02-02 03:25:03 -05:00
(0, index_js_2.assertArgument)(comp.length <= length, `label ${JSON.stringify(name)} exceeds ${length} bytes`, "name", name);
2022-09-05 16:57:11 -04:00
const bytes = new Uint8Array(comp.length + 1);
bytes.set(comp, 1);
bytes[0] = bytes.length - 1;
return bytes;
}))) + "00";
}
exports.dnsEncode = dnsEncode;
//# sourceMappingURL=namehash.js.map