@dorafactory/maci-sdk 0.0.50 → 0.0.52

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/dist/index.js CHANGED
@@ -570,9 +570,10 @@ var import_ethers3 = require("ethers");
570
570
  var import_baby_jubjub2 = require("@zk-kit/baby-jubjub");
571
571
  var import_eddsa_poseidon2 = require("@zk-kit/eddsa-poseidon");
572
572
 
573
- // src/libs/crypto/babyjub.ts
573
+ // src/libs/crypto/hashing.ts
574
+ var import_poseidon_cipher = require("@zk-kit/poseidon-cipher");
575
+ var import_ethers2 = require("ethers");
574
576
  var import_assert2 = __toESM(require("assert"));
575
- var import_crypto_js = __toESM(require("crypto-js"));
576
577
 
577
578
  // src/libs/crypto/constants.ts
578
579
  var import_baby_jubjub = require("@zk-kit/baby-jubjub");
@@ -589,100 +590,7 @@ var PAD_KEY_HASH = BigInt(
589
590
  "1309255631273308531193241901289907343161346846555918942743921933037802809814"
590
591
  );
591
592
 
592
- // src/libs/crypto/babyjub.ts
593
- var G1Point = class {
594
- /**
595
- * Create a new instance of G1Point
596
- * @param x the x coordinate
597
- * @param y the y coordinate
598
- */
599
- constructor(x, y) {
600
- (0, import_assert2.default)(x < SNARK_FIELD_SIZE && x >= 0, "G1Point x out of range");
601
- (0, import_assert2.default)(y < SNARK_FIELD_SIZE && y >= 0, "G1Point y out of range");
602
- this.x = x;
603
- this.y = y;
604
- }
605
- /**
606
- * Check whether two points are equal
607
- * @param pt the point to compare with
608
- * @returns whether they are equal or not
609
- */
610
- equals(pt) {
611
- return this.x === pt.x && this.y === pt.y;
612
- }
613
- /**
614
- * Return the point as a contract param in the form of an object
615
- * @returns the point as a contract param
616
- */
617
- asContractParam() {
618
- return {
619
- x: this.x.toString(),
620
- y: this.y.toString()
621
- };
622
- }
623
- };
624
- var G2Point = class {
625
- /**
626
- * Create a new instance of G2Point
627
- * @param x the x coordinate
628
- * @param y the y coordinate
629
- */
630
- constructor(x, y) {
631
- this.checkPointsRange(x, "x");
632
- this.checkPointsRange(y, "y");
633
- this.x = x;
634
- this.y = y;
635
- }
636
- /**
637
- * Check whether two points are equal
638
- * @param pt the point to compare with
639
- * @returns whether they are equal or not
640
- */
641
- equals(pt) {
642
- return this.x[0] === pt.x[0] && this.x[1] === pt.x[1] && this.y[0] === pt.y[0] && this.y[1] === pt.y[1];
643
- }
644
- /**
645
- * Return the point as a contract param in the form of an object
646
- * @returns the point as a contract param
647
- */
648
- asContractParam() {
649
- return {
650
- x: this.x.map((n) => n.toString()),
651
- y: this.y.map((n) => n.toString())
652
- };
653
- }
654
- /**
655
- * Check whether the points are in range
656
- * @param x the x coordinate
657
- * @param type the type of the coordinate
658
- */
659
- checkPointsRange(x, type) {
660
- (0, import_assert2.default)(
661
- x.every((n) => n < SNARK_FIELD_SIZE && n >= 0),
662
- `G2Point ${type} out of range`
663
- );
664
- }
665
- };
666
- var genRandomBabyJubValue = () => {
667
- const min = BigInt(
668
- "6350874878119819312338956282401532410528162663560392320966563075034087161851"
669
- );
670
- let privKey = SNARK_FIELD_SIZE;
671
- do {
672
- const rand = BigInt(
673
- `0x${import_crypto_js.default.lib.WordArray.random(32).toString(import_crypto_js.default.enc.Hex)}`
674
- );
675
- if (rand >= min) {
676
- privKey = rand % SNARK_FIELD_SIZE;
677
- }
678
- } while (privKey >= SNARK_FIELD_SIZE);
679
- return privKey;
680
- };
681
-
682
593
  // src/libs/crypto/hashing.ts
