Socket
Socket
Sign inDemoInstall

@metamask/abi-utils

Package Overview
Dependencies
7
Maintainers
7
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

46

dist/abi.d.ts

@@ -63,5 +63,10 @@ import { BytesLike } from '@metamask/utils';

* the types array.
* @param packed - Whether to use the non-standard packed mode. Defaults to
* `false`.
* @param tight - Whether to pack the values tightly. When enabled, the values
* will be packed without any padding. This matches the behaviour of
* `ethereumjs-abi`. Defaults to `false`.
* @returns The ABI encoded bytes.
*/
export declare const encode: <Type extends readonly string[]>(types: Type, values: TypeMap<Type, "input">) => Uint8Array;
export declare const encode: <Type extends readonly string[]>(types: Type, values: TypeMap<Type, "input">, packed?: boolean, tight?: boolean) => Uint8Array;
/**

@@ -89,2 +94,41 @@ * Encode the data with the provided type. The type must be a valid Solidity

/**
* Encode the data with the provided types. The types must be valid Solidity
* ABI types. This is similar to {@link encode}, but the values are encoded in
* the non-standard packed mode. This differs from the standard encoding in the
* following ways:
*
* - Most values are packed tightly, without alignment padding.
* - The exception is array values, which are padded to 32 bytes.
* - Values are still padded to their full size, i.e., `uint16` values are still
* padded to 2 bytes, regardless of the length of the value.
* - The encoding of dynamic types (`bytes`, `string`) is different. The length
* of the dynamic type is not included in the encoding, and the dynamic type is
* not padded to a multiple of 32 bytes.
* - All values are encoded in-place, without any offsets.
*
* The encoding of this is ambiguous as soon as there is more than one dynamic
* type. That means that these values cannot be decoded with {@link decode} or
* Solidity's `abi.decode` function.
*
* See {@link encode} for more information on how values are parsed.
*
* @example
* ```typescript
* import { encodePacked } from '@metamask/abi-utils';
*
* const encoded = encodePacked(['uint8'], [42]);
*
* console.log(encoded); // `Uint8Array [ 42 ]`
* ```
* @see https://docs.soliditylang.org/en/v0.8.17/abi-spec.html#types
* @see https://docs.soliditylang.org/en/v0.8.17/abi-spec.html#non-standard-packed-mode
* @param types - The types to encode.
* @param values - The values to encode.
* @param tight - Whether to pack the values tightly. When enabled, `bytesN`
* values in arrays will be packed without any padding. This matches the
* behaviour of `ethereumjs-abi`. Defaults to `false`.
* @returns The ABI encoded bytes.
*/
export declare const encodePacked: <Type extends readonly string[]>(types: Type, values: TypeMap<Type, "input">, tight?: boolean) => Uint8Array;
/**
* Decode an ABI encoded buffer with the specified types. The types must be

@@ -91,0 +135,0 @@ * valid Solidity ABI types.

@@ -6,3 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeSingle = exports.decode = exports.encodeSingle = exports.encode = void 0;
exports.decodeSingle = exports.decode = exports.encodePacked = exports.encodeSingle = exports.encode = void 0;
const utils_1 = require("@metamask/utils");

@@ -71,7 +71,12 @@ const packer_1 = require("./packer");

* the types array.
* @param packed - Whether to use the non-standard packed mode. Defaults to
* `false`.
* @param tight - Whether to pack the values tightly. When enabled, the values
* will be packed without any padding. This matches the behaviour of
* `ethereumjs-abi`. Defaults to `false`.
* @returns The ABI encoded bytes.
*/
const encode = (types, values) => {
const encode = (types, values, packed, tight) => {
try {
return (0, packer_1.pack)(types, values);
return (0, packer_1.pack)({ types, values, packed, tight });
}

@@ -111,2 +116,44 @@ catch (error) {

/**
* Encode the data with the provided types. The types must be valid Solidity
* ABI types. This is similar to {@link encode}, but the values are encoded in
* the non-standard packed mode. This differs from the standard encoding in the
* following ways:
*
* - Most values are packed tightly, without alignment padding.
* - The exception is array values, which are padded to 32 bytes.
* - Values are still padded to their full size, i.e., `uint16` values are still
* padded to 2 bytes, regardless of the length of the value.
* - The encoding of dynamic types (`bytes`, `string`) is different. The length
* of the dynamic type is not included in the encoding, and the dynamic type is
* not padded to a multiple of 32 bytes.
* - All values are encoded in-place, without any offsets.
*
* The encoding of this is ambiguous as soon as there is more than one dynamic
* type. That means that these values cannot be decoded with {@link decode} or
* Solidity's `abi.decode` function.
*
* See {@link encode} for more information on how values are parsed.
*
* @example
* ```typescript
* import { encodePacked } from '@metamask/abi-utils';
*
* const encoded = encodePacked(['uint8'], [42]);
*
* console.log(encoded); // `Uint8Array [ 42 ]`
* ```
* @see https://docs.soliditylang.org/en/v0.8.17/abi-spec.html#types
* @see https://docs.soliditylang.org/en/v0.8.17/abi-spec.html#non-standard-packed-mode
* @param types - The types to encode.
* @param values - The values to encode.
* @param tight - Whether to pack the values tightly. When enabled, `bytesN`
* values in arrays will be packed without any padding. This matches the
* behaviour of `ethereumjs-abi`. Defaults to `false`.
* @returns The ABI encoded bytes.
*/
const encodePacked = (types, values, tight) => {
return (0, exports.encode)(types, values, true, tight);
};
exports.encodePacked = encodePacked;
/**
* Decode an ABI encoded buffer with the specified types. The types must be

@@ -113,0 +160,0 @@ * valid Solidity ABI types.

@@ -21,2 +21,31 @@ import { Parser } from './parsers';

export declare const isDynamicParser: (parser: Parser, type: string) => boolean;
export declare type PackArgs<Type extends readonly string[]> = {
/**
* The types of the values to pack.
*/
types: Type;
/**
* The values to pack.
*/
values: TypeMap<Type, 'input'>;
/**
* Whether to use the non-standard packed mode.
*/
packed?: boolean | undefined;
/**
* Whether to use tight packing mode. Only applicable when `packed` is true.
* When true, the packed mode will not add any padding bytes. This matches
* the packing behaviour of `ethereumjs-abi`, but is not standard.
*/
tight?: boolean | undefined;
/**
* Whether to use the non-standard packed mode in "array" mode. This is
* normally only used by the {@link array} parser.
*/
arrayPacked?: boolean | undefined;
/**
* The byte array to encode the values into.
*/
byteArray?: Uint8Array;
};
/**

@@ -27,8 +56,17 @@ * Pack the provided values in a buffer, encoded with the specified types. If a

*
* @param types - The types to use for encoding.
* @param values - The values to encode.
* @param buffer - The buffer to concatenate with.
* @param args - The arguments object.
* @param args.types - The types of the values to pack.
* @param args.values - The values to pack.
* @param args.packed - Whether to use the non-standard packed mode. Defaults to
* `false`.
* @param args.arrayPacked - Whether to use the non-standard packed mode for
* arrays. Defaults to `false`.
* @param args.byteArray - The byte array to encode the values into. Defaults to
* an empty array.
* @param args.tight - Whether to use tight packing mode. Only applicable when
* `packed` is true. When true, the packed mode will not add any padding bytes.
* This matches the packing behaviour of `ethereumjs-abi`, but is not standard.
* @returns The resulting encoded buffer.
*/
export declare const pack: <Type extends readonly string[]>(types: Type, values: TypeMap<Type, "input">, buffer?: Uint8Array) => Uint8Array;
export declare const pack: <Type extends readonly string[]>({ types, values, packed, tight, arrayPacked, byteArray, }: PackArgs<Type>) => Uint8Array;
export declare const unpack: <Type extends readonly string[], Output = TypeMap<Type, "output">>(types: Type, buffer: Uint8Array) => Output;

35

dist/packer.js

@@ -61,8 +61,17 @@ "use strict";

*
* @param types - The types to use for encoding.
* @param values - The values to encode.
* @param buffer - The buffer to concatenate with.
* @param args - The arguments object.
* @param args.types - The types of the values to pack.
* @param args.values - The values to pack.
* @param args.packed - Whether to use the non-standard packed mode. Defaults to
* `false`.
* @param args.arrayPacked - Whether to use the non-standard packed mode for
* arrays. Defaults to `false`.
* @param args.byteArray - The byte array to encode the values into. Defaults to
* an empty array.
* @param args.tight - Whether to use tight packing mode. Only applicable when
* `packed` is true. When true, the packed mode will not add any padding bytes.
* This matches the packing behaviour of `ethereumjs-abi`, but is not standard.
* @returns The resulting encoded buffer.
*/
const pack = (types, values, buffer = new Uint8Array()) => {
const pack = ({ types, values, packed = false, tight = false, arrayPacked = false, byteArray = new Uint8Array(), }) => {
(0, utils_1.assert)(types.length === values.length, new errors_1.ParserError(`The number of types (${types.length}) does not match the number of values (${values.length}).`));

@@ -74,5 +83,13 @@ const { staticBuffer, dynamicBuffer, pointers } = types.reduce(

const value = values[index];
if (!(0, exports.isDynamicParser)(parser, type)) {
// If packed mode is enabled, we can skip the dynamic check, as all
// values are encoded in the static buffer.
if (packed || arrayPacked || !(0, exports.isDynamicParser)(parser, type)) {
return {
staticBuffer: parser.encode({ buffer: staticBuffer, value, type }),
staticBuffer: parser.encode({
buffer: staticBuffer,
value,
type,
packed,
tight,
}),
dynamicBuffer,

@@ -87,2 +104,4 @@ pointers,

type,
packed,
tight,
});

@@ -102,2 +121,4 @@ return {

});
// If packed mode is enabled, there shouldn't be any dynamic values.
(0, utils_1.assert)((!packed && !arrayPacked) || dynamicBuffer.length === 0, new errors_1.ParserError('Invalid pack state.'));
const dynamicStart = staticBuffer.length;

@@ -108,3 +129,3 @@ const updatedBuffer = pointers.reduce((target, { pointer, position }) => {

}, staticBuffer);
return (0, utils_1.concatBytes)([buffer, updatedBuffer, dynamicBuffer]);
return (0, utils_1.concatBytes)([byteArray, updatedBuffer, dynamicBuffer]);
};

@@ -111,0 +132,0 @@ exports.pack = pack;

@@ -20,4 +20,4 @@ "use strict";

const bytesValue = (0, utils_1.createBytes)(value);
(0, utils_1.assert)(bytesValue.length === 20, new errors_1.ParserError(`Invalid address value. Expected address to be 20 bytes long, but received ${bytesValue.length} bytes.`));
return bytesValue;
(0, utils_1.assert)(bytesValue.length <= 20, new errors_1.ParserError(`Invalid address value. Expected address to be 20 bytes long, but received ${bytesValue.length} bytes.`));
return (0, utils_2.padStart)(bytesValue, 20);
};

@@ -53,6 +53,13 @@ exports.getAddress = getAddress;

* @param args.value - The address to encode.
* @param args.packed - Whether to use packed encoding.
* @returns The bytes with the encoded address added to it.
*/
encode({ buffer, value }) {
const addressBuffer = (0, utils_2.padStart)((0, exports.getAddress)(value));
encode({ buffer, value, packed }) {
const addressValue = (0, exports.getAddress)(value);
// If we're using packed encoding, we can just add the address bytes to the
// byte array, without adding any padding.
if (packed) {
return (0, utils_1.concatBytes)([buffer, addressValue]);
}
const addressBuffer = (0, utils_2.padStart)(addressValue);
return (0, utils_1.concatBytes)([buffer, addressBuffer]);

@@ -59,0 +66,0 @@ },

@@ -20,2 +20,3 @@ "use strict";

const tuple_1 = require("./tuple");
const fixed_bytes_1 = require("./fixed-bytes");
const ARRAY_REGEX = /^(?<type>.*)\[(?<length>\d*?)\]$/u;

@@ -100,6 +101,22 @@ const isArrayType = (type) => ARRAY_REGEX.test(type);

* @param args.value - The array to encode.
* @param args.packed - Whether to use non-standard packed encoding.
* @param args.tight - Whether to use non-standard tight encoding.
* @returns The bytes with the encoded array added to it.
*/
encode({ type, buffer, value }) {
encode({ type, buffer, value, packed, tight }) {
const [arrayType, fixedLength] = (0, exports.getArrayType)(type);
// Packed encoding does not support nested arrays.
(0, utils_1.assert)(!packed || !(0, exports.isArrayType)(arrayType), new errors_1.ParserError(`Cannot pack nested arrays.`));
// Tightly pack `T[]` where `T` is a dynamic type. This is not supported in
// Solidity, but is commonly used in the Ethereum ecosystem.
if (packed && (0, packer_1.isDynamicParser)((0, packer_1.getParser)(arrayType), arrayType)) {
return (0, packer_1.pack)({
types: new Array(value.length).fill(arrayType),
values: value,
byteArray: buffer,
packed,
arrayPacked: true,
tight,
});
}
if (fixedLength) {

@@ -112,4 +129,24 @@ (0, utils_1.assert)(fixedLength === value.length, new errors_1.ParserError(`Array length does not match type length. Expected a length of ${fixedLength}, but received ${value.length}.`));

value,
// In "tight" mode, we don't pad the values to 32 bytes if the value is
// of type `bytesN`. This is an edge case in `ethereumjs-abi` that we
// support to provide compatibility with it.
packed: fixed_bytes_1.fixedBytes.isType(arrayType) && tight,
tight,
});
}
// For packed encoding, we don't need to encode the length of the array,
// so we can just encode the values.
if (packed) {
return (0, packer_1.pack)({
types: new Array(value.length).fill(arrayType),
values: value,
byteArray: buffer,
// In "tight" mode, we don't pad the values to 32 bytes if the value is
// of type `bytesN`. This is an edge case in `ethereumjs-abi` that we
// support to provide compatibility with it.
packed: fixed_bytes_1.fixedBytes.isType(arrayType) && tight,
arrayPacked: true,
tight,
});
}
// `T[]` with `k` elements is encoded as `k (T[0], ..., T[k - 1])`. That

@@ -119,3 +156,9 @@ // means that we just need to encode the length of the array, and then the

const arrayLength = (0, utils_2.padStart)((0, utils_1.numberToBytes)(value.length));
return (0, packer_1.pack)(new Array(value.length).fill(arrayType), value, (0, utils_1.concatBytes)([buffer, arrayLength]));
return (0, packer_1.pack)({
types: new Array(value.length).fill(arrayType),
values: value,
byteArray: (0, utils_1.concatBytes)([buffer, arrayLength]),
packed,
tight,
});
},

@@ -122,0 +165,0 @@ /**

@@ -5,2 +5,3 @@ "use strict";

const superstruct_1 = require("superstruct");
const utils_1 = require("@metamask/utils");
const errors_1 = require("../errors");

@@ -60,9 +61,22 @@ const number_1 = require("./number");

* @param args.value - The boolean to encode.
* @param args.packed - Whether the value is packed.
* @param args.tight - Whether to use non-standard tight encoding.
* @returns The bytes with the encoded boolean added to it.
*/
encode({ buffer, value }) {
encode({ buffer, value, packed, tight }) {
const booleanValue = (0, exports.getBooleanValue)(value);
// For packed encoding, we add a single byte (`0x00` or `0x01`) to the byte
// array.
if (packed) {
return (0, utils_1.concatBytes)([buffer, (0, utils_1.bigIntToBytes)(booleanValue)]);
}
// Booleans are encoded as 32-byte integers, so we use the number parser
// to encode the boolean value.
return number_1.number.encode({ type: 'uint256', buffer, value: booleanValue });
return number_1.number.encode({
type: 'uint256',
buffer,
value: booleanValue,
packed,
tight,
});
},

@@ -69,0 +83,0 @@ /**

@@ -34,6 +34,13 @@ "use strict";

* @param args.value - The bytes value to encode.
* @param args.packed - Whether to use packed encoding.
* @returns The bytes with the encoded bytes value added to it.
*/
encode({ buffer, value }) {
encode({ buffer, value, packed }) {
const bufferValue = (0, utils_1.createBytes)(value);
// For packed encoding, we can just add the bytes value to the byte array,
// without adding any padding or alignment. There is also no need to
// encode the length of the bytes.
if (packed) {
return (0, utils_1.concatBytes)([buffer, bufferValue]);
}
const paddedSize = Math.ceil(bufferValue.byteLength / 32) * 32;

@@ -40,0 +47,0 @@ // Bytes of length `k` are encoded as `k pad_right(bytes)`.

@@ -50,8 +50,14 @@ "use strict";

* @param args.value - The value to encode.
* @param args.packed - Whether to use packed encoding.
* @returns The bytes with the encoded value added to it.
*/
encode({ type, buffer, value }) {
encode({ type, buffer, value, packed }) {
const length = (0, exports.getByteLength)(type);
const bufferValue = (0, utils_1.createBytes)(value);
(0, utils_1.assert)(bufferValue.length === length, new errors_1.ParserError(`Expected a value of length ${length}, but received a value of length ${bufferValue.length}.`));
(0, utils_1.assert)(bufferValue.length <= length, new errors_1.ParserError(`Expected a value of length ${length}, but received a value of length ${bufferValue.length}.`));
// For packed encoding, the value is padded to the length of the type, and
// then added to the byte array.
if (packed) {
return (0, utils_1.concatBytes)([buffer, (0, utils_2.padEnd)(bufferValue, length)]);
}
return (0, utils_1.concatBytes)([buffer, (0, utils_2.padEnd)(bufferValue)]);

@@ -58,0 +64,0 @@ },

@@ -67,9 +67,17 @@ "use strict";

* @param args.value - The function to encode.
* @param args.packed - Whether to use packed encoding.
* @param args.tight - Whether to use non-standard tight encoding.
* @returns The bytes with the encoded function added to it.
*/
encode({ buffer, value }) {
encode({ buffer, value, packed, tight }) {
const fnValue = (0, exports.getFunction)(value);
// Functions are encoded as `bytes24`, so we use the fixedBytes parser to
// encode the function.
return fixed_bytes_1.fixedBytes.encode({ type: 'bytes24', buffer, value: fnValue });
return fixed_bytes_1.fixedBytes.encode({
type: 'bytes24',
buffer,
value: fnValue,
packed,
tight,
});
},

@@ -76,0 +84,0 @@ /**

@@ -11,2 +11,20 @@ import { NumberLike } from '@metamask/utils';

/**
* Get the length of the specified type. If a length is not specified, if the
* length is out of range (8 <= n <= 256), or if the length is not a multiple of
* 8, this will throw an error.
*
* @param type - The type to get the length for.
* @returns The bit length of the type.
*/
export declare const getLength: (type: string) => number;
/**
* Assert that the byte length of the given value is in range for the given
* number type.
*
* @param value - The value to check.
* @param type - The type of the value.
* @throws If the value is out of range for the type.
*/
export declare const assertNumberLength: (value: bigint, type: string) => void;
/**
* Normalize a `bigint` value. This accepts the value as:

@@ -13,0 +31,0 @@ *

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.number = exports.getBigInt = exports.isSigned = void 0;
exports.number = exports.getBigInt = exports.assertNumberLength = exports.getLength = exports.isSigned = void 0;
const utils_1 = require("@metamask/utils");
const utils_2 = require("../utils");
const errors_1 = require("../errors");
const NUMBER_REGEX = /^u?int([0-9]*)?$/u;
const NUMBER_REGEX = /^u?int(?<length>[0-9]*)?$/u;
/**

@@ -19,2 +19,44 @@ * Check if a number type is signed.

/**
* Get the length of the specified type. If a length is not specified, if the
* length is out of range (8 <= n <= 256), or if the length is not a multiple of
* 8, this will throw an error.
*
* @param type - The type to get the length for.
* @returns The bit length of the type.
*/
const getLength = (type) => {
var _a;
if (type === 'int' || type === 'uint') {
return 256;
}
const match = type.match(NUMBER_REGEX);
(0, utils_1.assert)((_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.length, new errors_1.ParserError(`Invalid number type. Expected a number type, but received "${type}".`));
const length = parseInt(match.groups.length, 10);
(0, utils_1.assert)(length >= 8 && length <= 256, new errors_1.ParserError(`Invalid number length. Expected a number between 8 and 256, but received "${type}".`));
(0, utils_1.assert)(length % 8 === 0, new errors_1.ParserError(`Invalid number length. Expected a multiple of 8, but received "${type}".`));
return length;
};
exports.getLength = getLength;
/**
* Assert that the byte length of the given value is in range for the given
* number type.
*
* @param value - The value to check.
* @param type - The type of the value.
* @throws If the value is out of range for the type.
*/
const assertNumberLength = (value, type) => {
const length = (0, exports.getLength)(type);
const maxValue = BigInt(2) ** BigInt(length - ((0, exports.isSigned)(type) ? 1 : 0)) - BigInt(1);
if ((0, exports.isSigned)(type)) {
// Signed types must be in the range of `-(2^(length - 1))` to
// `2^(length - 1) - 1`.
(0, utils_1.assert)(value >= -(maxValue + BigInt(1)) && value <= maxValue, new errors_1.ParserError(`Number "${value}" is out of range for type "${type}".`));
return;
}
// Unsigned types must be in the range of `0` to `2^length - 1`.
(0, utils_1.assert)(value <= maxValue, new errors_1.ParserError(`Number "${value}" is out of range for type "${type}".`));
};
exports.assertNumberLength = assertNumberLength;
/**
* Normalize a `bigint` value. This accepts the value as:

@@ -66,7 +108,15 @@ *

* @param args.value - The value to encode.
* @param args.packed - Whether to use packed encoding.
* @returns The bytes with the encoded value added to it.
*/
encode({ type, buffer, value }) {
encode({ type, buffer, value, packed }) {
const bigIntValue = (0, exports.getBigInt)(value);
(0, exports.assertNumberLength)(bigIntValue, type);
if ((0, exports.isSigned)(type)) {
// For packed encoding, the value is padded to the length of the type, and
// then added to the byte array.
if (packed) {
const length = (0, exports.getLength)(type) / 8;
return (0, utils_1.concatBytes)([buffer, (0, utils_1.signedBigIntToBytes)(bigIntValue, length)]);
}
return (0, utils_1.concatBytes)([

@@ -77,2 +127,11 @@ buffer,

}
// For packed encoding, the value is padded to the length of the type, and
// then added to the byte array.
if (packed) {
const length = (0, exports.getLength)(type) / 8;
return (0, utils_1.concatBytes)([
buffer,
(0, utils_2.padStart)((0, utils_1.bigIntToBytes)(bigIntValue), length),
]);
}
return (0, utils_1.concatBytes)([buffer, (0, utils_2.padStart)((0, utils_1.bigIntToBytes)(bigIntValue))]);

@@ -89,9 +148,13 @@ },

decode({ type, value }) {
const buffer = value.slice(0, 32);
const buffer = value.subarray(0, 32);
if ((0, exports.isSigned)(type)) {
return (0, utils_1.bytesToSignedBigInt)(buffer);
const numberValue = (0, utils_1.bytesToSignedBigInt)(buffer);
(0, exports.assertNumberLength)(numberValue, type);
return numberValue;
}
return (0, utils_1.bytesToBigInt)(buffer);
const numberValue = (0, utils_1.bytesToBigInt)(buffer);
(0, exports.assertNumberLength)(numberValue, type);
return numberValue;
},
};
//# sourceMappingURL=number.js.map

@@ -15,2 +15,10 @@ export declare type DynamicFunction = (type: string) => boolean;

value: Value;
/**
* Whether to use the non-standard packed mode.
*/
packed: boolean;
/**
* Whether to use tight packing mode. Only applicable when `packed` is true.
*/
tight: boolean;
};

@@ -17,0 +25,0 @@ export declare type DecodeArgs = {

@@ -34,8 +34,16 @@ "use strict";

* @param args.value - The string value to encode.
* @param args.packed - Whether to use packed encoding.
* @param args.tight - Whether to use non-standard tight encoding.
* @returns The bytes with the encoded string value added to it.
*/
encode({ buffer, value }) {
encode({ buffer, value, packed, tight }) {
// Strings are encoded as UTF-8 bytes, so we use the bytes parser to encode
// the string as bytes.
return bytes_1.bytes.encode({ type: 'bytes', buffer, value: (0, utils_1.stringToBytes)(value) });
return bytes_1.bytes.encode({
type: 'bytes',
buffer,
value: (0, utils_1.stringToBytes)(value),
packed,
tight,
});
},

@@ -42,0 +50,0 @@ /**

@@ -90,7 +90,15 @@ "use strict";

* @param args.value - The value to encode.
* @param args.packed - Whether to use non-standard packed encoding.
* @param args.tight - Whether to use non-standard tight encoding.
* @returns The bytes with the encoded value added to it.
*/
encode({ type, buffer, value }) {
encode({ type, buffer, value, packed, tight }) {
const elements = (0, exports.getTupleElements)(type);
return (0, packer_1.pack)(elements, value, buffer);
return (0, packer_1.pack)({
types: elements,
values: value,
byteArray: buffer,
packed,
tight,
});
},

@@ -97,0 +105,0 @@ /**

{
"name": "@metamask/abi-utils",
"version": "1.0.0",
"version": "1.1.0",
"description": "Lightweight utilities for encoding and decoding Solidity ABI.",

@@ -54,2 +54,3 @@ "keywords": [

"@metamask/eslint-config-typescript": "^10.0.0",
"@noble/hashes": "^1.1.3",
"@types/jest": "^28.1.6",

@@ -56,0 +57,0 @@ "@types/node": "^17.0.23",

@@ -49,2 +49,19 @@ # `@metamask/abi-utils`

### Encoding packed values
Encoding packed values, using the non-standard packed mode, is also supported.
This behaves the same as `abi.encodePacked` in Solidity.
```typescript
import { encodePacked } from '@metamask/abi-utils';
const encoded = encodePacked(['uint256', 'string'], [42, 'Hello, world!']);
// `abi-utils` returns a `Uint8Array`, so you can convert it to a hex string
// using `bytesToHex`.
console.log(bytesToHex(encoded));
// 0x000000000000000000000000000000000000000000000000000000000000002a48656c6c6f2c20776f726c6421
```
### Decoding values

@@ -51,0 +68,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc