2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
|
|
|
import { arrayify, concat, hexlify, zeroPad } from "@ethersproject/bytes";
|
|
|
|
import { keccak256 as hashKeccak256 } from "@ethersproject/keccak256";
|
|
|
|
import { sha256 as hashSha256 } from "@ethersproject/sha2";
|
|
|
|
import { toUtf8Bytes } from "@ethersproject/strings";
|
|
|
|
|
|
|
|
const regexBytes = new RegExp("^bytes([0-9]+)$");
|
|
|
|
const regexNumber = new RegExp("^(u?int)([0-9]*)$");
|
|
|
|
const regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$");
|
|
|
|
|
|
|
|
const Zeros = "0000000000000000000000000000000000000000000000000000000000000000";
|
|
|
|
|
2021-10-05 16:05:40 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
|
|
|
|
switch(type) {
|
|
|
|
case "address":
|
|
|
|
if (isArray) { return zeroPad(value, 32); }
|
|
|
|
return arrayify(value);
|
|
|
|
case "string":
|
|
|
|
return toUtf8Bytes(value);
|
|
|
|
case "bytes":
|
|
|
|
return arrayify(value);
|
|
|
|
case "bool":
|
|
|
|
value = (value ? "0x01": "0x00");
|
|
|
|
if (isArray) { return zeroPad(value, 32); }
|
|
|
|
return arrayify(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
let match = type.match(regexNumber);
|
|
|
|
if (match) {
|
|
|
|
//let signed = (match[1] === "int")
|
|
|
|
let size = parseInt(match[2] || "256")
|
2020-07-13 07:28:05 -04:00
|
|
|
|
|
|
|
if ((match[2] && String(size) !== match[2]) || (size % 8 !== 0) || size === 0 || size > 256) {
|
2021-10-05 16:05:40 -04:00
|
|
|
logger.throwArgumentError("invalid number type", "type", type)
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isArray) { size = 256; }
|
|
|
|
|
|
|
|
value = BigNumber.from(value).toTwos(size);
|
|
|
|
|
|
|
|
return zeroPad(value, size / 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
match = type.match(regexBytes);
|
|
|
|
if (match) {
|
2019-11-01 23:33:51 +09:00
|
|
|
const size = parseInt(match[1]);
|
2020-07-13 07:28:05 -04:00
|
|
|
|
|
|
|
if (String(size) !== match[1] || size === 0 || size > 32) {
|
2021-10-05 16:05:40 -04:00
|
|
|
logger.throwArgumentError("invalid bytes type", "type", type)
|
|
|
|
}
|
|
|
|
if (arrayify(value).byteLength !== size) {
|
|
|
|
logger.throwArgumentError(`invalid value for ${ type }`, "value", value)
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
if (isArray) { return arrayify((value + Zeros).substring(0, 66)); }
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
match = type.match(regexArray);
|
|
|
|
if (match && Array.isArray(value)) {
|
2019-11-01 23:33:51 +09:00
|
|
|
const baseType = match[1];
|
|
|
|
const count = parseInt(match[2] || String(value.length));
|
2021-10-05 16:05:40 -04:00
|
|
|
if (count != value.length) {
|
|
|
|
logger.throwArgumentError(`invalid array length for ${ type }`, "value", value)
|
|
|
|
}
|
2019-11-01 23:33:51 +09:00
|
|
|
const result: Array<Uint8Array> = [];
|
2019-05-14 18:25:46 -04:00
|
|
|
value.forEach(function(value) {
|
|
|
|
result.push(_pack(baseType, value, true));
|
|
|
|
});
|
|
|
|
return concat(result);
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:05:40 -04:00
|
|
|
return logger.throwArgumentError("invalid type", "type", type)
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: Array Enum
|
|
|
|
|
2021-03-30 14:28:43 -04:00
|
|
|
export function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
|
2021-10-05 16:05:40 -04:00
|
|
|
if (types.length != values.length) {
|
|
|
|
logger.throwArgumentError("wrong number of values; expected ${ types.length }", "values", values)
|
|
|
|
}
|
2019-11-01 23:33:51 +09:00
|
|
|
const tight: Array<Uint8Array> = [];
|
2019-05-14 18:25:46 -04:00
|
|
|
types.forEach(function(type, index) {
|
|
|
|
tight.push(_pack(type, values[index]));
|
|
|
|
});
|
|
|
|
return hexlify(concat(tight));
|
|
|
|
}
|
|
|
|
|
2021-03-30 14:28:43 -04:00
|
|
|
export function keccak256(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
|
2019-05-14 18:25:46 -04:00
|
|
|
return hashKeccak256(pack(types, values));
|
|
|
|
}
|
|
|
|
|
2021-03-30 14:28:43 -04:00
|
|
|
export function sha256(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
|
2019-05-14 18:25:46 -04:00
|
|
|
return hashSha256(pack(types, values));
|
|
|
|
}
|