-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(contracts)!: introduces Semaphore extension contracts with v4 This PR implements extension contracts using version 4 re semaphore-protocol/semaphore#353 * refactor(contracts): fixing the voters contracts * docs(contracts): updated instructions on setting up contracts * ci(contracts): scripts that deploy contracts and gets addresses * test(contracts): added test for both whistleblowing and voting * refactor(contracts): refactored contracts to use v4 and added license and README Removed the need of merkletreesize whcih came with v3 * build(contracts): added dependencies and deploye contracts N * chore(contracts): corrected dependency switch @semaphore-protocol/contracts from a dev dependecy to a dependency * docs(contracts): refactored the docs * chore(contracts): fixed documentation and configuration files * docs(contracts): created a sym link to README A sym link of contracts/contracts/README.md to contracts/README.md * ci(contracts): added scripts to test and compile * ci(contracts): fixed config files and solhint script * docs(contracts): made README docs more general * fix(contracts): fixed event errors * refactor(contracts): changed the groups variable to semaphore since I am using the interface re n * refactor(contracts): removed unsused argument * fix(contracts): refactored the contracts and removed unuses parameters * docs(contracts): updated Documentation by removing groupId re N * refactor(contracts): removed inheritance from SemaphoreGroups * refactor(contracts): removed SemaphoreGroups import * fix(contracts): added proof Validation and fixed contracts deploy * refactor(contracts): refactored code and removed bool value in saved nullifier * refactor(contracts): removed nullifiers redudant mapping
- Loading branch information
Showing
23 changed files
with
5,209 additions
and
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"**/*.{js,ts,jsx,tsx,md,json,sol}": "prettier --write", | ||
"**/*.{js,ts,jsx,tsx,md,json,sol,yaml,yml}": "prettier --write", | ||
"**/*.{js,ts,jsx,tsx}": "eslint" | ||
} |
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 |
---|---|---|
|
@@ -4,13 +4,15 @@ | |
"packageManager": "[email protected]", | ||
"scripts": { | ||
"build": "yarn build:libraries", | ||
"build:libraries": "yarn workspaces foreach -A -t --no-private run build", | ||
"build:libraries": "yarn workspaces foreach -A --no-private run build", | ||
"compile:contracts": "yarn workspace extension-contracts compile", | ||
"test": "yarn test:libraries && yarn test:circuits", | ||
"test:libraries": "jest --coverage", | ||
"test:library": "jest packages/${0}", | ||
"test:circuits": "yarn workspace @semaphore-extensions/identity-proof.circom test", | ||
"test:contracts": "yarn workspace extension-contracts test:coverage", | ||
"lint:eslint": "eslint . --ext .js,.ts,.tsx", | ||
"lint": "yarn lint:eslint", | ||
"lint": "yarn lint:eslint && yarn workspace extension-contracts lint", | ||
"lint:fix": "yarn lint:eslint --fix", | ||
"format:prettier": "prettier -c .", | ||
"format:prettier:write": "prettier -w .", | ||
|
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ETHEREUM_PRIVATE_KEY= | ||
REPORT_GAS=false | ||
ETHERSCAN_API_KEY= | ||
COINMARKETCAP_API_KEY= |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"semi": false, | ||
"arrowParens": "always", | ||
"trailingComma": "none", | ||
"plugins": ["prettier-plugin-solidity"] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"extends": "solhint:recommended", | ||
"plugins": ["prettier"], | ||
"rules": { | ||
"code-complexity": ["error", 7], | ||
"compiler-version": ["error", ">=0.8.0"], | ||
"var-name-mixedcase": "off", | ||
"const-name-snakecase": "off", | ||
"no-empty-blocks": "off", | ||
"constructor-syntax": "error", | ||
"func-visibility": ["error", { "ignoreConstructors": true }], | ||
"max-line-length": ["error", 120], | ||
"not-rely-on-time": "off", | ||
"prettier/prettier": [ | ||
"error", | ||
{ | ||
"endOfLine": "auto" | ||
} | ||
], | ||
"reason-string": ["warn", { "maxLength": 80 }] | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
contracts/README.md |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Ethereum Foundation | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<p align="center"> | ||
<h1 align="center"> | ||
Semaphore Extension Contracts | ||
</h1> | ||
<p align="center">Solidity contracts to enhance Semaphore with extra features and customization</p> | ||
</p> | ||
<p align="center"> | ||
<a href="https://github.com/semaphore-protocol"> | ||
<img src="https://img.shields.io/badge/project-Semaphore-blue.svg?style=flat-square"> | ||
</a> | ||
<a href="https://github.com/semaphore-protocol/extensions/blob/main/packages/contracts/contracts/LICENSE"> | ||
<img alt="NPM license" src="https://img.shields.io/npm/l/%40semaphore-extensions%2Fcontracts?style=flat-square"> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@semaphore-extensions/contracts"> | ||
<img alt="NPM version" src="https://img.shields.io/npm/v/@semaphore-extensions/contracts?style=flat-square" /> | ||
</a> | ||
<a href="https://npmjs.org/package/@semaphore-extensions/contracts"> | ||
<img alt="Downloads" src="https://img.shields.io/npm/dm/@semaphore-extensions/contracts.svg?style=flat-square" /> | ||
</a> | ||
</p> | ||
<div align="center"> | ||
<h4> | ||
<a href="https://github.com/semaphore-protocol/extensions/blob/main/CONTRIBUTING.md"> | ||
👥 Contributing | ||
</a> | ||
<span> | </span> | ||
<a href="https://github.com/semaphore-protocol/extensions/blob/main/CODE_OF_CONDUCT.md"> | ||
🤝 Code of conduct | ||
</a> | ||
<span> | </span> | ||
<a href="https://github.com/semaphore-protocol/extensions/contribute"> | ||
🔎 Issues | ||
</a> | ||
<span> | </span> | ||
<a href="https://semaphore.pse.dev/discord"> | ||
🗣️ Chat & Support | ||
</a> | ||
</h4> | ||
</div> | ||
|
||
## 🛠 Install | ||
|
||
### npm or yarn | ||
|
||
Install the `@semaphore-extension/contracts` package with npm: | ||
|
||
```bash | ||
npm i @semaphore-extension/contracts | ||
``` | ||
|
||
or yarn: | ||
|
||
```bash | ||
yarn add @semaphore-extension/contracts | ||
``` | ||
|
||
## Usage | ||
|
||
### Compile contracts | ||
|
||
```bash | ||
yarn compile | ||
``` | ||
|
||
### Test contracts | ||
|
||
```bash | ||
yarn test | ||
``` | ||
|
||
You can also generate a test coverage report: | ||
|
||
```bash | ||
yarn test:coverage | ||
``` | ||
|
||
Or a test gas report: | ||
|
||
```bash | ||
yarn test:report-gas | ||
``` | ||
|
||
1. Copy the `.env.example` file as `.env`. | ||
|
||
```bash | ||
cp .env.example .env | ||
``` | ||
|
||
2. Add your environment variables. | ||
|
||
> **Note** | ||
> You should at least set a valid Ethereum URL (e.g. Infura) and a private key with some ethers. | ||
### Deploy contracts | ||
|
||
To deploy the `SemaphoreVoting.sol`: | ||
|
||
```bash | ||
yarn deploy:semaphore-voting | ||
``` | ||
|
||
To deploy the `SemaphoreWhistleblowing.sol`: | ||
|
||
```bash | ||
yarn deploy:semaphore-whistleblowing | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.23; | ||
|
||
import {ISemaphore} from "@semaphore-protocol/contracts/interfaces/ISemaphore.sol"; | ||
import {ISemaphoreVoting} from "./interfaces/ISemaphoreVoting.sol"; | ||
|
||
/// @title Semaphore Voting contract. | ||
/// @notice It allows users to vote anonymously in a poll. | ||
/// @dev The following code allows you to create polls, add voters and allow them to vote anonymously. | ||
contract SemaphoreVoting is ISemaphoreVoting { | ||
ISemaphore public semaphore; | ||
|
||
/// @dev Gets a poll id and returns the poll data. | ||
mapping(uint256 => Poll) internal polls; | ||
|
||
/// @dev Checks if the poll coordinator is the transaction sender. | ||
/// @param pollId: Id of the poll. | ||
modifier onlyCoordinator(uint256 pollId) { | ||
if (polls[pollId].coordinator != msg.sender) { | ||
revert SemaphoreVoting__CallerIsNotThePollCoordinator(); | ||
} | ||
|
||
_; | ||
} | ||
|
||
/// @dev Initializes the Semaphore group used to verify the user's ZK proofs. | ||
/// @param _semaphore: Semaphore group address. | ||
constructor(ISemaphore _semaphore) { | ||
semaphore = _semaphore; | ||
} | ||
|
||
/// @dev See {ISemaphoreVoting-createPoll}. | ||
function createPoll(address coordinator) public { | ||
uint256 groupId = semaphore.createGroup(); | ||
|
||
polls[groupId].coordinator = coordinator; | ||
polls[groupId].state = PollState.Created; | ||
|
||
emit PollCreated(groupId, coordinator); | ||
} | ||
|
||
/// @dev See {ISemaphoreVoting-addVoter}. | ||
function addVoter(uint256 pollId, uint256 identityCommitment) public onlyCoordinator(pollId) { | ||
if (polls[pollId].state != PollState.Created) { | ||
revert SemaphoreVoting__PollHasAlreadyBeenStarted(); | ||
} | ||
|
||
semaphore.addMember(pollId, identityCommitment); | ||
} | ||
|
||
/// @dev See {ISemaphoreVoting-startPoll}. | ||
function startPoll(uint256 pollId, uint256 encryptionKey) public override onlyCoordinator(pollId) { | ||
if (polls[pollId].state != PollState.Created) { | ||
revert SemaphoreVoting__PollHasAlreadyBeenStarted(); | ||
} | ||
|
||
polls[pollId].state = PollState.Ongoing; | ||
|
||
emit PollStarted(pollId, msg.sender, encryptionKey); | ||
} | ||
|
||
/// @dev See {ISemaphoreVoting-castVote}. | ||
function castVote( | ||
uint256 vote, | ||
uint256 pollId, | ||
uint256 merkleTreeDepth, | ||
uint256 nullifier, | ||
uint256 merkleTreeRoot, | ||
uint256[8] calldata proof | ||
) public { | ||
if (polls[pollId].state != PollState.Ongoing) { | ||
revert SemaphoreVoting__PollIsNotOngoing(); | ||
} | ||
|
||
ISemaphore.SemaphoreProof memory semaphoreProof = ISemaphore.SemaphoreProof({ | ||
merkleTreeDepth: merkleTreeDepth, | ||
merkleTreeRoot: merkleTreeRoot, | ||
nullifier: nullifier, | ||
message: vote, | ||
scope: pollId, | ||
points: proof | ||
}); | ||
|
||
semaphore.validateProof(pollId, semaphoreProof); | ||
emit VoteAdded(pollId, vote); | ||
} | ||
|
||
/// @dev See {ISemaphoreVoting-endPoll}. | ||
function endPoll(uint256 pollId, uint256 decryptionKey) public override onlyCoordinator(pollId) { | ||
polls[pollId].state = PollState.Ended; | ||
|
||
emit PollEnded(pollId, msg.sender, decryptionKey); | ||
} | ||
} |
Oops, something went wrong.