@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
|
@@ -61,13 +61,4 @@ export declare class G2Point {
|
|
|
61
61
|
*/
|
|
62
62
|
private checkPointsRange;
|
|
63
63
|
}
|
|
64
|
-
/**
|
|
65
|
-
* Returns a BabyJub-compatible random value. We create it by first generating
|
|
66
|
-
* a random value (initially 256 bits large) modulo the snark field size as
|
|
67
|
-
* described in EIP197. This results in a key size of roughly 253 bits and no
|
|
68
|
-
* more than 254 bits. To prevent modulo bias, we then use this efficient
|
|
69
|
-
* algorithm:
|
|
70
|
-
* http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/lib/libc/crypt/arc4random_uniform.c
|
|
71
|
-
* @returns A BabyJub-compatible random value.
|
|
72
|
-
*/
|
|
73
64
|
export declare const genRandomBabyJubValue: () => bigint;
|
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
export declare class Tree {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
readonly DEPTH: number;
|
|
3
|
+
readonly HEIGHT: number;
|
|
4
|
+
readonly DEGREE: number;
|
|
5
|
+
readonly LEAVES_COUNT: number;
|
|
6
|
+
readonly LEAVES_IDX_0: number;
|
|
7
|
+
readonly NODES_COUNT: number;
|
|
8
|
+
protected nodes: bigint[];
|
|
9
|
+
protected zeros: bigint[];
|
|
10
10
|
constructor(degree: number, depth: number, zero: bigint);
|
|
11
11
|
get root(): bigint;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
initLeaves(leaves:
|
|
12
|
+
initZero(zero: bigint): void;
|
|
13
|
+
initNodes(): void;
|
|
14
|
+
initLeaves(leaves: bigint[]): void;
|
|
15
15
|
leaf(leafIdx: number): bigint;
|
|
16
16
|
leaves(): bigint[];
|
|
17
17
|
updateLeaf(leafIdx: number, leaf: bigint): void;
|
|
18
|
-
pathIdxOf(leafIdx: number):
|
|
18
|
+
pathIdxOf(leafIdx: number): bigint[];
|
|
19
19
|
pathElementOf(leafIdx: number): bigint[][];
|
|
20
20
|
subTree(length: number): Tree;
|
|
21
|
-
|
|
21
|
+
protected _update(nodeIdx: number): void;
|
|
22
22
|
}
|
|
23
|
-
export default Tree;
|
package/dist/maci.d.ts
CHANGED
|
@@ -113,6 +113,19 @@ export declare class MaciClient {
|
|
|
113
113
|
signer?: OfflineSigner;
|
|
114
114
|
contractAddress: string;
|
|
115
115
|
}): Promise<OracleWhitelistConfig>;
|
|
116
|
+
getRounds(after?: string, limit?: number): Promise<import("./types").SuccessResponse<{
|
|
117
|
+
rounds: {
|
|
118
|
+
pageInfo: {
|
|
119
|
+
endCursor: string;
|
|
120
|
+
hasNextPage: boolean;
|
|
121
|
+
};
|
|
122
|
+
edges: {
|
|
123
|
+
cursor: string;
|
|
124
|
+
node: import("./types").RoundType;
|
|
125
|
+
}[];
|
|
126
|
+
totalCount: number;
|
|
127
|
+
};
|
|
128
|
+
}>>;
|
|
116
129
|
getRoundInfo({ contractAddress }: {
|
|
117
130
|
contractAddress: string;
|
|
118
131
|
}): Promise<import("./types").RoundType>;
|
package/package.json
CHANGED
|
@@ -108,34 +108,57 @@ export class G2Point {
|
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
/**
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
export const genRandomBabyJubValue = (): bigint => {
|
|
111
|
+
// /**
|
|
112
|
+
// * Returns a BabyJub-compatible random value. We create it by first generating
|
|
113
|
+
// * a random value (initially 256 bits large) modulo the snark field size as
|
|
114
|
+
// * described in EIP197. This results in a key size of roughly 253 bits and no
|
|
115
|
+
// * more than 254 bits. To prevent modulo bias, we then use this efficient
|
|
116
|
+
// * algorithm:
|
|
117
|
+
// * http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/lib/libc/crypt/arc4random_uniform.c
|
|
118
|
+
// * @returns A BabyJub-compatible random value.
|
|
119
|
+
// */
|
|
120
|
+
// export const genRandomBabyJubValue = (): bigint => {
|
|
121
|
+
// // Prevent modulo bias
|
|
122
|
+
// // const lim = BigInt('0x10000000000000000000000000000000000000000000000000000000000000000')
|
|
123
|
+
// // const min = (lim - SNARK_FIELD_SIZE) % SNARK_FIELD_SIZE
|
|
124
|
+
// const min = BigInt(
|
|
125
|
+
// '6350874878119819312338956282401532410528162663560392320966563075034087161851'
|
|
126
|
+
// );
|
|
127
|
+
|
|
128
|
+
// let privKey: PrivKey = SNARK_FIELD_SIZE;
|
|
129
|
+
|
|
130
|
+
// do {
|
|
131
|
+
// const rand = BigInt(
|
|
132
|
+
// `0x${CryptoJS.lib.WordArray.random(32).toString(CryptoJS.enc.Hex)}`
|
|
133
|
+
// );
|
|
134
|
+
|
|
135
|
+
// if (rand >= min) {
|
|
136
|
+
// privKey = rand % SNARK_FIELD_SIZE;
|
|
137
|
+
// }
|
|
138
|
+
// } while (privKey >= SNARK_FIELD_SIZE);
|
|
139
|
+
// privKey =
|
|
140
|
+
// return privKey;
|
|
141
|
+
// };
|
|
142
|
+
|
|
143
|
+
export const genRandomBabyJubValue = () => {
|
|
121
144
|
// Prevent modulo bias
|
|
122
|
-
//
|
|
123
|
-
//
|
|
124
|
-
const min =
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
do {
|
|
131
|
-
const rand = BigInt(
|
|
145
|
+
//const lim = BigInt('0x10000000000000000000000000000000000000000000000000000000000000000')
|
|
146
|
+
//const min = (lim - SNARK_FIELD_SIZE) % SNARK_FIELD_SIZE
|
|
147
|
+
const min =
|
|
148
|
+
6350874878119819312338956282401532410528162663560392320966563075034087161851n;
|
|
149
|
+
|
|
150
|
+
let rand;
|
|
151
|
+
while (true) {
|
|
152
|
+
rand = BigInt(
|
|
132
153
|
`0x${CryptoJS.lib.WordArray.random(32).toString(CryptoJS.enc.Hex)}`
|
|
133
154
|
);
|
|
134
155
|
|
|
135
156
|
if (rand >= min) {
|
|
136
|
-
|
|
157
|
+
break;
|
|
137
158
|
}
|
|
138
|
-
}
|
|
159
|
+
}
|
|
139
160
|
|
|
161
|
+
// const privKey = rand % SNARK_FIELD_SIZE;
|
|
162
|
+
const privKey = rand % 2n ** 253n;
|
|
140
163
|
return privKey;
|
|
141
164
|
};
|
package/src/libs/crypto/keys.ts
CHANGED
|
@@ -14,10 +14,10 @@ import { solidityPackedSha256 } from 'ethers';
|
|
|
14
14
|
import { mulPointEscalar } from '@zk-kit/baby-jubjub';
|
|
15
15
|
import { packPublicKey, unpackPublicKey } from '@zk-kit/eddsa-poseidon';
|
|
16
16
|
|
|
17
|
-
import { genRandomBabyJubValue } from './babyjub';
|
|
18
17
|
import { EcdhSharedKey, Keypair, PrivKey, PubKey } from './types';
|
|
19
18
|
import { poseidon } from './hashing';
|
|
20
|
-
import Tree from './tree';
|
|
19
|
+
import { Tree } from './tree';
|
|
20
|
+
import { genRandomBabyJubValue } from './babyjub';
|
|
21
21
|
|
|
22
22
|
const SNARK_FIELD_SIZE =
|
|
23
23
|
21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
package/src/libs/crypto/tree.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { poseidon } from './hashing';
|
|
2
2
|
|
|
3
3
|
export class Tree {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
public readonly DEPTH: number;
|
|
5
|
+
public readonly HEIGHT: number;
|
|
6
|
+
public readonly DEGREE: number;
|
|
7
|
+
public readonly LEAVES_COUNT: number;
|
|
8
|
+
public readonly LEAVES_IDX_0: number;
|
|
9
|
+
public readonly NODES_COUNT: number;
|
|
10
|
+
|
|
11
|
+
protected nodes: bigint[] = [];
|
|
12
|
+
protected zeros: bigint[] = [];
|
|
12
13
|
|
|
13
14
|
constructor(degree: number, depth: number, zero: bigint) {
|
|
14
15
|
this.DEPTH = depth;
|
|
@@ -23,34 +24,38 @@ export class Tree {
|
|
|
23
24
|
this.initNodes();
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
get root()
|
|
27
|
+
get root() {
|
|
27
28
|
return this.nodes[0];
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
for (let i = 1; i <
|
|
34
|
-
|
|
31
|
+
initZero(zero: bigint) {
|
|
32
|
+
const zeros = new Array(this.HEIGHT);
|
|
33
|
+
zeros[0] = zero;
|
|
34
|
+
for (let i = 1; i < zeros.length; i++) {
|
|
35
|
+
const children = new Array(this.DEGREE).fill(zeros[i - 1]);
|
|
36
|
+
zeros[i] = poseidon(children);
|
|
35
37
|
}
|
|
38
|
+
this.zeros = zeros;
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
|
|
41
|
+
initNodes() {
|
|
39
42
|
const DEGREE = this.DEGREE;
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
const nodes = new Array(this.NODES_COUNT);
|
|
42
45
|
|
|
43
46
|
for (let d = this.DEPTH; d >= 0; d--) {
|
|
44
47
|
const size = DEGREE ** d;
|
|
45
48
|
const idx0 = (DEGREE ** d - 1) / (DEGREE - 1);
|
|
46
49
|
const zero = this.zeros[this.DEPTH - d];
|
|
47
50
|
for (let i = 0; i < size; i++) {
|
|
48
|
-
|
|
51
|
+
nodes[idx0 + i] = zero;
|
|
49
52
|
}
|
|
50
53
|
}
|
|
54
|
+
|
|
55
|
+
this.nodes = nodes;
|
|
51
56
|
}
|
|
52
57
|
|
|
53
|
-
initLeaves(leaves:
|
|
58
|
+
initLeaves(leaves: bigint[]) {
|
|
54
59
|
const DEGREE = this.DEGREE;
|
|
55
60
|
for (let i = 0; i < leaves.length; i++) {
|
|
56
61
|
if (i >= this.LEAVES_COUNT) {
|
|
@@ -71,7 +76,7 @@ export class Tree {
|
|
|
71
76
|
}
|
|
72
77
|
}
|
|
73
78
|
|
|
74
|
-
leaf(leafIdx: number)
|
|
79
|
+
leaf(leafIdx: number) {
|
|
75
80
|
if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
|
|
76
81
|
throw new Error('wrong leaf index');
|
|
77
82
|
}
|
|
@@ -79,11 +84,11 @@ export class Tree {
|
|
|
79
84
|
return this.nodes[nodeIdx];
|
|
80
85
|
}
|
|
81
86
|
|
|
82
|
-
leaves()
|
|
87
|
+
leaves() {
|
|
83
88
|
return this.nodes.slice(this.LEAVES_IDX_0);
|
|
84
89
|
}
|
|
85
90
|
|
|
86
|
-
updateLeaf(leafIdx: number, leaf: bigint)
|
|
91
|
+
updateLeaf(leafIdx: number, leaf: bigint) {
|
|
87
92
|
if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
|
|
88
93
|
throw new Error('wrong leaf index');
|
|
89
94
|
}
|
|
@@ -93,18 +98,18 @@ export class Tree {
|
|
|
93
98
|
this._update(nodeIdx);
|
|
94
99
|
}
|
|
95
100
|
|
|
96
|
-
pathIdxOf(leafIdx: number)
|
|
101
|
+
pathIdxOf(leafIdx: number) {
|
|
97
102
|
if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
|
|
98
103
|
throw new Error('wrong leaf index');
|
|
99
104
|
}
|
|
100
105
|
let idx = this.LEAVES_IDX_0 + leafIdx;
|
|
101
|
-
const pathIdx:
|
|
106
|
+
const pathIdx: bigint[] = [];
|
|
102
107
|
|
|
103
108
|
for (let i = 0; i < this.DEPTH; i++) {
|
|
104
109
|
const parentIdx = Math.floor((idx - 1) / this.DEGREE);
|
|
105
110
|
const childrenIdx0 = parentIdx * this.DEGREE + 1;
|
|
106
111
|
|
|
107
|
-
pathIdx.push(idx - childrenIdx0);
|
|
112
|
+
pathIdx.push(BigInt(idx - childrenIdx0));
|
|
108
113
|
|
|
109
114
|
idx = parentIdx;
|
|
110
115
|
}
|
|
@@ -112,7 +117,7 @@ export class Tree {
|
|
|
112
117
|
return pathIdx;
|
|
113
118
|
}
|
|
114
119
|
|
|
115
|
-
pathElementOf(leafIdx: number)
|
|
120
|
+
pathElementOf(leafIdx: number) {
|
|
116
121
|
if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
|
|
117
122
|
throw new Error('wrong leaf index');
|
|
118
123
|
}
|
|
@@ -137,7 +142,7 @@ export class Tree {
|
|
|
137
142
|
return pathElement;
|
|
138
143
|
}
|
|
139
144
|
|
|
140
|
-
subTree(length: number)
|
|
145
|
+
subTree(length: number) {
|
|
141
146
|
const subTree = new Tree(this.DEGREE, this.DEPTH, this.zeros[0]);
|
|
142
147
|
const nodes = [...this.nodes];
|
|
143
148
|
|
|
@@ -159,18 +164,16 @@ export class Tree {
|
|
|
159
164
|
return subTree;
|
|
160
165
|
}
|
|
161
166
|
|
|
162
|
-
|
|
167
|
+
protected _update(nodeIdx: number) {
|
|
163
168
|
let idx = nodeIdx;
|
|
164
169
|
while (idx > 0) {
|
|
165
170
|
const parentIdx = Math.floor((idx - 1) / this.DEGREE);
|
|
166
171
|
const childrenIdx0 = parentIdx * this.DEGREE + 1;
|
|
167
|
-
this.nodes[parentIdx] =
|
|
168
|
-
this.nodes.slice(childrenIdx0, childrenIdx0 +
|
|
172
|
+
this.nodes[parentIdx] = poseidon(
|
|
173
|
+
this.nodes.slice(childrenIdx0, childrenIdx0 + 5)
|
|
169
174
|
);
|
|
170
175
|
|
|
171
176
|
idx = parentIdx;
|
|
172
177
|
}
|
|
173
178
|
}
|
|
174
179
|
}
|
|
175
|
-
|
|
176
|
-
export default Tree;
|
package/src/maci.ts
CHANGED
|
@@ -19,6 +19,7 @@ import { OracleWhitelistConfig } from './libs/contract/ts/OracleMaci.types';
|
|
|
19
19
|
import { SignatureResponse } from './libs/oracle-certificate/types';
|
|
20
20
|
import { StdFee } from '@cosmjs/amino';
|
|
21
21
|
import { Groth16ProofType } from './libs/contract/ts/Maci.types';
|
|
22
|
+
import { isErrorResponse } from './libs/maci/maci';
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* @class MaciClient
|
|
@@ -365,6 +366,18 @@ export class MaciClient {
|
|
|
365
366
|
});
|
|
366
367
|
}
|
|
367
368
|
|
|
369
|
+
async getRounds(after?: string, limit?: number) {
|
|
370
|
+
const rounds = await this.indexer.getRounds(after || '', limit || 10);
|
|
371
|
+
|
|
372
|
+
if (isErrorResponse(rounds)) {
|
|
373
|
+
throw new Error(
|
|
374
|
+
`Failed to get rounds: ${rounds.code} ${rounds.error.message}`
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return rounds;
|
|
379
|
+
}
|
|
380
|
+
|
|
368
381
|
async getRoundInfo({ contractAddress }: { contractAddress: string }) {
|
|
369
382
|
return await this.maci.getRoundInfo({ contractAddress });
|
|
370
383
|
}
|