-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54fd4d1
commit bf4a17f
Showing
6 changed files
with
78 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,69 @@ | ||
import { useEffect, useState } from 'react' | ||
import { ZKPClient } from 'enclave-circuits/src/client' | ||
import { useState } from 'react' | ||
import { Proof, ZKPClient } from 'enclave-circuits/src/client' | ||
import { Buffer } from 'buffer' | ||
import { handleGenericError } from '@/utils/handle-generic-error' | ||
import { useNotificationAlertContext } from '@/context/NotificationAlert' | ||
|
||
let cache: ZKPClient | undefined | ||
|
||
function useCircuit(): { client?: ZKPClient } { | ||
const [client, setClient] = useState<ZKPClient>() | ||
export const useCircuitHook = () => { | ||
const { showToast } = useNotificationAlertContext() | ||
const [zpkClient, setZPKClient] = useState<ZKPClient | null>(null) | ||
const [isLoading, setIsLoading] = useState<boolean>(false) | ||
|
||
useEffect(() => { | ||
const initCircuits = async () => { | ||
if (!cache) { | ||
Promise.all([ | ||
fetch(`${import.meta.env.PUBLIC_URL}/libs/wasm/circuits/vote_integrity/vote_integrity.wasm`).then((res) => res.arrayBuffer()), | ||
fetch(`${import.meta.env.PUBLIC_URL}/libs/wasm/circuits/vote_integrity/vote_integrity_0001.zkey`).then((res) => res.arrayBuffer()), | ||
]).then(([wasm, zkey]) => { | ||
console.log('wasm', wasm) | ||
console.log('zkey', zkey) | ||
new ZKPClient().init(Buffer.from(wasm), Buffer.from(zkey)).then((_client) => { | ||
if (!cache) { | ||
cache = _client | ||
} | ||
setClient(_client) | ||
try { | ||
setIsLoading(true) | ||
const [wasmBuffer, zkeyBuffer] = await Promise.all([ | ||
fetch('libs/wasm/circuits/vote_integrity/vote_integrity.wasm').then((res) => res.arrayBuffer()), | ||
fetch('libs/wasm/circuits/vote_integrity/vote_integrity_0001.zkey').then((res) => res.arrayBuffer()), | ||
]) | ||
|
||
const newClient = new ZKPClient() | ||
await newClient.init(Buffer.from(wasmBuffer), Buffer.from(zkeyBuffer)) | ||
|
||
cache = newClient | ||
setZPKClient(newClient) | ||
} catch (err) { | ||
showToast({ | ||
type: 'danger', | ||
message: 'Failed to initialize ZKPClient', | ||
}) | ||
}) | ||
handleGenericError('initCircuit', err as Error) | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
} else { | ||
setClient(cache) | ||
setZPKClient(cache) | ||
} | ||
} | ||
|
||
const proveVote = async (vote: number): Promise<Proof | undefined> => { | ||
if (!zpkClient) { | ||
console.error('ZKPClient not initialized') | ||
return | ||
} | ||
try { | ||
setIsLoading(true) | ||
return await zpkClient.prove({ vote }) | ||
} catch (err) { | ||
showToast({ | ||
type: 'danger', | ||
message: 'Failed to generate proof', | ||
}) | ||
handleGenericError('proveVote', err as Error) | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
}, []) | ||
} | ||
|
||
return { client } | ||
return { | ||
isLoading, | ||
zpkClient, | ||
initCircuits, | ||
proveVote, | ||
} | ||
} | ||
|
||
export default useCircuit | ||
export default useCircuitHook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters