ethers.js/lib.commonjs/hash/solidity.js

102 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-09-05 23:57:11 +03:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2022-09-16 05:58:45 +03:00
exports.solidityPackedSha256 = exports.solidityPackedKeccak256 = exports.solidityPacked = void 0;
const index_js_1 = require("../crypto/index.js");
const index_js_2 = require("../utils/index.js");
2022-09-05 23:57:11 +03:00
const regexBytes = new RegExp("^bytes([0-9]+)$");
const regexNumber = new RegExp("^(u?int)([0-9]*)$");
const regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$");
function _pack(type, value, isArray) {
switch (type) {
case "address":
if (isArray) {
2022-09-16 05:58:45 +03:00
return (0, index_js_2.getBytes)((0, index_js_2.zeroPadValue)(value, 32));
2022-09-05 23:57:11 +03:00
}
2022-09-16 05:58:45 +03:00
return (0, index_js_2.getBytes)(value);
2022-09-05 23:57:11 +03:00
case "string":
2022-09-16 05:58:45 +03:00
return (0, index_js_2.toUtf8Bytes)(value);
2022-09-05 23:57:11 +03:00
case "bytes":
2022-09-16 05:58:45 +03:00
return (0, index_js_2.getBytes)(value);
2022-09-05 23:57:11 +03:00
case "bool":
value = (!!value ? "0x01" : "0x00");
if (isArray) {
2022-09-16 05:58:45 +03:00
return (0, index_js_2.getBytes)((0, index_js_2.zeroPadValue)(value, 32));
2022-09-05 23:57:11 +03:00
}
2022-09-16 05:58:45 +03:00
return (0, index_js_2.getBytes)(value);
2022-09-05 23:57:11 +03:00
}
let match = type.match(regexNumber);
if (match) {
let size = parseInt(match[2] || "256");
if ((match[2] && String(size) !== match[2]) || (size % 8 !== 0) || size === 0 || size > 256) {
2022-09-16 05:58:45 +03:00
return (0, index_js_2.throwArgumentError)("invalid number type", "type", type);
2022-09-05 23:57:11 +03:00
}
if (isArray) {
size = 256;
}
2022-09-16 05:58:45 +03:00
value = (0, index_js_2.toTwos)(value, size);
return (0, index_js_2.getBytes)((0, index_js_2.zeroPadValue)((0, index_js_2.toArray)(value), size / 8));
2022-09-05 23:57:11 +03:00
}
match = type.match(regexBytes);
if (match) {
const size = parseInt(match[1]);
if (String(size) !== match[1] || size === 0 || size > 32) {
2022-09-16 05:58:45 +03:00
return (0, index_js_2.throwArgumentError)("invalid bytes type", "type", type);
2022-09-05 23:57:11 +03:00
}
2022-09-16 05:58:45 +03:00
if ((0, index_js_2.dataLength)(value) !== size) {
return (0, index_js_2.throwArgumentError)(`invalid value for ${type}`, "value", value);
2022-09-05 23:57:11 +03:00
}
if (isArray) {
2022-09-16 05:58:45 +03:00
return (0, index_js_2.getBytes)((0, index_js_2.zeroPadBytes)(value, 32));
2022-09-05 23:57:11 +03:00
}
return value;
}
match = type.match(regexArray);
if (match && Array.isArray(value)) {
const baseType = match[1];
const count = parseInt(match[2] || String(value.length));
if (count != value.length) {
2022-09-16 05:58:45 +03:00
(0, index_js_2.throwArgumentError)(`invalid array length for ${type}`, "value", value);
2022-09-05 23:57:11 +03:00
}
const result = [];
value.forEach(function (value) {
result.push(_pack(baseType, value, true));
});
2022-09-16 05:58:45 +03:00
return (0, index_js_2.getBytes)((0, index_js_2.concat)(result));
2022-09-05 23:57:11 +03:00
}
2022-09-16 05:58:45 +03:00
return (0, index_js_2.throwArgumentError)("invalid type", "type", type);
2022-09-05 23:57:11 +03:00
}
// @TODO: Array Enum
function solidityPacked(types, values) {
if (types.length != values.length) {
2022-09-16 05:58:45 +03:00
(0, index_js_2.throwArgumentError)("wrong number of values; expected ${ types.length }", "values", values);
2022-09-05 23:57:11 +03:00
}
const tight = [];
types.forEach(function (type, index) {
tight.push(_pack(type, values[index]));
});
2022-09-16 05:58:45 +03:00
return (0, index_js_2.hexlify)((0, index_js_2.concat)(tight));
2022-09-05 23:57:11 +03:00
}
exports.solidityPacked = solidityPacked;
/**
* Computes the non-standard packed (tightly packed) keccak256 hash of
* the values given the types.
*
* @param {Array<string>} types - The Solidity types to interpret each value as [default: bar]
* @param {Array<any>} values - The values to pack
*
* @returns: {HexString} the hexstring of the hash
* @example:
* solidityPackedKeccak256([ "address", "uint" ], [ "0x1234", 45 ]);
* //_result:
*
* @see https://docs.soliditylang.org/en/v0.8.14/abi-spec.html#non-standard-packed-mode
*/
function solidityPackedKeccak256(types, values) {
2022-09-16 05:58:45 +03:00
return (0, index_js_1.keccak256)(solidityPacked(types, values));
2022-09-05 23:57:11 +03:00
}
exports.solidityPackedKeccak256 = solidityPackedKeccak256;
function solidityPackedSha256(types, values) {
2022-09-16 05:58:45 +03:00
return (0, index_js_1.sha256)(solidityPacked(types, values));
2022-09-05 23:57:11 +03:00
}
exports.solidityPackedSha256 = solidityPackedSha256;
//# sourceMappingURL=solidity.js.map