ethers.js/packages/hash/lib/namehash.js
2020-10-18 23:19:16 -04:00

41 lines
1.4 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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++) {
if (strings_1.nameprep(comps[i]).length === 0) {
throw new Error("empty");
}
}
return true;
}
catch (error) { }
return false;
}
exports.isValidName = isValidName;
function namehash(name) {
/* istanbul ignore if */
if (typeof (name) !== "string") {
logger.throwArgumentError("invalid address - " + String(name), "name", name);
}
var result = Zeros;
while (name.length) {
var partition = name.match(Partition);
var label = strings_1.toUtf8Bytes(strings_1.nameprep(partition[3]));
result = keccak256_1.keccak256(bytes_1.concat([result, keccak256_1.keccak256(label)]));
name = partition[2] || "";
}
return bytes_1.hexlify(result);
}
exports.namehash = namehash;
//# sourceMappingURL=namehash.js.map