UNPKG

@etherspot/data-utils

Version:
95 lines (92 loc) 2.56 kB
import { AbiEncodingLengthMismatchError, BytesSizeMismatchError, InvalidAddressError, UnsupportedPackedAbiType, arrayRegex, boolToHex, bytesRegex, concatHex, integerRegex, isAddress, numberToHex, pad, stringToHex } from "./chunk-NH5DXPP4.mjs"; // node_modules/viem/_esm/utils/abi/encodePacked.js function encodePacked(types, values) { if (types.length !== values.length) throw new AbiEncodingLengthMismatchError({ expectedLength: types.length, givenLength: values.length }); const data = []; for (let i = 0; i < types.length; i++) { const type = types[i]; const value = values[i]; data.push(encode(type, value)); } return concatHex(data); } function encode(type, value, isArray = false) { if (type === "address") { const address = value; if (!isAddress(address)) throw new InvalidAddressError({ address }); return pad(address.toLowerCase(), { size: isArray ? 32 : null }); } if (type === "string") return stringToHex(value); if (type === "bytes") return value; if (type === "bool") return pad(boolToHex(value), { size: isArray ? 32 : 1 }); const intMatch = type.match(integerRegex); if (intMatch) { const [_type, baseType, bits = "256"] = intMatch; const size = Number.parseInt(bits) / 8; return numberToHex(value, { size: isArray ? 32 : size, signed: baseType === "int" }); } const bytesMatch = type.match(bytesRegex); if (bytesMatch) { const [_type, size] = bytesMatch; if (Number.parseInt(size) !== (value.length - 2) / 2) throw new BytesSizeMismatchError({ expectedSize: Number.parseInt(size), givenSize: (value.length - 2) / 2 }); return pad(value, { dir: "right", size: isArray ? 32 : null }); } const arrayMatch = type.match(arrayRegex); if (arrayMatch && Array.isArray(value)) { const [_type, childType] = arrayMatch; const data = []; for (let i = 0; i < value.length; i++) { data.push(encode(childType, value[i], true)); } if (data.length === 0) return "0x"; return concatHex(data); } throw new UnsupportedPackedAbiType(type); } // node_modules/viem/_esm/utils/data/isBytes.js function isBytes(value) { if (!value) return false; if (typeof value !== "object") return false; if (!("BYTES_PER_ELEMENT" in value)) return false; return value.BYTES_PER_ELEMENT === 1 && value.constructor.name === "Uint8Array"; } export { encodePacked, isBytes }; //# sourceMappingURL=chunk-ZRPFBHDV.mjs.map