diff --git a/docs/docs/dev_docs/cli/main.md b/docs/docs/dev_docs/cli/main.md index d74ff15b1cd5..745fd4b216c9 100644 --- a/docs/docs/dev_docs/cli/main.md +++ b/docs/docs/dev_docs/cli/main.md @@ -135,4 +135,10 @@ The `call` command calls a read-only method on a contract, one that will not gen - `--contract-artifact` - The artifact of the contract we are calling. - `--contract-address` - The address of the deployed contract -As you can see from the result, this address has a public balance of 543, as expected. \ No newline at end of file +As you can see from the result, this address has a public balance of 543, as expected. + +## Compute Function Selector +`aztec-cli --compute-selector ` gives the function selector. + +## Inspect Contract +`aztec-cli --compute-selector ` gives the list of all callable functions along with their function signature and selector. \ No newline at end of file diff --git a/yarn-project/cli/src/index.ts b/yarn-project/cli/src/index.ts index 3f0deea6ce12..fa72b0dc1b6d 100644 --- a/yarn-project/cli/src/index.ts +++ b/yarn-project/cli/src/index.ts @@ -10,7 +10,12 @@ import { getSchnorrAccount, isContractDeployed, } from '@aztec/aztec.js'; -import { StructType, decodeFunctionSignatureWithParameterNames } from '@aztec/foundation/abi'; +import { + FunctionSelector, + StructType, + decodeFunctionSignature, + decodeFunctionSignatureWithParameterNames, +} from '@aztec/foundation/abi'; import { JsonStringify } from '@aztec/foundation/json-rpc'; import { DebugLogger, LogFn } from '@aztec/foundation/log'; import { sleep } from '@aztec/foundation/sleep'; @@ -677,11 +682,24 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command { log(`No external functions found for contract ${contractArtifact.name}`); } for (const fn of contractFns) { - const signature = decodeFunctionSignatureWithParameterNames(fn.name, fn.parameters); - log(`${fn.functionType} ${signature}`); + const signatureWithParameterNames = decodeFunctionSignatureWithParameterNames(fn.name, fn.parameters); + const signature = decodeFunctionSignature(fn.name, fn.parameters); + const selector = FunctionSelector.fromSignature(signature); + log( + `${fn.functionType} ${signatureWithParameterNames} \n\tfunction signature: ${signature}\n\tselector: ${selector}`, + ); } }); + program + .command('compute-selector') + .description('Given a function signature, it computes a selector') + .argument('', 'Function signature to compute selector for e.g. foo(Field)') + .action((functionSignature: string) => { + const selector = FunctionSelector.fromSignature(functionSignature); + log(`${selector}`); + }); + compileContract(program, 'compile', log); generateTypescriptInterface(program, 'generate-typescript', log); generateNoirInterface(program, 'generate-noir-interface', log);