Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: encoder runtime arguments check #1246

Merged
merged 6 commits into from
Jul 31, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,13 @@ describe('Private Execution test suite', () => {

oracle.getPortalContractAddress.mockImplementation(() => Promise.resolve(EthAddress.ZERO));

const args = [amountToTransfer, owner, insertFnSelector, getThenNullifyFnSelector, getZeroFnSelector];
const args = [
amountToTransfer,
owner,
Fr.fromBuffer(insertFnSelector),
Fr.fromBuffer(getThenNullifyFnSelector),
Fr.fromBuffer(getZeroFnSelector),
];
const result = await runSimulator({
args: args,
abi: abi,
Expand Down
78 changes: 78 additions & 0 deletions yarn-project/foundation/src/abi/encoder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { ABIParameterVisibility, FunctionAbi, FunctionType } from './abi.js';
import { encodeArguments } from './encoder.js';

describe('abi/encoder', () => {
it('throws when passing string argument as field', () => {
const testFunctionAbi: FunctionAbi = {
name: 'constructor',
functionType: FunctionType.SECRET,
isInternal: false,
parameters: [
{
name: 'owner',
type: {
kind: 'field',
},
visibility: ABIParameterVisibility.SECRET,
},
],
returnTypes: [],
bytecode: '',
verificationKey: '',
};
const args = ['garbage'];

expect(() => encodeArguments(testFunctionAbi, args)).toThrowError('Invalid argument "garbage" of type field');
});

it('throws when passing string argument as integer', () => {
const testFunctionAbi: FunctionAbi = {
name: 'constructor',
functionType: FunctionType.SECRET,
isInternal: false,
parameters: [
{
name: 'isOwner',
type: {
sign: 'value',
width: 5,
kind: 'integer',
},
visibility: ABIParameterVisibility.SECRET,
},
],
returnTypes: [],
bytecode: '',
verificationKey: '',
};
const args = ['garbage'];
expect(() => encodeArguments(testFunctionAbi, args)).toThrowError('Cannot convert garbage to a BigInt');
});

it.only('throws when passing object argument as field', () => {
const testFunctionAbi: FunctionAbi = {
name: 'constructor',
functionType: FunctionType.SECRET,
isInternal: false,
parameters: [
{
name: 'owner',
type: {
kind: 'field',
},
visibility: ABIParameterVisibility.SECRET,
},
],
returnTypes: [],
bytecode: '',
verificationKey: '',
};
const args = [
{
value: 'garbage',
},
];

expect(() => encodeArguments(testFunctionAbi, args)).toThrowError('Argument cannot be serialised to a field');
});
});
6 changes: 5 additions & 1 deletion yarn-project/foundation/src/abi/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ class ArgumentEncoder {
} else if (typeof arg === 'object') {
if (typeof arg.toField === 'function') {
this.flattened.push(arg.toField());
} else {
} else if (arg instanceof Fr) {
this.flattened.push(arg);
} else {
throw new Error('Argument cannot be serialised to a field');
}
} else {
throw new Error(`Invalid argument "${arg}" of type ${abiType.kind}`);
}
break;
case 'boolean':
Expand Down