UNPKG

@crtxio/abi

Version:

A tiny Solidity ABI encoder and decoder

40 lines (34 loc) 910 B
import { pack, unpack } from '../packer'; import { concat, toBuffer, toNumber } from '../utils'; const ARRAY_REGEX = /^(.*)\[]$/; export const getArrayType = type => { const match = type.match(ARRAY_REGEX); if (match) { return match[1]; } throw new Error('Type is not an array type'); }; export const array = { isDynamic: true, isType(type) { return ARRAY_REGEX.test(type); }, encode({ type, buffer, value }) { const arrayType = getArrayType(type); const arrayLength = toBuffer(value.length); return pack(new Array(value.length).fill(arrayType), value, concat([buffer, arrayLength])); }, decode({ type, value }) { const arrayType = getArrayType(type); const arrayLength = Number(toNumber(value.subarray(0, 32))); return unpack(new Array(arrayLength).fill(arrayType), value.subarray(32)); } }; //# sourceMappingURL=array.js.map