@crisp-e3/sdk 0.5.3 → 0.5.4

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
@@ -19,23 +19,60 @@ npm install @crisp-e3/sdk
19
19
 
20
20
  ## Usage
21
21
 
22
- ### Get Round Details
22
+ ### CrispSDK Class (Recommended)
23
+
24
+ The `CrispSDK` class provides a convenient interface that automatically handles server communication
25
+ for fetching previous ciphertexts and checking slot status.
26
+
27
+ ```typescript
28
+ import { CrispSDK } from '@crisp-e3/sdk'
29
+
30
+ const sdk = new CrispSDK(serverUrl)
31
+
32
+ // Generate a vote proof (automatically fetches previous ciphertext if needed)
33
+ const voteProof = await sdk.generateVoteProof({
34
+ e3Id: 1,
35
+ vote: { yes: 100n, no: 0n },
36
+ publicKey: publicKeyBytes,
37
+ signature: '0x...',
38
+ messageHash: '0x...',
39
+ balance: 1000n,
40
+ slotAddress: '0x...',
41
+ merkleLeaves: [...],
42
+ })
43
+
44
+ // Generate a mask vote proof (automatically fetches previous ciphertext if needed)
45
+ const maskProof = await sdk.generateMaskVoteProof({
46
+ e3Id: 1,
47
+ balance: 1000n,
48
+ slotAddress: '0x...',
49
+ publicKey: publicKeyBytes,
50
+ merkleLeaves: [...],
51
+ })
52
+ ```
53
+
54
+ ### Standalone Functions
55
+
56
+ #### Get Round Details
23
57
 
24
58
  ```typescript
25
- import { getRoundDetails } from '@crisp-e3/sdk'
59
+ import { getRoundDetails, getRoundTokenDetails } from '@crisp-e3/sdk'
26
60
 
27
61
  const roundDetails = await getRoundDetails(serverUrl, e3Id)
62
+ const tokenDetails = await getRoundTokenDetails(serverUrl, e3Id)
28
63
  ```
29
64
 
30
- ### Get Token Balance
65
+ #### Get Token Balance and Supply
31
66
 
32
67
  ```typescript
33
- import { getBalanceAt } from '@crisp-e3/sdk'
68
+ import { getBalanceAt, getTotalSupplyAt, getTreeData } from '@crisp-e3/sdk'
34
69
 
35
70
  const balance = await getBalanceAt(voterAddress, tokenAddress, snapshotBlock, chainId)
71
+ const totalSupply = await getTotalSupplyAt(tokenAddress, snapshotBlock, chainId)
72
+ const merkleLeaves = await getTreeData(serverUrl, e3Id)
36
73
  ```
37
74
 
38
- ### Generate Vote Proof
75
+ #### Generate Vote Proof (Low-level)
39
76
 
40
77
  ```typescript
41
78
  import { generateVoteProof } from '@crisp-e3/sdk'
@@ -44,12 +81,15 @@ const proof = await generateVoteProof({
44
81
  vote: { yes: 100n, no: 0n },
45
82
  publicKey: publicKeyBytes,
46
83
  signature: '0x...',
84
+ messageHash: '0x...',
47
85
  balance: 1000n,
86
+ slotAddress: '0x...',
48
87
  merkleLeaves: [...],
88
+ previousCiphertext: previousCiphertextBytes, // optional
49
89
  })
50
90
  ```
51
91
 
52
- ### Generate Mask Vote Proof
92
+ #### Generate Mask Vote Proof (Low-level)
53
93
 
54
94
  ```typescript
55
95
  import { generateMaskVoteProof } from '@crisp-e3/sdk'
@@ -63,7 +103,7 @@ const maskProof = await generateMaskVoteProof({
63
103
  })
64
104
  ```
65
105
 
66
- ### Verify Proof
106
+ #### Verify Proof
67
107
 
68
108
  ```typescript
69
109
  import { verifyProof } from '@crisp-e3/sdk'
@@ -71,9 +111,112 @@ import { verifyProof } from '@crisp-e3/sdk'
71
111
  const isValid = await verifyProof(proof)
72
112
  ```
73
113
 
