Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/CRISP/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ playwright-report/
.enclave/data/
.enclave/config/
.enclave/caches/
cache_hardhat/
cache_hardhat/
3 changes: 1 addition & 2 deletions examples/CRISP/client/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
VITE_ENCLAVE_API=http://127.0.0.1:4000
VITE_TWITTER_SERVERLESS_API=
VITE_WALLETCONNECT_PROJECT_ID=
VITE_E3_PROGRAM_ADDRESS=0x322813Fd9A801c5507c9de605d63CEA4f2CE6c44 # Default E3 program address from anvil
VITE_SEMAPHORE_ADDRESS=0x9A9f2CCfdE556A7E9Ff0848998Aa4a0CFD8863AE
VITE_E3_PROGRAM_ADDRESS=0x959922bE3CAee4b8Cd9a407cc3ac1C251C2007B1 # Default E3 program address from anvil
4 changes: 0 additions & 4 deletions examples/CRISP/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
"@noir-lang/noir_js": "1.0.0-beta.3",
"@noir-lang/noirc_abi": "1.0.0-beta.3",
"@phosphor-icons/react": "^2.1.4",
"@semaphore-protocol/identity": "^4.12.1",
"@semaphore-protocol/data": "^4.12.1",
"@semaphore-protocol/group": "^4.12.1",
"@hashcloak/semaphore-noir-proof": "1.0.0",
"@svgr/rollup": "^8.1.0",
"@tanstack/react-query": "^5.74.3",
"axios": "^1.6.8",
Expand Down
53 changes: 0 additions & 53 deletions examples/CRISP/client/src/components/RegistrationModal.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import { VoteManagementContextType, VoteManagementProviderProps } from '@/contex
import { useWebAssemblyHook } from '@/hooks/wasm/useWebAssembly'
import { useEffect, useState } from 'react'
import { useAccount } from 'wagmi'
import { Identity } from '@semaphore-protocol/identity'
import { VoteStateLite, VotingRound } from '@/model/vote.model'
import { useEnclaveServer } from '@/hooks/enclave/useEnclaveServer'
import { convertPollData, convertTimestampToDate } from '@/utils/methods'
import { Poll, PollResult } from '@/model/poll.model'
import { generatePoll } from '@/utils/generate-random-poll'
import { handleGenericError } from '@/utils/handle-generic-error'
import { useSemaphoreGroupManagement } from '@/hooks/semaphore/useSemaphoreGroupManagement'

const [useVoteManagementContext, VoteManagementContextProvider] = createGenericContext<VoteManagementContextType>()

