Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/abi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ describe('encodeSingle', () => {
});

describe('encodePacked', () => {
it.each([false, true])(
'validates fixed arrays with dynamic elements with tight=%s',
(tight) => {
expect(() => encodePacked(['string[2]'], [['foo']], tight)).toThrow(
'Unable to encode value: Array length does not match type length. Expected a length of 2, but received 1.',
);
expect(() => encodePacked(['bytes[2]'], [['0x12']], tight)).toThrow(
'Unable to encode value: Array length does not match type length. Expected a length of 2, but received 1.',
);

expect(
bytesToHex(encodePacked(['string[2]'], [['foo', 'bar']], tight)),
).toBe('0x666f6f626172');
expect(
bytesToHex(encodePacked(['bytes[2]'], [['0x12', '0x34']], tight)),
).toBe('0x1234');
},
);

it.each(PACKED_ABI_TEST_VECTORS)(
'encodes values in packed mode',
({ types, values, result }) => {
Expand Down
26 changes: 26 additions & 0 deletions src/parsers/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ describe('array', () => {
});

describe('encode', () => {
describe.each([false, true])(
'packed fixed arrays with tight=%s',
(tight) => {
it.each([
['string[2]', []],
['string[2]', ['foo']],
['string[2]', ['foo', 'bar', 'baz']],
['bytes[2]', []],
['bytes[2]', ['0x12']],
['bytes[2]', ['0x12', '0x34', '0x56']],
])('rejects a mismatched length for %s with %j', (type, value) => {
expect(() =>
array.encode({
type,
value,
buffer: new Uint8Array(),
packed: true,
tight,
}),
).toThrow(
`Array length does not match type length. Expected a length of 2, but received ${value.length}.`,
);
});
},
);

it('encodes an array', () => {
expect(
bytesToHex(
Expand Down
16 changes: 9 additions & 7 deletions src/parsers/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ export const array: Parser<unknown[]> = {
new ParserError(`Cannot pack nested arrays.`),
);

if (fixedLength !== undefined) {
assert(
fixedLength === value.length,
new ParserError(
`Array length does not match type length. Expected a length of ${fixedLength}, but received ${value.length}.`,
),
);
}

// 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 && isDynamicParser(getParser(arrayType), arrayType)) {
Expand All @@ -135,13 +144,6 @@ export const array: Parser<unknown[]> = {
}

if (fixedLength) {
assert(
fixedLength === value.length,
new ParserError(
`Array length does not match type length. Expected a length of ${fixedLength}, but received ${value.length}.`,
),
);

// `T[k]` for any `T` and `k` is encoded as `(T[0], ..., T[k - 1])`.
return tuple.encode({
type: getTupleType(arrayType, fixedLength),
Expand Down