@crtxio/abi
Version:
A tiny Solidity ABI encoder and decoder
51 lines (40 loc) • 1.11 kB
JavaScript
import { addPadding, concat, toBuffer } from '../utils';
const BYTES_REGEX = /^bytes([0-9]{1,2})$/;
export const getByteLength = type => {
var _type$match;
const bytes = (_type$match = type.match(BYTES_REGEX)) === null || _type$match === void 0 ? void 0 : _type$match[1];
if (bytes) {
const length = Number(bytes);
if (length <= 0 || length > 32) {
throw new Error('Invalid type: length is out of range');
}
return length;
}
throw new Error('Invalid type: no length');
};
export const fixedBytes = {
isDynamic: false,
isType(type) {
return BYTES_REGEX.test(type);
},
encode({
type,
buffer,
value
}) {
const length = getByteLength(type);
const bufferValue = toBuffer(value);
if (bufferValue.length !== length) {
throw new Error(`Buffer has invalid length, expected ${length}, got ${bufferValue.length}`);
}
return concat([buffer, addPadding(bufferValue)]);
},
decode({
type,
value
}) {
const length = getByteLength(type);
return value.slice(0, length);
}
};
//# sourceMappingURL=fixed-bytes.js.map