2020-10-19 06:19:16 +03:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2022-03-09 10:56:08 +03:00
|
|
|
exports.dnsEncode = exports.namehash = exports.isValidName = 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);
|
|
|
|
var Zeros = new Uint8Array(32);
|
|
|
|
Zeros.fill(0);
|
|
|
|
var Partition = new RegExp("^((.*)\\.)?([^.]+)$");
|
|
|
|
function isValidName(name) {
|
|
|
|
try {
|
|
|
|
var comps = name.split(".");
|
|
|
|
for (var i = 0; i < comps.length; i++) {
|
2021-10-16 09:29:27 +03:00
|
|
|
if ((0, strings_1.nameprep)(comps[i]).length === 0) {
|
2020-10-19 06:19:16 +03:00
|
|
|
throw new Error("empty");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2021-06-01 02:06:24 +03:00
|
|
|
var current = name;
|
2020-10-19 06:19:16 +03:00
|
|
|
var result = Zeros;
|
2021-06-01 02:06:24 +03:00
|
|
|
while (current.length) {
|
|
|
|
var partition = current.match(Partition);
|
|
|
|
if (partition == null || partition[2] === "") {
|
|
|
|
logger.throwArgumentError("invalid ENS address; missing component", "name", name);
|
|
|
|
}
|
2021-10-16 09:29:27 +03:00
|
|
|
var label = (0, strings_1.toUtf8Bytes)((0, strings_1.nameprep)(partition[3]));
|
|
|
|
result = (0, keccak256_1.keccak256)((0, bytes_1.concat)([result, (0, keccak256_1.keccak256)(label)]));
|
2021-06-01 02:06:24 +03:00
|
|
|
current = partition[2] || "";
|
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) {
|
|
|
|
return (0, bytes_1.hexlify)((0, bytes_1.concat)(name.split(".").map(function (comp) {
|
|
|
|
// We jam in an _ prefix to fill in with the length later
|
|
|
|
// Note: Nameprep throws if the component is over 63 bytes
|
|
|
|
var bytes = (0, strings_1.toUtf8Bytes)("_" + (0, strings_1.nameprep)(comp));
|
|
|
|
bytes[0] = bytes.length - 1;
|
|
|
|
return bytes;
|
|
|
|
}))) + "00";
|
|
|
|
}
|
|
|
|
exports.dnsEncode = dnsEncode;
|
2020-10-19 06:19:16 +03:00
|
|
|
//# sourceMappingURL=namehash.js.map
|