|
1 | 1 | import { poseidon_gencontract as poseidonContract } from "circomlibjs" |
2 | | -import { Contract } from "ethers" |
3 | 2 | import { task, types } from "hardhat/config" |
4 | 3 |
|
5 | 4 | task("deploy:semaphore", "Deploy a Semaphore contract") |
6 | | - .addParam("verifiers", "Tree depths and verifier addresses", [], types.json) |
| 5 | + .addOptionalParam<boolean>("pairing", "Pairing library address", undefined, types.string) |
| 6 | + .addOptionalParam<boolean>("semaphoreVerifier", "SemaphoreVerifier contract address", undefined, types.string) |
| 7 | + .addOptionalParam<boolean>("poseidon", "Poseidon library address", undefined, types.string) |
| 8 | + .addOptionalParam<boolean>( |
| 9 | + "incrementalBinaryTree", |
| 10 | + "IncrementalBinaryTree library address", |
| 11 | + undefined, |
| 12 | + types.string |
| 13 | + ) |
7 | 14 | .addOptionalParam<boolean>("logs", "Print the logs", true, types.boolean) |
8 | | - .setAction(async ({ logs, verifiers }, { ethers }): Promise<Contract> => { |
9 | | - const poseidonABI = poseidonContract.generateABI(2) |
10 | | - const poseidonBytecode = poseidonContract.createCode(2) |
| 15 | + .setAction( |
| 16 | + async ( |
| 17 | + { |
| 18 | + logs, |
| 19 | + pairing: pairingAddress, |
| 20 | + semaphoreVerifier: semaphoreVerifierAddress, |
| 21 | + poseidon: poseidonAddress, |
| 22 | + incrementalBinaryTree: incrementalBinaryTreeAddress |
| 23 | + }, |
| 24 | + { ethers } |
| 25 | + ): Promise<any> => { |
| 26 | + if (!semaphoreVerifierAddress) { |
| 27 | + if (!pairingAddress) { |
| 28 | + const PairingFactory = await ethers.getContractFactory("Pairing") |
| 29 | + const pairing = await PairingFactory.deploy() |
11 | 30 |
|
12 | | - const [signer] = await ethers.getSigners() |
| 31 | + await pairing.deployed() |
13 | 32 |
|
14 | | - const PoseidonLibFactory = new ethers.ContractFactory(poseidonABI, poseidonBytecode, signer) |
15 | | - const poseidonLib = await PoseidonLibFactory.deploy() |
| 33 | + if (logs) { |
| 34 | + console.info(`Pairing library has been deployed to: ${pairing.address}`) |
| 35 | + } |
16 | 36 |
|
17 | | - await poseidonLib.deployed() |
| 37 | + pairingAddress = pairing.address |
| 38 | + } |
18 | 39 |
|
19 | | - if (logs) { |
20 | | - console.info(`Poseidon library has been deployed to: ${poseidonLib.address}`) |
21 | | - } |
| 40 | + const SemaphoreVerifierFactory = await ethers.getContractFactory("SemaphoreVerifier", { |
| 41 | + libraries: { |
| 42 | + Pairing: pairingAddress |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + const semaphoreVerifier = await SemaphoreVerifierFactory.deploy() |
| 47 | + |
| 48 | + await semaphoreVerifier.deployed() |
22 | 49 |
|
23 | | - const IncrementalBinaryTreeLibFactory = await ethers.getContractFactory("IncrementalBinaryTree", { |
24 | | - libraries: { |
25 | | - PoseidonT3: poseidonLib.address |
| 50 | + if (logs) { |
| 51 | + console.info(`SemaphoreVerifier contract has been deployed to: ${semaphoreVerifier.address}`) |
| 52 | + } |
| 53 | + |
| 54 | + semaphoreVerifierAddress = semaphoreVerifier.address |
26 | 55 | } |
27 | | - }) |
28 | | - const incrementalBinaryTreeLib = await IncrementalBinaryTreeLibFactory.deploy() |
29 | 56 |
|
30 | | - await incrementalBinaryTreeLib.deployed() |
| 57 | + if (!incrementalBinaryTreeAddress) { |
| 58 | + if (!poseidonAddress) { |
| 59 | + const poseidonABI = poseidonContract.generateABI(2) |
| 60 | + const poseidonBytecode = poseidonContract.createCode(2) |
31 | 61 |
|
32 | | - if (logs) { |
33 | | - console.info(`IncrementalBinaryTree library has been deployed to: ${incrementalBinaryTreeLib.address}`) |
34 | | - } |
| 62 | + const [signer] = await ethers.getSigners() |
| 63 | + |
| 64 | + const PoseidonFactory = new ethers.ContractFactory(poseidonABI, poseidonBytecode, signer) |
| 65 | + const poseidon = await PoseidonFactory.deploy() |
| 66 | + |
| 67 | + await poseidon.deployed() |
| 68 | + |
| 69 | + if (logs) { |
| 70 | + console.info(`Poseidon library has been deployed to: ${poseidon.address}`) |
| 71 | + } |
| 72 | + |
| 73 | + poseidonAddress = poseidon.address |
| 74 | + } |
35 | 75 |
|
36 | | - const SemaphoreContractFactory = await ethers.getContractFactory("Semaphore", { |
37 | | - libraries: { |
38 | | - IncrementalBinaryTree: incrementalBinaryTreeLib.address |
| 76 | + const IncrementalBinaryTreeFactory = await ethers.getContractFactory("IncrementalBinaryTree", { |
| 77 | + libraries: { |
| 78 | + PoseidonT3: poseidonAddress |
| 79 | + } |
| 80 | + }) |
| 81 | + const incrementalBinaryTree = await IncrementalBinaryTreeFactory.deploy() |
| 82 | + |
| 83 | + await incrementalBinaryTree.deployed() |
| 84 | + |
| 85 | + if (logs) { |
| 86 | + console.info(`IncrementalBinaryTree library has been deployed to: ${incrementalBinaryTree.address}`) |
| 87 | + } |
| 88 | + |
| 89 | + incrementalBinaryTreeAddress = incrementalBinaryTree.address |
39 | 90 | } |
40 | | - }) |
41 | 91 |
|
42 | | - const semaphoreContract = await SemaphoreContractFactory.deploy(verifiers) |
| 92 | + const SemaphoreFactory = await ethers.getContractFactory("Semaphore", { |
| 93 | + libraries: { |
| 94 | + IncrementalBinaryTree: incrementalBinaryTreeAddress |
| 95 | + } |
| 96 | + }) |
43 | 97 |
|
44 | | - await semaphoreContract.deployed() |
| 98 | + const semaphore = await SemaphoreFactory.deploy(semaphoreVerifierAddress) |
45 | 99 |
|
46 | | - if (logs) { |
47 | | - console.info(`Semaphore contract has been deployed to: ${semaphoreContract.address}`) |
48 | | - } |
| 100 | + await semaphore.deployed() |
49 | 101 |
|
50 | | - return semaphoreContract |
51 | | - }) |
| 102 | + if (logs) { |
| 103 | + console.info(`Semaphore contract has been deployed to: ${semaphore.address}`) |
| 104 | + } |
| 105 | + |
| 106 | + return { |
| 107 | + semaphore, |
| 108 | + pairingAddress, |
| 109 | + semaphoreVerifierAddress, |
| 110 | + poseidonAddress, |
| 111 | + incrementalBinaryTreeAddress |
| 112 | + } |
| 113 | + } |
| 114 | + ) |
0 commit comments