@axiom-crypto/tools
Version:
Useful data, field, and byte manipulation tools for Axiom.
136 lines (135 loc) • 2.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateSize = exports.getByteLength = exports.MaxSizes = exports.ByteLengths = void 0;
exports.ByteLengths = Object.freeze({
bool: 1,
address: 20,
mapping: 32,
array: 32,
string: 32,
uint8: 1,
uint16: 2,
uint24: 3,
uint32: 4,
uint40: 5,
uint48: 6,
uint56: 7,
uint64: 8,
uint72: 9,
uint80: 10,
uint88: 11,
uint96: 12,
uint104: 13,
uint112: 14,
uint120: 15,
uint128: 16,
uint136: 17,
uint144: 18,
uint152: 19,
uint160: 20,
uint168: 21,
uint176: 22,
uint184: 23,
uint192: 24,
uint200: 25,
uint208: 26,
uint216: 27,
uint224: 28,
uint232: 29,
uint240: 30,
uint248: 31,
uint256: 32,
int8: 1,
int16: 2,
int24: 3,
int32: 4,
int40: 5,
int48: 6,
int56: 7,
int64: 8,
int72: 9,
int80: 10,
int88: 11,
int96: 12,
int104: 13,
int112: 14,
int120: 15,
int128: 16,
int136: 17,
int144: 18,
int152: 19,
int160: 20,
int168: 21,
int176: 22,
int184: 23,
int192: 24,
int200: 25,
int208: 26,
int216: 27,
int224: 28,
int232: 29,
int240: 30,
int248: 31,
int256: 32,
bytes1: 1,
bytes2: 2,
bytes3: 3,
bytes4: 4,
bytes5: 5,
bytes6: 6,
bytes7: 7,
bytes8: 8,
bytes9: 9,
bytes10: 10,
bytes11: 11,
bytes12: 12,
bytes13: 13,
bytes14: 14,
bytes15: 15,
bytes16: 16,
bytes17: 17,
bytes18: 18,
bytes19: 19,
bytes20: 20,
bytes21: 21,
bytes22: 22,
bytes23: 23,
bytes24: 24,
bytes25: 25,
bytes26: 26,
bytes27: 27,
bytes28: 28,
bytes29: 29,
bytes30: 30,
bytes31: 31,
bytes32: 32,
});
exports.MaxSizes = Object.freeze({
uint8: 256n,
uint16: 65536n,
uint32: 4294967296n,
uint64: 18446744073709551616n,
uint128: 340282366920938463463374607431768211456n,
uint256: 115792089237316195423570985008687907853269984665640564039457584007913129639936n,
});
function getByteLength(type) {
if (exports.ByteLengths[type] === undefined) {
throw new Error(`Unknown type ${type}`);
}
return exports.ByteLengths[type];
}
exports.getByteLength = getByteLength;
function validateSize(value, type) {
if (typeof value === "string" || typeof value === "number") {
value = BigInt(value);
}
const typeKeys = Object.keys(exports.MaxSizes);
if (!typeKeys.includes(type)) {
throw new Error(`validateSize only validates types: ${typeKeys}`);
}
const max = exports.MaxSizes[type];
if (value.valueOf() < 0n || value >= max) {
throw new Error(`Value ${value} overflows ${type}`);
}
}
exports.validateSize = validateSize;