Skip to content

Commit

Permalink
refactor(contracts): update scripts to save deployed contract addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Jan 31, 2024
1 parent caf1601 commit c610213
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 102 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ dist
artifacts
cache
typechain-types
packages/contracts/deployed-contracts/undefined.json
packages/contracts/deployed-contracts/hardhat.json
packages/contracts/deployed-contracts/localhost.json

# Stores VSCode versions used for testing VSCode extensions
.vscode-test
Expand Down
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ coverage.json
artifacts
cache
typechain-types
packages/contracts/deployed-contracts/undefined.json
packages/contracts/deployed-contracts/hardhat.json
packages/contracts/deployed-contracts/localhost.json

# production
dist
Expand Down
20 changes: 20 additions & 0 deletions packages/contracts/deployed-contracts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"network": "sepolia",
"contracts": [
{
"name": "SemaphoreVerifier",
"address": "0x1E979ECf8C23d45577904974642592f80C464522"
},
{
"name": "PoseidonT3",
"address": "0x43AE9518d9FE43898cD705a06C22A73B015bCf12"
},
{
"name": "Semaphore",
"address": "0x5B8e7cC7bAC61A4b952d472b67056B2f260ba6dc"
}
]
}
]

7 changes: 0 additions & 7 deletions packages/contracts/deployed-contracts/arbitrum.json

This file was deleted.

7 changes: 0 additions & 7 deletions packages/contracts/deployed-contracts/goerli.json

This file was deleted.

7 changes: 0 additions & 7 deletions packages/contracts/deployed-contracts/mumbai.json

This file was deleted.

5 changes: 0 additions & 5 deletions packages/contracts/deployed-contracts/sepolia.json

This file was deleted.

68 changes: 35 additions & 33 deletions packages/contracts/scripts/create-mock-groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,58 @@ import { Group } from "@semaphore-protocol/group"
import { Identity } from "@semaphore-protocol/identity"
import { generateProof } from "@semaphore-protocol/proof"
import { ethers, hardhatArguments } from "hardhat"
import { getDeployedContracts } from "./utils"
import { getDeployedContractAddress } from "./utils"

async function main() {
const deployedContracts = getDeployedContracts(hardhatArguments.network)
if (!hardhatArguments.network) {
throw Error("Please, define a supported network")
}

if (deployedContracts) {
const semaphoreContract = await ethers.getContractAt("Semaphore", deployedContracts.Semaphore)
const semaphoreAddress = getDeployedContractAddress(hardhatArguments.network, "Semaphore")

const [admin] = await ethers.getSigners()
const adminAddress = await admin.getAddress()
const semaphoreContract = await ethers.getContractAt("Semaphore", semaphoreAddress)

const identity = new Identity(0)
const members = Array.from({ length: 3 }, (_, i) => new Identity(i)).map(({ commitment }) => commitment)
const group = new Group(members)
const [admin] = await ethers.getSigners()
const adminAddress = await admin.getAddress()

const groupId = 42
const identity = new Identity(0)
const members = Array.from({ length: 3 }, (_, i) => new Identity(i)).map(({ commitment }) => commitment)
const group = new Group(members)

console.info(`Creating group '${groupId}' with ${members.length} members...`)
const groupId = 42

// Create a group and add 3 members.
await semaphoreContract["createGroup(uint256,address)"](groupId, adminAddress)
await semaphoreContract.addMembers(groupId, members)
console.info(`Creating group '${groupId}' with ${members.length} members...`)

console.info(`Removing third member from group '${groupId}'...`)
// Create a group and add 3 members.
await semaphoreContract["createGroup(uint256,address)"](groupId, adminAddress)
await semaphoreContract.addMembers(groupId, members)

// Remove the third member.
{
group.removeMember(2)
const { siblings } = group.generateMerkleProof(2)
console.info(`Removing third member from group '${groupId}'...`)

await semaphoreContract.removeMember(groupId, members[2], siblings)
}
// Remove the third member.
{
group.removeMember(2)
const { siblings } = group.generateMerkleProof(2)

console.info(`Updating second member from group '${groupId}'...`)
await semaphoreContract.removeMember(groupId, members[2], siblings)
}

// Update the second member.
{
group.updateMember(1, members[2])
const { siblings } = group.generateMerkleProof(1)
console.info(`Updating second member from group '${groupId}'...`)

await semaphoreContract.updateMember(groupId, members[1], members[2], siblings)
}
// Update the second member.
{
group.updateMember(1, members[2])
const { siblings } = group.generateMerkleProof(1)

console.info(`Validating a proof generated by the first member of group '${groupId}'...`)
await semaphoreContract.updateMember(groupId, members[1], members[2], siblings)
}

// Validate a proof.
const proof = await generateProof(identity, group, 42, 9, 10)
console.info(`Validating a proof generated by the first member of group '${groupId}'...`)

await semaphoreContract.validateProof(groupId, proof)
}
// Validate a proof.
const proof = await generateProof(identity, group, 42, 9, 10)

await semaphoreContract.validateProof(groupId, proof)
}

