Skip to content

Commit

Permalink
refactor: join poll sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Feb 14, 2025
1 parent bc55826 commit 52607a7
Show file tree
Hide file tree
Showing 11 changed files with 752 additions and 32 deletions.
38 changes: 24 additions & 14 deletions packages/cli/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#!/usr/bin/env node

import { Command } from "@commander-js/extra-typings";
import { generateTallyCommitments, getPollParams, verify, getPoll, isUserRegistered, signup } from "maci-sdk";
import {
generateTallyCommitments,
getPollParams,
verify,
getPoll,
getSignedupUserData,
signup,
joinPoll,
getJoinedUserData,
} from "maci-sdk";

import fs from "fs";
import path from "path";
Expand All @@ -26,13 +35,13 @@ import {
checkVerifyingKeys,
genLocalState,
extractVkToFile,
joinPoll,
isJoinedUser,
} from "./commands";
import {
DEFAULT_IVCP_DATA,
DEFAULT_SG_DATA,
TallyData,
banner,
info,
logError,
logGreen,
logRed,
Expand Down Expand Up @@ -234,7 +243,7 @@ program
.description("join the poll")
.requiredOption("-k, --priv-key <privKey>", "the private key")
.option("-i, --state-index <stateIndex>", "the user's state index", BigInt)
.requiredOption("-s, --sg-data <sgData>", "the signup gateway data")
.option("-s, --sg-data <sgData>", "the signup gatekeeper data")
.option("-v, --ivcp-data <ivcpData>", "the initial voice credit proxy data")
.option(
"-n, --new-voice-credit-balance <newVoiceCreditBalance>",
Expand Down Expand Up @@ -265,26 +274,28 @@ program
const maciAddress = cmdObj.maciAddress || (await readContractAddress("MACI", network?.name));
const privateKey = cmdObj.privKey || (await promptSensitiveValue("Insert your MACI private key"));

await joinPoll({
const data = await joinPoll({
maciAddress,
privateKey,
stateIndex: cmdObj.stateIndex || undefined,
stateIndex: cmdObj.stateIndex,
stateFile: cmdObj.stateFile,
pollId: cmdObj.pollId,
signer,
startBlock: cmdObj.startBlock,
endBlock: cmdObj.endBlock,
blocksPerBatch: cmdObj.blocksPerBatch,
transactionHash: cmdObj.transactionHash,
pollJoiningZkey: cmdObj.pollJoiningZkey,
pollWasm: cmdObj.pollWasm,
quiet: cmdObj.quiet,
useWasm: cmdObj.wasm,
rapidsnark: cmdObj.rapidsnark,
pollWitgen: cmdObj.pollWitnessgen,
sgDataArg: cmdObj.sgData,
ivcpDataArg: cmdObj.ivcpData,
sgDataArg: cmdObj.sgData ?? DEFAULT_SG_DATA,
ivcpDataArg: cmdObj.ivcpData ?? DEFAULT_IVCP_DATA,
});

logGreen(cmdObj.quiet, info(`User joined poll with nullifier: ${data.nullifier}`));
logGreen(cmdObj.quiet, info(`User joined poll with state index: ${data.pollStateIndex}`));
logGreen(cmdObj.quiet, info(`User joined poll with ${data.voiceCredits} voice credits`));
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
Expand Down Expand Up @@ -509,7 +520,7 @@ program
});
program
.command("isRegisteredUser")
.description("Checks if user is registered with public key")
.description("Checks if user is registered with their public key and get their data")
.requiredOption("-p, --pubkey <maciPubKey>", "the MACI public key")
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
Expand All @@ -520,7 +531,7 @@ program

const maciAddress = cmdObj.maciAddress || (await readContractAddress("MACI", network?.name));

const data = await isUserRegistered({
const data = await getSignedupUserData({
maciPubKey: cmdObj.pubkey,
maciAddress,
signer,
Expand Down Expand Up @@ -552,13 +563,12 @@ program

const maciAddress = cmdObj.maciAddress || (await readContractAddress("MACI", network?.name));

const data = await isJoinedUser({
const data = await getJoinedUserData({
pollPubKey: cmdObj.pubkey,
startBlock: cmdObj.startBlock!,
maciAddress,
pollId: cmdObj.pollId,
signer,
quiet: cmdObj.quiet,
});

if (data.isJoined) {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/ts/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ export interface IJoinedUserArgs {
*/
quiet: boolean;
}

/**
* Interface for the arguments to the joinPoll command
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/contracts/contracts/Poll.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ contract Poll is Params, Utilities, SnarkCommon, IPoll {

uint8 internal constant VOTE_TREE_ARITY = 5;

/// @notice Poll voting nullifier
mapping(uint256 => bool) private pollNullifier;
/// @notice Poll joining nullifiers
mapping(uint256 => bool) public pollNullifiers;

/// @notice The Id of this poll
uint256 public immutable pollId;
Expand Down Expand Up @@ -356,12 +356,12 @@ contract Poll is Params, Utilities, SnarkCommon, IPoll {
bytes memory _initialVoiceCreditProxyData
) external virtual isWithinVotingDeadline {
// Whether the user has already joined
if (pollNullifier[_nullifier]) {
if (pollNullifiers[_nullifier]) {
revert UserAlreadyJoined();
}

// Set nullifier for user's private key
pollNullifier[_nullifier] = true;
pollNullifiers[_nullifier] = true;

// Verify user's proof
if (!verifyJoiningPollProof(_nullifier, _stateRootIndex, _pubKey, _proof)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"ethers": "^6.13.4",
"maci-contracts": "^2.5.0",
"maci-crypto": "^2.5.0",
"maci-domainobjs": "^2.5.0"
"maci-domainobjs": "^2.5.0",
"maci-core": "^2.5.0"
},
"devDependencies": {
"@types/jest": "^29.5.2",
Expand Down
20 changes: 18 additions & 2 deletions packages/sdk/ts/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
export { isUserRegistered, signup, isJoinedUser } from "./user";
export type { IJoinedUserArgs, IIsRegisteredUser, IIsJoinedUser, ISignupArgs, IRegisteredUserArgs } from "./types";
export { getSignedupUserData, signup, getJoinedUserData, joinPoll, hasUserJoinedPoll } from "./user";
export type {
IJoinedUserArgs,
IIsRegisteredUser,
IIsJoinedUser,
ISignupArgs,
IRegisteredUserArgs,
IPollJoinedCircuitInputs,
IPollJoiningCircuitInputs,
IJoinPollArgs,
IIsNullifierOnChainArgs,
IGetPollJoiningCircuitEventsArgs,
IGetPollJoiningCircuitInputsFromStateFileArgs,
IJoinPollData,
IParsePollJoinEventsArgs,
IParseSignupEventsArgs,
ISignupData,
} from "./types";
Loading

0 comments on commit 52607a7

Please sign in to comment.