ethers.js/packages/hash/index.js
2019-07-27 19:02:24 -03:00

64 lines
2.2 KiB
JavaScript

"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var errors = __importStar(require("@ethersproject/errors"));
var bytes_1 = require("@ethersproject/bytes");
var strings_1 = require("@ethersproject/strings");
var keccak256_1 = require("@ethersproject/keccak256");
///////////////////////////////
var Zeros = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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) {
if (typeof (name) !== "string") {
errors.throwError("invalid address - " + String(name), errors.INVALID_ARGUMENT, {
argument: "name",
value: 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;
function id(text) {
return keccak256_1.keccak256(strings_1.toUtf8Bytes(text));
}
exports.id = id;
exports.messagePrefix = "\x19Ethereum Signed Message:\n";
function hashMessage(message) {
if (typeof (message) === "string") {
message = strings_1.toUtf8Bytes(message);
}
return keccak256_1.keccak256(bytes_1.concat([
strings_1.toUtf8Bytes(exports.messagePrefix),
strings_1.toUtf8Bytes(String(message.length)),
message
]));
}
exports.hashMessage = hashMessage;