main()
Expand Down
65 changes: 56 additions & 9 deletions packages/contracts/scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
import { readFileSync } from "fs"
import { readFileSync, writeFileSync } from "fs"

export type NetworkDeployedContracts = {
name: "Semaphore" | "SemaphoreVerifier" | "PoseidonT3"
address: string
}[]

export type DeployedContracts = {
Poseidon: string
Semaphore: string
Verifier: string
network: string
contracts: NetworkDeployedContracts
}[]

const supportedNetworks = ["sepolia", "arbitrum", "mumbai", "optimism-sepolia", "arbitrum-sepolia"]

export function getDeployedContracts(): DeployedContracts {
return JSON.parse(readFileSync(`./deployed-contracts.json`, "utf8"))
}

export function getDeployedContractsByNetwork(network: string): NetworkDeployedContracts {
const deployedContracts = getDeployedContracts()
const networkDeployedContracts = deployedContracts.find((n) => n.network === network)

if (!networkDeployedContracts) {
throw Error(`Network '${network}' is not supported`)
}

return networkDeployedContracts.contracts
}

export function getDeployedContractAddress(network: string, contractName: string): string {
const contracts = getDeployedContractsByNetwork(network)
const semaphoreAddress = contracts.find((contract) => contract.name === contractName)

if (!semaphoreAddress) {
throw Error(`Contract with name '${contractName}' does not exist`)
}

return semaphoreAddress.address
}

