ethers.js/packages/hash/index.js

64 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-05-15 01:48:48 +03:00
"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("^((.*)\\.)?([^.]+)$");
2019-07-28 01:02:24 +03:00
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;
2019-05-15 01:48:48 +03:00
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);
2019-07-28 01:02:24 +03:00
var label = strings_1.toUtf8Bytes(strings_1.nameprep(partition[3]));
2019-05-15 01:48:48 +03:00
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;