Skip to content

Commit

Permalink
feat: encoder runtime arguments check (#1246)
Browse files Browse the repository at this point in the history
# Description

Fixes #1227
  • Loading branch information
benesjan authored Jul 31, 2023
1 parent 5bf5674 commit 407b8b5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
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

0 comments on commit 407b8b5

Please sign in to comment.