@dorafactory/maci-sdk 0.0.51 → 0.0.53
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 +125 -113
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +125 -113
- package/dist/index.mjs.map +1 -1
- package/dist/libs/crypto/babyjub.d.ts +0 -9
- package/dist/libs/crypto/tree.d.ts +13 -14
- package/dist/maci.d.ts +13 -0
- package/package.json +1 -1
- package/src/libs/crypto/babyjub.ts +45 -22
- package/src/libs/crypto/keys.ts +2 -2
- package/src/libs/crypto/tree.ts +34 -31
- package/src/maci.ts +13 -0
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/
|
|
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,
|
|
608
|
+
(0, import_assert2.default)(inputs.length === 2);
|
|
701
609
|
return poseidon(inputs);
|
|
702
610
|
};
|
|
703
611
|
var poseidonT4 = (inputs) => {
|
|
704
|
-
(0,
|
|
612
|
+
(0, import_assert2.default)(inputs.length === 3);
|
|
705
613
|
return poseidon(inputs);
|
|
706
614
|
};
|
|
707
615
|
var poseidonT5 = (inputs) => {
|
|
708
|
-
(0,
|
|
616
|
+
(0, import_assert2.default)(inputs.length === 4);
|
|
709
617
|
return poseidon(inputs);
|
|
710
618
|
};
|
|
711
619
|
var poseidonT6 = (inputs) => {
|
|
712
|
-
(0,
|
|
620
|
+
(0, import_assert2.default)(inputs.length === 5);
|
|
713
621
|
return poseidon(inputs);
|
|
714
622
|
};
|
|
715
623
|
var hashLeftRight = (left, right) => poseidonT3([left, right]);
|
|
@@ -765,6 +673,8 @@ var hashOne = (preImage) => poseidonT3([preImage, BigInt(0)]);
|
|
|
765
673
|
// src/libs/crypto/tree.ts
|
|
766
674
|
var Tree = class _Tree {
|
|
767
675
|
constructor(degree, depth, zero) {
|
|
676
|
+
this.nodes = [];
|
|
677
|
+
this.zeros = [];
|
|
768
678
|
this.DEPTH = depth;
|
|
769
679
|
this.HEIGHT = depth + 1;
|
|
770
680
|
this.DEGREE = degree;
|
|
@@ -777,24 +687,27 @@ var Tree = class _Tree {
|
|
|
777
687
|
get root() {
|
|
778
688
|
return this.nodes[0];
|
|
779
689
|
}
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
for (let i = 1; i <
|
|
784
|
-
|
|
690
|
+
initZero(zero) {
|
|
691
|
+
const zeros = new Array(this.HEIGHT);
|
|
692
|
+
zeros[0] = zero;
|
|
693
|
+
for (let i = 1; i < zeros.length; i++) {
|
|
694
|
+
const children = new Array(this.DEGREE).fill(zeros[i - 1]);
|
|
695
|
+
zeros[i] = poseidon(children);
|
|
785
696
|
}
|
|
697
|
+
this.zeros = zeros;
|
|
786
698
|
}
|
|
787
|
-
|
|
699
|
+
initNodes() {
|
|
788
700
|
const DEGREE = this.DEGREE;
|
|
789
|
-
|
|
701
|
+
const nodes = new Array(this.NODES_COUNT);
|
|
790
702
|
for (let d = this.DEPTH; d >= 0; d--) {
|
|
791
703
|
const size = DEGREE ** d;
|
|
792
704
|
const idx0 = (DEGREE ** d - 1) / (DEGREE - 1);
|
|
793
705
|
const zero = this.zeros[this.DEPTH - d];
|
|
794
706
|
for (let i = 0; i < size; i++) {
|
|
795
|
-
|
|
707
|
+
nodes[idx0 + i] = zero;
|
|
796
708
|
}
|
|
797
709
|
}
|
|
710
|
+
this.nodes = nodes;
|
|
798
711
|
}
|
|
799
712
|
initLeaves(leaves) {
|
|
800
713
|
const DEGREE = this.DEGREE;
|
|
@@ -842,7 +755,7 @@ var Tree = class _Tree {
|
|
|
842
755
|
for (let i = 0; i < this.DEPTH; i++) {
|
|
843
756
|
const parentIdx = Math.floor((idx - 1) / this.DEGREE);
|
|
844
757
|
const childrenIdx0 = parentIdx * this.DEGREE + 1;
|
|
845
|
-
pathIdx.push(idx - childrenIdx0);
|
|
758
|
+
pathIdx.push(BigInt(idx - childrenIdx0));
|
|
846
759
|
idx = parentIdx;
|
|
847
760
|
}
|
|
848
761
|
return pathIdx;
|
|
@@ -884,19 +797,109 @@ var Tree = class _Tree {
|
|
|
884
797
|
subTree._update(this.LEAVES_IDX_0 + length - 1);
|
|
885
798
|
return subTree;
|
|
886
799
|
}
|
|
887
|
-
|
|
800
|
+
_update(nodeIdx) {
|
|
888
801
|
let idx = nodeIdx;
|
|
889
802
|
while (idx > 0) {
|
|
890
803
|
const parentIdx = Math.floor((idx - 1) / this.DEGREE);
|
|
891
804
|
const childrenIdx0 = parentIdx * this.DEGREE + 1;
|
|
892
|
-
this.nodes[parentIdx] =
|
|
893
|
-
this.nodes.slice(childrenIdx0, childrenIdx0 +
|
|
805
|
+
this.nodes[parentIdx] = poseidon(
|
|
806
|
+
this.nodes.slice(childrenIdx0, childrenIdx0 + 5)
|
|
894
807
|
);
|
|
895
808
|
idx = parentIdx;
|
|
896
809
|
}
|
|
897
810
|
}
|
|
898
811
|
};
|
|
899
|
-
|
|
812
|
+
|
|
813
|
+
// src/libs/crypto/babyjub.ts
|
|
814
|
+
var import_assert3 = __toESM(require("assert"));
|
|
815
|
+
var import_crypto_js = __toESM(require("crypto-js"));
|
|
816
|
+
var G1Point = class {
|
|
817
|
+
/**
|
|
818
|
+
* Create a new instance of G1Point
|
|
819
|
+
* @param x the x coordinate
|
|
820
|
+
* @param y the y coordinate
|
|
821
|
+
*/
|
|
822
|
+
constructor(x, y) {
|
|
823
|
+
(0, import_assert3.default)(x < SNARK_FIELD_SIZE && x >= 0, "G1Point x out of range");
|
|
824
|
+
(0, import_assert3.default)(y < SNARK_FIELD_SIZE && y >= 0, "G1Point y out of range");
|
|
825
|
+
this.x = x;
|
|
826
|
+
this.y = y;
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* Check whether two points are equal
|
|
830
|
+
* @param pt the point to compare with
|
|
831
|
+
* @returns whether they are equal or not
|
|
832
|
+
*/
|
|
833
|
+
equals(pt) {
|
|
834
|
+
return this.x === pt.x && this.y === pt.y;
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* Return the point as a contract param in the form of an object
|
|
838
|
+
* @returns the point as a contract param
|
|
839
|
+
*/
|
|
840
|
+
asContractParam() {
|
|
841
|
+
return {
|
|
842
|
+
x: this.x.toString(),
|
|
843
|
+
y: this.y.toString()
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
var G2Point = class {
|
|
848
|
+
/**
|
|
849
|
+
* Create a new instance of G2Point
|
|
850
|
+
* @param x the x coordinate
|
|
851
|
+
* @param y the y coordinate
|
|
852
|
+
*/
|
|
853
|
+
constructor(x, y) {
|
|
854
|
+
this.checkPointsRange(x, "x");
|
|
855
|
+
this.checkPointsRange(y, "y");
|
|
856
|
+
this.x = x;
|
|
857
|
+
this.y = y;
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Check whether two points are equal
|
|
861
|
+
* @param pt the point to compare with
|
|
862
|
+
* @returns whether they are equal or not
|
|
863
|
+
*/
|
|
864
|
+
equals(pt) {
|
|
865
|
+
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];
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* Return the point as a contract param in the form of an object
|
|
869
|
+
* @returns the point as a contract param
|
|
870
|
+
*/
|
|
871
|
+
asContractParam() {
|
|
872
|
+
return {
|
|
873
|
+
x: this.x.map((n) => n.toString()),
|
|
874
|
+
y: this.y.map((n) => n.toString())
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Check whether the points are in range
|
|
879
|
+
* @param x the x coordinate
|
|
880
|
+
* @param type the type of the coordinate
|
|
881
|
+
*/
|
|
882
|
+
checkPointsRange(x, type) {
|
|
883
|
+
(0, import_assert3.default)(
|
|
884
|
+
x.every((n) => n < SNARK_FIELD_SIZE && n >= 0),
|
|
885
|
+
`G2Point ${type} out of range`
|
|
886
|
+
);
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
var genRandomBabyJubValue = () => {
|
|
890
|
+
const min = 6350874878119819312338956282401532410528162663560392320966563075034087161851n;
|
|
891
|
+
let rand;
|
|
892
|
+
while (true) {
|
|
893
|
+
rand = BigInt(
|
|
894
|
+
`0x${import_crypto_js.default.lib.WordArray.random(32).toString(import_crypto_js.default.enc.Hex)}`
|
|
895
|
+
);
|
|
896
|
+
if (rand >= min) {
|
|
897
|
+
break;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
const privKey = rand % 2n ** 253n;
|
|
901
|
+
return privKey;
|
|
902
|
+
};
|
|
900
903
|
|
|
901
904
|
// src/libs/crypto/keys.ts
|
|
902
905
|
var SNARK_FIELD_SIZE2 = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
@@ -1014,7 +1017,7 @@ var genAddKeyInput = (depth, {
|
|
|
1014
1017
|
BigInt(oldKey.formatedPrivKey),
|
|
1015
1018
|
1444992409218394441042n
|
|
1016
1019
|
]);
|
|
1017
|
-
const tree = new
|
|
1020
|
+
const tree = new Tree(5, depth, 0n);
|
|
1018
1021
|
const leaves = deactivates.map((d) => poseidon(d));
|
|
1019
1022
|
tree.initLeaves(leaves);
|
|
1020
1023
|
const deactivateRoot = tree.root;
|
|
@@ -6203,6 +6206,15 @@ var MaciClient2 = class {
|
|
|
6203
6206
|
contractAddress
|
|
6204
6207
|
});
|
|
6205
6208
|
}
|
|
6209
|
+
async getRounds(after, limit) {
|
|
6210
|
+
const rounds = await this.indexer.getRounds(after || "", limit || 10);
|
|
6211
|
+
if (isErrorResponse(rounds)) {
|
|
6212
|
+
throw new Error(
|
|
6213
|
+
`Failed to get rounds: ${rounds.code} ${rounds.error.message}`
|
|
6214
|
+
);
|
|
6215
|
+
}
|
|
6216
|
+
return rounds;
|
|
6217
|
+
}
|
|
6206
6218
|
async getRoundInfo({ contractAddress }) {
|
|
6207
6219
|
return await this.maci.getRoundInfo({ contractAddress });
|
|
6208
6220
|
}
|