683
- var import_poseidon_cipher = require("@zk-kit/poseidon-cipher");
684
- var import_ethers2 = require("ethers");
685
- var import_assert3 = __toESM(require("assert"));
686
594
  var sha256Hash = (input) => {
687
595
  const types = [];
688
596
  input.forEach(() => {
@@ -697,19 +605,19 @@ var sha256Hash = (input) => {
697
605
  };
698
606
  var poseidon = (inputs) => (0, import_poseidon_cipher.poseidonPerm)([BigInt(0), ...inputs.map((x) => BigInt(x))])[0];
699
607
  var poseidonT3 = (inputs) => {
700
- (0, import_assert3.default)(inputs.length === 2);
608
+ (0, import_assert2.default)(inputs.length === 2);
701
609
  return poseidon(inputs);
702
610
  };
703
611
  var poseidonT4 = (inputs) => {
704
- (0, import_assert3.default)(inputs.length === 3);
612
+ (0, import_assert2.default)(inputs.length === 3);
705
613
  return poseidon(inputs);
706
614
  };
707
615
  var poseidonT5 = (inputs) => {
708
- (0, import_assert3.default)(inputs.length === 4);
616
+ (0, import_assert2.default)(inputs.length === 4);
709
617
  return poseidon(inputs);
710
618
  };
711
619
  var poseidonT6 = (inputs) => {
712
- (0, import_assert3.default)(inputs.length === 5);
620
+ (0, import_assert2.default)(inputs.length === 5);
713
621
  return poseidon(inputs);
714
622
  };
715
623
  var hashLeftRight = (left, right) => poseidonT3([left, right]);
@@ -777,14 +685,14 @@ var Tree = class _Tree {
777
685
  get root() {
778
686
  return this.nodes[0];
779
687
  }
780
- async initZero(zero) {
688
+ initZero(zero) {
781
689
  this.zeros = new Array(this.HEIGHT);
782
690
  this.zeros[0] = zero;
783
691
  for (let i = 1; i < this.zeros.length; i++) {
784
- this.zeros[i] = await poseidon([this.zeros[i - 1], this.zeros[i - 1]]);
692
+ this.zeros[i] = poseidon([this.zeros[i - 1], this.zeros[i - 1]]);
785
693
  }
786
694
  }
787
- async initNodes() {
695
+ initNodes() {
788
696
  const DEGREE = this.DEGREE;
789
697
  this.nodes = new Array(this.NODES_COUNT);
790
698
  for (let d = this.DEPTH; d >= 0; d--) {
@@ -884,12 +792,12 @@ var Tree = class _Tree {
884
792
  subTree._update(this.LEAVES_IDX_0 + length - 1);
885
793
  return subTree;
886
794
  }
887
- async _update(nodeIdx) {
795
+ _update(nodeIdx) {
888
796
  let idx = nodeIdx;
889
797
  while (idx > 0) {
890
798
  const parentIdx = Math.floor((idx - 1) / this.DEGREE);
891
799
  const childrenIdx0 = parentIdx * this.DEGREE + 1;
892
- this.nodes[parentIdx] = await poseidon(
800
+ this.nodes[parentIdx] = poseidon(
893
801
  this.nodes.slice(childrenIdx0, childrenIdx0 + this.DEGREE)
894
802
  );
895
803
  idx = parentIdx;
@@ -898,6 +806,97 @@ var Tree = class _Tree {
898
806
  };
899
807
  var tree_default = Tree;
900
808
 
809
+ // src/libs/crypto/babyjub.ts
810
+ var import_assert3 = __toESM(require("assert"));
811
+ var import_crypto_js = __toESM(require("crypto-js"));
812
+ var G1Point = class {
813
+ /**
814
+ * Create a new instance of G1Point
815
+ * @param x the x coordinate
816
+ * @param y the y coordinate
817
+ */
818
+ constructor(x, y) {
819
+ (0, import_assert3.default)(x < SNARK_FIELD_SIZE && x >= 0, "G1Point x out of range");
820
+ (0, import_assert3.default)(y < SNARK_FIELD_SIZE && y >= 0, "G1Point y out of range");
821
+ this.x = x;
822
+ this.y = y;
823
+ }
824
+ /**
825
+ * Check whether two points are equal
826
+ * @param pt the point to compare with
827
+ * @returns whether they are equal or not
828
+ */
829
+ equals(pt) {
830
+ return this.x === pt.x && this.y === pt.y;
831
+ }
832
+ /**
833
+ * Return the point as a contract param in the form of an object
834
+ * @returns the point as a contract param
835
+ */
836
+ asContractParam() {
837
+ return {
838
+ x: this.x.toString(),
839
+ y: this.y.toString()
840
+ };
841
+ }
842
+ };
843
+ var G2Point = class {
844
+ /**
845
+ * Create a new instance of G2Point
846
+ * @param x the x coordinate
847
+ * @param y the y coordinate
848
+ */
849
+ constructor(x, y) {
850
+ this.checkPointsRange(x, "x");
851
+ this.checkPointsRange(y, "y");
852
+ this.x = x;
853
+ this.y = y;
854
+ }
855
+ /**
856
+ * Check whether two points are equal
857
+ * @param pt the point to compare with
858
+ * @returns whether they are equal or not
859
+ */
860
+ equals(pt) {
861
+ return this.x[0] === pt.x[0] && this.x[1] === pt.x[1] && this.y[0] === pt.y[0] && this.y[1] === pt.y[1];
862
+ }
863
+ /**
864
+ * Return the point as a contract param in the form of an object
865
+ * @returns the point as a contract param
866
+ */
867
+ asContractParam() {
868
+ return {
869
+ x: this.x.map((n) => n.toString()),
870
+ y: this.y.map((n) => n.toString())
871
+ };
872
+ }
873
+ /**
874
+ * Check whether the points are in range
875
+ * @param x the x coordinate
876
+ * @param type the type of the coordinate
877
+ */
878
+ checkPointsRange(x, type) {
879
+ (0, import_assert3.default)(
880
+ x.every((n) => n < SNARK_FIELD_SIZE && n >= 0),
881
+ `G2Point ${type} out of range`
882
+ );
883
+ }
884
+ };
885
+ var genRandomBabyJubValue = () => {
886
+ const min = 6350874878119819312338956282401532410528162663560392320966563075034087161851n;
887
+ let rand;
888
+ while (true) {
889
+ rand = BigInt(
890
+ `0x${import_crypto_js.default.lib.WordArray.random(32).toString(import_crypto_js.default.enc.Hex)}`
891
+ );
892
+ if (rand >= min) {
893
+ break;
894
+ }
895
+ }
896
+ const privKey = rand % 2n ** 253n;
897
+ return privKey;
898
+ };
899
+
901
900
  // src/libs/crypto/keys.ts
902
901
  var SNARK_FIELD_SIZE2 = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
903
902
  var genPrivKey = () => BigInt(`0x${import_crypto_js2.default.lib.WordArray.random(32).toString(import_crypto_js2.default.enc.Hex)}`);
@@ -5772,63 +5771,54 @@ var MACI = class {
5772
5771
  const deactivates = await this.indexer.fetchAllDeactivateLogs(contractAddress);
5773
5772
  return deactivates;
5774
5773
  }
5775
- // async addNewKey({
5776
- // signer,
5777
- // client,
5778
- // address,
5779
- // maciKeypair,
5780
- // newMaciKeypair,
5781
- // contractAddress,
5782
- // gasStation,
5783
- // fee,
5784
- // }: {
5785
- // signer: OfflineSigner;
5786
- // client: SigningCosmWasmClient;
5787
- // address?: string;
5788
- // maciKeypair: Keypair;
5789
- // newMaciKeypair: Keypair;
5790
- // contractAddress: string;
5791
- // gasStation: boolean;
5792
- // fee?: StdFee;
5793
- // }) {
5794
- // const deactivates = await this.fetchAllDeactivateLogs({
5795
- // contractAddress,
5796
- // });
5797
- // const roundInfo = await this.getRoundInfo({
5798
- // contractAddress,
5799
- // });
5800
- // const inputObj = await genAddKeyProof(4, {
5801
- // coordPubKey: [
5802
- // BigInt(roundInfo.coordinatorPubkeyX),
5803
- // BigInt(roundInfo.coordinatorPubkeyY),
5804
- // ],
5805
- // oldKey: maciKeypair,
5806
- // deactivates: deactivates.map((d: any) => d.map(BigInt)),
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;
5831
- // }
5774
+ async genAddKeyInput({
5775
+ maciKeypair,
5776
+ contractAddress
5777
+ }) {
5778
+ const deactivates = await this.fetchAllDeactivateLogs({
5779
+ contractAddress
5780
+ });
5781
+ const roundInfo = await this.getRoundInfo({
5782
+ contractAddress
5783
+ });
5784
+ const circuitPower = roundInfo.circuitPower;
5785
+ const stateTreeDepth = Number(circuitPower.split("-")[0]);
5786
+ const inputObj = genAddKeyInput(stateTreeDepth + 2, {
5787
+ coordPubKey: [
5788
+ BigInt(roundInfo.coordinatorPubkeyX),
5789
+ BigInt(roundInfo.coordinatorPubkeyY)
5790
+ ],
5791
+ oldKey: maciKeypair,
5792
+ deactivates: deactivates.map((d) => d.map(BigInt))
5793
+ });
5794
+ return inputObj;
5795
+ }
5796
+ async addNewKey({
5797
+ signer,
5798
+ contractAddress,
5799
+ d,
5800
+ proof,
5801
+ nullifier,
5802
+ newMaciKeypair,
5803
+ fee = "auto"
5804
+ }) {
5805
+ const client = await this.contract.amaciClient({
5806
+ signer,
5807
+ contractAddress
5808
+ });
5809
+ return await client.addNewKey(
5810
+ {
5811
+ d,
5812
+ groth16Proof: proof,
5813
+ nullifier: nullifier.toString(),
5814
+ pubkey: {
5815
+ x: newMaciKeypair.pubKey[0].toString(),
5816
+ y: newMaciKeypair.pubKey[1].toString()
5817
+ }
5818
+ },
5819
+ fee
5820
+ );
5821
+ }
5832
5822
  async claimAMaciRound({
5833
5823
  signer,
5834
5824
  contractAddress,
@@ -6212,6 +6202,15 @@ var MaciClient2 = class {
6212
6202
  contractAddress
6213
6203
  });
6214
6204
  }
6205
+ async getRounds(after, limit) {
6206
+ const rounds = await this.indexer.getRounds(after || "", limit || 10);
6207
+ if (isErrorResponse(rounds)) {
6208
+ throw new Error(
6209
+ `Failed to get rounds: ${rounds.code} ${rounds.error.message}`
6210
+ );
6211
+ }
6212
+ return rounds;
6213
+ }
6215
6214
  async getRoundInfo({ contractAddress }) {
6216
6215
  return await this.maci.getRoundInfo({ contractAddress });
6217
6216
  }
@@ -6319,6 +6318,34 @@ var MaciClient2 = class {
6319
6318
  fee
6320
6319
  });
6321
6320
  }
6321
+ async genAddKeyInput({
6322
+ contractAddress,
6323
+ maciKeypair
6324
+ }) {
6325
+ return await this.maci.genAddKeyInput({
6326
+ maciKeypair: maciKeypair || this.maciKeypair,
6327
+ contractAddress
6328
+ });
6329
+ }
6330
+ async addNewKey({
6331
+ signer,
6332
+ contractAddress,
6333
+ d,
6334
+ proof,
6335
+ nullifier,
6336
+ newMaciKeypair,
6337
+ fee = "auto"
6338
+ }) {
6339
+ return await this.maci.addNewKey({
6340
+ signer: this.getSigner(signer),
6341
+ contractAddress,
6342
+ d,
6343
+ proof,
6344
+ nullifier,
6345
+ newMaciKeypair,
6346
+ fee
6347
+ });
6348
+ }
6322
6349
  async claimAMaciRound({
6323
6350
  signer,
6324
6351
  contractAddress,