Skip to content

Commit

Permalink
feat(joinpoll cli): add optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandar Veljković committed Aug 19, 2024
1 parent 9425969 commit 26d69e8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
63 changes: 54 additions & 9 deletions cli/ts/commands/joinPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { IJoinPollArgs, IJoinedUserArgs, IParsePollJoinEventsArgs } from ".

import { contractExists, logError, logYellow, info, logGreen, success } from "../utils";
import { banner } from "../utils/banner";

Check failure on line 14 in cli/ts/commands/joinPoll.ts

View workflow job for this annotation

GitHub Actions / check (lint:ts)

There should be at least one empty line between import groups
import { error } from "console";

Check failure on line 15 in cli/ts/commands/joinPoll.ts

View workflow job for this annotation

GitHub Actions / check (lint:ts)

`console` import should occur before import of `fs`

/**
* Create circuit input for pollJoining
Expand Down Expand Up @@ -124,17 +125,20 @@ export const joinPoll = async ({
}

const userMaciPrivKey = PrivKey.deserialize(privateKey);
const userMaciPubKey = new Keypair(userMaciPrivKey).pubKey;
const nullifier = poseidon([BigInt(userMaciPrivKey.asCircuitInputs())]);

// Create poll public key from poll private key
const pollPrivKeyDeserialized = PrivKey.deserialize(pollPrivKey);
const pollKeyPair = new Keypair(pollPrivKeyDeserialized);
const pollPubKey = pollKeyPair.pubKey;

// check < 1 cause index zero is a blank state leaf
if (stateIndex < 1) {
logError("Invalid state index");
}

Check failure on line 136 in cli/ts/commands/joinPoll.ts

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Delete `⏎`
// let loadedStateLeafIndex = stateLeafIndex;

// if (stateLeafIndex == null) {
// loadedStateLeafIndex = BigInt(stateLeaves.findIndex(leaf => leaf.pubKey.equals());
// }

if (pollId < 0) {
logError("Invalid poll id");
Expand All @@ -148,6 +152,9 @@ export const joinPoll = async ({
}

const pollContract = PollFactory.connect(pollAddress, signer);

Check failure on line 155 in cli/ts/commands/joinPoll.ts

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Delete `··`
let loadedStateIndex = stateIndex;
let loadedCreditBalance = newVoiceCreditBalance;

let maciState: MaciState | undefined;
let signUpData: IGenSignUpTree | undefined;
Expand All @@ -167,14 +174,33 @@ export const joinPoll = async ({
throw new Error("User the given nullifier has already joined");
}

if (stateIndex == null) {
const index = maciState?.stateLeaves.findIndex(leaf => leaf.pubKey.equals(userMaciPubKey));

Check failure on line 178 in cli/ts/commands/joinPoll.ts

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Replace `leaf` with `(leaf)`
if (index != null) {
loadedStateIndex = BigInt(index);
} else {
error('State leaf not found');

Check failure on line 182 in cli/ts/commands/joinPoll.ts

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Replace `'State·leaf·not·found'` with `"State·leaf·not·found"`
process.exit();
}
}

// check < 1 cause index zero is a blank state leaf
if (loadedStateIndex! < 1) {
logError("Invalid state index");
}

currentStateRootIndex = poll.maciStateRef.numSignUps - 1;

poll.updatePoll(BigInt(maciState!.stateLeaves.length));

if (newVoiceCreditBalance == null) {
loadedCreditBalance = maciState?.stateLeaves[Number(loadedStateIndex!)].voiceCreditBalance!;

Check failure on line 197 in cli/ts/commands/joinPoll.ts

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong
}

circuitInputs = poll.joiningCircuitInputs({
maciPrivKey: userMaciPrivKey,
stateLeafIndex: stateIndex,
credits: newVoiceCreditBalance,
stateLeafIndex: loadedStateIndex!,
credits: loadedCreditBalance!,
pollPrivKey: pollPrivKeyDeserialized,
pollPubKey,
}) as unknown as CircuitInputs;
Expand Down Expand Up @@ -209,12 +235,31 @@ export const joinPoll = async ({

currentStateRootIndex = Number(numSignups) - 1;

if (stateIndex == null) {
const index = signUpData.stateLeaves.findIndex(leaf => leaf.pubKey.equals(userMaciPubKey));

Check failure on line 239 in cli/ts/commands/joinPoll.ts

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Replace `leaf` with `(leaf)`
if (index != null) {

Check failure on line 240 in cli/ts/commands/joinPoll.ts

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Unnecessary conditional, the types have no overlap
loadedStateIndex = BigInt(index);
} else {
error('State leaf not found');
process.exit();
}
}

// check < 1 cause index zero is a blank state leaf
if (loadedStateIndex! < 1) {
logError("Invalid state index");
}

if (newVoiceCreditBalance == null) {
loadedCreditBalance = signUpData.stateLeaves[Number(loadedStateIndex!)].voiceCreditBalance!;
}

circuitInputs = joiningCircuitInputs(
signUpData,
stateTreeDepth,
userMaciPrivKey,
stateIndex,
newVoiceCreditBalance,
loadedStateIndex!,
loadedCreditBalance!,
pollPrivKeyDeserialized,
pollPubKey,
) as unknown as CircuitInputs;
Expand Down Expand Up @@ -247,7 +292,7 @@ export const joinPoll = async ({
const tx = await pollContract.joinPoll(
nullifier,
pollPubKey.asContractParam(),
newVoiceCreditBalance,
loadedCreditBalance!,
currentStateRootIndex,
proof,
);
Expand Down
8 changes: 4 additions & 4 deletions cli/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ program
.command("joinPoll")
.description("join the poll")
.requiredOption("-sk, --priv-key <privKey>", "the private key")
.requiredOption("-i, --state-index <stateIndex>", "the user's state index", BigInt)
.option("-i, --state-index <stateIndex>", "the user's state index", BigInt)
.requiredOption("-esk, --poll-priv-key <pollPrivKey>", "the user ephemeral private key for the poll")
.requiredOption(
.option(
"-nv, --new-voice-credit-balance <newVoiceCreditBalance>",
"the voice credit balance of the user for the poll",
BigInt,
Expand Down Expand Up @@ -250,8 +250,8 @@ program
maciAddress,
privateKey,
pollPrivKey: cmdObj.pollPrivKey,
stateIndex: cmdObj.stateIndex,
newVoiceCreditBalance: cmdObj.newVoiceCreditBalance,
stateIndex: cmdObj.stateIndex || null,
newVoiceCreditBalance: cmdObj.newVoiceCreditBalance || null,
stateFile: cmdObj.stateFile,
pollId: cmdObj.pollId,
signer,
Expand Down
4 changes: 2 additions & 2 deletions cli/ts/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ export interface IJoinPollArgs {
/**
* User's credit balance for voting within this poll
*/
newVoiceCreditBalance: bigint;
newVoiceCreditBalance: bigint | null;

/**
* The id of the poll
Expand All @@ -376,7 +376,7 @@ export interface IJoinPollArgs {
/**
* The index of the state leaf
*/
stateIndex: bigint;
stateIndex: bigint | null;

/**
* Whether to log the output
Expand Down

0 comments on commit 26d69e8

Please sign in to comment.