Skip to content

Commit

Permalink
Fix CI workflows for contract extensions (#15)
Browse files Browse the repository at this point in the history
* Feat/v4 contracts (#14)

* 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

* chore: include contracts in scripts

* fix(contracts): fix contract extensions (#16)

* chore: update dependencies

* docs: add contracts package to readme

* refactor(contracts): update visibility of some functions (#17)

---------

Co-authored-by: Chidozie DAVID <[email protected]>
Co-authored-by: Vivian Plasencia <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2024
1 parent 0f81734 commit f4a913d
Show file tree
Hide file tree
Showing 26 changed files with 5,572 additions and 110 deletions.
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"
}
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@
<th>Version</th>
<th>Downloads</th>
<tbody>
<tr>
<td>
<a href="https://github.com/semaphore-protocol/extensions/tree/main/packages/contracts">
@semaphore-extensions/contracts
</a>
</td>
<td>
<!-- NPM version -->
<a href="https://www.npmjs.com/package/@semaphore-extensions/contracts">
<img alt="NPM Version" src="https://img.shields.io/npm/v/%40semaphore-extensions%2Fcontracts">
</a>
</td>
<td>
<!-- Downloads -->
<a href="https://npmjs.org/package/@semaphore-extensions/contracts">
<img alt="NPM Downloads" src="https://img.shields.io/npm/dm/%40semaphore-extensions%2Fcontracts">
</a>
</td>
</tr>
<tr>
<td>
<a href="https://github.com/semaphore-protocol/extensions/tree/main/packages/heyauthn">
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"private": true,
"packageManager": "[email protected]",
"scripts": {
"build": "yarn build:libraries",
"build:libraries": "yarn workspaces foreach -A -t --no-private run build",
"test": "yarn test:libraries && yarn test:circuits",
"build": "yarn build:libraries && yarn compile:libraries",
"build:libraries": "yarn workspaces foreach -A --no-private run build",
"compile:contracts": "yarn workspace contract-extensions compile",
"test": "yarn test:libraries && yarn test:contracts && 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 contract-extensions test:coverage",
"lint:eslint": "eslint . --ext .js,.ts,.tsx",
"lint": "yarn lint:eslint",
"lint": "yarn lint:eslint && yarn workspace contract-extensions lint",
"lint:fix": "yarn lint:eslint --fix",
"format:prettier": "prettier -c .",
"format:prettier:write": "prettier -w .",
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=
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"]
}
22 changes: 22 additions & 0 deletions packages/contracts/.solhint.json
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 }]
}
}
1 change: 1 addition & 0 deletions packages/contracts/README.md
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.
106 changes: 106 additions & 0 deletions packages/contracts/contracts/README.md
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>&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:

```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
```
98 changes: 98 additions & 0 deletions packages/contracts/contracts/SemaphoreVoting.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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) external override {
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) external override 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) external 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
) external override {
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) external override onlyCoordinator(pollId) {
if (polls[pollId].state != PollState.Ongoing) {
revert SemaphoreVoting__PollIsNotOngoing();
}

polls[pollId].state = PollState.Ended;

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

0 comments on commit f4a913d

Please sign in to comment.