ethers.js/lib.esm/hash/solidity.js

103 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-12-10 02:24:58 +03:00
import { getAddress } from "../address/index.js";
2022-09-16 05:58:45 +03:00
import { keccak256 as _keccak256, sha256 as _sha256 } from "../crypto/index.js";
2022-12-10 02:24:58 +03:00
import { concat, dataLength, getBytes, hexlify, toBeArray, toTwos, toUtf8Bytes, zeroPadBytes, zeroPadValue, assertArgument } from "../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 getBytes(zeroPadValue(value, 32));
2022-09-05 23:57:11 +03:00
}
2022-12-10 02:24:58 +03:00
return getBytes(getAddress(value));
2022-09-05 23:57:11 +03:00
case "string":
return toUtf8Bytes(value);
case "bytes":
2022-09-16 05:58:45 +03:00
return 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 getBytes(zeroPadValue(value, 32));
2022-09-05 23:57:11 +03:00
}
2022-09-16 05:58:45 +03:00
return getBytes(value);
2022-09-05 23:57:11 +03:00
}
let match = type.match(regexNumber);
if (match) {
2022-11-30 23:44:23 +03:00
let signed = (match[1] === "int");
2022-09-05 23:57:11 +03:00
let size = parseInt(match[2] || "256");
2022-11-09 10:57:02 +03:00
assertArgument((!match[2] || match[2] === String(size)) && (size % 8 === 0) && size !== 0 && size <= 256, "invalid number type", "type", type);
2022-09-05 23:57:11 +03:00
if (isArray) {
size = 256;
}
2022-11-30 23:44:23 +03:00
if (signed) {
value = toTwos(value, size);
}
2022-12-10 02:24:58 +03:00
return getBytes(zeroPadValue(toBeArray(value), size / 8));
2022-09-05 23:57:11 +03:00
}
match = type.match(regexBytes);
if (match) {
const size = parseInt(match[1]);
2022-11-09 10:57:02 +03:00
assertArgument(String(size) === match[1] && size !== 0 && size <= 32, "invalid bytes type", "type", type);
assertArgument(dataLength(value) === size, `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 getBytes(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));
2022-11-09 10:57:02 +03:00
assertArgument(count === value.length, `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 getBytes(concat(result));
2022-09-05 23:57:11 +03:00
}
2022-11-09 10:57:02 +03:00
assertArgument(false, "invalid type", "type", type);
2022-09-05 23:57:11 +03:00
}
// @TODO: Array Enum
2022-12-10 02:24:58 +03:00
/**
* Computes the [[link-solc-packed]] representation of %%values%%
* respectively to their %%types%%.
*
* @example:
* addr = "0x8ba1f109551bd432803012645ac136ddd64dba72"
* solidityPacked([ "address", "uint" ], [ addr, 45 ]);
* //_result:
*/
2022-09-05 23:57:11 +03:00
export function solidityPacked(types, values) {
2022-11-09 10:57:02 +03:00
assertArgument(types.length === values.length, "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]));
});
return hexlify(concat(tight));
}
/**
2022-12-10 02:24:58 +03:00
* Computes the [[link-solc-packed]] [[keccak256]] hash of %%values%%
* respectively to their %%types%%.
2022-09-05 23:57:11 +03:00
*
* @example:
2022-12-10 02:24:58 +03:00
* addr = "0x8ba1f109551bd432803012645ac136ddd64dba72"
* solidityPackedKeccak256([ "address", "uint" ], [ addr, 45 ]);
* //_result:
2022-09-05 23:57:11 +03:00
*/
export function solidityPackedKeccak256(types, values) {
return _keccak256(solidityPacked(types, values));
}
2022-12-10 02:24:58 +03:00
/**
* Computes the [[link-solc-packed]] [[sha256]] hash of %%values%%
* respectively to their %%types%%.
*
* @example:
* addr = "0x8ba1f109551bd432803012645ac136ddd64dba72"
* solidityPackedSha256([ "address", "uint" ], [ addr, 45 ]);
* //_result:
*/
2022-09-05 23:57:11 +03:00
export function solidityPackedSha256(types, values) {
return _sha256(solidityPacked(types, values));
}
//# sourceMappingURL=solidity.js.map