This is a secure implementation of a voting system using the commit-reveal scheme in Solidity. The system ensures vote privacy during the voting period and prevents vote manipulation.
The system is split into several contracts, each with a specific responsibility:
- CommitRevealVoting.sol: Main contract that orchestrates the voting process
- VotingPhases.sol: Library handling the different phases of voting
- VoterRegistry.sol: Manages voter registration
- CommitRevealLogic.sol: Handles the commit-reveal voting logic
The voting process has three phases:
-
Commit Phase:
- Voters submit a hash (commitment) of their vote and a secret
- The actual vote remains hidden during this phase
-
Reveal Phase:
- Voters reveal their vote and secret
- The contract verifies that the hash matches the original commitment
-
Results Phase:
- After the reveal phase ends, anyone can query the results
- Register as a voter:
await voting.registerVoter(voterAddress);
- Create a commitment:
const secret = ethers.utils.randomBytes(32);
const commitment = ethers.utils.keccak256(
ethers.utils.defaultAbiCoder.encode(
["bool", "bytes32"],
[voteChoice, secret]
)
);
- Submit your commitment during the commit phase:
await voting.commitVote(commitment);
- Reveal your vote during the reveal phase:
await voting.revealVote(voteChoice, secret);
- Deploy the contract:
const voting = await CommitRevealVoting.deploy(commitDuration, revealDuration);
- Start the voting:
await voting.startVoting();
- Get results after voting ends:
const [yesVotes, noVotes] = await voting.getResults();
- Vote privacy during commit phase
- Prevention of vote manipulation
- Immutable vote commitments
- Verifiable vote reveals
- Phase-based access control
Run the tests using:
npx hardhat test