diff --git a/yarn-project/foundation/src/abi/decoder.ts b/yarn-project/foundation/src/abi/decoder.ts index 778e109bf51b..4153be9f94c3 100644 --- a/yarn-project/foundation/src/abi/decoder.ts +++ b/yarn-project/foundation/src/abi/decoder.ts @@ -1,4 +1,4 @@ -import { ABIType, FunctionAbi } from '@aztec/foundation/abi'; +import { ABIParameter, ABIType, FunctionAbi } from '@aztec/foundation/abi'; import { Fr } from '@aztec/foundation/fields'; /** @@ -86,3 +86,44 @@ class ReturnValuesDecoder { export function decodeReturnValues(abi: FunctionAbi, returnValues: Fr[]) { return new ReturnValuesDecoder(abi, returnValues.slice()).decode(); } + +/** + * Decodes the signature of a function from the name and parameters. + */ +export class FunctionSignatureDecoder { + constructor(private name: string, private parameters: ABIParameter[]) {} + + /** + * Decodes a single function parameter for the function signature. + * @param param - The parameter to decode. + * @returns A string representing the parameter type. + */ + private decodeParameter(param: ABIType): string { + switch (param.kind) { + case 'field': + return 'Field'; + case 'integer': + if (param.sign === 'signed') { + throw new Error('Unsupported type: signed integer'); + } + return `u${param.width}`; + case 'boolean': + return 'bool'; + case 'array': + return `[${this.decodeParameter(param.type)};${param.length}]`; + case 'struct': + return `(${param.fields.map(field => `${this.decodeParameter(field.type)}`).join(',')})`; + default: + throw new Error(`Unsupported type: ${param.kind}`); + } + } + + /** + * Decodes all the parameters and build the function signature + * @returns The function signature. + */ + public decode(): string { + const hmm = this.parameters.map(param => this.decodeParameter(param.type)); + return `${this.name}(${hmm.join(',')})`; + } +} diff --git a/yarn-project/foundation/src/abi/function_selector.ts b/yarn-project/foundation/src/abi/function_selector.ts index dac9909eaaa7..bb932554a62f 100644 --- a/yarn-project/foundation/src/abi/function_selector.ts +++ b/yarn-project/foundation/src/abi/function_selector.ts @@ -1,4 +1,4 @@ -import { ABIParameter } from '@aztec/foundation/abi'; +import { ABIParameter, FunctionSignatureDecoder } from '@aztec/foundation/abi'; import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer'; import { keccak } from '@aztec/foundation/crypto'; import { Fr } from '@aztec/foundation/fields'; @@ -97,7 +97,7 @@ export class FunctionSelector { * @returns A Buffer containing the 4-byte function selector. */ static fromNameAndParameters(name: string, parameters: ABIParameter[]) { - const signature = name === 'constructor' ? name : `${name}(${parameters.map(p => p.type.kind).join(',')})`; + const signature = new FunctionSignatureDecoder(name, parameters).decode(); return FunctionSelector.fromSignature(signature); }