114
+ #### Decode Tally
115
+
116
+ ```typescript
117
+ import { decodeTally } from '@crisp-e3/sdk'
118
+
119
+ const tally = decodeTally(tallyBytes)
120
+ // Returns: { yes: bigint, no: bigint }
121
+ ```
122
+
123
+ #### Cryptographic Utilities
124
+
125
+ ```typescript
126
+ import { generatePublicKey, encryptVote, encodeSolidityProof } from '@crisp-e3/sdk'
127
+
128
+ const publicKey = generatePublicKey()
129
+ const encryptedVote = encryptVote(vote, publicKey)
130
+ const encodedProof = encodeSolidityProof(proof)
131
+ ```
132
+
133
+ #### Merkle Tree Utilities
134
+
135
+ ```typescript
136
+ import {
137
+ generateMerkleProof,
138
+ generateMerkleTree,
139
+ hashLeaf,
140
+ getAddressFromSignature,
141
+ } from '@crisp-e3/sdk'
142
+
143
+ const leaf = hashLeaf(address, balance)
144
+ const tree = generateMerkleTree(leaves)
145
+ const proof = generateMerkleProof(balance, address, merkleLeaves)
146
+ const address = await getAddressFromSignature(signature, messageHash)
147
+ ```
148
+
149
+ #### State Utilities
150
+
151
+ ```typescript
152
+ import { getPreviousCiphertext, getIsSlotEmpty } from '@crisp-e3/sdk'
153
+
154
+ const previousCiphertext = await getPreviousCiphertext(serverUrl, e3Id, slotAddress)
155
+ const isEmpty = await getIsSlotEmpty(serverUrl, e3Id, slotAddress)
156
+ ```
157
+
74
158
  ## API
75
159
 
76
- - **State**: `getRoundDetails`, `getRoundTokenDetails`
77
- - **Token**: `getBalanceAt`, `getTotalSupplyAt`, `getTreeData`
78
- - **Vote**: `generateVoteProof`, `generateMaskVoteProof`, `verifyProof`, `decodeTally`
79
- - **Utils**: `generateMerkleProof`, `generateMerkleTree`, `hashLeaf`, `getAddressFromSignature`
160
+ ### CrispSDK Class
161
+
162
+ - `constructor(serverUrl: string)` - Create a new SDK instance
163
+ - `generateVoteProof(voteProofRequest: VoteProofRequest): Promise<ProofData>` - Generate a vote
164
+ proof (automatically handles previous ciphertext)
165
+ - `generateMaskVoteProof(maskVoteProofRequest: MaskVoteProofRequest): Promise<ProofData>` - Generate
166
+ a mask vote proof (automatically handles previous ciphertext)
167
+
168
+ ### State Functions
169
+
170
+ - `getRoundDetails(serverUrl: string, e3Id: number): Promise<RoundDetails>` - Get round details
171
+ - `getRoundTokenDetails(serverUrl: string, e3Id: number): Promise<TokenDetails>` - Get token details
172
+ for a round
173
+ - `getPreviousCiphertext(serverUrl: string, e3Id: number, address: string): Promise<Uint8Array>` -
174
+ Get previous ciphertext for a slot
175
+ - `getIsSlotEmpty(serverUrl: string, e3Id: number, address: string): Promise<boolean>` - Check if a
176
+ slot is empty
177
+
178
+ ### Token Functions
179
+
180
+ - `getBalanceAt(voterAddress: string, tokenAddress: string, snapshotBlock: number, chainId: number): Promise<bigint>` -
181
+ Get token balance at a specific block
182
+ - `getTotalSupplyAt(tokenAddress: string, snapshotBlock: number, chainId: number): Promise<bigint>` -
183
+ Get total supply at a specific block
184
+ - `getTreeData(serverUrl: string, e3Id: number): Promise<bigint[]>` - Get merkle tree leaves from
185
+ server
186
+
187
+ ### Vote Functions
188
+
189
+ - `generateVoteProof(voteProofInputs: VoteProofInputs): Promise<ProofData>` - Generate a vote proof
190
+ (low-level)
191
+ - `generateMaskVoteProof(maskVoteProofInputs: MaskVoteProofInputs): Promise<ProofData>` - Generate a
192
+ mask vote proof (low-level)
193
+ - `verifyProof(proof: ProofData): Promise<boolean>` - Verify a proof locally
194
+ - `decodeTally(tallyBytes: string): Vote` - Decode an encoded tally
195
+ - `generatePublicKey(): Uint8Array` - Generate a random public key
196
+ - `encryptVote(vote: Vote, publicKey: Uint8Array): Uint8Array` - Encrypt a vote
197
+ - `encodeSolidityProof(proof: ProofData): Hex` - Encode proof for Solidity contract
198
+
199
+ ### Utility Functions
200
+
201
+ - `generateMerkleProof(balance: bigint, address: string, leaves: bigint[] | string[]): MerkleProof` -
202
+ Generate merkle proof
203
+ - `generateMerkleTree(leaves: bigint[]): LeanIMT` - Generate merkle tree
204
+ - `hashLeaf(address: string, balance: bigint): bigint` - Hash a leaf node
205
+ - `getAddressFromSignature(signature: \`0x${string}\`, messageHash?: \`0x${string}\`):
206
+ Promise<string>` - Extract address from signature
207
+
208
+ ### Constants
209
+
210
+ - `MERKLE_TREE_MAX_DEPTH` - Maximum depth of the merkle tree
211
+ - `SIGNATURE_MESSAGE` - Message used for signature verification
212
+ - `MAXIMUM_VOTE_VALUE` - Maximum allowed vote value
213
+ - `SIGNATURE_MESSAGE_HASH` - Hash of the signature message
214
+
215
+ ### Types
216
+
217
+ - `RoundDetails` - Round details type
218
+ - `RoundDetailsResponse` - Server response type for round details
219
+ - `TokenDetails` - Token details type
220
+ - `Vote` - Vote type with `yes` and `no` bigint fields
221
+ - `MaskVoteProofInputs` - Inputs for mask vote proof generation
222
+ - `VoteProofInputs` - Inputs for vote proof generation
package/dist/index.d.ts CHANGED
@@ -98,8 +98,7 @@ type MaskVoteProofInputs = {
98
98
  balance: bigint;
99
99
  slotAddress: string;
100
100
  merkleLeaves: string[] | bigint[];
101
- isFirstVote: boolean;
102
- previousCiphertext: Uint8Array;
101
+ previousCiphertext?: Uint8Array;
103
102
  };
