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

Fix CI workflows for contract extensions #15

Merged
merged 6 commits into from
Aug 29, 2024
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
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