-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: sdk functions for poll and verification
- Loading branch information
Showing
20 changed files
with
978 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
root: true, | ||
extends: ["../../.eslintrc.js"], | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
project: path.resolve(__dirname, "./tsconfig.json"), | ||
sourceType: "module", | ||
typescript: true, | ||
ecmaVersion: 2022, | ||
experimentalDecorators: true, | ||
requireConfigFile: false, | ||
ecmaFeatures: { | ||
classes: true, | ||
impliedStrict: true, | ||
}, | ||
warnOnUnsupportedTypeScriptVersion: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
contractAddresses.json | ||
contractAddresses.old.json | ||
contractAddress.old | ||
contractAddress.txt | ||
localState.json | ||
zkeys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
tests | ||
build/tests | ||
.etherlime-store | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# maci-sdk | ||
|
||
[![NPM Package][cli-npm-badge]][cli-npm-link] | ||
[![Actions Status][cli-actions-badge]][cli-actions-link] | ||
|
||
Please refer to the [documentation for the | ||
CLI](https://maci.pse.dev/docs/developers-references/typescript-code/cli). | ||
|
||
[cli-npm-badge]: https://img.shields.io/npm/v/maci-sdk.svg | ||
[cli-actions-badge]: https://github.com/privacy-scaling-explorations/maci/actions/workflows/e2e.yml/badge.svg | ||
[cli-npm-link]: https://www.npmjs.com/package/maci-sdk | ||
[cli-actions-link]: https://github.com/privacy-scaling-explorations/maci/actions?query=workflow%3ACI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
{ | ||
"name": "maci-sdk", | ||
"version": "0.0.1", | ||
"description": "MACI's SDK", | ||
"main": "build/ts/index.js", | ||
"exports": { | ||
".": { | ||
"types": "./build/ts/index.d.ts", | ||
"default": "./build/ts/index.js" | ||
}, | ||
"./sdk": { | ||
"types": "./build/ts/sdk/index.d.ts", | ||
"default": "./build/ts/sdk/index.js" | ||
} | ||
}, | ||
"bin": { | ||
"maci-sdk": "./build/ts/index.js" | ||
}, | ||
"files": [ | ||
"build", | ||
"CHANGELOG.md", | ||
"README.md" | ||
], | ||
"scripts": { | ||
"watch": "tsc --watch", | ||
"build": "tsc -p tsconfig.build.json", | ||
"postbuild": "cp package.json ./build && mkdir -p ./zkeys", | ||
"types": "tsc -p tsconfig.json --noEmit", | ||
"test": "nyc ts-mocha --exit tests/unit/*.test.ts", | ||
"docs": "typedoc --plugin typedoc-plugin-markdown --options ./typedoc.json" | ||
}, | ||
"dependencies": { | ||
"@commander-js/extra-typings": "^12.1.0", | ||
"@nomicfoundation/hardhat-toolbox": "^5.0.0", | ||
"commander": "^12.1.0", | ||
"dotenv": "^16.4.5", | ||
"ethers": "^6.13.4", | ||
"hardhat": "^2.22.8", | ||
"maci-contracts": "^2.5.0", | ||
"maci-crypto": "^2.5.0" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.3.9", | ||
"@types/chai-as-promised": "^7.1.8", | ||
"@types/mocha": "^10.0.8", | ||
"@types/node": "^22.9.0", | ||
"@types/snarkjs": "^0.7.8", | ||
"chai": "^4.3.10", | ||
"chai-as-promised": "^7.1.2", | ||
"mocha": "^10.7.3", | ||
"nyc": "^17.1.0", | ||
"snarkjs": "^0.7.5", | ||
"ts-mocha": "^10.0.0", | ||
"typescript": "^5.6.3" | ||
}, | ||
"nyc": { | ||
"reporter": [ | ||
"text", | ||
"lcov" | ||
], | ||
"extensions": [ | ||
".ts" | ||
], | ||
"all": true, | ||
"exclude": [ | ||
"**/*.js", | ||
"**/*.d.ts", | ||
"hardhat.config.ts", | ||
"tests/**/*.ts", | ||
"ts/index.ts" | ||
], | ||
"branches": ">50%", | ||
"lines": ">50%", | ||
"functions": ">50%", | ||
"statements": ">50%" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export { getPoll, getPollParams } from "./poll"; | ||
export { verify } from "./verify"; | ||
export { generateTallyCommitments } from "./tallyCommitments"; | ||
|
||
export { | ||
linkPoseidonLibraries, | ||
Deployment, | ||
ContractStorage, | ||
EContracts, | ||
EMode, | ||
type IVerifyingKeyStruct, | ||
} from "maci-contracts"; | ||
|
||
export * from "maci-contracts/typechain-types"; | ||
|
||
export type { TallyData, VerifyArgs, IGetPollArgs, IGetPollData } from "./utils"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { ZeroAddress } from "ethers"; | ||
import { | ||
MACI__factory as MACIFactory, | ||
Poll__factory as PollFactory, | ||
Tally__factory as TallyFactory, | ||
} from "maci-contracts/typechain-types"; | ||
|
||
import type { IGetPollArgs, IGetPollData, IGetPollParamsArgs, IPollParams } from "./utils/interfaces"; | ||
|
||
/** | ||
* Get deployed poll from MACI contract | ||
* @param {IGetPollArgs} args - The arguments for the get poll command | ||
* @returns {IGetPollData} poll data | ||
*/ | ||
export const getPoll = async ({ maciAddress, signer, provider, pollId }: IGetPollArgs): Promise<IGetPollData> => { | ||
if (!signer && !provider) { | ||
throw new Error("No signer and provider are provided"); | ||
} | ||
|
||
const maciContract = MACIFactory.connect(maciAddress, signer ?? provider); | ||
const id = | ||
pollId === undefined ? await maciContract.nextPollId().then((nextPollId) => nextPollId - 1n) : BigInt(pollId); | ||
|
||
if (id < 0n) { | ||
throw new Error(`Invalid poll id ${id}`); | ||
} | ||
|
||
const pollContracts = await maciContract.polls(id); | ||
|
||
if (pollContracts.poll === ZeroAddress) { | ||
throw new Error(`MACI contract doesn't have any deployed poll ${id}`); | ||
} | ||
|
||
const pollContract = PollFactory.connect(pollContracts.poll, signer ?? provider); | ||
|
||
const [[deployTime, duration], mergedStateRoot] = await Promise.all([ | ||
pollContract.getDeployTimeAndDuration(), | ||
pollContract.mergedStateRoot(), | ||
]); | ||
const isMerged = mergedStateRoot !== BigInt(0); | ||
const numSignups = await (isMerged ? pollContract.numSignups() : maciContract.numSignUps()); | ||
|
||
// get the poll mode | ||
const tallyContract = TallyFactory.connect(pollContracts.tally, signer ?? provider); | ||
const mode = await tallyContract.mode(); | ||
|
||
return { | ||
id, | ||
address: pollContracts.poll, | ||
deployTime, | ||
duration, | ||
numSignups, | ||
isMerged, | ||
mode, | ||
}; | ||
}; | ||
|
||
/** | ||
* Get the parameters for the poll | ||
* @param {IGetPollParamsArgs} args - The arguments for the get poll command | ||
* @returns {IPollParams} poll parameters | ||
*/ | ||
export const getPollParams = async ({ | ||
pollId, | ||
signer, | ||
maciContractAddress, | ||
}: IGetPollParamsArgs): Promise<IPollParams> => { | ||
// get the contract objects | ||
const maciContract = MACIFactory.connect(maciContractAddress, signer); | ||
const pollContracts = await maciContract.polls(pollId); | ||
const pollContract = PollFactory.connect(pollContracts.poll, signer); | ||
|
||
const treeDepths = await pollContract.treeDepths(); | ||
const voteOptionTreeDepth = Number(treeDepths.voteOptionTreeDepth); | ||
const numVoteOptions = 5 ** voteOptionTreeDepth; | ||
|
||
const messageBatchSize = Number.parseInt((await pollContract.messageBatchSize()).toString(), 10); | ||
|
||
const intStateTreeDepth = Number(treeDepths.intStateTreeDepth); | ||
const tallyBatchSize = 5 ** intStateTreeDepth; | ||
|
||
return { | ||
messageBatchSize, | ||
numVoteOptions, | ||
tallyBatchSize, | ||
voteOptionTreeDepth, | ||
intStateTreeDepth, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { genTreeCommitment, hash2, hash3, hashLeftRight } from "maci-crypto"; | ||
|
||
import { IGenerateTallyCommitmentsArgs, ITallyCommitments } from "./utils/interfaces"; | ||
|
||
/** | ||
* Generate the tally commitments for this current batch of proving | ||
* @param tallyData - The tally data | ||
* @param voteOptionTreeDepth - The vote option tree depth | ||
* @returns The commitments to the Tally data | ||
*/ | ||
export const generateTallyCommitments = ({ | ||
tallyData, | ||
voteOptionTreeDepth, | ||
}: IGenerateTallyCommitmentsArgs): ITallyCommitments => { | ||
// compute newResultsCommitment | ||
const newResultsCommitment = genTreeCommitment( | ||
tallyData.results.tally.map((x) => BigInt(x)), | ||
BigInt(tallyData.results.salt), | ||
voteOptionTreeDepth, | ||
); | ||
|
||
// compute newSpentVoiceCreditsCommitment | ||
const newSpentVoiceCreditsCommitment = hash2([ | ||
BigInt(tallyData.totalSpentVoiceCredits.spent), | ||
BigInt(tallyData.totalSpentVoiceCredits.salt), | ||
]); | ||
|
||
let newTallyCommitment: bigint; | ||
let newPerVOSpentVoiceCreditsCommitment: bigint | undefined; | ||
|
||
if (tallyData.isQuadratic) { | ||
// compute newPerVOSpentVoiceCreditsCommitment | ||
newPerVOSpentVoiceCreditsCommitment = genTreeCommitment( | ||
tallyData.perVOSpentVoiceCredits!.tally.map((x) => BigInt(x)), | ||
BigInt(tallyData.perVOSpentVoiceCredits!.salt), | ||
voteOptionTreeDepth, | ||
); | ||
|
||
// compute newTallyCommitment | ||
newTallyCommitment = hash3([ | ||
newResultsCommitment, | ||
newSpentVoiceCreditsCommitment, | ||
newPerVOSpentVoiceCreditsCommitment, | ||
]); | ||
} else { | ||
newTallyCommitment = hashLeftRight(newResultsCommitment, newSpentVoiceCreditsCommitment); | ||
} | ||
|
||
return { | ||
newTallyCommitment, | ||
newSpentVoiceCreditsCommitment, | ||
newPerVOSpentVoiceCreditsCommitment, | ||
newResultsCommitment, | ||
}; | ||
}; |
Oops, something went wrong.