Expand All @@ -29,7 +27,6 @@ const VoteManagementProvider = ({ children }: VoteManagementProviderProps) => {
/**
* Voting Management States
**/
const [semaphoreIdentity, setSemaphoreIdentity] = useState<Identity | null>(null)
const [user, setUser] = useState<{ address: string } | null>(null)
const [roundState, setRoundState] = useState<VoteStateLite | null>(null)
const [votingRound, setVotingRound] = useState<VotingRound | null>(null)
Expand All @@ -53,15 +50,6 @@ const VoteManagementProvider = ({ children }: VoteManagementProviderProps) => {
broadcastVote,
} = useEnclaveServer()

const {
groupId: currentSemaphoreGroupId,
groupMembers: currentGroupMembers,
isFetchingMembers: fetchingMembers,
isRegistering,
isCommitted: isRegisteredForCurrentRound,
registerIdentity: registerIdentityOnContract,
} = useSemaphoreGroupManagement(roundState?.id, roundState?.start_block, semaphoreIdentity)

const initialLoad = async () => {
console.log('Loading wasm')
const currentRound = await getCurrentRound()
Expand Down Expand Up @@ -116,24 +104,6 @@ const VoteManagementProvider = ({ children }: VoteManagementProviderProps) => {
setIsLoading(false)
}, [wasmLoading, enclaveLoading])

useEffect(() => {
if (!(votingRound?.round_id == null) && user?.address) {
// TODO: important: generate this from signature entropy and store encrypted in browser storage based on a password
const seedString = `semaphore-identity-${user.address}-${votingRound.round_id}`
try {
const identity = new Identity(seedString)
setSemaphoreIdentity(identity)
console.log('Deterministic Semaphore identity generated.')
} catch (error) {
console.error('Failed to generate deterministic Semaphore identity.', error)
setSemaphoreIdentity(null)
}
} else {
setSemaphoreIdentity(null)
console.log('No round ID or user address found, Semaphore identity set to null.')
}
}, [user?.address, votingRound?.round_id])

useEffect(() => {
if (isConnected && address) {
setUser({ address })
Expand All @@ -147,13 +117,7 @@ const VoteManagementProvider = ({ children }: VoteManagementProviderProps) => {
value={{
isLoading,
user,
semaphoreIdentity,
votingRound,
isRegistering,
fetchingMembers,
currentSemaphoreGroupId,
currentGroupMembers,
isRegisteredForCurrentRound,
roundEndDate,
pollOptions,
roundState,
Expand All @@ -171,7 +135,6 @@ const VoteManagementProvider = ({ children }: VoteManagementProviderProps) => {
setPollOptions,
initialLoad,
broadcastVote,
registerIdentityOnContract,
setVotingRound,
setUser,
encryptVote,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@
import { ReactNode } from 'react'
import { BroadcastVoteRequest, BroadcastVoteResponse, VoteStateLite, VotingRound, EncryptedVote } from '@/model/vote.model'
import { Poll, PollRequestResult, PollResult } from '@/model/poll.model'
import { Identity } from '@semaphore-protocol/identity'

export type VoteManagementContextType = {
isLoading: boolean
user: { address: string } | null
semaphoreIdentity: Identity | null
isRegistering: boolean
isRegisteredForCurrentRound: boolean
fetchingMembers: boolean
currentGroupMembers: string[]
currentSemaphoreGroupId: bigint | null
votingRound: VotingRound | null
roundEndDate: Date | null
pollOptions: Poll[]
Expand All @@ -35,7 +28,6 @@ export type VoteManagementContextType = {
setVotingRound: React.Dispatch<React.SetStateAction<VotingRound | null>>
setUser: React.Dispatch<React.SetStateAction<{ address: string } | null>>
encryptVote: (voteId: bigint, publicKey: Uint8Array) => Promise<EncryptedVote | undefined>
registerIdentityOnContract: () => void
broadcastVote: (vote: BroadcastVoteRequest) => Promise<BroadcastVoteResponse | undefined>
getRoundStateLite: (roundCount: number) => Promise<void>
setPastPolls: React.Dispatch<React.SetStateAction<PollResult[]>>
Expand Down

This file was deleted.

37 changes: 3 additions & 34 deletions examples/CRISP/client/src/hooks/voting/useVoteCasting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ import { useVoteManagementContext } from '@/context/voteManagement';
import { useNotificationAlertContext } from '@/context/NotificationAlert/NotificationAlert.context.tsx';
import { Poll } from '@/model/poll.model';
import { BroadcastVoteRequest } from '@/model/vote.model';
import { Group } from '@semaphore-protocol/group';
import { generateNoirProof, SemaphoreNoirProof, initSemaphoreNoirBackend } from '@hashcloak/semaphore-noir-proof';
import { encodeSemaphoreProof } from '@/utils/proof-encoding';

export const useVoteCasting = () => {
const {
user,
roundState,
votingRound,
semaphoreIdentity,
currentGroupMembers,
fetchingMembers,
isRegisteredForCurrentRound,
encryptVote,
broadcastVote,
setTxUrl,
Expand All @@ -46,19 +39,9 @@ export const useVoteCasting = () => {
showToast({ type: 'danger', message: 'Please select a poll option first.' });
return;
}
if (!user || !isRegisteredForCurrentRound || !roundState || !semaphoreIdentity) {
console.error("Cannot cast vote: Missing user, registration, round state, or identity.");
showToast({ type: 'danger', message: 'Cannot cast vote. Ensure you are connected, registered, and the round is active.' });
return;
}
if (fetchingMembers) {
console.log("Cannot cast vote: Still fetching group members.");
showToast({ type: 'danger', message: 'Group members are still loading. Please wait.' });
return;
}
if (!currentGroupMembers || currentGroupMembers.length === 0) {
console.error("Cannot cast vote: No group members found for this round.");
showToast({ type: 'danger', message: 'Could not load group members for this round.' });
if (!user || !roundState) {
console.error("Cannot cast vote: Missing user or round state.");
showToast({ type: 'danger', message: 'Cannot cast vote. Ensure you are connected, and the round is active.' });
return;
}

Expand All @@ -71,22 +54,12 @@ export const useVoteCasting = () => {
throw new Error("Failed to encrypt vote.");
}

const group = new Group(currentGroupMembers);
const scope = String(roundState.id);
// the message does not matter
const message = "e3";
const merkleTreeDepth = 10;
const noirBackend = await initSemaphoreNoirBackend(merkleTreeDepth);
const fullProof: SemaphoreNoirProof = await generateNoirProof(semaphoreIdentity, group, message, scope, noirBackend, true);
const proofBytes = encodeSemaphoreProof(fullProof);

const voteRequest: BroadcastVoteRequest = {
round_id: roundState.id,
enc_vote_bytes: Array.from(voteEncrypted.vote),
proof: Array.from(voteEncrypted.proof),
public_inputs: voteEncrypted.public_inputs,
address: user.address,
proof_sem: Array.from(proofBytes)
};

const broadcastVoteResponse = await broadcastVote(voteRequest);
Expand Down Expand Up @@ -136,10 +109,6 @@ export const useVoteCasting = () => {
user,
roundState,
votingRound,
semaphoreIdentity,
currentGroupMembers,
fetchingMembers,
isRegisteredForCurrentRound,
encryptVote,
broadcastVote,
setTxUrl,
Expand Down
Loading
Loading