Skip to content

Commit

Permalink
feat(cli): accept signer as parameter to cli commands
Browse files Browse the repository at this point in the history
Allow cli commands to be used on a frontend by taking an optional signer param
  • Loading branch information
ctrlc03 committed Jan 30, 2024
1 parent 4804b92 commit b1802e7
Show file tree
Hide file tree
Showing 28 changed files with 322 additions and 140 deletions.
4 changes: 2 additions & 2 deletions cli/tests/ceremony-params/ceremonyParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("stress tests", function test() {
// before all tests we deploy the vk registry contract and set the verifying keys
before(async () => {
// we deploy the vk registry contract
await deployVkRegistryContract(true);
await deployVkRegistryContract({});
// we set the verifying keys
await setVerifyingKeys(verifyingKeysArgs);
});
Expand Down Expand Up @@ -142,7 +142,7 @@ describe("stress tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration);
await timeTravel({ seconds: pollDuration });
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
await genProofs(genProofsCeremonyArgs);
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MergeSignupsArgs,
ProveOnChainArgs,
SetVerifyingKeysArgs,
TimeTravelArgs,
VerifyArgs,
} from "../ts/utils";

Expand Down Expand Up @@ -83,6 +84,10 @@ export const checkVerifyingKeysArgs: CheckVerifyingKeysArgs = {
tallyVotesZkeyPath: tallyVotesTestZkeyPath,
};

export const timeTravelArgs: TimeTravelArgs = {
seconds: pollDuration,
};

export const mergeMessagesArgs: MergeMessagesArgs = {
pollId: 0n,
};
Expand Down
14 changes: 7 additions & 7 deletions cli/tests/e2e/e2e.subsidy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import { DeployedContracts, GenProofsArgs, PollContracts } from "../../ts/utils";
import {
coordinatorPrivKey,
pollDuration,
verifyArgs,
proveOnChainArgs,
processMessageTestZkeyPath,
Expand All @@ -44,6 +43,7 @@ import {
setVerifyingKeysArgs,
deployArgs,
deployPollArgs,
timeTravelArgs,
} from "../constants";
import { cleanSubsidy, isArm } from "../utils";

Expand Down Expand Up @@ -82,7 +82,7 @@ describe("e2e with Subsidy tests", function test() {

before(async () => {
// we deploy the vk registry contract
vkRegistryContractAddress = await deployVkRegistryContract(true);
vkRegistryContractAddress = await deployVkRegistryContract({});
// we set the verifying keys
await setVerifyingKeys({ ...setVerifyingKeysArgs, subsidyZkeyPath: subsidyTestZkeyPath });
});
Expand Down Expand Up @@ -178,7 +178,7 @@ describe("e2e with Subsidy tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
await genProofs(genProofsArgs);
Expand Down Expand Up @@ -234,7 +234,7 @@ describe("e2e with Subsidy tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
const tallyData = await genProofs(genProofsArgs);
Expand Down Expand Up @@ -282,7 +282,7 @@ describe("e2e with Subsidy tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
const tallyData = await genProofs(genProofsArgs);
Expand Down Expand Up @@ -335,7 +335,7 @@ describe("e2e with Subsidy tests", function test() {
privateKey: users[0].privKey.serialize(),
});
// time travel
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
// generate proofs
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
Expand Down Expand Up @@ -428,7 +428,7 @@ describe("e2e with Subsidy tests", function test() {
});

it("should complete the second poll", async () => {
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
await mergeMessages({ pollId: 1n });
await mergeSignups({ pollId: 1n });
const tallyData = await genProofs({
Expand Down
50 changes: 29 additions & 21 deletions cli/tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Signer } from "ethers";
import { getDefaultSigner } from "maci-contracts";
import { genRandomSalt } from "maci-crypto";
import { Keypair } from "maci-domainobjs";

Expand Down Expand Up @@ -44,6 +46,7 @@ import {
testTallyVotesWitnessDatPath,
testTallyVotesWitnessPath,
deployArgs,
timeTravelArgs,
} from "../constants";
import { cleanVanilla, isArm } from "../utils";

Expand Down Expand Up @@ -86,27 +89,30 @@ describe("e2e tests", function test() {
// before all tests we deploy the vk registry contract and set the verifying keys
before(async () => {
// we deploy the vk registry contract
await deployVkRegistryContract(true);
await deployVkRegistryContract({});
// we set the verifying keys
await setVerifyingKeys(setVerifyingKeysArgs);
});

describe("1 signup, 1 message", () => {
describe("1 signup, 1 message (with signer as argument)", () => {
let signer: Signer;

after(() => {
cleanVanilla();
});

const user = new Keypair();

before(async () => {
signer = await getDefaultSigner();
// deploy the smart contracts
maciAddresses = await deploy(deployArgs);
maciAddresses = await deploy({ ...deployArgs, signer });
// deploy a poll contract
pollAddresses = await deployPoll(deployPollArgs);
pollAddresses = await deployPoll({ ...deployPollArgs, signer });
});

it("should signup one user", async () => {
await signup({ maciPubKey: user.pubKey.serialize() });
await signup({ maciPubKey: user.pubKey.serialize(), signer });
});

it("should publish one message", async () => {
Expand All @@ -120,18 +126,20 @@ describe("e2e tests", function test() {
maciContractAddress: maciAddresses.maciAddress,
salt: genRandomSalt(),
privateKey: user.privKey.serialize(),
signer,
});
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
const tallyFileData = await genProofs(genProofsArgs);
await proveOnChain(proveOnChainArgs);
await timeTravel({ seconds: pollDuration, signer });
await mergeMessages({ ...mergeMessagesArgs, signer });
await mergeSignups({ ...mergeSignupsArgs, signer });
const tallyFileData = await genProofs({ ...genProofsArgs, signer });
await proveOnChain({ ...proveOnChainArgs, signer });
await verify({
...verifyArgs,
tallyData: tallyFileData,
signer,
});
});
});
Expand Down Expand Up @@ -169,7 +177,7 @@ describe("e2e tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
const tallyFileData = await genProofs(genProofsArgs);
Expand Down Expand Up @@ -251,7 +259,7 @@ describe("e2e tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
const tallyFileData = await genProofs(genProofsArgs);
Expand Down Expand Up @@ -352,7 +360,7 @@ describe("e2e tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
await genProofs(genProofsArgs);
Expand Down Expand Up @@ -408,7 +416,7 @@ describe("e2e tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
const tallyFileData = await genProofs(genProofsArgs);
Expand Down Expand Up @@ -456,7 +464,7 @@ describe("e2e tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
await genProofs(genProofsArgs);
Expand Down Expand Up @@ -502,7 +510,7 @@ describe("e2e tests", function test() {
privateKey: user.privKey.serialize(),
});
// time travel
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
// generate proofs
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
Expand Down Expand Up @@ -531,7 +539,7 @@ describe("e2e tests", function test() {
});

it("should generate proofs and verify them", async () => {
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
await mergeMessages({ pollId: 1n });
await mergeSignups({ pollId: 1n });
await genProofs({ ...genProofsArgs, pollId: 1n });
Expand Down Expand Up @@ -587,7 +595,7 @@ describe("e2e tests", function test() {
});

// time travel
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
// generate proofs
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
Expand Down Expand Up @@ -680,7 +688,7 @@ describe("e2e tests", function test() {
});

it("should complete the second poll", async () => {
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
await mergeMessages({ pollId: 1n });
await mergeSignups({ pollId: 1n });
const tallyData = await genProofs({ ...genProofsArgs, pollId: 1n });
Expand Down Expand Up @@ -761,7 +769,7 @@ describe("e2e tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
await genLocalState({
Expand Down Expand Up @@ -833,7 +841,7 @@ describe("e2e tests", function test() {
});

it("should generate proofs and verify them", async () => {
await timeTravel(pollDuration, true);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
await genProofs(genProofsArgs);
Expand Down
9 changes: 5 additions & 4 deletions cli/tests/e2e/keyChange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
testTallyVotesWitnessPath,
proveOnChainArgs,
verifyArgs,
timeTravelArgs,
} from "../constants";
import { cleanVanilla, isArm } from "../utils";

Expand Down Expand Up @@ -71,7 +72,7 @@ describe("keyChange tests", function test() {
// before all tests we deploy the vk registry contract and set the verifying keys
before(async () => {
// we deploy the vk registry contract
await deployVkRegistryContract(true);
await deployVkRegistryContract({});
// we set the verifying keys
await setVerifyingKeys(setVerifyingKeysArgs);
});
Expand Down Expand Up @@ -125,7 +126,7 @@ describe("keyChange tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(90, true);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
await genProofs(genProofsArgs);
Expand Down Expand Up @@ -189,7 +190,7 @@ describe("keyChange tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(90, true);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
await genProofs(genProofsArgs);
Expand Down Expand Up @@ -253,7 +254,7 @@ describe("keyChange tests", function test() {
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(90, true);
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
await genProofs(genProofsArgs);
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/unit/fundWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("fundWallet", () => {
const signer = await getDefaultSigner();

const balanceBefore = await signer.provider?.getBalance(ZeroAddress);
await fundWallet(1000000000, ZeroAddress);
await fundWallet({ amount: 1000000000, address: ZeroAddress });
const balanceAfter = await signer.provider?.getBalance(ZeroAddress);
expect(balanceAfter).to.be.gt(balanceBefore!);
});
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/unit/timeTravel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("timeTravel", () => {
const signer = await getDefaultSigner();
const blockNumber = await signer.provider?.getBlock("latest");

await expect(timeTravel(5)).to.be.fulfilled;
await expect(timeTravel({ seconds: 5 })).to.be.fulfilled;

const blockNumberAfter = await signer.provider?.getBlock("latest");
expect(blockNumberAfter!.timestamp).to.be.gt(blockNumber!.timestamp);
Expand Down
11 changes: 6 additions & 5 deletions cli/ts/commands/airdrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ export const airdrop = async ({
contractAddress,
pollId,
maciAddress,
signer,
quiet = true,
}: AirdropArgs): Promise<void> => {
banner(quiet);

// get the signer
const signer = await getDefaultSigner();
const ethSigner = signer || (await getDefaultSigner());
const network = await getDefaultNetwork();

// get the topup credit address from storage
Expand All @@ -34,14 +35,14 @@ export const airdrop = async ({
const ERC20Address = contractAddress || topupCredit;

// check if the contract exists
if (!(await contractExists(signer.provider!, ERC20Address))) {
if (!(await contractExists(ethSigner.provider!, ERC20Address))) {
logError("Invalid ERC20 contract address");
}

const tokenAbi = parseArtifact("TopupCredit")[0];

// create the contract instance
const tokenContract = new BaseContract(ERC20Address, tokenAbi, signer) as TopupCredit;
const tokenContract = new BaseContract(ERC20Address, tokenAbi, ethSigner) as TopupCredit;

if (amount < 0) {
logError("Invalid amount");
Expand All @@ -54,7 +55,7 @@ export const airdrop = async ({
});
await tx.wait();

logGreen(quiet, success(`Airdropped ${amount} credits to ${await signer.getAddress()}`));
logGreen(quiet, success(`Airdropped ${amount} credits to ${await ethSigner.getAddress()}`));
} catch (error) {
logError((error as Error).message);
}
Expand All @@ -71,7 +72,7 @@ export const airdrop = async ({
}

const maciAbi = parseArtifact("MACI")[0];
const maciContract = new BaseContract(maciContractAddress!, maciAbi, signer) as MACI;
const maciContract = new BaseContract(maciContractAddress!, maciAbi, ethSigner) as MACI;

const pollAddr = await maciContract.getPoll(pollId);
try {
Expand Down
Loading

0 comments on commit b1802e7

Please sign in to comment.