Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/v4 contracts #14

Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
48e547f
feat(contracts)!: introduces Semaphore extension contracts with v4
Dozie2001 Jul 12, 2024
69eba3f
refactor(contracts): fixing the voters contracts
Dozie2001 Jul 12, 2024
c05a825
docs(contracts): updated instructions on setting up contracts
Dozie2001 Jul 16, 2024
a7e1ac4
ci(contracts): scripts that deploy contracts and gets addresses
Dozie2001 Jul 16, 2024
606b72f
test(contracts): added test for both whistleblowing and voting
Dozie2001 Jul 17, 2024
bd82eef
refactor(contracts): refactored contracts to use v4 and added license…
Dozie2001 Jul 17, 2024
fb0f323
build(contracts): added dependencies and deploye contracts
Dozie2001 Jul 17, 2024
863e967
chore(contracts): corrected dependency
Dozie2001 Jul 17, 2024
0b04d8a
docs(contracts): refactored the docs
Dozie2001 Jul 17, 2024
1824a75
chore(contracts): fixed documentation and configuration files
Dozie2001 Jul 17, 2024
a7d0e46
docs(contracts): created a sym link to README
Dozie2001 Jul 20, 2024
be166c2
ci(contracts): added scripts to test and compile
Dozie2001 Jul 20, 2024
b22c3e2
ci(contracts): fixed config files and solhint script
Dozie2001 Jul 23, 2024
a384dcb
docs(contracts): made README docs more general
Dozie2001 Jul 23, 2024
92c7fc9
fix(contracts): fixed event errors
Dozie2001 Jul 24, 2024
f2ee102
refactor(contracts): changed the groups variable to semaphore since I…
Dozie2001 Jul 26, 2024
4ebca0f
refactor(contracts): removed unsused argument
Dozie2001 Jul 29, 2024
22ca14f
fix(contracts): refactored the contracts and removed unuses parameters
Dozie2001 Jul 30, 2024
ab24a70
docs(contracts): updated Documentation by removing groupId
Dozie2001 Aug 2, 2024
7223265
refactor(contracts): removed inheritance from SemaphoreGroups
Dozie2001 Aug 2, 2024
517ea8f
refactor(contracts): removed SemaphoreGroups import
Dozie2001 Aug 2, 2024
2a436b5
fix(contracts): added proof Validation and fixed contracts deploy
Dozie2001 Aug 5, 2024
f348d67
refactor(contracts): refactored code and removed bool value in saved …
Dozie2001 Aug 7, 2024
1e94729
refactor(contracts): removed nullifiers redudant mapping
Dozie2001 Aug 7, 2024
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
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,27 @@ node_modules/
# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# Production
build
dist
/docs

# Docusaurus cache and generated files
.docusaurus
.cache-loader

# Hardhat
artifacts
cache
typechain-types

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

Expand All @@ -69,3 +85,12 @@ dist
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# asdf
.tool-versions

# direnv
.envrc

# Other
snark-artifacts
2 changes: 1 addition & 1 deletion .lintstagedrc.json
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"
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"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:fix": "yarn lint:eslint --fix",
Expand Down
4 changes: 4 additions & 0 deletions packages/contracts/.env.example
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=
24 changes: 24 additions & 0 deletions packages/contracts/.eslintrc.json
Dozie2001 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"root": true,
"env": {
"es6": true
},
"extends": ["airbnb-base", "airbnb-typescript/base", "prettier"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"project": ["./tsconfig.json"]
},
"plugins": ["@typescript-eslint"],
"rules": {
"no-underscore-dangle": "off",
"import/no-extraneous-dependencies": "off",
"no-bitwise": "off",
"no-await-in-loop": "off",
"no-restricted-syntax": "off",
"no-console": ["warn", { "allow": ["info", "warn", "error", "log"] }],
"@typescript-eslint/lines-between-class-members": "off",
"no-param-reassign": "off"
}
}
6 changes: 6 additions & 0 deletions packages/contracts/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": false,
"arrowParens": "always",
"trailingComma": "none",
"plugins": ["prettier-plugin-solidity"]
}
1 change: 1 addition & 0 deletions packages/contracts/README.md
Dozie2001 marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 21 additions & 0 deletions packages/contracts/contracts/LICENSE
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.
103 changes: 103 additions & 0 deletions packages/contracts/contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<p align="center">
<h1 align="center">
Semaphore Extension Contracts
</h1>
<p align="center">Extension contracts to manage Voting and whistleBlowing</p>
Dozie2001 marked this conversation as resolved.
Show resolved Hide resolved
</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>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
<a href="https://github.com/semaphore-protocol/extensions/blob/main/CODE_OF_CONDUCT.md">
🤝 Code of conduct
</a>
<span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
<a href="https://github.com/semaphore-protocol/extensions/contribute">
🔎 Issues
</a>
<span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
<a href="https://semaphore.pse.dev/discord">
🗣️ Chat &amp; Support
</a>
</h4>
</div>

