ethers.js/packages/hash/lib/namehash.js

79 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-10-19 06:19:16 +03:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2022-08-18 21:48:39 +03:00
exports.dnsEncode = exports.namehash = exports.isValidName = exports.ensNormalize = void 0;
2020-10-19 06:19:16 +03:00
var bytes_1 = require("@ethersproject/bytes");
var strings_1 = require("@ethersproject/strings");
var keccak256_1 = require("@ethersproject/keccak256");
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2022-08-18 21:48:39 +03:00
var lib_1 = require("./ens-normalize/lib");
2020-10-19 06:19:16 +03:00
var Zeros = new Uint8Array(32);
Zeros.fill(0);
2022-08-18 21:48:39 +03:00
function checkComponent(comp) {
if (comp.length === 0) {
throw new Error("invalid ENS name; empty component");
}
return comp;
}
function ensNameSplit(name) {
var bytes = (0, strings_1.toUtf8Bytes)((0, lib_1.ens_normalize)(name));
var comps = [];
if (name.length === 0) {
return comps;
}
var last = 0;
for (var i = 0; i < bytes.length; i++) {
var 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;
}
function ensNormalize(name) {
return ensNameSplit(name).map(function (comp) { return (0, strings_1.toUtf8String)(comp); }).join(".");
}
exports.ensNormalize = ensNormalize;
2020-10-19 06:19:16 +03:00
function isValidName(name) {
try {
2022-08-18 21:48:39 +03:00
return (ensNameSplit(name).length !== 0);
2020-10-19 06:19:16 +03:00
}
catch (error) { }
return false;
}
exports.isValidName = isValidName;
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
}
var result = Zeros;
2022-08-18 21:48:39 +03:00
var comps = ensNameSplit(name);
while (comps.length) {
result = (0, keccak256_1.keccak256)((0, bytes_1.concat)([result, (0, keccak256_1.keccak256)(comps.pop())]));
2020-10-19 06:19:16 +03:00
}
2021-10-16 09:29:27 +03:00
return (0, bytes_1.hexlify)(result);
2020-10-19 06:19:16 +03:00
}
exports.namehash = namehash;
2022-03-09 10:56:08 +03:00
function dnsEncode(name) {
2022-08-18 21:48:39 +03:00
return (0, bytes_1.hexlify)((0, bytes_1.concat)(ensNameSplit(name).map(function (comp) {
// DNS does not allow components over 63 bytes in length
if (comp.length > 63) {
throw new Error("invalid DNS encoded entry; length exceeds 63 bytes");
}
var bytes = new Uint8Array(comp.length + 1);
bytes.set(comp, 1);
2022-03-09 10:56:08 +03:00
bytes[0] = bytes.length - 1;
return bytes;
}))) + "00";
}
exports.dnsEncode = dnsEncode;
2020-10-19 06:19:16 +03:00
//# sourceMappingURL=namehash.js.map