Skip to content

Commit

Permalink
refactor finished
Browse files Browse the repository at this point in the history
  • Loading branch information
signorecello committed Feb 14, 2024
1 parent cdf911d commit 91eedaf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
6 changes: 4 additions & 2 deletions vite-hardhat/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import React from 'react';

import { useOnChainVerification } from '../hooks/useOnChainVerification.jsx';
import { useProofGeneration } from '../hooks/useProofGeneration.jsx';
import { useOffChainVerification } from '../hooks/useOffChainVerification.jsx';

function Component() {
const [input, setInput] = useState<{ x: string; y: string } | undefined>();
const { proofData } = useProofGeneration(input);
// useOnChainVerification(proofData);
const { noir, proofData } = useProofGeneration(input);
useOffChainVerification(noir, proofData);
useOnChainVerification(proofData);

const submit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
Expand Down
19 changes: 19 additions & 0 deletions vite-hardhat/hooks/useOffChainVerification.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client';

import { ProofData } from '@noir-lang/types';
import { useEffect } from 'react';
import { toast } from 'react-toastify';
import { BarretenbergBackend } from '@noir-lang/backend_barretenberg';
import { Noir } from '@noir-lang/noir_js';

export function useOffChainVerification(noir?: Noir, proofData?: ProofData) {
useEffect(() => {
if (!proofData || !noir) return;

toast.promise(noir.verifyFinalProof(proofData), {
pending: 'Verifying proof off-chain',
success: 'Proof verified off-chain',
error: 'Error verifying proof off-chain',
});
}, [proofData]);
}
1 change: 0 additions & 1 deletion vite-hardhat/hooks/useOnChainVerification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export function useOnChainVerification(proofData?: ProofData) {
}, [isConnected]);

useEffect(() => {
console.log(data);
if (data) {
toast.update(onChainToast, {
type: 'success',
Expand Down
12 changes: 4 additions & 8 deletions vite-hardhat/hooks/useProofGeneration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Noir } from '@noir-lang/noir_js';

export function useProofGeneration(inputs?: { [key: string]: string }) {
const [proofData, setProofData] = useState<ProofData | undefined>();
const [noir, setNoir] = useState<Noir | undefined>();

const proofGeneration = async () => {
if (!inputs) return;
Expand All @@ -25,19 +26,14 @@ export function useProofGeneration(inputs?: { [key: string]: string }) {
error: 'Error generating proof',
});

const res = await toast.promise(noir.verifyFinalProof(data), {
pending: 'Verifying proof off-chain',
success: 'Proof verified off-chain',
error: 'Error verifying proof off-chain',
});

console.log(res);
setProofData(data);
setNoir(noir);
};

useEffect(() => {
if (!inputs) return;
proofGeneration();
}, [inputs]);

return { proofData };
return { noir, proofData };
}
2 changes: 1 addition & 1 deletion vite-hardhat/utils/addresses.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"chainId":31337,"verifier":"0x4ed7c70f96b99c776995fb64377f0d4ab3b0e1c1"}
{"chainId":31337,"verifier":"0x322813fd9a801c5507c9de605d63cea4f2ce6c44"}

0 comments on commit 91eedaf

Please sign in to comment.