Skip to content

Commit

Permalink
Merge pull request #2091 from privacy-scaling-explorations/refactor/s…
Browse files Browse the repository at this point in the history
…dk-structure

refactor(sdk): change sdk package structure
  • Loading branch information
0xmad authored Jan 31, 2025
2 parents cc869fc + 3255909 commit 109b590
Show file tree
Hide file tree
Showing 27 changed files with 860 additions and 865 deletions.
12 changes: 7 additions & 5 deletions packages/cli/ts/commands/joinPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { type ContractTransactionReceipt } from "ethers";
import { CircuitInputs, IJsonMaciState, MaciState, IPollJoiningCircuitInputs } from "maci-core";
import { poseidon, stringifyBigInts } from "maci-crypto";
import { IVkObjectParams, Keypair, PrivKey, PubKey } from "maci-domainobjs";
import { formatProofForVerifierContract, genSignUpTree, IGenSignUpTree } from "maci-sdk";
import {
type IGenerateSignUpTree,
type FullProveResult,
extractVk,
genProofSnarkjs,
genProofRapidSnark,
verifyProof,
type FullProveResult,
formatProofForVerifierContract,
generateSignUpTree,
MACI__factory as MACIFactory,
Poll__factory as PollFactory,
} from "maci-sdk";
Expand Down Expand Up @@ -109,7 +111,7 @@ export const generateAndVerifyProof = async (
* @returns stringified circuit inputs
*/
const joiningCircuitInputs = (
signUpData: IGenSignUpTree,
signUpData: IGenerateSignUpTree,
stateTreeDepth: bigint,
maciPrivKey: PrivKey,
stateLeafIndex: bigint,
Expand Down Expand Up @@ -220,7 +222,7 @@ export const joinPoll = async ({

let loadedStateIndex: bigint | undefined;
let maciState: MaciState | undefined;
let signUpData: IGenSignUpTree | undefined;
let signUpData: IGenerateSignUpTree | undefined;
let currentStateRootIndex: number;
let circuitInputs: CircuitInputs;

Expand Down Expand Up @@ -274,7 +276,7 @@ export const joinPoll = async ({

logYellow(quiet, info(`starting to fetch logs from block ${fromBlock}`));

signUpData = await genSignUpTree({
signUpData = await generateSignUpTree({
provider: signer.provider!,
address: await maciContract.getAddress(),
blocksPerRequest: blocksPerBatch || 50,
Expand Down
27 changes: 7 additions & 20 deletions packages/sdk/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
export { getPoll, getPollParams } from "./poll";
export { verify } from "./verify";
export { generateTallyCommitments } from "./tallyCommitments";
export { isUserRegistered, isJoinedUser, signup } from "./user";
export { getAllOnChainVks, compareVks, extractAllVks } from "./verifyingKeys";
export { isArm } from "./utils";
export { genSignUpTree } from "./trees";
export { generateVote, getCoordinatorPubKey } from "./votes";
export * from "./keys";
export * from "./poll";
export * from "./tally";
export * from "./trees";
export * from "./vote";
export * from "./utils";
export * from "./user";

export {
EMode,
Expand Down Expand Up @@ -48,15 +47,3 @@ export type {
} from "maci-contracts";

export * from "maci-contracts/typechain-types";

export type {
TallyData,
VerifyArgs,
IGetPollArgs,
IGetPollData,
IIsRegisteredUser,
IIsJoinedUser,
IExtractAllVksArgs,
IMaciVks,
IGenSignUpTree,
} from "./utils";
2 changes: 2 additions & 0 deletions packages/sdk/ts/keys/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { getAllOnChainVks, compareVks, extractAllVks } from "./verifyingKeys";
export type { IGetAllVksArgs, IMaciVerifyingKeys, IExtractAllVksArgs, IMaciVks } from "./types";
103 changes: 103 additions & 0 deletions packages/sdk/ts/keys/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import type { Signer } from "ethers";
import type { EMode } from "maci-contracts";
import type { IVkContractParams, VerifyingKey } from "maci-domainobjs";

/**
* Arguments for the getAllVks function
*/
export interface IGetAllVksArgs {
/**
* The address of the VkRegistry contract
*/
vkRegistryAddress: string;
/**
* The signer to use for the contract calls
*/
signer: Signer;
/**
* The depth of the state tree
*/
stateTreeDepth: number;
/**
* The depth of the vote option tree
*/
voteOptionTreeDepth: number;
/**
* The batch size for the process messages
*/
messageBatchSize: number;
/**
* The depth of the ballot tree
*/
intStateTreeDepth: number;
/**
* The mode to use for the contract calls
*/
mode: EMode;
}

/**
* MACI's verifying keys
*/
export interface IMaciVerifyingKeys {
/**
* The verifying key for the poll joining circuit
*/
pollJoiningVkOnChain: IVkContractParams;
/**
* The verifying key for the poll joined circuit
*/
pollJoinedVkOnChain: IVkContractParams;
/**
* The verifying key for the process messages circuit
*/
processVkOnChain: IVkContractParams;
/**
* The verifying key for the tally votes circuit
*/
tallyVkOnChain: IVkContractParams;
}

/**
* Arguments for the extractAllVks function
*/
export interface IExtractAllVksArgs {
/**
* The path to the poll joining zkey
*/
pollJoiningZkeyPath?: string;
/**
* The path to the poll joined zkey
*/
pollJoinedZkeyPath?: string;
/**
* The path to the process messages zkey
*/
processMessagesZkeyPath?: string;
/**
* The path to the tally votes zkey
*/
tallyVotesZkeyPath?: string;
}

/**
* Maci verifying keys
*/
export interface IMaciVks {
/**
* The poll joining verifying key
*/
pollJoiningVk?: VerifyingKey;
/**
* The poll joined verifying key
*/
pollJoinedVk?: VerifyingKey;
/**
* The message processing verifying key
*/
processVk?: VerifyingKey;
/**
* The tally verifying key
*/
tallyVk?: VerifyingKey;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VkRegistry__factory as VkRegistryFactory, extractVk } from "maci-contracts";
import { IVkContractParams, VerifyingKey } from "maci-domainobjs";

import type { GetAllVksArgs, IExtractAllVksArgs, IMaciVks, IMaciVerifyingKeys } from "./utils/types";
import type { IGetAllVksArgs, IExtractAllVksArgs, IMaciVks, IMaciVerifyingKeys } from "./types";

/**
* Get all the verifying keys from the contract
Expand All @@ -16,7 +16,7 @@ export const getAllOnChainVks = async ({
messageBatchSize,
intStateTreeDepth,
mode,
}: GetAllVksArgs): Promise<IMaciVerifyingKeys> => {
}: IGetAllVksArgs): Promise<IMaciVerifyingKeys> => {
const vkRegistryContractInstance = VkRegistryFactory.connect(vkRegistryAddress, signer);

const [pollJoiningVkOnChain, pollJoinedVkOnChain, processVkOnChain, tallyVkOnChain] = await Promise.all([
Expand Down
9 changes: 9 additions & 0 deletions packages/sdk/ts/poll/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export { getPoll, getPollParams } from "./poll";
export type {
IGetPollArgs,
IGetPollData,
IGetPollParamsArgs,
IPollParams,
IPollJoiningInputs,
IPollJoinedInputs,
} from "./types";
2 changes: 1 addition & 1 deletion packages/sdk/ts/poll.ts → packages/sdk/ts/poll/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Tally__factory as TallyFactory,
} from "maci-contracts/typechain-types";

import type { IGetPollArgs, IGetPollData, IGetPollParamsArgs, IPollParams } from "./utils/types";
import type { IGetPollArgs, IGetPollData, IGetPollParamsArgs, IPollParams } from "./types";

/**
* Get deployed poll from MACI contract
Expand Down
143 changes: 143 additions & 0 deletions packages/sdk/ts/poll/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import type { BigNumberish, Provider, Signer } from "ethers";

/**
* Interface for the arguments to the get poll command
*/
export interface IGetPollArgs {
/**
* A signer object
*/
signer?: Signer;

/**
* A provider fallback object
*/
provider?: Provider;

/**
* The address of the MACI contract
*/
maciAddress: string;

/**
* The poll id. If not specified, latest poll id will be used
*/
pollId?: BigNumberish;
}

/**
* Interface for the return data to the get poll command
*/
export interface IGetPollData {
/**
* The poll id
*/
id: BigNumberish;

/**
* The poll address
*/
address: string;

/**
* The poll deployment time
*/
deployTime: BigNumberish;

/**
* The poll duration
*/
duration: BigNumberish;

/**
* The poll number of signups
*/
numSignups: BigNumberish;

/**
* Whether the MACI contract's state root has been merged
*/
isMerged: boolean;

/**
* Mode of the poll
*/
mode: BigNumberish;
}

/**
* Arguments for the get poll params command
*/
export interface IGetPollParamsArgs {
/**
* The poll id
*/
pollId: bigint;
/**
* The signer
*/
signer: Signer;
/**
* The MACI contract address
*/
maciContractAddress: string;
}

/**
* Poll parameters
*/
export interface IPollParams {
/**
* The message batch size
*/
messageBatchSize: number;
/**
* The number of vote options
*/
numVoteOptions: number;

/**
* Tally Batch Size
*/
tallyBatchSize: number;

/**
* The vote option tree depth
*/
voteOptionTreeDepth: number;

/**
* The depth of the tree holding the user ballots
*/
intStateTreeDepth: number;
}

/**
* Inputs for circuit PollJoining
*/
export interface IPollJoiningInputs {
privKey: bigint;
pollPubKey: bigint[][];
stateLeaf: bigint[];
siblings: bigint[][];
indices: bigint[];
nullifier: bigint;
credits: bigint;
stateRoot: bigint;
actualStateTreeDepth: bigint;
pollId: bigint;
}

/**
* Inputs for circuit PollJoined
*/
export interface IPollJoinedInputs {
privKey: bigint;
voiceCreditsBalance: bigint;
joinTimestamp: bigint;
stateLeaf: bigint[];
pathElements: bigint[][];
pathIndices: bigint[];
credits: bigint;
stateRoot: bigint;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { genTreeCommitment, hash2, hash3, hashLeftRight } from "maci-crypto";

import { IGenerateTallyCommitmentsArgs, ITallyCommitments } from "./utils/types";
import type { IGenerateTallyCommitmentsArgs, ITallyCommitments } from "./types";

/**
* Generate the tally commitments for this current batch of proving
Expand Down
9 changes: 9 additions & 0 deletions packages/sdk/ts/tally/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export { generateTallyCommitments } from "./commitments";
export { verify } from "./verification";
export type {
ITallyData,
IVerifyArgs,
IGenerateTallyCommitmentsArgs,
ITallyCommitments,
ITallyVotesInputs,
} from "./types";
Loading

0 comments on commit 109b590

Please sign in to comment.