## 🛠 Install

### npm or yarn

Install the `@semaphore-extension/contracts` package with npm:

```bash
npm i @semaphore-extension/contracts
```

or yarn: <br>
Dozie2001 marked this conversation as resolved.
Show resolved Hide resolved

```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.

### Deploy contracts

Deploy the `SemaphoreVoting.sol` and `SemaphoreWhistleblowing.sol` contract without any parameter:

```bash
yarn deploy
Dozie2001 marked this conversation as resolved.
Show resolved Hide resolved
```

> **Note**
> You should at least set a valid Ethereum URL (e.g. Infura) and a private key with some ethers.

> **Warning**
> The group id is a number!
Dozie2001 marked this conversation as resolved.
Show resolved Hide resolved
103 changes: 103 additions & 0 deletions packages/contracts/contracts/SemaphoreVoting.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {ISemaphore} from "@semaphore-protocol/contracts/interfaces/ISemaphore.sol";
import {ISemaphoreVoting} from "./interfaces/ISemaphoreVoting.sol";
import {SemaphoreGroups} from "@semaphore-protocol/contracts/base/SemaphoreGroups.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, SemaphoreGroups {
ISemaphore public group;
cedoor marked this conversation as resolved.
Show resolved Hide resolved

/// @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 Semaphore__CallerIsNotThePollCoordinator();
}

_;
}

/// @dev Initializes the Semaphore group used to verify the user's ZK proofs.
/// @param _group: Semaphore group address.
constructor(ISemaphore _group) {
group = _group;
}

/// @dev See {ISemaphoreVoting-createPoll}.
function createPoll(uint256 pollId, address coordinator) public override {
Dozie2001 marked this conversation as resolved.
Show resolved Hide resolved
uint256 groupId = group.createGroup();

polls[pollId].coordinator = coordinator;
polls[pollId].groupId = groupId;

emit PollCreated(pollId, coordinator);
}

/// @dev See {ISemaphoreVoting-addVoter}.
function addVoter(uint256 pollId, uint256 identityCommitment) public override onlyCoordinator(pollId) {
if (polls[pollId].state != PollState.Created) {
revert Semaphore__PollHasAlreadyBeenStarted();
}

group.addMember(polls[pollId].groupId, identityCommitment);
}

/// @dev See {ISemaphoreVoting-startPoll}.
function startPoll(uint256 pollId, uint256 encryptionKey) public override onlyCoordinator(pollId) {
if (polls[pollId].state != PollState.Created) {
revert Semaphore__PollHasAlreadyBeenStarted();
}

polls[pollId].state = PollState.Ongoing;

emit PollStarted(pollId, msg.sender, encryptionKey);
}

/// @dev See {ISemaphoreVoting-castVote}.
function castVote(uint256 vote, uint256 nullifierHash, uint256 pollId, uint256[8] calldata proof) public override {
if (polls[pollId].state != PollState.Ongoing) {
revert Semaphore__PollIsNotOngoing();
Dozie2001 marked this conversation as resolved.
Show resolved Hide resolved
}

if (polls[pollId].nullifierHashes[nullifierHash]) {
revert Semaphore__YouAreUsingTheSameNullifierTwice();
}
Dozie2001 marked this conversation as resolved.
Show resolved Hide resolved

uint256 merkleTreeRoot = getMerkleTreeRoot(polls[pollId].groupId);

ISemaphore.SemaphoreProof memory semaphoreProof = ISemaphore.SemaphoreProof({
merkleTreeDepth: 32,
merkleTreeRoot: merkleTreeRoot,
nullifier: nullifierHash,
message: vote,
scope: pollId,
points: proof
});

group.verifyProof(polls[pollId].groupId, semaphoreProof);

polls[pollId].nullifierHashes[nullifierHash] = true;
Dozie2001 marked this conversation as resolved.
Show resolved Hide resolved

emit VoteAdded(pollId, vote);
}

/// @dev See {ISemaphoreVoting-endPoll}.
function endPoll(uint256 pollId, uint256 decryptionKey) public override onlyCoordinator(pollId) {
if (polls[pollId].state != PollState.Ongoing) {
revert Semaphore__PollIsNotOngoing();
}

polls[pollId].state = PollState.Ended;

emit PollEnded(pollId, msg.sender, decryptionKey);
}

function createPoll(uint256 pollId, address coordinator, uint256 merkleTreeDepth) external {}
}
Loading
Loading