2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2021-03-07 18:24:04 -05:00
|
|
|
exports.sha256 = exports.keccak256 = exports.pack = void 0;
|
2019-05-14 18:48:48 -04:00
|
|
|
var bignumber_1 = require("@ethersproject/bignumber");
|
|
|
|
var bytes_1 = require("@ethersproject/bytes");
|
|
|
|
var keccak256_1 = require("@ethersproject/keccak256");
|
|
|
|
var sha2_1 = require("@ethersproject/sha2");
|
|
|
|
var strings_1 = require("@ethersproject/strings");
|
|
|
|
var regexBytes = new RegExp("^bytes([0-9]+)$");
|
|
|
|
var regexNumber = new RegExp("^(u?int)([0-9]*)$");
|
|
|
|
var regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$");
|
|
|
|
var Zeros = "0000000000000000000000000000000000000000000000000000000000000000";
|
2021-10-16 02:29:27 -04:00
|
|
|
var logger_1 = require("@ethersproject/logger");
|
|
|
|
var _version_1 = require("./_version");
|
|
|
|
var logger = new logger_1.Logger(_version_1.version);
|
2019-05-14 18:48:48 -04:00
|
|
|
function _pack(type, value, isArray) {
|
|
|
|
switch (type) {
|
|
|
|
case "address":
|
|
|
|
if (isArray) {
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bytes_1.zeroPad)(value, 32);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bytes_1.arrayify)(value);
|
2019-05-14 18:48:48 -04:00
|
|
|
case "string":
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, strings_1.toUtf8Bytes)(value);
|
2019-05-14 18:48:48 -04:00
|
|
|
case "bytes":
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bytes_1.arrayify)(value);
|
2019-05-14 18:48:48 -04:00
|
|
|
case "bool":
|
|
|
|
value = (value ? "0x01" : "0x00");
|
|
|
|
if (isArray) {
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bytes_1.zeroPad)(value, 32);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bytes_1.arrayify)(value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
var match = type.match(regexNumber);
|
|
|
|
if (match) {
|
|
|
|
//let signed = (match[1] === "int")
|
|
|
|
var size = parseInt(match[2] || "256");
|
2020-07-13 08:03:56 -04:00
|
|
|
if ((match[2] && String(size) !== match[2]) || (size % 8 !== 0) || size === 0 || size > 256) {
|
2021-10-16 02:29:27 -04:00
|
|
|
logger.throwArgumentError("invalid number type", "type", type);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
if (isArray) {
|
|
|
|
size = 256;
|
|
|
|
}
|
|
|
|
value = bignumber_1.BigNumber.from(value).toTwos(size);
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bytes_1.zeroPad)(value, size / 8);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
match = type.match(regexBytes);
|
|
|
|
if (match) {
|
|
|
|
var size = parseInt(match[1]);
|
2020-07-13 08:03:56 -04:00
|
|
|
if (String(size) !== match[1] || size === 0 || size > 32) {
|
2021-10-16 02:29:27 -04:00
|
|
|
logger.throwArgumentError("invalid bytes type", "type", type);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
if ((0, bytes_1.arrayify)(value).byteLength !== size) {
|
|
|
|
logger.throwArgumentError("invalid value for " + type, "value", value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
if (isArray) {
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bytes_1.arrayify)((value + Zeros).substring(0, 66));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
match = type.match(regexArray);
|
|
|
|
if (match && Array.isArray(value)) {
|
|
|
|
var baseType_1 = match[1];
|
|
|
|
var count = parseInt(match[2] || String(value.length));
|
|
|
|
if (count != value.length) {
|
2021-10-16 02:29:27 -04:00
|
|
|
logger.throwArgumentError("invalid array length for " + type, "value", value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
var result_1 = [];
|
|
|
|
value.forEach(function (value) {
|
|
|
|
result_1.push(_pack(baseType_1, value, true));
|
|
|
|
});
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bytes_1.concat)(result_1);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
return logger.throwArgumentError("invalid type", "type", type);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
// @TODO: Array Enum
|
|
|
|
function pack(types, values) {
|
|
|
|
if (types.length != values.length) {
|
2021-10-16 02:29:27 -04:00
|
|
|
logger.throwArgumentError("wrong number of values; expected ${ types.length }", "values", values);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
var tight = [];
|
|
|
|
types.forEach(function (type, index) {
|
|
|
|
tight.push(_pack(type, values[index]));
|
|
|
|
});
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bytes_1.hexlify)((0, bytes_1.concat)(tight));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
exports.pack = pack;
|
|
|
|
function keccak256(types, values) {
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, keccak256_1.keccak256)(pack(types, values));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
exports.keccak256 = keccak256;
|
|
|
|
function sha256(types, values) {
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, sha2_1.sha256)(pack(types, values));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
exports.sha256 = sha256;
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=index.js.map
|