@crtxio/abi
Version:
A tiny Solidity ABI encoder and decoder
48 lines (39 loc) • 884 B
JavaScript
import { concat, fromTwosComplement, toBuffer, toNumber, toTwosComplement } from '../utils';
const NUMBER_REGEX = /^u?int([0-9]*)?$/;
export const isSigned = type => {
return !type.startsWith('u');
};
export const asNumber = value => {
if (typeof value === 'bigint') {
return value;
}
return BigInt(value);
};
export const number = {
isDynamic: false,
isType(type) {
return NUMBER_REGEX.test(type);
},
encode({
type,
buffer,
value
}) {
const number = asNumber(value);
if (isSigned(type)) {
return concat([buffer, toTwosComplement(number, 32)]);
}
return concat([buffer, toBuffer(number)]);
},
decode({
type,
value
}) {
const buffer = value.slice(0, 32);
if (isSigned(type)) {
return fromTwosComplement(buffer);
}
return toNumber(buffer);
}
};
//# sourceMappingURL=number.js.map