Skip to content

Commit

Permalink
refactor deployment scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-korotya committed Jan 24, 2025
1 parent fe5de03 commit c80722a
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 98 deletions.
77 changes: 30 additions & 47 deletions helpers/DeployAnonAadharV1Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { ethers, upgrades } from "hardhat";
import { Contract } from "ethers";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";

import { DeployHelper } from "./DeployHelper";
import { OnchainIdentityDeployHelper } from "./OnchainIdentityDeployHelper";
import { deployPoseidons } from "./PoseidonDeployHelper";

export class Groth16VerifierAnonAadhaarV1DeployHelper {
export class AnonAadhaarDeployHelper {
constructor(
private signers: SignerWithAddress[],
private readonly enableLogging: boolean = false,
Expand All @@ -15,15 +11,15 @@ export class Groth16VerifierAnonAadhaarV1DeployHelper {
static async initialize(
signers: SignerWithAddress[] | null = null,
enableLogging = true,
): Promise<Groth16VerifierAnonAadhaarV1DeployHelper> {
): Promise<AnonAadhaarDeployHelper> {
let sgrs;
if (signers === null) {
sgrs = await ethers.getSigners();
} else {
sgrs = signers;
}

return new Groth16VerifierAnonAadhaarV1DeployHelper(sgrs, enableLogging);
return new AnonAadhaarDeployHelper(sgrs, enableLogging);
}

public async deployGroth16Wrapper(): Promise<Contract> {
Expand Down Expand Up @@ -54,62 +50,49 @@ export class Groth16VerifierAnonAadhaarV1DeployHelper {
return deployment;
}

public async deployIdentityLib(smtLibAddress: string): Promise<Contract> {
const identityDeployHelper = await OnchainIdentityDeployHelper.initialize();
const [poseidon3Elements, poseidon4Elements] = await deployPoseidons([3, 4]);

const contracts = await identityDeployHelper.deployIdentityLib(
smtLibAddress,
await poseidon3Elements.getAddress(),
await poseidon4Elements.getAddress(),
);
contracts.waitForDeployment();

return contracts;
}

public async deployAnonAadhaarCredentialIssuing(): Promise<Contract> {
const deployHelper = await DeployHelper.initialize(this.signers, true);
const state = await deployHelper.deployStateWithLibraries();
await state.state.waitForDeployment();
const stateContractAddress = await state.state.getAddress();

const anonAadhaarProofValidator = await this.deployAnonAadhaarV1Validator(stateContractAddress);

const validatorAddress = await anonAadhaarProofValidator.getAddress();

console.log("id type", state.defaultIdType);
const verifierLib = await deployHelper.deployVerifierLib();
const identityLib = await this.deployIdentityLib(await state.smtLib.getAddress());

const issuer = await ethers.getContractFactory("AnonAadhaarCredentialIssuing", {
public async deployAnonAadhaarCredentialIssuing(
verifierLibAddress: string,
identityLibAddress: string,
stateContractAddress: string,
defaultIdType: string,
): Promise<Contract> {
const aadhaarIssuer = await ethers.getContractFactory("AnonAadhaarCredentialIssuing", {
libraries: {
VerifierLib: await verifierLib.getAddress(),
IdentityLib: await identityLib.getAddress(),
VerifierLib: verifierLibAddress,
IdentityLib: identityLibAddress,
},
});
const deployment = await issuer.deploy();
const deployment = await aadhaarIssuer.deploy();
await deployment.waitForDeployment();
const aadhaarIssuerTx = await deployment.initialize(
const tx = await deployment.initialize(
12345678,
BigInt("15134874015316324267425466444584014077184337590635665158241104437045239495873"),
15776640,
BigInt("19885546056720838706860449020869651677281577675447204956487418402102594191373"),
stateContractAddress,
state.defaultIdType,
defaultIdType,
);
await aadhaarIssuerTx.wait();
await tx.wait();

const requestId = 940499666; // calculateRequestIdForCircuit(CircuitId.AuthV2);
return deployment;
}

const setRequestTx = await deployment.setZKPRequest(requestId, {
public async setZKPRequest(
issuer: Contract,
requestId: number,
stateContractAddress: string,
validator?: Contract,
): Promise<void> {
const validatorAddress = validator
? await validator.getAddress()
: await (await this.deployAnonAadhaarV1Validator(stateContractAddress)).getAddress();

const tx = await issuer.setZKPRequest(requestId, {
metadata: "0x",
validator: validatorAddress,
data: "0x",
});
await setRequestTx.wait();

return deployment;
await tx.wait();
}

private log(...args): void {
Expand Down
20 changes: 20 additions & 0 deletions helpers/DeployHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,26 @@ export class DeployHelper {
return identityTreeStore;
}

async deployIdentityLib(
smtpAddress: string,
poseidonUtil3lAddress: string,
poseidonUtil4lAddress: string,
): Promise<Contract> {
const Identity = await ethers.getContractFactory("IdentityLib", {
libraries: {
SmtLib: smtpAddress,
PoseidonUnit3L: poseidonUtil3lAddress,
PoseidonUnit4L: poseidonUtil4lAddress,
},
});
const il = await Identity.deploy();
await il.waitForDeployment();

this.log(`IdentityLib deployed to address ${await il.getAddress()}`);

return il;
}

private log(...args): void {
this.enableLogging && console.log(args);
}
Expand Down
26 changes: 2 additions & 24 deletions helpers/OnchainIdentityDeployHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ export class OnchainIdentityDeployHelper {

async deployIdentity(
stateAddr: string,
smtLibAddr: string,
poseidon3Addr: string,
poseidon4Addr: string,
identityLibAddr: string,
idType: string,
): Promise<{
identity: Contract;
Expand All @@ -35,13 +33,12 @@ export class OnchainIdentityDeployHelper {
this.log("======== Identity: deploy started ========");

const cb = await this.deployClaimBuilder();
const il = await this.deployIdentityLib(smtLibAddr, poseidon3Addr, poseidon4Addr);

this.log("deploying Identity...");
const IdentityFactory = await ethers.getContractFactory("IdentityExample", {
libraries: {
ClaimBuilder: await cb.getAddress(),
IdentityLib: await il.getAddress(),
IdentityLib: await identityLibAddr,
},
});
const Identity = await upgrades.deployProxy(IdentityFactory, [stateAddr, idType], {
Expand All @@ -68,25 +65,6 @@ export class OnchainIdentityDeployHelper {
return cb;
}

async deployIdentityLib(
smtpAddress: string,
poseidonUtil3lAddress: string,
poseidonUtil4lAddress: string,
): Promise<Contract> {
const Identity = await ethers.getContractFactory("IdentityLib", {
libraries: {
SmtLib: smtpAddress,
PoseidonUnit3L: poseidonUtil3lAddress,
PoseidonUnit4L: poseidonUtil4lAddress,
},
});
const il = await Identity.deploy();
await il.waitForDeployment();
this.enableLogging && this.log(`ClaimBuilder deployed to: ${await il.getAddress()}`);

return il;
}

async deployClaimBuilderWrapper(): Promise<Contract> {
const cb = await this.deployClaimBuilder();

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@
"lint:contracts": "npx solhint contracts/**/*.sol",
"prettier:contracts": "prettier --write --plugin=prettier-plugin-solidity 'contracts/**/*.sol'",
"slither": "slither .",
"postinstall": "patch-package"
"postinstall": "patch-package",
"deploy:anonaadhaar:polygon-amoy": "npx hardhat run scripts/deploy/deployAnonAadhaarIssuer.ts --network polygon-amoy"
},
"overrides": {
"ws": "^8.17.1",
Expand Down
38 changes: 38 additions & 0 deletions scripts/deploy/deployAnonAadhaarIssuer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { AnonAadhaarDeployHelper } from "../../helpers/DeployAnonAadharV1Validator";
import { DeployHelper } from "../../helpers/DeployHelper";
import { contractsInfo } from "../../helpers/constants";
import { getStateContractAddress } from "../../helpers/helperUtils";

async function main() {
const stDeployHelper = await DeployHelper.initialize();
// TODO (illia-korotia): possible problem is here.
// By default I have issuer did:iden3:privado-main
const { defaultIdType } = await stDeployHelper.getDefaultIdType();

const stateContractAddress = getStateContractAddress();

const verifierLib = await stDeployHelper.deployVerifierLib();
const identityLib = await stDeployHelper.deployIdentityLib(
contractsInfo.SMT_LIB.unifiedAddress,
contractsInfo.POSEIDON_3.unifiedAddress,
contractsInfo.POSEIDON_4.unifiedAddress,
);

const f = await AnonAadhaarDeployHelper.initialize();
const issuer = await f.deployAnonAadhaarCredentialIssuing(
await verifierLib.getAddress(),
await identityLib.getAddress(),
await stateContractAddress,
defaultIdType,
);
await f.setZKPRequest(issuer, 23095784, stateContractAddress);

console.log("AnonAadhaar deployed at: ", await issuer.getAddress());
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
10 changes: 7 additions & 3 deletions scripts/deploy/deployIdentityExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ async function main() {

const stateContractAddress = getStateContractAddress();

const identityLib = await stDeployHelper.deployIdentityLib(
contractsInfo.SMT_LIB.unifiedAddress,
contractsInfo.POSEIDON_3.unifiedAddress,
contractsInfo.POSEIDON_4.unifiedAddress,
);

const identityDeployHelper = await OnchainIdentityDeployHelper.initialize();

const contracts = await identityDeployHelper.deployIdentity(
stateContractAddress,
contractsInfo.SMT_LIB.unifiedAddress,
contractsInfo.POSEIDON_3.unifiedAddress,
contractsInfo.POSEIDON_4.unifiedAddress,
await identityLib.getAddress(),
defaultIdType,
);

Expand Down
Loading

0 comments on commit c80722a

Please sign in to comment.