-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathindex.ts
885 lines (840 loc) · 36.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
#!/usr/bin/env node
import { Command } from "@commander-js/extra-typings";
import {
generateTallyCommitments,
getPollParams,
verify,
getPoll,
getSignedupUserData,
signup,
joinPoll,
getJoinedUserData,
} from "maci-sdk";
import fs from "fs";
import path from "path";
import type { Signer } from "ethers";
import "./cliInit";
import {
genKeyPair,
genMaciPubKey,
deployVkRegistryContract,
deploy,
showContracts,
deployPoll,
publish,
setVerifyingKeys,
mergeSignups,
timeTravel,
genProofs,
fundWallet,
proveOnChain,
checkVerifyingKeys,
genLocalState,
extractVkToFile,
} from "./commands";
import {
DEFAULT_IVCP_DATA,
DEFAULT_SG_DATA,
TallyData,
banner,
info,
logError,
logGreen,
logRed,
promptSensitiveValue,
readContractAddress,
success,
} from "./utils";
// set the description version and name of the cli tool
const { description, version, name } = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "..", "package.json"), "utf8"),
) as { description: string; version: string; name: string };
const program = new Command();
program.name(name).description(description).version(version);
const getSigner = async (): Promise<Signer> => import("maci-contracts").then((m) => m.getDefaultSigner());
// add the commands
program
.command("create")
.description("deploy the contracts")
.option("-i, --initialVoiceCredits <initialVoiceCredits>", "the initial voice credits", parseInt)
.option(
"-p, --initialVoiceCreditsProxyAddress <initialVoiceCreditsProxyAddress>",
"the initial voice credits proxy contract address",
)
.option("--poseidonT3Address <poseidonT3Address>", "PoseidonT3 contract address")
.option("--poseidonT4Address <poseidonT4Address>", "PoseidonT4 contract address")
.option("--poseidonT5Address <poseidonT5Address>", "PoseidonT5 contract address")
.option("--poseidonT6Address <poseidonT6Address>", "PoseidonT6 contract address")
.option("-g, --signupGatekeeperAddress <signupGatekeeperAddress>", "the signup gatekeeper contract address")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.requiredOption("-s, --stateTreeDepth <stateTreeDepth>", "the state tree depth", parseInt)
.action(async (cmdOptions) => {
try {
const signer = await getSigner();
await deploy({
stateTreeDepth: cmdOptions.stateTreeDepth,
initialVoiceCredits: cmdOptions.initialVoiceCredits,
initialVoiceCreditsProxyAddress: cmdOptions.initialVoiceCreditsProxyAddress,
signupGatekeeperAddress: cmdOptions.signupGatekeeperAddress,
poseidonT3Address: cmdOptions.poseidonT3Address,
poseidonT4Address: cmdOptions.poseidonT4Address,
poseidonT5Address: cmdOptions.poseidonT5Address,
poseidonT6Address: cmdOptions.poseidonT6Address,
quiet: cmdOptions.quiet,
signer,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("checkVerifyingKeys")
.description("check that the verifying keys in the contract match the local ones")
.option(
"-u, --use-quadratic-voting <useQuadraticVoting>",
"whether to use quadratic voting",
(value) => value === "true",
true,
)
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.option("-k, --vk-contract <vkContract>", "the VkRegistry contract address")
.requiredOption("-s, --state-tree-depth <stateTreeDepth>", "the state tree depth", parseInt)
.requiredOption("-i, --int-state-tree-depth <intStateTreeDepth>", "the intermediate state tree depth", parseInt)
.requiredOption("-v, --vote-option-tree-depth <voteOptionTreeDepth>", "the vote option tree depth", parseInt)
.requiredOption("-b, --msg-batch-size <messageBatchSize>", "the message batch size", parseInt)
.requiredOption(
"-p, --process-messages-zkey <processMessagesZkeyPath>",
"the process messages zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.requiredOption(
"-t, --tally-votes-zkey <tallyVotesZkeyPath>",
"the tally votes zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.requiredOption(
"--poll-joining-zkey <pollJoiningZkey>",
"the poll joining zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.requiredOption(
"--poll-joined-zkey <pollJoinedZkey>",
"the poll joined zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.action(async (cmdOptions) => {
try {
const signer = await getSigner();
await checkVerifyingKeys({
stateTreeDepth: cmdOptions.stateTreeDepth,
intStateTreeDepth: cmdOptions.intStateTreeDepth,
voteOptionTreeDepth: cmdOptions.voteOptionTreeDepth,
messageBatchSize: cmdOptions.msgBatchSize,
processMessagesZkeyPath: cmdOptions.processMessagesZkey,
tallyVotesZkeyPath: cmdOptions.tallyVotesZkey,
pollJoiningZkeyPath: cmdOptions.pollJoiningZkey,
pollJoinedZkeyPath: cmdOptions.pollJoinedZkey,
vkRegistry: cmdOptions.vkContract,
quiet: cmdOptions.quiet,
useQuadraticVoting: cmdOptions.useQuadraticVoting,
signer,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("genMaciPubKey")
.description("generate a new MACI public key")
.requiredOption("-k, --privkey <privkey>", "the private key")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action((cmdObj) => {
genMaciPubKey(cmdObj.privkey, cmdObj.quiet);
});
program
.command("genMaciKeyPair")
.description("generate a new MACI key pair")
.option("-s, --seed <seed>", "seed value for keypair", (value) => (value ? BigInt(value) : undefined), undefined)
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action((cmdObj) => {
genKeyPair({ seed: cmdObj.seed, quiet: cmdObj.quiet });
});
program
.command("deployVkRegistry")
.description("deploy a new verification key registry contract")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action(async (cmdObj) => {
try {
const signer = await getSigner();
await deployVkRegistryContract({ quiet: cmdObj.quiet, signer });
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("show")
.description("show the deployed contract addresses")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action(async (cmdObj) => {
try {
await showContracts(cmdObj.quiet);
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("deployPoll")
.description("deploy a new poll")
.option("-k, --vkRegistryAddress <vkRegistryAddress>", "the vk registry contract address")
.requiredOption("-s, --start <pollStartDate>", "the poll start date", parseInt)
.requiredOption("-e, --end <pollEndDate>", "the poll end date", parseInt)
.requiredOption("-i, --int-state-tree-depth <intStateTreeDepth>", "the int state tree depth", parseInt)
.requiredOption("-b, --msg-batch-size <messageBatchSize>", "the message batch size", parseInt)
.requiredOption("-v, --vote-option-tree-depth <voteOptionTreeDepth>", "the vote option tree depth", parseInt)
.requiredOption("-p, --pubkey <coordinatorPubkey>", "the coordinator public key")
.option(
"-u, --use-quadratic-voting <useQuadraticVoting>",
"whether to use quadratic voting",
(value) => value === "true",
true,
)
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.option("-m, --relayers <relayers>", "the relayer addresses", (value) => value.split(",").map((item) => item.trim()))
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.option("-o, --vote-options <voteOptions>", "the number of vote options", parseInt)
.action(async (cmdObj) => {
try {
const signer = await getSigner();
await deployPoll({
pollStartDate: cmdObj.start,
pollEndDate: cmdObj.end,
intStateTreeDepth: cmdObj.intStateTreeDepth,
messageBatchSize: cmdObj.msgBatchSize,
voteOptionTreeDepth: cmdObj.voteOptionTreeDepth,
coordinatorPubkey: cmdObj.pubkey,
maciAddress: cmdObj.maciAddress,
vkRegistryAddress: cmdObj.vkRegistryAddress,
relayers: cmdObj.relayers,
quiet: cmdObj.quiet,
useQuadraticVoting: cmdObj.useQuadraticVoting,
signer,
voteOptions: cmdObj.voteOptions,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("joinPoll")
.description("join the poll")
.requiredOption("-k, --priv-key <privKey>", "the private key")
.option("-i, --state-index <stateIndex>", "the user's state index", BigInt)
.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>",
"the voice credit balance of the user for the poll",
BigInt,
)
.requiredOption("-p, --poll-id <pollId>", "the id of the poll", BigInt)
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("--state-file <stateFile>", "the path to the state file containing the serialized maci state")
.option("--start-block <startBlock>", "the block number to start looking for events from", parseInt)
.option("--end-block <endBlock>", "the block number to end looking for events from", parseInt)
.option("--blocks-per-batch <blockPerBatch>", "the number of blocks to process per batch", parseInt)
.option("--transaction-hash <transactionHash>", "transaction hash of MACI contract creation")
.option("--poll-wasm <pollWasm>", "the path to the poll witness generation wasm binary")
.requiredOption(
"--poll-joining-zkey <pollZkeyPath>",
"the poll join zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.option("-w, --wasm", "whether to use the wasm binaries")
.option("-r, --rapidsnark <rapidsnark>", "the path to the rapidsnark binary")
.option("-g, --poll-witnessgen <pollWitnessgen>", "the path to the poll witness generation binary")
.action(async (cmdObj) => {
try {
const signer = await getSigner();
const network = await signer.provider?.getNetwork();
const maciAddress = cmdObj.maciAddress || (await readContractAddress("MACI", network?.name));
const privateKey = cmdObj.privKey || (await promptSensitiveValue("Insert your MACI private key"));
const data = await joinPoll({
maciAddress,
privateKey,
stateIndex: cmdObj.stateIndex,
stateFile: cmdObj.stateFile,
pollId: cmdObj.pollId,
signer,
startBlock: cmdObj.startBlock,
endBlock: cmdObj.endBlock,
blocksPerBatch: cmdObj.blocksPerBatch,
pollJoiningZkey: cmdObj.pollJoiningZkey,
pollWasm: cmdObj.pollWasm,
useWasm: cmdObj.wasm,
rapidsnark: cmdObj.rapidsnark,
pollWitgen: cmdObj.pollWitnessgen,
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 });
}
});
program
.command("setVerifyingKeys")
.description("set the verifying keys")
.requiredOption("-s, --state-tree-depth <stateTreeDepth>", "the state tree depth", parseInt)
.requiredOption("-i, --int-state-tree-depth <intStateTreeDepth>", "the intermediate state tree depth", parseInt)
.requiredOption("-v, --vote-option-tree-depth <voteOptionTreeDepth>", "the vote option tree depth", parseInt)
.requiredOption("-b, --msg-batch-size <messageBatchSize>", "the message batch size", parseInt)
.option(
"--poll-joining-zkey <pollJoiningZkeyPath>",
"the poll joining zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.option(
"--poll-joined-zkey <pollJoinedZkeyPath>",
"the poll joined zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.option(
"--process-messages-zkey-qv <processMessagesZkeyPathQv>",
"the process messages qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.option(
"--tally-votes-zkey-qv <tallyVotesZkeyPathQv>",
"the tally votes qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.option(
"--process-messages-zkey-non-qv <processMessagesZkeyPathNonQv>",
"the process messages non-qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.option(
"--tally-votes-zkey-non-qv <tallyVotesZkeyPathNonQv>",
"the tally votes non-qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.option(
"-u, --use-quadratic-voting <useQuadraticVoting>",
"whether to use quadratic voting",
(value) => value === "true",
true,
)
.option("-k, --vk-registry <vkRegistry>", "the vk registry contract address")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action(async (cmdObj) => {
try {
const signer = await getSigner();
await setVerifyingKeys({
stateTreeDepth: cmdObj.stateTreeDepth,
intStateTreeDepth: cmdObj.intStateTreeDepth,
voteOptionTreeDepth: cmdObj.voteOptionTreeDepth,
messageBatchSize: cmdObj.msgBatchSize,
pollJoiningZkeyPath: cmdObj.pollJoiningZkey,
pollJoinedZkeyPath: cmdObj.pollJoinedZkey,
processMessagesZkeyPathQv: cmdObj.processMessagesZkeyQv,
tallyVotesZkeyPathQv: cmdObj.tallyVotesZkeyQv,
processMessagesZkeyPathNonQv: cmdObj.processMessagesZkeyNonQv,
tallyVotesZkeyPathNonQv: cmdObj.tallyVotesZkeyNonQv,
vkRegistry: cmdObj.vkRegistry,
quiet: cmdObj.quiet,
useQuadraticVoting: cmdObj.useQuadraticVoting,
signer,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("publish")
.description("publish a new message to a MACI Poll contract")
.requiredOption(
"-p, --pubkey <pubkey>",
"the MACI public key which should replace the user's public key in the state tree",
)
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.option("-k, --privkey <privkey>", "your serialized MACI private key")
.requiredOption("-i, --state-index <stateIndex>", "the user's state index", BigInt)
.requiredOption("-v, --vote-option-index <voteOptionIndex>", "the vote option index", BigInt)
.requiredOption("-n, --nonce <nonce>", "the message nonce", BigInt)
.option("-s, --salt <salt>", "the message salt", BigInt)
.requiredOption("-o, --poll-id <pollId>", "the poll id", BigInt)
.requiredOption("-w, --new-vote-weight <newVoteWeight>", "the new vote weight", BigInt)
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action(async (cmdObj) => {
try {
const signer = await getSigner();
const network = await signer.provider?.getNetwork();
const maciAddress = cmdObj.maciAddress || (await readContractAddress("MACI", network?.name));
const privateKey = cmdObj.privkey || (await promptSensitiveValue("Insert your MACI private key"));
await publish({
pubkey: cmdObj.pubkey,
stateIndex: cmdObj.stateIndex,
voteOptionIndex: cmdObj.voteOptionIndex,
nonce: cmdObj.nonce,
pollId: cmdObj.pollId,
newVoteWeight: cmdObj.newVoteWeight,
maciAddress,
salt: cmdObj.salt,
privateKey,
quiet: cmdObj.quiet,
signer,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("mergeSignups")
.description("merge the signups accumulator queue")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.requiredOption("-o, --poll-id <pollId>", "the poll id", BigInt)
.option("-n, --num-queue-ops <numQueueOps>", "the number of queue operations", parseInt)
.action(async (cmdObj) => {
try {
const signer = await getSigner();
await mergeSignups({
pollId: cmdObj.pollId,
maciAddress: cmdObj.maciAddress,
numQueueOps: cmdObj.numQueueOps?.toString(),
quiet: cmdObj.quiet,
signer,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("timeTravel")
.description("fast-forward the time (only works for local hardhat testing")
.requiredOption("-s, --seconds <seconds>", "the number of seconds to fast-forward", parseInt)
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action(async (cmdObj) => {
try {
const signer = await getSigner();
await timeTravel({ seconds: cmdObj.seconds, quiet: cmdObj.quiet, signer });
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("extractVkToFile")
.description("extract vkey to json file")
.requiredOption(
"--poll-joining-zkey <pollJoiningZkey>",
"the poll joining zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.requiredOption(
"--poll-joined-zkey <pollJoinedZkey>",
"the poll joined zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.requiredOption(
"--process-messages-zkey-qv <processMessagesZkeyPathQv>",
"the process messages qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.requiredOption(
"--tally-votes-zkey-qv <tallyVotesZkeyPathQv>",
"the tally votes qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.requiredOption(
"--process-messages-zkey-non-qv <processMessagesZkeyPathNonQv>",
"the process messages non-qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.requiredOption(
"--tally-votes-zkey-non-qv <tallyVotesZkeyPathNonQv>",
"the tally votes non-qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)",
)
.requiredOption("-o, --output-file <outputFile>", "the output file path of extracted vkeys")
.action(async (cmdObj) => {
try {
await extractVkToFile({
processMessagesZkeyPathQv: cmdObj.processMessagesZkeyQv,
tallyVotesZkeyPathQv: cmdObj.tallyVotesZkeyQv,
processMessagesZkeyPathNonQv: cmdObj.processMessagesZkeyNonQv,
tallyVotesZkeyPathNonQv: cmdObj.tallyVotesZkeyNonQv,
pollJoiningZkeyPath: cmdObj.pollJoiningZkey,
pollJoinedZkeyPath: cmdObj.pollJoinedZkey,
outputFilePath: cmdObj.outputFile,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("signup")
.description("Sign up to a MACI contract")
.requiredOption("-p, --pubkey <maciPubKey>", "the MACI public key")
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.option("-s, --sg-data <sgData>", "the signup gateway data")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action(async (cmdObj) => {
try {
const signer = await getSigner();
const network = await signer.provider?.getNetwork();
const maciAddress = cmdObj.maciAddress || (await readContractAddress("MACI", network?.name));
const data = await signup({
maciPubKey: cmdObj.pubkey,
maciAddress,
sgData: cmdObj.sgData ?? DEFAULT_SG_DATA,
signer,
});
logGreen(
cmdObj.quiet,
success(`State index: ${data.stateIndex.toString()}\n Transaction hash: ${data.transactionHash}`),
);
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("isRegisteredUser")
.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)
.action(async (cmdObj) => {
try {
const signer = await getSigner();
const network = await signer.provider?.getNetwork();
const maciAddress = cmdObj.maciAddress || (await readContractAddress("MACI", network?.name));
const data = await getSignedupUserData({
maciPubKey: cmdObj.pubkey,
maciAddress,
signer,
});
if (data.isRegistered) {
logGreen(cmdObj.quiet, success(`State index: ${data.stateIndex?.toString()}`));
} else {
logRed(cmdObj.quiet, "User is not registered");
}
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("isJoinedUser")
.description("Checks if user is joined to the poll with public key")
.requiredOption("-p, --pubkey <pubkey>", "the MACI public key")
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.requiredOption("-o, --poll-id <pollId>", "the poll id", BigInt)
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("--start-block <startBlock>", "the block number to start looking for events from", parseInt)
.option("--end-block <endBlock>", "the block number to end looking for events from", parseInt)
.option("--blocks-per-batch <blockPerBatch>", "the number of blocks to process per batch", parseInt)
.action(async (cmdObj) => {
try {
const signer = await getSigner();
const network = await signer.provider?.getNetwork();
const maciAddress = cmdObj.maciAddress || (await readContractAddress("MACI", network?.name));
const data = await getJoinedUserData({
pollPubKey: cmdObj.pubkey,
startBlock: cmdObj.startBlock!,
maciAddress,
pollId: cmdObj.pollId,
signer,
});
if (data.isJoined) {
logGreen(
cmdObj.quiet,
success(
[
`Poll state index: ${data.pollStateIndex?.toString()}, registered: ${data.isJoined}`,
`Voice credits: ${data.voiceCredits?.toString()}`,
].join("\n"),
),
);
} else {
logRed(cmdObj.quiet, "User has not joined the poll");
}
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("getPoll")
.description("Get deployed poll from MACI contract")
.option("-p, --poll <poll>", "the poll id")
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.action(async (cmdObj) => {
try {
const signer = await getSigner();
const network = await signer.provider?.getNetwork();
const maciAddress = cmdObj.maciAddress || (await readContractAddress("MACI", network?.name));
const details = await getPoll({
pollId: cmdObj.poll,
maciAddress,
signer,
});
logGreen(
true,
success(
[
`ID: ${details.id}`,
`Start time: ${new Date(Number(details.startDate) * 1000).toString()}`,
`End time: ${new Date(Number(details.endDate) * 1000).toString()}`,
`Number of signups ${details.numSignups}`,
`State tree merged: ${details.isMerged}`,
`Mode: ${details.mode === 0n ? "Quadratic Voting" : "Non-Quadratic Voting"}`,
].join("\n"),
),
);
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("fundWallet")
.description("Fund a wallet with Ether")
.requiredOption("-a, --amount <amount>", "the amount of Ether", parseInt)
.requiredOption("-w, --address <address>", "the address to fund")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action(async (cmdObj) => {
try {
const signer = await getSigner();
await fundWallet({ amount: cmdObj.amount, address: cmdObj.address, quiet: cmdObj.quiet, signer });
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("verify")
.description("verify the results of a poll")
.requiredOption("-o, --poll-id <pollId>", "the poll id", BigInt)
.requiredOption(
"-t, --tally-file <tallyFile>",
"the tally file with results, per vote option spent credits, spent voice credits total",
)
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action(async (cmdObj) => {
try {
banner(cmdObj.quiet);
const signer = await getSigner();
const network = await signer.provider?.getNetwork();
// read the tally file
const isTallyFileExists = fs.existsSync(cmdObj.tallyFile);
if (!cmdObj.tallyFile || !isTallyFileExists) {
logError(`Unable to open ${cmdObj.tallyFile}`);
}
const tallyData = JSON.parse(await fs.promises.readFile(cmdObj.tallyFile, { encoding: "utf8" })) as TallyData;
const maciAddress = tallyData.maci || cmdObj.maciAddress || (await readContractAddress("MACI", network?.name));
const pollParams = await getPollParams({ pollId: cmdObj.pollId, maciContractAddress: maciAddress, signer });
const tallyCommitments = generateTallyCommitments({
tallyData,
voteOptionTreeDepth: pollParams.voteOptionTreeDepth,
});
await verify({
tallyData,
pollId: cmdObj.pollId,
maciAddress,
signer,
tallyCommitments,
numVoteOptions: pollParams.numVoteOptions,
voteOptionTreeDepth: pollParams.voteOptionTreeDepth,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("genProofs")
.description("generate the proofs for a poll")
.option("-k, --privkey <privkey>", "your serialized MACI private key")
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.requiredOption("-o, --poll-id <pollId>", "the poll id", BigInt)
.requiredOption(
"-t, --tally-file <tallyFile>",
"the tally file with results, per vote option spent credits, spent voice credits total",
)
.option("-r, --rapidsnark <rapidsnark>", "the path to the rapidsnark binary")
.option("-g, --process-witnessgen <processWitnessgen>", "the path to the process witness generation binary")
.option("--process-witnessdat <processWitnessdat>", "the path to the process witness dat file")
.option("--tally-witnessgen <tallyWitnessgen>", "the path to the tally witness generation binary")
.option("--tally-witnessdat <tallyWitnessdat>", "the path to the tally witness dat file")
.requiredOption("--poll-joining-zkey <processJoinZkey>", "the path to the poll join zkey")
.requiredOption("--process-zkey <processZkey>", "the path to the process zkey")
.requiredOption("--tally-zkey <tallyZkey>", "the path to the tally zkey")
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-p, --rpc-provider <provider>", "the rpc provider URL")
.requiredOption("-f, --output <outputDir>", "the output directory for proofs")
.option("--transaction-hash <transactionHash>", "transaction hash of MACI contract creation")
.option("-w, --wasm", "whether to use the wasm binaries")
.option("--process-wasm <processWasm>", "the path to the process witness generation wasm binary")
.option("--tally-wasm <tallyWasm>", "the path to the tally witness generation wasm binary")
.option("--state-file <stateFile>", "the path to the state file containing the serialized maci state")
.option("--start-block <startBlock>", "the block number to start looking for events from", parseInt)
.option("--end-block <endBlock>", "the block number to end looking for events from", parseInt)
.option("--blocks-per-batch <blockPerBatch>", "the number of blocks to process per batch", parseInt)
.option(
"-u, --use-quadratic-voting <useQuadraticVoting>",
"whether to use quadratic voting",
(value) => value === "true",
true,
)
.option(
"-b, --ipfs-message-backup-files <ipfsMessageBackupFiles>",
"Backup files for ipfs messages (name format: ipfsHash1.json, ipfsHash2.json, ..., ipfsHashN.json)",
(value: string | undefined) => value?.split(/\s*,\s*/),
)
.action(async (cmdObj) => {
try {
const signer = await getSigner();
await genProofs({
outputDir: cmdObj.output,
tallyFile: cmdObj.tallyFile,
tallyZkey: cmdObj.tallyZkey,
processZkey: cmdObj.processZkey,
pollId: cmdObj.pollId,
rapidsnark: cmdObj.rapidsnark,
processWitgen: cmdObj.processWitnessgen,
processDatFile: cmdObj.processWitnessdat,
tallyWitgen: cmdObj.tallyWitnessgen,
tallyDatFile: cmdObj.tallyWitnessdat,
coordinatorPrivKey: cmdObj.privkey,
maciAddress: cmdObj.maciAddress,
transactionHash: cmdObj.transactionHash,
processWasm: cmdObj.processWasm,
tallyWasm: cmdObj.tallyWasm,
useWasm: cmdObj.wasm,
stateFile: cmdObj.stateFile,
startBlock: cmdObj.startBlock,
endBlock: cmdObj.endBlock,
blocksPerBatch: cmdObj.blocksPerBatch,
useQuadraticVoting: cmdObj.useQuadraticVoting,
ipfsMessageBackupFiles: cmdObj.ipfsMessageBackupFiles,
quiet: cmdObj.quiet,
signer,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("genLocalState")
.description("generate a local MACI state from the smart contracts events")
.requiredOption("-o, --output <outputPath>", "the path where to write the state")
.requiredOption("-p, --poll-id <pollId>", "the id of the poll", BigInt)
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.option("-k, --privkey <privkey>", "your serialized MACI private key")
.option("--start-block <startBlock>", "the start block number", parseInt)
.option("--end-block <endBlock>", "the end block number", parseInt)
.option("--blocks-per-batch <blockPerBatch>", "the blocks per batch", parseInt)
.option("--transaction-hash <transactionHash>", "the transaction hash")
.option("-s, --sleep <sleep>", "the sleep time between batches", parseInt)
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.option(
"-b, --ipfs-message-backup-files <ipfsMessageBackupFiles>",
"Backup files for ipfs messages (name format: ipfsHash1.json, ipfsHash2.json, ..., ipfsHashN.json)",
(value: string | undefined) => value?.split(/\s*,\s*/),
)
.action(async (cmdObj) => {
try {
const signer = await getSigner();
await genLocalState({
outputPath: cmdObj.output.toString(),
pollId: cmdObj.pollId,
maciAddress: cmdObj.maciAddress,
coordinatorPrivateKey: cmdObj.privkey,
ethereumProvider: cmdObj.rpcProvider,
endBlock: cmdObj.endBlock,
startBlock: cmdObj.startBlock,
blockPerBatch: cmdObj.blocksPerBatch,
transactionHash: cmdObj.transactionHash,
ipfsMessageBackupFiles: cmdObj.ipfsMessageBackupFiles,
sleep: cmdObj.sleep,
quiet: cmdObj.quiet,
signer,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
program
.command("proveOnChain")
.description("prove the results of a poll on chain")
.requiredOption("-o, --poll-id <pollId>", "the poll id", BigInt)
.option(
"-t, --tally-file <tallyFile>",
"the tally file with results, per vote option spent credits, spent voice credits total",
)
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.option("-x, --maci-address <maciAddress>", "the MACI contract address")
.requiredOption("-f, --proof-dir <proofDir>", "the proof output directory from the genProofs subcommand")
.action(async (cmdObj) => {
try {
const signer = await getSigner();
await proveOnChain({
pollId: cmdObj.pollId,
tallyFile: cmdObj.tallyFile,
proofDir: cmdObj.proofDir,
maciAddress: cmdObj.maciAddress,
quiet: cmdObj.quiet,
signer,
});
} catch (error) {
program.error((error as Error).message, { exitCode: 1 });
}
});
if (require.main === module) {
program.parseAsync(process.argv);
}
// export everything so we can use in other packages
export {
checkVerifyingKeys,
deploy,
deployPoll,
getPoll,
deployVkRegistryContract,
fundWallet,
genLocalState,
genKeyPair,
genMaciPubKey,
genProofs,
mergeSignups,
publish,
publishBatch,
proveOnChain,
setVerifyingKeys,
signup,
isRegisteredUser,
timeTravel,
verify,
joinPoll,
isJoinedUser,
generateAndVerifyProof,
} from "./commands";
export type {
DeployedContracts,
PollContracts,
TallyData,
DeployPollArgs,
GenLocalStateArgs,
GenProofsArgs,
PublishArgs,
SignupArgs,
MergeSignupsArgs,
VerifyArgs,
ProveOnChainArgs,
DeployArgs,
IRegisteredUserArgs,
IGenKeypairArgs,
IGetPollArgs,
IGetPollData,
IPublishBatchArgs,
IPublishBatchData,
IPublishMessage,
ISignupData,
} from "./utils";