@@ -6,48 +6,62 @@ import GradientBG from '../components/GradientBG.js';
66import styles from '../styles/Home.module.css';
77import heroMinaLogo from '../public/assets/hero-mina-logo.svg';
88import arrowRightSmall from '../public/assets/arrow-right-small.svg';
9- import {fetchAccount, Mina, PublicKey, Field, Proof, Cache} from "o1js";
10- import { Add, AddZkProgram } from "../../contracts";
11- import cacheJSONList from "./cache.json";
9+ import {JsonProof} from "o1js";
10+ import ZkappWorkerClient from "./ZkappWorkerClient"
1211
1312// We've already deployed the Add contract on testnet at this address
1413// https://minascan.io/devnet/account/B62qnfpb1Wz7DrW7279B8nR8m4yY6wGJz4dnbAdkzfeUkpyp8aB9VCp
1514const zkAppAddress = "B62qnfpb1Wz7DrW7279B8nR8m4yY6wGJz4dnbAdkzfeUkpyp8aB9VCp";
1615
1716export default function Home() {
18- const zkApp = useRef<Add>(new Add(PublicKey.fromBase58(zkAppAddress)));
19-
17+
18+ const [zkappWorkerClient, setZkappWorkerClient] =
19+ useState<null | ZkappWorkerClient>(null);
2020 const [transactionLink, setTransactionLink] = useState<string | null>(null);
2121 const [contractState, setContractState] = useState<string | null>(null);
2222 const [zkProgramState, setZkProgramState] = useState<string | null>(null);
23- const [proof, setProof] = useState<Proof<Field, Field> | null>(null);
23+ const [proof, setProof] = useState<JsonProof | null>(null);
2424 const [error, setError] = useState<string | null>(null);
2525 const [loading, setLoading] = useState<boolean>(true);
2626
2727 // fetch the zkapp state when the page loads
2828 useEffect(() => {
2929 (async () => {
30- Mina.setActiveInstance(Mina.Network('https://api.minascan.io/node/devnet/v1/graphql'));
31- await fetchAccount({publicKey: zkAppAddress});
32- const num = zkApp.current.num.get();
30+ console.log("Loading zkApp worker client ...");
31+ const zkappWorkerClient = new ZkappWorkerClient();
32+ setZkappWorkerClient(zkappWorkerClient);
33+ await new Promise((resolve) => setTimeout(resolve, 5000));
34+ console.log("done Loading zkApp worker client");
35+
36+ console.log("Setting active instance to Devnet..");
37+ await zkappWorkerClient.setActiveInstanceToDevnet();
38+ await zkappWorkerClient.loadContract();
39+ await zkappWorkerClient.initZkappInstance(zkAppAddress);
40+
41+ await zkappWorkerClient.fetchAccount(zkAppAddress);
42+ const num = await zkappWorkerClient.getNum();
3343 setContractState(num.toString());
3444 setZkProgramState(num.toString());
3545
3646 // Compile the AddZkProgram
37- const cacheFiles = await fetchFiles();
3847 console.log("Compiling AddZkProgram");
3948 // ZkProgram cache in the browser is currently not fully supported.
40- await AddZkProgram.compile ();
41-
49+ await zkappWorkerClient.compileZkProgram ();
50+
4251 // Initialize the AddZkProgram with the initial state of the zkapp
43- console.log("Initialize AddZkProgram with intial contract state of zkapp");
44- const init = await AddZkProgram.init(num);
45- setProof(init.proof );
52+ console.log(
53+ "Initialize AddZkProgram with intial contract state of zkapp"
54+ );
4655
56+ const initProof = await zkappWorkerClient.initZkProgram(num.toString());
57+ setProof(initProof);
4758
4859 // Compile the contract so that o1js has the proving key required to execute contract calls
49- console.log("Compiling Add contract to generate proving and verification keys");
50- await Add.compile({ cache: FileSystem(cacheFiles) });
60+ console.log(
61+ "Compiling Add contract to generate proving and verification keys"
62+ );
63+
64+ await zkappWorkerClient.compileContract();
5165
5266 setLoading(false);
5367 })();
@@ -62,24 +76,23 @@ export default function Home() {
6276 const mina = (window as any).mina;
6377 const walletKey: string = (await mina.requestAccounts())[0];
6478 console.log("Connected wallet address: " + walletKey);
65- await fetchAccount({publicKey: PublicKey.fromBase58(walletKey)});
6679
80+ // await fetchAccount({ publicKey: PublicKey.fromBase58(walletKey) });
81+ await zkappWorkerClient!.fetchAccount(walletKey);
6782 // Execute a transaction locally on the browser
6883 let hash;
6984 if (proof) {
70- const transaction = await Mina.transaction(async () => {
71- console.log("Executing Add.settleState() locally");
72- await zkApp.current.settleState(proof);
73- });
74-
85+
86+ await zkappWorkerClient!.createSettleStateTransaction(proof);
7587 // Prove execution of the contract using the proving key
7688 console.log("Proving execution of Add.settleState()");
77- await transaction.prove ();
89+ await zkappWorkerClient!.proveSettleStateTransaction ();
7890
7991 // Broadcast the transaction to the Mina network
92+ const transactionJSON = await zkappWorkerClient!.getTransactionJSON();
8093 console.log("Broadcasting proof of execution to the Mina network");
8194 ({ hash } = await mina.sendTransaction({
82- transaction: transaction.toJSON()
95+ transaction: transactionJSON
8396 }));
8497 } else {
8598 throw Error("Proof passed to Add.settleState is null");
@@ -92,7 +105,11 @@ export default function Home() {
92105 console.error(e.message);
93106 let errorMessage = "";
94107
95- if (e.message.includes("Cannot read properties of undefined (reading 'requestAccounts')")) {
108+ if (
109+ e.message.includes(
110+ "Cannot read properties of undefined (reading 'requestAccounts')"
111+ )
112+ ) {
96113 errorMessage = "Is Auro installed?";
97114 } else if (e.message.includes("Please create or restore wallet first.")) {
98115 errorMessage = "Have you created a wallet?";
@@ -109,63 +126,22 @@ export default function Home() {
109126
110127 const updateZkProgram = useCallback(async () => {
111128 setLoading(true);
112-
129+
113130 if (contractState && proof) {
114- // Call the AddZkProgram update method
115- console.log("Calling AddZkProgram.update");
116- const update = await AddZkProgram.update(Field(contractState), proof);
117- setProof(update.proof);
118- setZkProgramState(update.proof.publicOutput.toString())
131+ const updateProof = await zkappWorkerClient!.updateZkProgram(
132+ contractState,
133+ proof
134+ );
135+
136+ setProof(updateProof);
137+ setZkProgramState(updateProof.publicOutput.toString());
119138 } else {
120- throw Error("Proof and or ContractState passed to AddZkProgram.update is null");
139+ throw Error(
140+ "Proof and or ContractState passed to AddZkProgram.update is null"
141+ );
121142 }
122- setLoading(false);
123- }, [proof]);
124-
125- const fetchFiles = async () => {
126- const cacheJson = cacheJSONList;
127- const cacheListPromises = cacheJson.files.map(async (file) => {
128- const [header, data] = await Promise.all([
129- fetch(\`/cache/\${file}.header\`).then((res) => res.text()),
130- fetch(\`/cache/\${file}\`).then((res) => res.text()),
131- ]);
132- return { file, header, data };
133- });
134-
135- const cacheList = await Promise.all(cacheListPromises);
136-
137- return cacheList.reduce((acc: any, { file, header, data }) => {
138- acc[file] = { file, header, data };
139- return acc;
140- }, {});
141- };
142-
143- const FileSystem = (files: any): Cache => ({
144- read({ persistentId, uniqueId, dataType }: any) {
145- if (!files[persistentId]) {
146- return undefined;
147- }
148-
149- const currentId = files[persistentId].header;
150-
151- if (currentId !== uniqueId) {
152- return undefined;
153- }
154-
155- if (dataType === "string") {
156- console.log("found in cache:", { persistentId, uniqueId, dataType });
157-
158- return new TextEncoder().encode(files[persistentId].data);
159- }
160- return undefined;
161- },
162-
163- write({ persistentId, uniqueId, dataType }: any, data: any) {
164- console.log({ persistentId, uniqueId, dataType });
165- },
166-
167- canWrite: true
168- });
143+ setLoading(false);
144+ }, [proof]);
169145
170146 return (
171147 <>
0 commit comments