ethers.js/utils/hash.js

58 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2018-06-18 05:42:41 -04:00
'use strict';
2018-12-12 16:59:25 -05:00
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;
};
2018-06-18 05:42:41 -04:00
Object.defineProperty(exports, "__esModule", { value: true });
2018-12-12 16:59:25 -05:00
var errors = __importStar(require("../errors"));
2018-06-18 05:42:41 -04:00
var bytes_1 = require("./bytes");
var utf8_1 = require("./utf8");
var keccak256_1 = require("./keccak256");
///////////////////////////////
2018-06-18 05:42:41 -04:00
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("^((.*)\\.)?([^.]+)$");
var UseSTD3ASCIIRules = new RegExp("^[a-z0-9.-]*$");
function namehash(name) {
2018-12-12 16:59:25 -05:00
if (typeof (name) !== 'string') {
errors.throwError('invalid address - ' + String(name), errors.INVALID_ARGUMENT, {
argument: 'name',
value: name
});
}
2018-06-18 05:42:41 -04:00
name = name.toLowerCase();
// Supporting the full UTF-8 space requires additional (and large)
// libraries, so for now we simply do not support them.
// It should be fairly easy in the future to support systems with
// String.normalize, but that is future work.
if (!name.match(UseSTD3ASCIIRules)) {
2018-12-12 16:59:25 -05:00
errors.throwError('contains invalid UseSTD3ASCIIRules characters', errors.INVALID_ARGUMENT, {
argument: 'name',
value: name
});
2018-06-18 05:42:41 -04:00
}
var result = Zeros;
while (name.length) {
var partition = name.match(Partition);
var label = utf8_1.toUtf8Bytes(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(utf8_1.toUtf8Bytes(text));
}
exports.id = id;
function hashMessage(message) {
2018-12-12 16:59:25 -05:00
return keccak256_1.keccak256(bytes_1.concat([
2018-06-18 05:42:41 -04:00
utf8_1.toUtf8Bytes('\x19Ethereum Signed Message:\n'),
utf8_1.toUtf8Bytes(String(message.length)),
((typeof (message) === 'string') ? utf8_1.toUtf8Bytes(message) : message)
2018-12-12 16:59:25 -05:00
]));
2018-06-18 05:42:41 -04:00
}
exports.hashMessage = hashMessage;