Skip to content
Closed
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
33 changes: 33 additions & 0 deletions yarn-project/stdlib/src/abi/encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,39 @@ describe('abi/encoder', () => {
expect(encodeArguments(abi, [arr])).toEqual(arr);
});

it('throws when array argument length does not match fixed-length ABI type', () => {
const abi: FunctionAbi = {
name: 'constructor',
isInitializer: true,
functionType: FunctionType.PRIVATE,
isOnlySelf: false,
isStatic: false,
parameters: [
{
name: 'proof',
type: {
kind: 'array',
length: 500,
type: { kind: 'field' },
},
visibility: 'private',
},
],
returnTypes: [],
errorTypes: {},
};

const oversized = Array.from({ length: 519 }, () => Fr.random());
expect(() => encodeArguments(abi, [oversized])).toThrow(
"Error encoding param 'proof': expected an array of length 500 and got 519 instead",
);

const undersized = Array.from({ length: 499 }, () => Fr.random());
expect(() => encodeArguments(abi, [undersized])).toThrow(
"Error encoding param 'proof': expected an array of length 500 and got 499 instead",
);
});

it('serializes string', () => {
const abi: FunctionAbi = {
name: 'constructor',
Expand Down
6 changes: 6 additions & 0 deletions yarn-project/stdlib/src/abi/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ class ArgumentEncoder {
this.flattened.push(new Fr(arg ? 1n : 0n));
break;
case 'array':
if (arg.length !== abiType.length) {
throw new Error(
`Error encoding param '${name ?? 'unnamed'}': ` +
`expected an array of length ${abiType.length} and got ${arg.length} instead`,
);
}
for (let i = 0; i < abiType.length; i += 1) {
this.encodeArgument(abiType.type, arg[i], `${name}[${i}]`);
}
Expand Down
Loading