@crisp-e3/sdk 0.2.3-test → 0.4.0
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/LICENSE.md +92 -123
- package/README.md +68 -7
- package/dist/index.d.ts +49 -226
- package/dist/index.js +137 -1057
- package/dist/index.js.map +1 -1
- package/package.json +7 -10
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { LeanIMTMerkleProof, LeanIMT } from '@zk-kit/lean-imt';
|
|
2
2
|
import { ProofData } from '@aztec/bb.js';
|
|
3
|
+
import { Hex } from 'viem';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Get the merkle tree data from the CRISP server
|
|
@@ -26,9 +27,9 @@ declare const getBalanceAt: (voterAddress: string, tokenAddress: string, snapsho
|
|
|
26
27
|
declare const getTotalSupplyAt: (tokenAddress: string, snapshotBlock: number, chainId: number) => Promise<bigint>;
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
|
-
*
|
|
30
|
+
* Type representing the details of a specific round returned by the CRISP server
|
|
30
31
|
*/
|
|
31
|
-
|
|
32
|
+
type RoundDetailsResponse = {
|
|
32
33
|
id: string;
|
|
33
34
|
chain_id: string;
|
|
34
35
|
enclave_address: string;
|
|
@@ -42,11 +43,11 @@ interface IRoundDetailsResponse {
|
|
|
42
43
|
emojis: [string, string];
|
|
43
44
|
token_address: string;
|
|
44
45
|
balance_threshold: string;
|
|
45
|
-
}
|
|
46
|
+
};
|
|
46
47
|
/**
|
|
47
|
-
*
|
|
48
|
+
* Type representing the details of a specific round in a more convenient format
|
|
48
49
|
*/
|
|
49
|
-
|
|
50
|
+
type RoundDetails = {
|
|
50
51
|
e3Id: bigint;
|
|
51
52
|
chainId: bigint;
|
|
52
53
|
enclaveAddress: string;
|
|
@@ -60,39 +61,29 @@ interface IRoundDetails {
|
|
|
60
61
|
emojis: [string, string];
|
|
61
62
|
tokenAddress: string;
|
|
62
63
|
balanceThreshold: bigint;
|
|
63
|
-
}
|
|
64
|
+
};
|
|
64
65
|
/**
|
|
65
|
-
*
|
|
66
|
+
* Type representing the token details required for participation in a round
|
|
66
67
|
*/
|
|
67
|
-
|
|
68
|
+
type TokenDetails = {
|
|
68
69
|
tokenAddress: string;
|
|
69
70
|
threshold: bigint;
|
|
70
71
|
snapshotBlock: bigint;
|
|
71
|
-
}
|
|
72
|
+
};
|
|
72
73
|
/**
|
|
73
|
-
*
|
|
74
|
+
* Type representing a Merkle proof
|
|
74
75
|
*/
|
|
75
|
-
|
|
76
|
+
type MerkleProof = {
|
|
76
77
|
leaf: bigint;
|
|
77
78
|
index: number;
|
|
78
79
|
proof: LeanIMTMerkleProof<bigint>;
|
|
79
80
|
length: number;
|
|
80
81
|
indices: number[];
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Enum representing the voting modes
|
|
84
|
-
*/
|
|
85
|
-
declare enum VotingMode {
|
|
86
|
-
/**
|
|
87
|
-
* Governance voting requires to spend all credits on one option
|
|
88
|
-
they cannot be split
|
|
89
|
-
*/
|
|
90
|
-
GOVERNANCE = "GOVERNANCE"
|
|
91
|
-
}
|
|
82
|
+
};
|
|
92
83
|
/**
|
|
93
|
-
*
|
|
84
|
+
* Type representing a vote with power for 'yes' and 'no'
|
|
94
85
|
*/
|
|
95
|
-
|
|
86
|
+
type Vote = {
|
|
96
87
|
/**
|
|
97
88
|
* The voting power for 'yes' votes
|
|
98
89
|
*/
|
|
@@ -101,144 +92,45 @@ interface IVote {
|
|
|
101
92
|
* The voting power for 'no' votes
|
|
102
93
|
*/
|
|
103
94
|
no: bigint;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
interface Polynomial {
|
|
109
|
-
coefficients: string[];
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Interface representing cryptographic parameters
|
|
113
|
-
*/
|
|
114
|
-
interface GrecoCryptographicParams {
|
|
115
|
-
q_mod_t: string;
|
|
116
|
-
qis: string[];
|
|
117
|
-
k0is: string[];
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Interface representing Greco bounds
|
|
121
|
-
*/
|
|
122
|
-
interface GrecoBoundParams {
|
|
123
|
-
e_bound: string;
|
|
124
|
-
u_bound: string;
|
|
125
|
-
k1_low_bound: string;
|
|
126
|
-
k1_up_bound: string;
|
|
127
|
-
p1_bounds: string[];
|
|
128
|
-
p2_bounds: string[];
|
|
129
|
-
pk_bounds: string[];
|
|
130
|
-
r1_low_bounds: string[];
|
|
131
|
-
r1_up_bounds: string[];
|
|
132
|
-
r2_bounds: string[];
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Interface representing Greco parameters
|
|
136
|
-
*/
|
|
137
|
-
interface GrecoParams {
|
|
138
|
-
crypto: GrecoCryptographicParams;
|
|
139
|
-
bounds: GrecoBoundParams;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* The inputs required for the CRISP circuit
|
|
143
|
-
*/
|
|
144
|
-
interface CRISPCircuitInputs {
|
|
145
|
-
prev_ct0is: Polynomial[];
|
|
146
|
-
prev_ct1is: Polynomial[];
|
|
147
|
-
sum_ct0is: Polynomial[];
|
|
148
|
-
sum_ct1is: Polynomial[];
|
|
149
|
-
sum_r0is: Polynomial[];
|
|
150
|
-
sum_r1is: Polynomial[];
|
|
151
|
-
params: GrecoParams;
|
|
152
|
-
ct0is: Polynomial[];
|
|
153
|
-
ct1is: Polynomial[];
|
|
154
|
-
pk0is: Polynomial[];
|
|
155
|
-
pk1is: Polynomial[];
|
|
156
|
-
r1is: Polynomial[];
|
|
157
|
-
r2is: Polynomial[];
|
|
158
|
-
p1is: Polynomial[];
|
|
159
|
-
p2is: Polynomial[];
|
|
160
|
-
u: Polynomial;
|
|
161
|
-
e0: Polynomial;
|
|
162
|
-
e1: Polynomial;
|
|
163
|
-
e0is: Polynomial[];
|
|
164
|
-
k1: Polynomial;
|
|
165
|
-
public_key_x: string[];
|
|
166
|
-
public_key_y: string[];
|
|
167
|
-
signature: string[];
|
|
168
|
-
hashed_message: string[];
|
|
169
|
-
merkle_root: string;
|
|
170
|
-
merkle_proof_length: string;
|
|
171
|
-
merkle_proof_indices: string[];
|
|
172
|
-
merkle_proof_siblings: string[];
|
|
173
|
-
slot_address: string;
|
|
174
|
-
balance: string;
|
|
175
|
-
is_first_vote: boolean;
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Interface representing the inputs for Noir signature verification
|
|
179
|
-
*/
|
|
180
|
-
interface NoirSignatureInputs {
|
|
181
|
-
/**
|
|
182
|
-
* X coordinate of the public key
|
|
183
|
-
*/
|
|
184
|
-
pub_key_x: Uint8Array;
|
|
185
|
-
/**
|
|
186
|
-
* Y coordinate of the public key
|
|
187
|
-
*/
|
|
188
|
-
pub_key_y: Uint8Array;
|
|
189
|
-
/**
|
|
190
|
-
* The signature to verify
|
|
191
|
-
*/
|
|
192
|
-
signature: Uint8Array;
|
|
193
|
-
/**
|
|
194
|
-
* The hashed message that was signed
|
|
195
|
-
*/
|
|
196
|
-
hashed_message: Uint8Array;
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Parameters for encryptVoteAndGenerateCRISPInputs function
|
|
200
|
-
*/
|
|
201
|
-
interface EncryptVoteAndGenerateCRISPInputsParams {
|
|
202
|
-
encodedVote: string[];
|
|
95
|
+
};
|
|
96
|
+
type MaskVoteProofInputs = {
|
|
97
|
+
previousCiphertext?: Uint8Array;
|
|
98
|
+
merkleLeaves: string[] | bigint[];
|
|
203
99
|
publicKey: Uint8Array;
|
|
204
|
-
previousCiphertext: Uint8Array;
|
|
205
|
-
signature: `0x${string}`;
|
|
206
|
-
message: string;
|
|
207
|
-
merkleData: IMerkleProof;
|
|
208
100
|
balance: bigint;
|
|
209
101
|
slotAddress: string;
|
|
210
|
-
|
|
211
|
-
|
|
102
|
+
};
|
|
103
|
+
type VoteProofInputs = {
|
|
104
|
+
merkleLeaves: string[] | bigint[];
|
|
105
|
+
publicKey: Uint8Array;
|
|
106
|
+
balance: bigint;
|
|
107
|
+
vote: Vote;
|
|
108
|
+
signature: `0x${string}`;
|
|
109
|
+
};
|
|
212
110
|
|
|
213
111
|
/**
|
|
214
112
|
* Get the details of a specific round
|
|
215
113
|
*/
|
|
216
|
-
declare const getRoundDetails: (serverUrl: string, e3Id: number) => Promise<
|
|
114
|
+
declare const getRoundDetails: (serverUrl: string, e3Id: number) => Promise<RoundDetails>;
|
|
217
115
|
/**
|
|
218
116
|
* Get the token address, balance threshold and snapshot block for a specific round
|
|
219
117
|
* @param serverUrl - The base URL of the CRISP server
|
|
220
118
|
* @param e3Id - The e3Id of the round
|
|
221
119
|
* @returns The token address, balance threshold and snapshot block
|
|
222
120
|
*/
|
|
223
|
-
declare const getRoundTokenDetails: (serverUrl: string, e3Id: number) => Promise<
|
|
121
|
+
declare const getRoundTokenDetails: (serverUrl: string, e3Id: number) => Promise<TokenDetails>;
|
|
224
122
|
|
|
225
|
-
declare const CRISP_SERVER_TOKEN_TREE_ENDPOINT = "state/token-holders";
|
|
226
|
-
declare const CRISP_SERVER_STATE_LITE_ENDPOINT = "state/lite";
|
|
227
123
|
declare const MERKLE_TREE_MAX_DEPTH = 20;
|
|
228
|
-
/**
|
|
229
|
-
* Half the minimum degree needed to support the maxium vote value
|
|
230
|
-
* If you change MAXIMUM_VOTE_VALUE, make sure to update this value too.
|
|
231
|
-
*/
|
|
232
|
-
declare const HALF_LARGEST_MINIMUM_DEGREE = 28;
|
|
233
124
|
/**
|
|
234
125
|
* This is the maximum value for a vote (Yes or No). This is 2^28 - 1
|
|
235
126
|
* The minimum degree that BFV should use is 56 (to accommodate both Yes and No votes)
|
|
236
127
|
*/
|
|
237
128
|
declare const MAXIMUM_VOTE_VALUE: bigint;
|
|
238
129
|
/**
|
|
239
|
-
*
|
|
130
|
+
* Message used by users to prove ownership of their Ethereum account
|
|
131
|
+
* This message is signed by the user's private key to authenticate their identity
|
|
240
132
|
*/
|
|
241
|
-
declare const
|
|
133
|
+
declare const SIGNATURE_MESSAGE = "CRISP: Sign this message to prove ownership of your Ethereum account";
|
|
242
134
|
|
|
243
135
|
/**
|
|
244
136
|
* Hash a leaf node for the Merkle tree
|
|
@@ -246,7 +138,7 @@ declare const MESSAGE = "Vote for round 0";
|
|
|
246
138
|
* @param balance The voter's balance
|
|
247
139
|
* @returns The hashed leaf as a bigint
|
|
248
140
|
*/
|
|
249
|
-
declare const hashLeaf: (address: string, balance:
|
|
141
|
+
declare const hashLeaf: (address: string, balance: bigint) => bigint;
|
|
250
142
|
/**
|
|
251
143
|
* Generate a new LeanIMT with the leaves provided
|
|
252
144
|
* @param leaves The leaves of the Merkle tree
|
|
@@ -255,99 +147,30 @@ declare const hashLeaf: (address: string, balance: string) => bigint;
|
|
|
255
147
|
declare const generateMerkleTree: (leaves: bigint[]) => LeanIMT;
|
|
256
148
|
/**
|
|
257
149
|
* Generate a Merkle proof for a given address to prove inclusion in the voters' list
|
|
258
|
-
* @param threshold The minimum balance required to be eligible
|
|
259
150
|
* @param balance The voter's balance
|
|
260
151
|
* @param address The voter's address
|
|
261
152
|
* @param leaves The leaves of the Merkle tree
|
|
262
153
|
*/
|
|
263
|
-
declare const generateMerkleProof: (
|
|
264
|
-
|
|
265
|
-
* Convert a number to its binary representation
|
|
266
|
-
* @param number The number to convert to binary
|
|
267
|
-
* @returns The binary representation of the number as a string
|
|
268
|
-
*/
|
|
269
|
-
declare const toBinary: (number: bigint) => string;
|
|
154
|
+
declare const generateMerkleProof: (balance: bigint, address: string, leaves: bigint[] | string[]) => MerkleProof;
|
|
155
|
+
declare const getAddressFromSignature: (signature: `0x${string}`) => Promise<string>;
|
|
270
156
|
|
|
271
157
|
/**
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
* @
|
|
275
|
-
* @dev Also, we will need to check in the circuit that anything within these indices is
|
|
276
|
-
* either 0 or 1.
|
|
277
|
-
* @param totalVotingPower The maximum vote amount (if a single voter had all of the power)
|
|
278
|
-
* @param degree The degree of the polynomial
|
|
158
|
+
* Decode an encoded tally into its decimal representation.
|
|
159
|
+
* @param tally The encoded tally to decode.
|
|
160
|
+
* @returns The decoded tally as an IVote.
|
|
279
161
|
*/
|
|
280
|
-
declare const
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
* Encode a vote based on the voting mode
|
|
286
|
-
* @param vote The vote to encode
|
|
287
|
-
* @param votingMode The voting mode to use for encoding
|
|
288
|
-
* @param votingPower The voting power of the voter
|
|
289
|
-
* @param bfvParams The BFV parameters to use for encoding
|
|
290
|
-
* @returns The encoded vote as a string
|
|
291
|
-
*/
|
|
292
|
-
declare const encodeVote: (vote: IVote, votingMode: VotingMode, votingPower: bigint) => string[];
|
|
293
|
-
/**
|
|
294
|
-
* Given an encoded tally, decode it into its decimal representation
|
|
295
|
-
* @param tally The encoded tally to decode
|
|
296
|
-
* @param votingMode The voting mode
|
|
297
|
-
*/
|
|
298
|
-
declare const decodeTally: (tally: string[], votingMode: VotingMode) => IVote;
|
|
299
|
-
/**
|
|
300
|
-
* Validate whether a vote is valid for a given voting mode
|
|
301
|
-
* @param votingMode The voting mode to validate against
|
|
302
|
-
* @param vote The vote to validate
|
|
303
|
-
* @param votingPower The voting power of the voter
|
|
304
|
-
*/
|
|
305
|
-
declare const validateVote: (votingMode: VotingMode, vote: IVote, votingPower: bigint) => void;
|
|
306
|
-
declare const encryptVote: (encodedVote: string[], publicKey: Uint8Array) => Promise<Uint8Array>;
|
|
307
|
-
declare const generatePublicKey: () => Promise<Uint8Array>;
|
|
308
|
-
/**
|
|
309
|
-
* This is a wrapper around enclave-e3/sdk encryption functions as CRISP circuit will require some more
|
|
310
|
-
* input values which generic Greco do not need.
|
|
311
|
-
* @param encodedVote The encoded vote as string array
|
|
312
|
-
* @param publicKey The public key to use for encryption
|
|
313
|
-
* @param previousCiphertext The previous ciphertext to use for addition operation
|
|
314
|
-
* @param bfvParams The BFV parameters to use for encryption
|
|
315
|
-
* @param merkleData The merkle proof data
|
|
316
|
-
* @param message The message that was signed
|
|
317
|
-
* @param signature The signature of the message
|
|
318
|
-
* @param balance The voter's balance
|
|
319
|
-
* @param slotAddress The voter's slot address
|
|
320
|
-
* @param isFirstVote Whether this is the first vote for this slot
|
|
321
|
-
* @returns The CRISP circuit inputs
|
|
322
|
-
*/
|
|
323
|
-
declare const encryptVoteAndGenerateCRISPInputs: ({ encodedVote, publicKey, previousCiphertext, merkleData, message, signature, balance, slotAddress, isFirstVote, }: EncryptVoteAndGenerateCRISPInputsParams) => Promise<CRISPCircuitInputs>;
|
|
324
|
-
/**
|
|
325
|
-
* A function to generate the data required to mask a vote
|
|
326
|
-
* @param voter The voter's address
|
|
327
|
-
* @param publicKey The voter's public key
|
|
328
|
-
* @param previousCiphertext The previous ciphertext
|
|
329
|
-
* @param merkleRoot The merkle root of the census tree
|
|
330
|
-
* @param slotAddress The voter's slot address
|
|
331
|
-
* @param isFirstVote Whether this is the first vote for this slot
|
|
332
|
-
* @returns The CRISP circuit inputs for a mask vote
|
|
333
|
-
*/
|
|
334
|
-
declare const generateMaskVote: (publicKey: Uint8Array, previousCiphertext: Uint8Array, merkleRoot: bigint, slotAddress: string, isFirstVote: boolean) => Promise<CRISPCircuitInputs>;
|
|
335
|
-
declare const generateProof: (crispInputs: CRISPCircuitInputs) => Promise<ProofData>;
|
|
336
|
-
declare const generateProofWithReturnValue: (crispInputs: CRISPCircuitInputs) => Promise<{
|
|
337
|
-
returnValue: unknown;
|
|
338
|
-
proof: ProofData;
|
|
339
|
-
}>;
|
|
340
|
-
declare const getCircuitOutputValue: (crispInputs: CRISPCircuitInputs) => Promise<{
|
|
341
|
-
returnValue: unknown;
|
|
342
|
-
}>;
|
|
162
|
+
declare const decodeTally: (tally: string[]) => Vote;
|
|
163
|
+
declare const encryptVote: (vote: Vote, publicKey: Uint8Array) => Uint8Array;
|
|
164
|
+
declare const generatePublicKey: () => Uint8Array;
|
|
165
|
+
declare const generateVoteProof: (voteProofInputs: VoteProofInputs) => Promise<ProofData>;
|
|
166
|
+
declare const generateMaskVoteProof: (maskVoteProofInputs: MaskVoteProofInputs) => Promise<ProofData>;
|
|
343
167
|
declare const verifyProof: (proof: ProofData) => Promise<boolean>;
|
|
344
|
-
|
|
345
168
|
/**
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
* @param
|
|
349
|
-
* @returns The
|
|
169
|
+
* Encode the proof data into a format that can be used by the CRISP program in Solidity
|
|
170
|
+
* to validate the proof.
|
|
171
|
+
* @param proof The proof data.
|
|
172
|
+
* @returns The encoded proof data as a hex string.
|
|
350
173
|
*/
|
|
351
|
-
declare const
|
|
174
|
+
declare const encodeSolidityProof: (proof: ProofData) => Hex;
|
|
352
175
|
|
|
353
|
-
export {
|
|
176
|
+
export { MAXIMUM_VOTE_VALUE, MERKLE_TREE_MAX_DEPTH, type MaskVoteProofInputs, type RoundDetails, type RoundDetailsResponse, SIGNATURE_MESSAGE, type TokenDetails, type Vote, type VoteProofInputs, decodeTally, encodeSolidityProof, encryptVote, generateMaskVoteProof, generateMerkleProof, generateMerkleTree, generatePublicKey, generateVoteProof, getAddressFromSignature, getBalanceAt, getRoundDetails, getRoundTokenDetails, getTotalSupplyAt, getTreeData, hashLeaf, verifyProof };
|