From 8cbfb172351c9d891dd9d55cafaee26d32d30323 Mon Sep 17 00:00:00 2001
From: benesjan <janbenes1234@gmail.com>
Date: Thu, 14 Dec 2023 09:44:34 +0000
Subject: [PATCH] refactor: moved computeContractFunctionTreeRoot function to
 aztec.js

---
 yarn-project/aztec.js/src/index.ts              |  1 +
 yarn-project/aztec.js/src/utils/l2_contracts.ts | 17 +++++++++++++++++
 .../src/e2e_inclusion_proofs_contract.test.ts   | 11 ++---------
 3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/yarn-project/aztec.js/src/index.ts b/yarn-project/aztec.js/src/index.ts
index bf7610adc414..4bfe7a298b79 100644
--- a/yarn-project/aztec.js/src/index.ts
+++ b/yarn-project/aztec.js/src/index.ts
@@ -42,6 +42,7 @@ export {
   isContractDeployed,
   EthCheatCodes,
   computeAuthWitMessageHash,
+  computeContractFunctionTreeRoot,
 } from './utils/index.js';
 
 export { createPXEClient } from './pxe_client.js';
diff --git a/yarn-project/aztec.js/src/utils/l2_contracts.ts b/yarn-project/aztec.js/src/utils/l2_contracts.ts
index 67fcdb2851b2..6f75e16fea0c 100644
--- a/yarn-project/aztec.js/src/utils/l2_contracts.ts
+++ b/yarn-project/aztec.js/src/utils/l2_contracts.ts
@@ -1,3 +1,6 @@
+import { Fr, generateFunctionLeaves } from '@aztec/circuits.js';
+import { computeFunctionTreeRoot } from '@aztec/circuits.js/abis';
+import { ContractArtifact, FunctionSelector } from '@aztec/foundation/abi';
 import { AztecAddress } from '@aztec/foundation/aztec-address';
 import { PXE } from '@aztec/types';
 
@@ -10,3 +13,17 @@ import { PXE } from '@aztec/types';
 export async function isContractDeployed(pxe: PXE, contractAddress: AztecAddress): Promise<boolean> {
   return !!(await pxe.getContractData(contractAddress));
 }
+
+/**
+ * Computes the root of a function tree for a given smart contract artifact.
+ * @param artifact - The smart contract artifact.
+ * @returns The computed function tree root based on the functions in the given contract artifact.
+ */
+export function computeContractFunctionTreeRoot(artifact: ContractArtifact): Fr {
+  const functions = artifact.functions.map(f => ({
+    ...f,
+    selector: FunctionSelector.fromNameAndParameters(f.name, f.parameters),
+  }));
+  const functionLeaves = generateFunctionLeaves(functions);
+  return computeFunctionTreeRoot(functionLeaves);
+}
diff --git a/yarn-project/end-to-end/src/e2e_inclusion_proofs_contract.test.ts b/yarn-project/end-to-end/src/e2e_inclusion_proofs_contract.test.ts
index a75b4f2997b1..55a05381182b 100644
--- a/yarn-project/end-to-end/src/e2e_inclusion_proofs_contract.test.ts
+++ b/yarn-project/end-to-end/src/e2e_inclusion_proofs_contract.test.ts
@@ -3,12 +3,10 @@ import {
   AztecAddress,
   CompleteAddress,
   Fr,
-  FunctionSelector,
   INITIAL_L2_BLOCK_NUM,
   PXE,
+  computeContractFunctionTreeRoot,
 } from '@aztec/aztec.js';
-import { generateFunctionLeaves } from '@aztec/circuits.js';
-import { computeFunctionTreeRoot } from '@aztec/circuits.js/abis';
 import { InclusionProofsContract } from '@aztec/noir-contracts/types';
 
 import { jest } from '@jest/globals';
@@ -212,12 +210,7 @@ describe('e2e_inclusion_proofs_contract', () => {
 
   const getContractFunctionTreeRoot = () => {
     if (!contractFunctionTreeRoot) {
-      const functions = contract.artifact.functions.map(f => ({
-        ...f,
-        selector: FunctionSelector.fromNameAndParameters(f.name, f.parameters),
-      }));
-      const functionLeaves = generateFunctionLeaves(functions);
-      contractFunctionTreeRoot = computeFunctionTreeRoot(functionLeaves);
+      contractFunctionTreeRoot = computeContractFunctionTreeRoot(contract.artifact);
     }
     return contractFunctionTreeRoot;
   };