From fa6de7773b06aebef4b4e42a372dfb68a71627d4 Mon Sep 17 00:00:00 2001 From: cedoor Date: Mon, 22 Jan 2024 20:40:26 +0000 Subject: [PATCH] chore(cli): update contracts-hardhat template --- .../.env.example | 1 + .../contracts/Feedback.sol | 15 ++++---- .../hardhat.config.ts | 23 ++++++------ .../package.json | 37 ++++++------------- .../scripts/download-snark-artifacts.ts | 24 ------------ .../tasks/deploy.ts | 6 +-- .../test/Feedback.ts | 34 +++++++++-------- 7 files changed, 52 insertions(+), 88 deletions(-) delete mode 100644 packages/cli-template-contracts-hardhat/scripts/download-snark-artifacts.ts diff --git a/packages/cli-template-contracts-hardhat/.env.example b/packages/cli-template-contracts-hardhat/.env.example index df8d3b75..43e4291c 100644 --- a/packages/cli-template-contracts-hardhat/.env.example +++ b/packages/cli-template-contracts-hardhat/.env.example @@ -1,4 +1,5 @@ INFURA_API_KEY= ETHEREUM_PRIVATE_KEY= REPORT_GAS=false +ETHERSCAN_API_KEY= COINMARKETCAP_API_KEY= diff --git a/packages/cli-template-contracts-hardhat/contracts/Feedback.sol b/packages/cli-template-contracts-hardhat/contracts/Feedback.sol index 0c846db5..7b2cda0f 100644 --- a/packages/cli-template-contracts-hardhat/contracts/Feedback.sol +++ b/packages/cli-template-contracts-hardhat/contracts/Feedback.sol @@ -1,5 +1,5 @@ //SPDX-License-Identifier: MIT -pragma solidity ^0.8.4; +pragma solidity ^0.8.23; import "@semaphore-protocol/contracts/interfaces/ISemaphore.sol"; @@ -8,11 +8,11 @@ contract Feedback { uint256 public groupId; - constructor(address semaphoreAddress, uint256 _groupId) { - semaphore = ISemaphore(semaphoreAddress); + constructor(ISemaphore _semaphore, uint256 _groupId) { + semaphore = _semaphore; groupId = _groupId; - semaphore.createGroup(groupId, 20, address(this)); + semaphore.createGroup(groupId, address(this)); } function joinGroup(uint256 identityCommitment) external { @@ -20,11 +20,12 @@ contract Feedback { } function sendFeedback( - uint256 feedback, + uint256 merkleTreeDepth, uint256 merkleTreeRoot, - uint256 nullifierHash, + uint256 nullifier, + uint256 feedback, uint256[8] calldata proof ) external { - semaphore.verifyProof(groupId, merkleTreeRoot, feedback, nullifierHash, groupId, proof); + semaphore.validateProof(groupId, merkleTreeDepth, merkleTreeRoot, nullifier, feedback, groupId, proof); } } diff --git a/packages/cli-template-contracts-hardhat/hardhat.config.ts b/packages/cli-template-contracts-hardhat/hardhat.config.ts index 8a3d6b9b..04610ad6 100644 --- a/packages/cli-template-contracts-hardhat/hardhat.config.ts +++ b/packages/cli-template-contracts-hardhat/hardhat.config.ts @@ -1,5 +1,6 @@ -import "@nomiclabs/hardhat-ethers" import "@nomicfoundation/hardhat-chai-matchers" +import "@nomicfoundation/hardhat-ethers" +import "@nomicfoundation/hardhat-verify" import "@semaphore-protocol/hardhat" import "@typechain/hardhat" import { config as dotenvConfig } from "dotenv" @@ -7,7 +8,6 @@ import "hardhat-gas-reporter" import { HardhatUserConfig } from "hardhat/config" import { NetworksUserConfig } from "hardhat/types" import "solidity-coverage" -import { config } from "./package.json" import "./tasks/deploy" dotenvConfig() @@ -38,7 +38,7 @@ function getNetworks(): NetworksUserConfig { }, "arbitrum-sepolia": { url: "https://sepolia-rollup.arbitrum.io/rpc", - chainId: 7745327, + chainId: 421614, accounts }, arbitrum: { @@ -50,13 +50,7 @@ function getNetworks(): NetworksUserConfig { } const hardhatConfig: HardhatUserConfig = { - solidity: config.solidity, - paths: { - sources: config.paths.contracts, - tests: config.paths.tests, - cache: config.paths.cache, - artifacts: config.paths.build.contracts - }, + solidity: "0.8.23", networks: { hardhat: { chainId: 1337 @@ -69,8 +63,13 @@ const hardhatConfig: HardhatUserConfig = { coinmarketcap: process.env.COINMARKETCAP_API_KEY }, typechain: { - outDir: config.paths.build.typechain, - target: "ethers-v5" + target: "ethers-v6" + }, + etherscan: { + apiKey: process.env.ETHERSCAN_API_KEY + }, + sourcify: { + enabled: true } } diff --git a/packages/cli-template-contracts-hardhat/package.json b/packages/cli-template-contracts-hardhat/package.json index cf4ee825..de18550e 100644 --- a/packages/cli-template-contracts-hardhat/package.json +++ b/packages/cli-template-contracts-hardhat/package.json @@ -20,9 +20,8 @@ "scripts": { "dev": "hardhat node & yarn compile && yarn deploy --network localhost", "compile": "hardhat compile", - "download:snark-artifacts": "hardhat run scripts/download-snark-artifacts.ts", "deploy": "yarn compile && hardhat deploy", - "test": "hardhat run scripts/download-snark-artifacts.ts && hardhat test", + "test": "hardhat test", "test:report-gas": "REPORT_GAS=true hardhat test", "test:coverage": "hardhat coverage", "typechain": "hardhat typechain", @@ -31,31 +30,29 @@ "devDependencies": { "@ethersproject/abi": "^5.4.7", "@ethersproject/providers": "^5.4.7", - "@nomicfoundation/hardhat-chai-matchers": "^1.0.0", + "@nomicfoundation/hardhat-chai-matchers": "^2.0.3", + "@nomicfoundation/hardhat-ethers": "^3.0.0", "@nomicfoundation/hardhat-network-helpers": "^1.0.0", - "@nomicfoundation/hardhat-toolbox": "^2.0.0", - "@nomiclabs/hardhat-ethers": "^2.0.0", - "@nomiclabs/hardhat-etherscan": "^3.0.0", + "@nomicfoundation/hardhat-toolbox": "^4.0.0", + "@nomicfoundation/hardhat-verify": "^2.0.0", "@semaphore-protocol/group": "3.15.2", "@semaphore-protocol/hardhat": "3.15.2", "@semaphore-protocol/identity": "3.15.2", "@semaphore-protocol/proof": "3.15.2", - "@typechain/ethers-v5": "^10.1.0", - "@typechain/hardhat": "^6.1.2", + "@typechain/ethers-v6": "^0.5.0", + "@typechain/hardhat": "^9.0.0", "@types/chai": "^4.2.0", - "@types/download": "^8.0.1", "@types/mocha": "^9.1.0", "@types/node": "^20", "chai": "^4.2.0", "dotenv": "^16.0.3", - "download": "^8.0.0", - "ethers": "^5.4.7", - "hardhat": "^2.11.0", + "ethers": "^6.4.0", + "hardhat": "^2.19.4", "hardhat-gas-reporter": "^1.0.8", "solidity-coverage": "^0.8.1", - "ts-node": ">=8.0.0", - "typechain": "^8.1.0", - "typescript": ">=4.5.0" + "ts-node": "^10.9.2", + "typechain": "^8.3.0", + "typescript": "^5.3.3" }, "dependencies": { "@semaphore-protocol/contracts": "3.15.2" @@ -63,16 +60,6 @@ "config": { "solidity": { "version": "0.8.4" - }, - "paths": { - "contracts": "./contracts", - "tests": "./test", - "cache": "./cache", - "build": { - "snark-artifacts": "./build/snark-artifacts", - "contracts": "./build/contracts", - "typechain": "./build/typechain" - } } } } diff --git a/packages/cli-template-contracts-hardhat/scripts/download-snark-artifacts.ts b/packages/cli-template-contracts-hardhat/scripts/download-snark-artifacts.ts deleted file mode 100644 index 6051397e..00000000 --- a/packages/cli-template-contracts-hardhat/scripts/download-snark-artifacts.ts +++ /dev/null @@ -1,24 +0,0 @@ -import download from "download" -import fs from "fs" -import { config } from "../package.json" - -async function main() { - const snarkArtifactsPath = config.paths.build["snark-artifacts"] - const url = `http://www.trusted-setup-pse.org/semaphore/${20}` - - if (!fs.existsSync(snarkArtifactsPath)) { - fs.mkdirSync(snarkArtifactsPath, { recursive: true }) - } - - if (!fs.existsSync(`${snarkArtifactsPath}/semaphore.zkey`)) { - await download(`${url}/semaphore.wasm`, snarkArtifactsPath) - await download(`${url}/semaphore.zkey`, snarkArtifactsPath) - } -} - -main() - .then(() => process.exit(0)) - .catch((error) => { - console.error(error) - process.exit(1) - }) diff --git a/packages/cli-template-contracts-hardhat/tasks/deploy.ts b/packages/cli-template-contracts-hardhat/tasks/deploy.ts index 85b43377..0dd175e4 100644 --- a/packages/cli-template-contracts-hardhat/tasks/deploy.ts +++ b/packages/cli-template-contracts-hardhat/tasks/deploy.ts @@ -10,7 +10,7 @@ task("deploy", "Deploy a Feedback contract") logs }) - semaphoreAddress = semaphore.address + semaphoreAddress = await semaphore.getAddress() } if (!groupId) { @@ -21,10 +21,8 @@ task("deploy", "Deploy a Feedback contract") const feedbackContract = await FeedbackFactory.deploy(semaphoreAddress, groupId) - await feedbackContract.deployed() - if (logs) { - console.info(`Feedback contract has been deployed to: ${feedbackContract.address}`) + console.info(`Feedback contract has been deployed to: ${await feedbackContract.getAddress()}`) } return feedbackContract diff --git a/packages/cli-template-contracts-hardhat/test/Feedback.ts b/packages/cli-template-contracts-hardhat/test/Feedback.ts index 08227e21..cd36975a 100644 --- a/packages/cli-template-contracts-hardhat/test/Feedback.ts +++ b/packages/cli-template-contracts-hardhat/test/Feedback.ts @@ -2,18 +2,17 @@ import { Group } from "@semaphore-protocol/group" import { Identity } from "@semaphore-protocol/identity" import { generateProof } from "@semaphore-protocol/proof" import { expect } from "chai" -import { formatBytes32String } from "ethers/lib/utils" +import { encodeBytes32String } from "ethers" import { run } from "hardhat" // @ts-ignore: typechain folder will be generated after contracts compilation import { Feedback } from "../build/typechain" -import { config } from "../package.json" describe("Feedback", () => { let feedbackContract: Feedback let semaphoreContract: string const groupId = "42" - const group = new Group(groupId) + const group = new Group() const users: Identity[] = [] before(async () => { @@ -21,7 +20,7 @@ describe("Feedback", () => { logs: false }) - feedbackContract = await run("deploy", { logs: false, group: groupId, semaphore: semaphore.address }) + feedbackContract = await run("deploy", { logs: false, group: groupId, semaphore: await semaphore.getAddress() }) semaphoreContract = semaphore users.push(new Identity()) @@ -43,27 +42,30 @@ describe("Feedback", () => { }) describe("# sendFeedback", () => { - const wasmFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.wasm` - const zkeyFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.zkey` - it("Should allow users to send feedback anonymously", async () => { - const feedback = formatBytes32String("Hello World") + const feedback = encodeBytes32String("Hello World") - const fullProof = await generateProof(users[1], group, groupId, feedback, { - wasmFilePath, - zkeyFilePath - }) + const fullProof = await generateProof(users[1], group, feedback, groupId) const transaction = feedbackContract.sendFeedback( - feedback, + fullProof.merkleTreeDepth, fullProof.merkleTreeRoot, - fullProof.nullifierHash, + fullProof.nullifier, + feedback, fullProof.proof ) await expect(transaction) - .to.emit(semaphoreContract, "ProofVerified") - .withArgs(groupId, fullProof.merkleTreeRoot, fullProof.nullifierHash, groupId, fullProof.signal) + .to.emit(semaphoreContract, "ProofValidated") + .withArgs( + groupId, + fullProof.merkleTreeDepth, + fullProof.merkleTreeRoot, + fullProof.nullifier, + fullProof.message, + groupId, + fullProof.proof + ) }) }) })