104
103
  type MaskVoteProofRequest = {
105
104
  e3Id: number;
@@ -108,8 +107,7 @@ type MaskVoteProofRequest = {
108
107
  slotAddress: string;
109
108
  merkleLeaves: string[] | bigint[];
110
109
  };
111
- type VoteProofRequest = {
112
- e3Id: number;
110
+ type VoteProofInputs = {
113
111
  merkleLeaves: string[] | bigint[];
114
112
  publicKey: Uint8Array;
115
113
  balance: bigint;
@@ -117,8 +115,10 @@ type VoteProofRequest = {
117
115
  signature: `0x${string}`;
118
116
  messageHash: `0x${string}`;
119
117
  slotAddress: string;
118
+ previousCiphertext?: Uint8Array;
120
119
  };
121
- type VoteProofInputs = {
120
+ type VoteProofRequest = {
121
+ e3Id: number;
122
122
  merkleLeaves: string[] | bigint[];
123
123
  publicKey: Uint8Array;
124
124
  balance: bigint;
@@ -126,8 +126,6 @@ type VoteProofInputs = {
126
126
  signature: `0x${string}`;
127
127
  messageHash: `0x${string}`;
128
128
  slotAddress: string;
129
- isFirstVote: boolean;
130
- previousCiphertext: Uint8Array;
131
129
  };
132
130
 
133
131
  /**
@@ -239,21 +237,23 @@ declare const verifyProof: (proof: ProofData) => Promise<boolean>;
239
237
  declare const encodeSolidityProof: (proof: ProofData) => Hex;
240
238
 
241
239
  /**
242
- * A class representing the Crisp SDK.
240
+ * A class representing the CRISP SDK.
243
241
  */
244
242
  declare class CrispSDK {
245
243
  /**
246
- * The server URL for the Crisp SDK.
247
- * It's used by methods that communicate directly with the Crisp server.
244
+ * The server URL for the CRISP SDK.
245
+ * It's used by methods that communicate directly with the CRISP server.
248
246
  */
249
247
  private serverUrl;
250
248
  /**
251
- * Create a new instance
249
+ * Create a new instance.
252
250
  * @param serverUrl
253
251
  */
254
252
  constructor(serverUrl: string);
255
253
  /**
256
254
  * Generate a proof for a vote masking.
255
+ * @param maskProofInputs - The inputs required to generate the mask vote proof.
256
+ * @returns A promise that resolves to the generated proof data.
257
257
  */
258
258
  generateMaskVoteProof(maskProofInputs: MaskVoteProofRequest): Promise<ProofData>;
259
259
  /**