export function getDeployedContracts(network: string | undefined): DeployedContracts | null {
try {
return JSON.parse(readFileSync(`./deployed-contracts/${network}.json`, "utf8"))
} catch (error) {
return null
export function saveDeployedContracts(contracts: NetworkDeployedContracts, network?: string) {
if (network && supportedNetworks.includes(network)) {
const deployedContracts = getDeployedContracts() as DeployedContracts

for (let i = 0; i < deployedContracts.length; i += 1) {
if (deployedContracts[i].network === network) {
deployedContracts[i].contracts = contracts

writeFileSync(`./deployed-contracts.json`, JSON.stringify(deployedContracts, null, 4))

return
}
}

deployedContracts.push({
network,
contracts
})

writeFileSync(`./deployed-contracts.json`, JSON.stringify(deployedContracts, null, 4))
}
}
18 changes: 11 additions & 7 deletions packages/contracts/scripts/verify-contracts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hardhatArguments, run } from "hardhat"
import { getDeployedContracts } from "./utils"
import { getDeployedContractAddress } from "./utils"

async function verify(address: string, constructorArguments?: any[]): Promise<void> {
try {
Expand All @@ -13,13 +13,17 @@ async function verify(address: string, constructorArguments?: any[]): Promise<vo
}

async function main() {
const deployedContracts = getDeployedContracts(hardhatArguments.network)

if (deployedContracts) {
await verify(deployedContracts.Verifier)
await verify(deployedContracts.Poseidon)
await verify(deployedContracts.Semaphore, [deployedContracts.Verifier])
if (!hardhatArguments.network) {
throw Error("Please, define a supported network")
}

const semaphoreVerifierAddress = getDeployedContractAddress(hardhatArguments.network, "SemaphoreVerifier")
const poseidonT3Address = getDeployedContractAddress(hardhatArguments.network, "PoseidonT3")
const semaphoreAddress = getDeployedContractAddress(hardhatArguments.network, "Semaphore")

await verify(semaphoreVerifierAddress)
await verify(poseidonT3Address)
await verify(semaphoreAddress, [semaphoreVerifierAddress])
}

main()
Expand Down
47 changes: 26 additions & 21 deletions packages/contracts/tasks/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeFileSync } from "fs"
import { task, types } from "hardhat/config"
import { saveDeployedContracts } from "../scripts/utils"
import { deployContract } from "./utils"

task("deploy", "Deploy a Semaphore contract")
Expand All @@ -8,41 +8,41 @@ task("deploy", "Deploy a Semaphore contract")
.addOptionalParam<boolean>("logs", "Print the logs", true, types.boolean)
.setAction(
async (
{ logs, verifier: verifierAddress, poseidon: poseidonAddress },
{ logs, verifier: semaphoreVerifierAddress, poseidon: poseidonT3Address },
{ ethers, hardhatArguments, defender }
): Promise<any> => {
if (!verifierAddress) {
if (!semaphoreVerifierAddress) {
const VerifierFactory = await ethers.getContractFactory(`SemaphoreVerifier`)

const verifier = await deployContract(defender, VerifierFactory, hardhatArguments.network)

verifierAddress = await verifier.getAddress()
semaphoreVerifierAddress = await verifier.getAddress()

if (logs) {
console.info(`SemaphoreVerifier contract has been deployed to: ${verifierAddress}`)
console.info(`SemaphoreVerifier contract has been deployed to: ${semaphoreVerifierAddress}`)
}
}

if (!poseidonAddress) {
if (!poseidonT3Address) {
const PoseidonT3Factory = await ethers.getContractFactory("PoseidonT3")

const poseidonT3 = await deployContract(defender, PoseidonT3Factory, hardhatArguments.network)

poseidonAddress = await poseidonT3.getAddress()
poseidonT3Address = await poseidonT3.getAddress()

if (logs) {
console.info(`Poseidon library has been deployed to: ${poseidonAddress}`)
console.info(`PoseidonT3 library has been deployed to: ${poseidonT3Address}`)
}
}

const SemaphoreFactory = await ethers.getContractFactory("Semaphore", {
libraries: {
PoseidonT3: poseidonAddress
PoseidonT3: poseidonT3Address
}
})

const semaphore = await deployContract(defender, SemaphoreFactory, hardhatArguments.network, [
verifierAddress
semaphoreVerifierAddress
])

const semaphoreAddress = await semaphore.getAddress()
Expand All @@ -51,23 +51,28 @@ task("deploy", "Deploy a Semaphore contract")
console.info(`Semaphore contract has been deployed to: ${semaphoreAddress}`)
}

writeFileSync(
`./deployed-contracts/${hardhatArguments.network}.json`,
JSON.stringify(
saveDeployedContracts(
[
{
Verifier: verifierAddress,
Poseidon: poseidonAddress,
Semaphore: semaphoreAddress
name: "SemaphoreVerifier",
address: semaphoreVerifierAddress
},
null,
4
)
{
name: "PoseidonT3",
address: poseidonT3Address
},
{
name: "Semaphore",
address: semaphoreAddress
}
],
hardhatArguments.network
)

return {
semaphore,
verifierAddress,
poseidonAddress
verifierAddress: semaphoreVerifierAddress,
poseidonAddress: poseidonT3Address
}
}
)

0 comments on commit c610213

Please sign in to comment.