@dorafactory/maci-sdk 0.0.41 → 0.0.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -217,10 +217,12 @@ const voteResponse = await client.maci.vote({
217
217
  **Voting Rules:**
218
218
 
219
219
  MACI supports two voting rules:
220
+
220
221
  1. 1P1V (One Person One Vote): Each voting weight (vc) directly counts as votes
221
222
  2. QV (Quadratic Voting): The sum of squares of voting weights (vc) is consumed as total voting power
222
223
 
223
224
  Vote options format:
225
+
224
226
  ```typescript
225
227
  selectedOptions: [
226
228
  { idx: number, vc: number }, // idx: option index, vc: voting weight
@@ -229,13 +231,14 @@ selectedOptions: [
229
231
  ```
230
232
 
231
233
  Examples:
234
+
232
235
  ```typescript
233
236
  // 1P1V mode example (total voting power: 4)
234
237
  const options1p1v = [
235
238
  { idx: 0, vc: 2 }, // 2 votes for option 0
236
239
  { idx: 1, vc: 1 }, // 1 vote for option 1
237
240
  { idx: 4, vc: 1 }, // 1 vote for option 4
238
- ];
241
+ ];
239
242
  // Total voting power consumed = 2 + 1 + 1 = 4
240
243
 
241
244
  // QV mode example (total voting power: 6)
@@ -248,6 +251,7 @@ const optionsQv = [
248
251
  ```
249
252
 
250
253
  **Important Notes:**
254
+
251
255
  - Option indices (idx) must be unique
252
256
  - Voting weights (vc) must be positive integers
253
257
  - In QV mode, total voting power consumed is the sum of squares of voting weights
package/dist/index.js CHANGED
@@ -51,7 +51,7 @@ __export(index_exports, {
51
51
  bigInt2Buffer: () => bigInt2Buffer,
52
52
  circuits: () => circuits,
53
53
  formatPrivKeyForBabyJub: () => formatPrivKeyForBabyJub,
54
- genAddKeyProof: () => genAddKeyProof,
54
+ genAddKeyInput: () => genAddKeyInput,
55
55
  genEcdhSharedKey: () => genEcdhSharedKey,
56
56
  genKeypair: () => genKeypair,
57
57
  genKeypairFromSign: () => genKeypairFromSign,
@@ -534,7 +534,7 @@ var Circuit = class {
534
534
  var import_bech32 = require("bech32");
535
535
 
536
536
  // src/libs/crypto/keys.ts
537
- var import_crypto2 = require("crypto");
537
+ var import_crypto_js2 = __toESM(require("crypto-js"));
538
538
 
539
539
  // src/libs/crypto/bigintUtils.ts
540
540
  var stringizing = (o, path = []) => {
@@ -572,7 +572,7 @@ var import_eddsa_poseidon2 = require("@zk-kit/eddsa-poseidon");
572
572
 
573
573
  // src/libs/crypto/babyjub.ts
574
574
  var import_assert2 = __toESM(require("assert"));
575
- var import_crypto = require("crypto");
575
+ var import_crypto_js = __toESM(require("crypto-js"));
576
576
 
577
577
  // src/libs/crypto/constants.ts
578
578
  var import_baby_jubjub = require("@zk-kit/baby-jubjub");
@@ -664,10 +664,14 @@ var G2Point = class {
664
664
  }
665
665
  };
666
666
  var genRandomBabyJubValue = () => {
667
- const min = BigInt("6350874878119819312338956282401532410528162663560392320966563075034087161851");
667
+ const min = BigInt(
668
+ "6350874878119819312338956282401532410528162663560392320966563075034087161851"
669
+ );
668
670
  let privKey = SNARK_FIELD_SIZE;
669
671
  do {
670
- const rand = BigInt(`0x${(0, import_crypto.randomBytes)(32).toString("hex")}`);
672
+ const rand = BigInt(
673
+ `0x${import_crypto_js.default.lib.WordArray.random(32).toString(import_crypto_js.default.enc.Hex)}`
674
+ );
671
675
  if (rand >= min) {
672
676
  privKey = rand % SNARK_FIELD_SIZE;
673
677
  }
@@ -896,7 +900,7 @@ var tree_default = Tree;
896
900
 
897
901
  // src/libs/crypto/keys.ts
898
902
  var SNARK_FIELD_SIZE2 = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
899
- var genPrivKey = () => BigInt(`0x${(0, import_crypto2.randomBytes)(32).toString("hex")}`);
903
+ var genPrivKey = () => BigInt(`0x${import_crypto_js2.default.lib.WordArray.random(32).toString(import_crypto_js2.default.enc.Hex)}`);
900
904
  var genRandomSalt = () => genRandomBabyJubValue();
901
905
  var formatPrivKeyForBabyJub = (privKey) => BigInt((0, import_eddsa_poseidon.deriveSecretScalar)(bigInt2Buffer(privKey)));
902
906
  var packPubKey = (pubKey) => BigInt((0, import_eddsa_poseidon2.packPublicKey)(pubKey));
@@ -918,7 +922,9 @@ var genKeypair = (pkey) => {
918
922
  var genEcdhSharedKey = (privKey, pubKey) => (0, import_baby_jubjub2.mulPointEscalar)(pubKey, formatPrivKeyForBabyJub(privKey));
919
923
  var genMessageFactory = (stateIdx, signPriKey, signPubKey, coordPubKey) => (encPriKey, nonce, voIdx, newVotes, isLastCmd, salt) => {
920
924
  if (!salt) {
921
- salt = BigInt("0x" + (0, import_crypto2.randomBytes)(7).toString("hex"));
925
+ salt = BigInt(
926
+ `0x${import_crypto_js2.default.lib.WordArray.random(7).toString(import_crypto_js2.default.enc.Hex)}`
927
+ );
922
928
  }
923
929
  const packaged = BigInt(nonce) + (BigInt(stateIdx) << 32n) + (BigInt(voIdx) << 64n) + (BigInt(newVotes) << 96n) + (BigInt(salt) << 192n);
924
930
  let newPubKey = [...signPubKey];
@@ -989,7 +995,7 @@ var rerandomize = (pubKey, ciphertext, randomVal = genRandomSalt()) => {
989
995
  d2
990
996
  };
991
997
  };
992
- var genAddKeyProof = async (depth, {
998
+ var genAddKeyInput = (depth, {
993
999
  coordPubKey,
994
1000
  oldKey,
995
1001
  deactivates
@@ -5215,31 +5221,39 @@ var MACI = class {
5215
5221
  signer,
5216
5222
  address,
5217
5223
  contractAddress,
5218
- certificate,
5219
- mode = "maci"
5224
+ certificate
5220
5225
  }) {
5221
5226
  if (!address) {
5222
5227
  address = (await signer.getAccounts())[0].address;
5223
5228
  }
5224
- if (mode === "amaci") {
5229
+ const round = await this.indexer.getRoundWithFields(contractAddress, [
5230
+ "maciType",
5231
+ "voiceCreditAmount"
5232
+ ]);
5233
+ if (isErrorResponse(round)) {
5234
+ throw new Error(
5235
+ `Failed to get round info: ${round.error.type} ${round.error.message}`
5236
+ );
5237
+ }
5238
+ if (round.data.round.maciType === "aMACI") {
5225
5239
  const isWhiteListed = await this.isWhitelisted({
5226
5240
  signer,
5227
5241
  address,
5228
5242
  contractAddress
5229
5243
  });
5230
5244
  if (isWhiteListed) {
5231
- const round = await this.indexer.getRoundWithFields(contractAddress, [
5245
+ const round2 = await this.indexer.getRoundWithFields(contractAddress, [
5232
5246
  "voiceCreditAmount"
5233
5247
  ]);
5234
- if (!isErrorResponse(round)) {
5235
- if (round.data.round.voiceCreditAmount) {
5236
- return round.data.round.voiceCreditAmount;
5248
+ if (!isErrorResponse(round2)) {
5249
+ if (round2.data.round.voiceCreditAmount) {
5250
+ return round2.data.round.voiceCreditAmount;
5237
5251
  } else {
5238
5252
  return "0";
5239
5253
  }
5240
5254
  } else {
5241
5255
  throw new Error(
5242
- `Failed to query amaci voice credit: ${round.error.type} ${round.error.message}`
5256
+ `Failed to query amaci voice credit: ${round2.error.type} ${round2.error.message}`
5243
5257
  );
5244
5258
  }
5245
5259
  } else {
@@ -5499,11 +5513,47 @@ var MACI = class {
5499
5513
  throw new Error("State index is not set, Please signup first");
5500
5514
  }
5501
5515
  try {
5502
- const voiceCreditBalance = await this.getVoiceCreditBalance({
5503
- signer,
5504
- stateIdx,
5505
- contractAddress
5506
- });
5516
+ const round = await this.indexer.getRoundWithFields(contractAddress, [
5517
+ "maciType",
5518
+ "voiceCreditAmount"
5519
+ ]);
5520
+ if (isErrorResponse(round)) {
5521
+ throw new Error(
5522
+ `Failed to get round info: ${round.error.type} ${round.error.message}`
5523
+ );
5524
+ }
5525
+ let voiceCreditBalance;
5526
+ if (round.data.round.maciType === "aMACI") {
5527
+ const isWhiteListed = await this.isWhitelisted({
5528
+ signer,
5529
+ address,
5530
+ contractAddress
5531
+ });
5532
+ if (isWhiteListed) {
5533
+ const round2 = await this.indexer.getRoundWithFields(contractAddress, [
5534
+ "voiceCreditAmount"
5535
+ ]);
5536
+ if (!isErrorResponse(round2)) {
5537
+ if (round2.data.round.voiceCreditAmount) {
5538
+ voiceCreditBalance = round2.data.round.voiceCreditAmount;
5539
+ } else {
5540
+ voiceCreditBalance = "0";
5541
+ }
5542
+ } else {
5543
+ throw new Error(
5544
+ `Failed to query amaci voice credit: ${round2.error.type} ${round2.error.message}`
5545
+ );
5546
+ }
5547
+ } else {
5548
+ voiceCreditBalance = "0";
5549
+ }
5550
+ } else {
5551
+ voiceCreditBalance = await this.getVoiceCreditBalance({
5552
+ signer,
5553
+ stateIdx,
5554
+ contractAddress
5555
+ });
5556
+ }
5507
5557
  const options = await this.processVoteOptions({
5508
5558
  selectedOptions,
5509
5559
  contractAddress,
@@ -5636,28 +5686,6 @@ var MACI = class {
5636
5686
  }) {
5637
5687
  const gasPrice = import_stargate2.GasPrice.fromString("100000000000peaka");
5638
5688
  fee = fee || (0, import_stargate2.calculateFee)(6e7, gasPrice);
5639
- if (gasStation === true) {
5640
- const grantFee = {
5641
- amount: fee.amount,
5642
- gas: fee.gas,
5643
- granter: contractAddress
5644
- };
5645
- return client.execute(
5646
- address,
5647
- contractAddress,
5648
- {
5649
- sign_up: {
5650
- pubkey: {
5651
- x: pubKey[0].toString(),
5652
- y: pubKey[1].toString()
5653
- },
5654
- amount: oracleCertificate.amount,
5655
- certificate: oracleCertificate.signature
5656
- }
5657
- },
5658
- grantFee
5659
- );
5660
- }
5661
5689
  return client.execute(
5662
5690
  address,
5663
5691
  contractAddress,
@@ -5671,74 +5699,73 @@ var MACI = class {
5671
5699
  certificate: oracleCertificate.signature
5672
5700
  }
5673
5701
  },
5674
- fee
5702
+ gasStation === true ? {
5703
+ amount: fee.amount,
5704
+ gas: fee.gas,
5705
+ granter: contractAddress
5706
+ } : fee
5675
5707
  );
5676
5708
  }
5677
- // async submitDeactivate({
5678
- // signer,
5679
- // client,
5680
- // address,
5681
- // maciAccount,
5682
- // contractAddress,
5683
- // gasStation,
5684
- // fee,
5685
- // }: {
5686
- // signer: OfflineSigner;
5687
- // client: SigningCosmWasmClient;
5688
- // address?: string;
5689
- // maciAccount: Keypair;
5690
- // contractAddress: string;
5691
- // gasStation: boolean;
5692
- // fee?: StdFee;
5693
- // }) {
5694
- // try {
5695
- // address = address || (await signer.getAccounts())[0].address;
5696
- // const stateIdx = await this.getStateIdxInc({
5697
- // signer,
5698
- // address,
5699
- // contractAddress,
5700
- // });
5701
- // const operatorCoordPubKey = await this.getRoundInfo({
5702
- // contractAddress,
5703
- // });
5704
- // const payload = batchGenMessage(
5705
- // Number(stateIdx),
5706
- // maciAccount,
5707
- // [
5708
- // BigInt(operatorCoordPubKey.coordinatorPubkeyX),
5709
- // BigInt(operatorCoordPubKey.coordinatorPubkeyY),
5710
- // ],
5711
- // [[0, 0]]
5712
- // );
5713
- // const { msg, encPubkeys } = payload[0];
5714
- // const gasPrice = GasPrice.fromString('100000000000peaka');
5715
- // fee = fee || calculateFee(20000000, gasPrice);
5716
- // return client.execute(
5717
- // address,
5718
- // contractAddress,
5719
- // stringizing({
5720
- // publish_deactivate_message: {
5721
- // enc_pub_key: {
5722
- // x: encPubkeys[0],
5723
- // y: encPubkeys[1],
5724
- // },
5725
- // message: {
5726
- // data: msg,
5727
- // },
5728
- // },
5729
- // }),
5730
- // gasStation === true
5731
- // ? {
5732
- // amount: fee.amount,
5733
- // gas: fee.gas,
5734
- // granter: contractAddress,
5735
- // }
5736
- // : fee
5737
- // );
5738
- // } catch (error) {
5739
- // throw Error(`Submit deactivate failed! ${error}`);
5740
- // }
5741
- // }
5709
+ async deactivate({
5710
+ signer,
5711
+ address,
5712
+ maciKeypair,
5713
+ contractAddress,
5714
+ gasStation,
5715
+ fee
5716
+ }) {
5717
+ try {
5718
+ address = address || (await signer.getAccounts())[0].address;
5719
+ if (maciKeypair === void 0) {
5720
+ maciKeypair = this.maciKeypair;
5721
+ }
5722
+ const client = await this.contract.contractClient({
5723
+ signer
5724
+ });
5725
+ const stateIdx = await this.getStateIdxInc({
5726
+ signer,
5727
+ address,
5728
+ contractAddress
5729
+ });
5730
+ const operatorCoordPubKey = await this.getRoundInfo({
5731
+ contractAddress
5732
+ });
5733
+ const payload = batchGenMessage(
5734
+ Number(stateIdx),
5735
+ maciKeypair,
5736
+ [
5737
+ BigInt(operatorCoordPubKey.coordinatorPubkeyX),
5738
+ BigInt(operatorCoordPubKey.coordinatorPubkeyY)
5739
+ ],
5740
+ [[0, 0]]
5741
+ );
5742
+ const { msg, encPubkeys } = payload[0];
5743
+ const gasPrice = import_stargate2.GasPrice.fromString("100000000000peaka");
5744
+ fee = fee || (0, import_stargate2.calculateFee)(2e7, gasPrice);
5745
+ return client.execute(
5746
+ address,
5747
+ contractAddress,
5748
+ stringizing({
5749
+ publish_deactivate_message: {
5750
+ enc_pub_key: {
5751
+ x: encPubkeys[0],
5752
+ y: encPubkeys[1]
5753
+ },
5754
+ message: {
5755
+ data: msg
5756
+ }
5757
+ }
5758
+ }),
5759
+ gasStation === true ? {
5760
+ amount: fee.amount,
5761
+ gas: fee.gas,
5762
+ granter: contractAddress
5763
+ } : fee
5764
+ );
5765
+ } catch (error) {
5766
+ throw Error(`Submit deactivate failed! ${error}`);
5767
+ }
5768
+ }
5742
5769
  async fetchAllDeactivateLogs({
5743
5770
  contractAddress
5744
5771
  }) {
@@ -5749,7 +5776,8 @@ var MACI = class {
5749
5776
  // signer,
5750
5777
  // client,
5751
5778
  // address,
5752
- // maciAccount,
5779
+ // maciKeypair,
5780
+ // newMaciKeypair,
5753
5781
  // contractAddress,
5754
5782
  // gasStation,
5755
5783
  // fee,
@@ -5757,7 +5785,8 @@ var MACI = class {
5757
5785
  // signer: OfflineSigner;
5758
5786
  // client: SigningCosmWasmClient;
5759
5787
  // address?: string;
5760
- // maciAccount: Keypair;
5788
+ // maciKeypair: Keypair;
5789
+ // newMaciKeypair: Keypair;
5761
5790
  // contractAddress: string;
5762
5791
  // gasStation: boolean;
5763
5792
  // fee?: StdFee;
@@ -5773,9 +5802,32 @@ var MACI = class {
5773
5802
  // BigInt(roundInfo.coordinatorPubkeyX),
5774
5803
  // BigInt(roundInfo.coordinatorPubkeyY),
5775
5804
  // ],
5776
- // oldKey: maciAccount,
5805
+ // oldKey: maciKeypair,
5777
5806
  // deactivates: deactivates.map((d: any) => d.map(BigInt)),
5778
5807
  // });
5808
+ // if (!inputObj) {
5809
+ // throw new Error('Failed to generate add key proof');
5810
+ // }
5811
+ // const amaciClient = await this.contract.amaciClient({
5812
+ // signer,
5813
+ // contractAddress,
5814
+ // });
5815
+ // const d = [
5816
+ // inputObj.c1[0].toString(),
5817
+ // inputObj.c1[1].toString(),
5818
+ // inputObj.c2[0].toString(),
5819
+ // inputObj.c2[1].toString(),
5820
+ // ];
5821
+ // const result = await amaciClient.addNewKey({
5822
+ // d: d,
5823
+ // groth16Proof: inputObj.proof,
5824
+ // nullifier: inputObj.nullifier.toString(),
5825
+ // pubkey: {
5826
+ // x: newMaciKeypair.pubKey[0].toString(),
5827
+ // y: newMaciKeypair.pubKey[1].toString(),
5828
+ // },
5829
+ // });
5830
+ // return result;
5779
5831
  // }
5780
5832
  async claimAMaciRound({
5781
5833
  signer,
@@ -6094,7 +6146,7 @@ var MaciClient2 = class {
6094
6146
  }) {
6095
6147
  return await this.maci.getStateIdxByPubKey({
6096
6148
  contractAddress,
6097
- pubKey
6149
+ pubKey: pubKey || this.maciKeypair.pubKey
6098
6150
  });
6099
6151
  }
6100
6152
  async feegrantAllowance({
@@ -6123,8 +6175,7 @@ var MaciClient2 = class {
6123
6175
  signer,
6124
6176
  address,
6125
6177
  contractAddress,
6126
- certificate,
6127
- mode = "maci"
6178
+ certificate
6128
6179
  }) {
6129
6180
  signer = this.getSigner(signer);
6130
6181
  if (!address) {
@@ -6134,8 +6185,7 @@ var MaciClient2 = class {
6134
6185
  signer,
6135
6186
  address,
6136
6187
  contractAddress,
6137
- certificate,
6138
- mode
6188
+ certificate
6139
6189
  });
6140
6190
  }
6141
6191
  async isWhitelisted({
@@ -6252,6 +6302,23 @@ var MaciClient2 = class {
6252
6302
  gasStation
6253
6303
  });
6254
6304
  }
6305
+ async deactivate({
6306
+ signer,
6307
+ address,
6308
+ contractAddress,
6309
+ gasStation = false,
6310
+ maciKeypair,
6311
+ fee
6312
+ }) {
6313
+ return await this.maci.deactivate({
6314
+ signer: this.getSigner(signer),
6315
+ address,
6316
+ maciKeypair,
6317
+ contractAddress,
6318
+ gasStation,
6319
+ fee
6320
+ });
6321
+ }
6255
6322
  async claimAMaciRound({
6256
6323
  signer,
6257
6324
  contractAddress,
@@ -6324,7 +6391,7 @@ var MaciClient2 = class {
6324
6391
  bigInt2Buffer,
6325
6392
  circuits,
6326
6393
  formatPrivKeyForBabyJub,
6327
- genAddKeyProof,
6394
+ genAddKeyInput,
6328
6395
  genEcdhSharedKey,
6329
6396
  genKeypair,
6330
6397
  genKeypairFromSign,