@cloak.ag/sdk 1.0.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/README.md +180 -0
- package/dist/index.cjs +3707 -0
- package/dist/index.d.cts +1990 -0
- package/dist/index.d.ts +1990 -0
- package/dist/index.js +3595 -0
- package/package.json +91 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,1990 @@
|
|
|
1
|
+
import { PublicKey, Transaction, Connection, Keypair, SendOptions, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Supported Solana networks
|
|
5
|
+
*/
|
|
6
|
+
type Network = "localnet" | "devnet" | "mainnet" | "testnet";
|
|
7
|
+
/**
|
|
8
|
+
* Minimal wallet adapter interface
|
|
9
|
+
* Compatible with @solana/wallet-adapter-base
|
|
10
|
+
*/
|
|
11
|
+
interface WalletAdapter {
|
|
12
|
+
publicKey: PublicKey | null;
|
|
13
|
+
signTransaction?<T extends Transaction>(transaction: T): Promise<T>;
|
|
14
|
+
signAllTransactions?<T extends Transaction>(transactions: T[]): Promise<T[]>;
|
|
15
|
+
sendTransaction?(transaction: Transaction, connection: any, options?: any): Promise<string>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Cloak-specific error with categorization
|
|
19
|
+
*/
|
|
20
|
+
declare class CloakError extends Error {
|
|
21
|
+
category: "network" | "indexer" | "prover" | "relay" | "validation" | "wallet" | "environment";
|
|
22
|
+
retryable: boolean;
|
|
23
|
+
originalError?: Error | undefined;
|
|
24
|
+
constructor(message: string, category: "network" | "indexer" | "prover" | "relay" | "validation" | "wallet" | "environment", retryable?: boolean, originalError?: Error | undefined);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Cloak Note - Represents a private transaction commitment
|
|
28
|
+
*
|
|
29
|
+
* A note contains all the information needed to withdraw funds from the Cloak protocol.
|
|
30
|
+
* Keep this safe and secret - anyone with access to this note can withdraw the funds!
|
|
31
|
+
*/
|
|
32
|
+
interface CloakNote {
|
|
33
|
+
/** Protocol version */
|
|
34
|
+
version: string;
|
|
35
|
+
/** Amount in lamports */
|
|
36
|
+
amount: number;
|
|
37
|
+
/** Blake3 commitment hash (hex) */
|
|
38
|
+
commitment: string;
|
|
39
|
+
/** Spending secret key (hex, 64 chars) */
|
|
40
|
+
sk_spend: string;
|
|
41
|
+
/** Randomness value (hex, 64 chars) */
|
|
42
|
+
r: string;
|
|
43
|
+
/** Transaction signature from deposit (optional until deposited) */
|
|
44
|
+
depositSignature?: string;
|
|
45
|
+
/** Solana slot when deposited (optional until deposited) */
|
|
46
|
+
depositSlot?: number;
|
|
47
|
+
/** Index in the Merkle tree (optional until deposited) */
|
|
48
|
+
leafIndex?: number;
|
|
49
|
+
/** Historical Merkle root at time of deposit (optional until deposited) */
|
|
50
|
+
root?: string;
|
|
51
|
+
/** Merkle proof at time of deposit (optional until deposited) */
|
|
52
|
+
merkleProof?: MerkleProof;
|
|
53
|
+
/** Creation timestamp */
|
|
54
|
+
timestamp: number;
|
|
55
|
+
/** Network where this note was created */
|
|
56
|
+
network: Network;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Merkle proof for a leaf in the commitment tree
|
|
60
|
+
*/
|
|
61
|
+
interface MerkleProof {
|
|
62
|
+
/** Sibling hashes along the path (hex strings) */
|
|
63
|
+
pathElements: string[];
|
|
64
|
+
/** Path directions (0 = left, 1 = right) */
|
|
65
|
+
pathIndices: number[];
|
|
66
|
+
/** Optional root for backward compatibility */
|
|
67
|
+
root?: string;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Transfer recipient - used in privateTransfer
|
|
71
|
+
*/
|
|
72
|
+
interface Transfer {
|
|
73
|
+
/** Recipient's Solana public key */
|
|
74
|
+
recipient: PublicKey;
|
|
75
|
+
/** Amount to send in lamports */
|
|
76
|
+
amount: number;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Type-safe array with maximum length constraint
|
|
80
|
+
* Used to enforce 1-5 recipients in privateTransfer
|
|
81
|
+
*/
|
|
82
|
+
type MaxLengthArray<T, Max extends number, A extends T[] = []> = A['length'] extends Max ? A : A | MaxLengthArray<T, Max, [T, ...A]>;
|
|
83
|
+
/**
|
|
84
|
+
* Result from a private transfer
|
|
85
|
+
*/
|
|
86
|
+
interface TransferResult {
|
|
87
|
+
/** Solana transaction signature */
|
|
88
|
+
signature: string;
|
|
89
|
+
/** Recipients and amounts that were sent */
|
|
90
|
+
outputs: Array<{
|
|
91
|
+
recipient: string;
|
|
92
|
+
amount: number;
|
|
93
|
+
}>;
|
|
94
|
+
/** Nullifier used (prevents double-spending) */
|
|
95
|
+
nullifier: string;
|
|
96
|
+
/** Merkle root that was proven against */
|
|
97
|
+
root: string;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Result from a deposit operation
|
|
101
|
+
*/
|
|
102
|
+
interface DepositResult {
|
|
103
|
+
/** The created note (save this securely!) */
|
|
104
|
+
note: CloakNote;
|
|
105
|
+
/** Solana transaction signature */
|
|
106
|
+
signature: string;
|
|
107
|
+
/** Leaf index in the Merkle tree */
|
|
108
|
+
leafIndex: number;
|
|
109
|
+
/** Current Merkle root */
|
|
110
|
+
root: string;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Configuration for the Cloak SDK
|
|
114
|
+
*/
|
|
115
|
+
interface CloakConfig {
|
|
116
|
+
/** Solana network */
|
|
117
|
+
network?: Network;
|
|
118
|
+
/**
|
|
119
|
+
* Keypair bytes for signing (deprecated - use wallet instead)
|
|
120
|
+
* @deprecated Use wallet parameter for better integration
|
|
121
|
+
*/
|
|
122
|
+
keypairBytes?: Uint8Array;
|
|
123
|
+
/**
|
|
124
|
+
* Wallet adapter for signing transactions
|
|
125
|
+
* Required unless using keypairBytes
|
|
126
|
+
*/
|
|
127
|
+
wallet?: WalletAdapter;
|
|
128
|
+
/**
|
|
129
|
+
* Cloak key pair for v2.0 features (note scanning, encryption)
|
|
130
|
+
* Optional but recommended for full functionality
|
|
131
|
+
*/
|
|
132
|
+
cloakKeys?: any;
|
|
133
|
+
/**
|
|
134
|
+
* Single API base URL for both Indexer and Relay services.
|
|
135
|
+
* If provided, it will be used for both services and overrides
|
|
136
|
+
* any `indexerUrl` or `relayUrl` values.
|
|
137
|
+
*/
|
|
138
|
+
apiUrl?: string;
|
|
139
|
+
/** Optional: Proof generation timeout in milliseconds (default: 5 minutes) */
|
|
140
|
+
proofTimeout?: number;
|
|
141
|
+
/** Optional: Program ID (defaults to Cloak mainnet program) */
|
|
142
|
+
programId?: PublicKey;
|
|
143
|
+
/** Optional: Pool account address (auto-derived from program ID if not provided) */
|
|
144
|
+
poolAddress?: PublicKey;
|
|
145
|
+
/** Optional: Merkle tree account address (auto-derived if not provided) */
|
|
146
|
+
merkleTreeAddress?: PublicKey;
|
|
147
|
+
/** Optional: Commitments account address (auto-derived if not provided) */
|
|
148
|
+
commitmentsAddress?: PublicKey;
|
|
149
|
+
/** Optional: Roots ring account address (auto-derived if not provided) */
|
|
150
|
+
rootsRingAddress?: PublicKey;
|
|
151
|
+
/** Optional: Nullifier shard account address (auto-derived if not provided) */
|
|
152
|
+
nullifierShardAddress?: PublicKey;
|
|
153
|
+
/** Optional: Treasury account address (auto-derived if not provided) */
|
|
154
|
+
treasuryAddress?: PublicKey;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Deposit progress status
|
|
158
|
+
*/
|
|
159
|
+
type DepositStatus = "generating_note" | "creating_transaction" | "simulating" | "sending" | "confirming" | "submitting_to_indexer" | "fetching_proof" | "complete";
|
|
160
|
+
/**
|
|
161
|
+
* Options for deposit operation
|
|
162
|
+
*/
|
|
163
|
+
interface DepositOptions {
|
|
164
|
+
/** Optional callback for progress updates with detailed status */
|
|
165
|
+
onProgress?: (status: DepositStatus | string, details?: {
|
|
166
|
+
message?: string;
|
|
167
|
+
step?: number;
|
|
168
|
+
totalSteps?: number;
|
|
169
|
+
retryAttempt?: number;
|
|
170
|
+
}) => void;
|
|
171
|
+
/** Callback when transaction is sent (before confirmation) */
|
|
172
|
+
onTransactionSent?: (signature: string) => void;
|
|
173
|
+
/** Callback when transaction is confirmed */
|
|
174
|
+
onConfirmed?: (signature: string, slot: number) => void;
|
|
175
|
+
/** Skip simulation (default: false) */
|
|
176
|
+
skipPreflight?: boolean;
|
|
177
|
+
/** Compute units to request (default: auto) */
|
|
178
|
+
computeUnits?: number;
|
|
179
|
+
/** Priority fee in micro-lamports (default: 0) */
|
|
180
|
+
priorityFee?: number;
|
|
181
|
+
/**
|
|
182
|
+
* Optional: Encrypt output for specific recipient's view key
|
|
183
|
+
* If not provided, encrypts for the wallet's own view key (for self-scanning)
|
|
184
|
+
*/
|
|
185
|
+
recipientViewKey?: string;
|
|
186
|
+
/**
|
|
187
|
+
* Skip privacy warning on testnet (default: false)
|
|
188
|
+
* Warning: Only skip if you understand the privacy limitations
|
|
189
|
+
*/
|
|
190
|
+
skipPrivacyWarning?: boolean;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Options for private transfer/withdraw operation
|
|
194
|
+
*/
|
|
195
|
+
interface TransferOptions {
|
|
196
|
+
/**
|
|
197
|
+
* Optional callback for progress updates
|
|
198
|
+
* Note: relayFeeBps is automatically calculated from protocol fees
|
|
199
|
+
*/
|
|
200
|
+
onProgress?: (status: string) => void;
|
|
201
|
+
/** Optional callback for proof generation progress (0-100) */
|
|
202
|
+
onProofProgress?: (percent: number) => void;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Options for withdrawal (convenience method with single recipient)
|
|
206
|
+
*/
|
|
207
|
+
interface WithdrawOptions extends TransferOptions {
|
|
208
|
+
/** Whether to withdraw full amount minus fees (default: true) */
|
|
209
|
+
withdrawAll?: boolean;
|
|
210
|
+
/** Specific amount to withdraw in lamports (if not withdrawing all) */
|
|
211
|
+
amount?: number;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* SP1 proof inputs for zero-knowledge proof generation
|
|
215
|
+
*/
|
|
216
|
+
interface SP1ProofInputs {
|
|
217
|
+
privateInputs: {
|
|
218
|
+
amount: number;
|
|
219
|
+
r: string;
|
|
220
|
+
sk_spend: string;
|
|
221
|
+
leaf_index: number;
|
|
222
|
+
merkle_path: {
|
|
223
|
+
path_elements: string[];
|
|
224
|
+
path_indices: number[];
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
publicInputs: {
|
|
228
|
+
root: string;
|
|
229
|
+
nf: string;
|
|
230
|
+
outputs_hash: string;
|
|
231
|
+
amount: number;
|
|
232
|
+
};
|
|
233
|
+
outputs: Array<{
|
|
234
|
+
address: string;
|
|
235
|
+
amount: number;
|
|
236
|
+
}>;
|
|
237
|
+
/**
|
|
238
|
+
* Optional swap parameters for token swaps
|
|
239
|
+
* When provided, the proof will be for a swap transaction
|
|
240
|
+
*/
|
|
241
|
+
swapParams?: {
|
|
242
|
+
output_mint: string;
|
|
243
|
+
recipient_ata: string;
|
|
244
|
+
min_output_amount: number;
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Result from proof generation
|
|
249
|
+
*/
|
|
250
|
+
interface SP1ProofResult {
|
|
251
|
+
success: boolean;
|
|
252
|
+
proof?: string;
|
|
253
|
+
publicInputs?: string;
|
|
254
|
+
generationTimeMs: number;
|
|
255
|
+
error?: string;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Merkle root response from indexer
|
|
259
|
+
*/
|
|
260
|
+
interface MerkleRootResponse {
|
|
261
|
+
root: string;
|
|
262
|
+
next_index: number;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Transaction status from relay service
|
|
266
|
+
*/
|
|
267
|
+
interface TxStatus {
|
|
268
|
+
status: "pending" | "processing" | "completed" | "failed";
|
|
269
|
+
txId?: string;
|
|
270
|
+
error?: string;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Note scanning options
|
|
274
|
+
*/
|
|
275
|
+
interface ScanNotesOptions {
|
|
276
|
+
/** Start index for scanning (default: 0) */
|
|
277
|
+
startIndex?: number;
|
|
278
|
+
/** End index for scanning (default: latest) */
|
|
279
|
+
endIndex?: number;
|
|
280
|
+
/** Batch size for fetching notes (default: 100) */
|
|
281
|
+
batchSize?: number;
|
|
282
|
+
/** Progress callback */
|
|
283
|
+
onProgress?: (current: number, total: number) => void;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Scanned note result with metadata
|
|
287
|
+
*/
|
|
288
|
+
interface ScannedNote extends CloakNote {
|
|
289
|
+
/** When this note was discovered */
|
|
290
|
+
scannedAt: number;
|
|
291
|
+
/** Whether this note has been spent (nullifier check) */
|
|
292
|
+
isSpent?: boolean;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Swap parameters for token swaps
|
|
296
|
+
*/
|
|
297
|
+
interface SwapParams {
|
|
298
|
+
/** Output token mint address */
|
|
299
|
+
output_mint: string;
|
|
300
|
+
/** Slippage tolerance in basis points (e.g., 100 = 1%) */
|
|
301
|
+
slippage_bps: number;
|
|
302
|
+
/** Minimum output amount in token's smallest unit */
|
|
303
|
+
min_output_amount: number;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Options for swap operation
|
|
307
|
+
*/
|
|
308
|
+
interface SwapOptions extends TransferOptions {
|
|
309
|
+
/** Output token mint address */
|
|
310
|
+
outputMint: string;
|
|
311
|
+
/** Slippage tolerance in basis points (default: 100 = 1%) */
|
|
312
|
+
slippageBps?: number;
|
|
313
|
+
/** Minimum output amount (will be calculated from quote if not provided) */
|
|
314
|
+
minOutputAmount?: number;
|
|
315
|
+
/** Optional callback to get swap quote */
|
|
316
|
+
getQuote?: (amountLamports: number, outputMint: string, slippageBps: number) => Promise<{
|
|
317
|
+
outAmount: number;
|
|
318
|
+
minOutputAmount: number;
|
|
319
|
+
}>;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Result from a swap operation
|
|
323
|
+
*/
|
|
324
|
+
interface SwapResult extends TransferResult {
|
|
325
|
+
/** Output token mint address */
|
|
326
|
+
outputMint: string;
|
|
327
|
+
/** Minimum output amount that was guaranteed */
|
|
328
|
+
minOutputAmount: number;
|
|
329
|
+
/** Actual output amount received (may be higher than min) */
|
|
330
|
+
actualOutputAmount?: number;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Cloak Key Hierarchy (v2.0)
|
|
335
|
+
*
|
|
336
|
+
* Implements view/spend key separation for privacy-preserving note scanning:
|
|
337
|
+
* - Master Seed → Spend Key → View Key → Public View Key
|
|
338
|
+
* - Enables note discovery without exposing spending authority
|
|
339
|
+
* - Compatible with v1.0 notes (backward compatible)
|
|
340
|
+
*/
|
|
341
|
+
interface MasterKey {
|
|
342
|
+
seed: Uint8Array;
|
|
343
|
+
seedHex: string;
|
|
344
|
+
}
|
|
345
|
+
interface SpendKey {
|
|
346
|
+
sk_spend: Uint8Array;
|
|
347
|
+
pk_spend: Uint8Array;
|
|
348
|
+
sk_spend_hex: string;
|
|
349
|
+
pk_spend_hex: string;
|
|
350
|
+
}
|
|
351
|
+
interface ViewKey {
|
|
352
|
+
vk_secret: Uint8Array;
|
|
353
|
+
pvk: Uint8Array;
|
|
354
|
+
vk_secret_hex: string;
|
|
355
|
+
pvk_hex: string;
|
|
356
|
+
}
|
|
357
|
+
interface CloakKeyPair {
|
|
358
|
+
master: MasterKey;
|
|
359
|
+
spend: SpendKey;
|
|
360
|
+
view: ViewKey;
|
|
361
|
+
}
|
|
362
|
+
interface EncryptedNote {
|
|
363
|
+
ephemeral_pk: string;
|
|
364
|
+
ciphertext: string;
|
|
365
|
+
nonce: string;
|
|
366
|
+
}
|
|
367
|
+
interface NoteData {
|
|
368
|
+
amount: number;
|
|
369
|
+
r: string;
|
|
370
|
+
sk_spend: string;
|
|
371
|
+
commitment: string;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Generate a new master seed from secure randomness
|
|
375
|
+
*/
|
|
376
|
+
declare function generateMasterSeed(): MasterKey;
|
|
377
|
+
/**
|
|
378
|
+
* Derive spend key from master seed
|
|
379
|
+
*/
|
|
380
|
+
declare function deriveSpendKey(masterSeed: Uint8Array): SpendKey;
|
|
381
|
+
/**
|
|
382
|
+
* Derive view key from spend key
|
|
383
|
+
*/
|
|
384
|
+
declare function deriveViewKey(sk_spend: Uint8Array): ViewKey;
|
|
385
|
+
/**
|
|
386
|
+
* Generate complete key hierarchy from master seed
|
|
387
|
+
*/
|
|
388
|
+
declare function generateCloakKeys(masterSeed?: Uint8Array): CloakKeyPair;
|
|
389
|
+
/**
|
|
390
|
+
* Encrypt note data for a recipient using their public view key
|
|
391
|
+
*
|
|
392
|
+
* Uses X25519 ECDH + XSalsa20-Poly1305 authenticated encryption
|
|
393
|
+
*/
|
|
394
|
+
declare function encryptNoteForRecipient(noteData: NoteData, recipientPvk: Uint8Array): EncryptedNote;
|
|
395
|
+
/**
|
|
396
|
+
* Attempt to decrypt an encrypted note using view key
|
|
397
|
+
*
|
|
398
|
+
* Returns null if decryption fails (note doesn't belong to this wallet)
|
|
399
|
+
* Returns NoteData if successful
|
|
400
|
+
*/
|
|
401
|
+
declare function tryDecryptNote(encryptedNote: EncryptedNote, viewKey: ViewKey): NoteData | null;
|
|
402
|
+
/**
|
|
403
|
+
* Scan a batch of encrypted outputs and return notes belonging to this wallet
|
|
404
|
+
*/
|
|
405
|
+
declare function scanNotesForWallet(encryptedOutputs: string[], // Base64 encoded encrypted note JSON
|
|
406
|
+
viewKey: ViewKey): NoteData[];
|
|
407
|
+
/**
|
|
408
|
+
* Export keys for backup (WARNING: contains secrets!)
|
|
409
|
+
*/
|
|
410
|
+
declare function exportKeys(keys: CloakKeyPair): string;
|
|
411
|
+
/**
|
|
412
|
+
* Import keys from backup
|
|
413
|
+
*/
|
|
414
|
+
declare function importKeys(exported: string): CloakKeyPair;
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Storage Interface
|
|
418
|
+
*
|
|
419
|
+
* Defines a pluggable storage interface for notes and keys.
|
|
420
|
+
* Applications can implement their own storage (localStorage, IndexedDB, file system, etc.)
|
|
421
|
+
*/
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Storage adapter interface
|
|
425
|
+
*
|
|
426
|
+
* Implement this interface to provide custom storage for notes and keys.
|
|
427
|
+
* The SDK will use this adapter for all persistence operations.
|
|
428
|
+
*/
|
|
429
|
+
interface StorageAdapter {
|
|
430
|
+
/**
|
|
431
|
+
* Save a note
|
|
432
|
+
*/
|
|
433
|
+
saveNote(note: CloakNote): Promise<void> | void;
|
|
434
|
+
/**
|
|
435
|
+
* Load all notes
|
|
436
|
+
*/
|
|
437
|
+
loadAllNotes(): Promise<CloakNote[]> | CloakNote[];
|
|
438
|
+
/**
|
|
439
|
+
* Update a note
|
|
440
|
+
*/
|
|
441
|
+
updateNote(commitment: string, updates: Partial<CloakNote>): Promise<void> | void;
|
|
442
|
+
/**
|
|
443
|
+
* Delete a note
|
|
444
|
+
*/
|
|
445
|
+
deleteNote(commitment: string): Promise<void> | void;
|
|
446
|
+
/**
|
|
447
|
+
* Clear all notes
|
|
448
|
+
*/
|
|
449
|
+
clearAllNotes(): Promise<void> | void;
|
|
450
|
+
/**
|
|
451
|
+
* Save wallet keys
|
|
452
|
+
*/
|
|
453
|
+
saveKeys(keys: CloakKeyPair): Promise<void> | void;
|
|
454
|
+
/**
|
|
455
|
+
* Load wallet keys
|
|
456
|
+
*/
|
|
457
|
+
loadKeys(): Promise<CloakKeyPair | null> | CloakKeyPair | null;
|
|
458
|
+
/**
|
|
459
|
+
* Delete wallet keys
|
|
460
|
+
*/
|
|
461
|
+
deleteKeys(): Promise<void> | void;
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* In-memory storage adapter (default, no persistence)
|
|
465
|
+
*
|
|
466
|
+
* Useful for testing or when storage is handled externally
|
|
467
|
+
*/
|
|
468
|
+
declare class MemoryStorageAdapter implements StorageAdapter {
|
|
469
|
+
private notes;
|
|
470
|
+
private keys;
|
|
471
|
+
saveNote(note: CloakNote): void;
|
|
472
|
+
loadAllNotes(): CloakNote[];
|
|
473
|
+
updateNote(commitment: string, updates: Partial<CloakNote>): void;
|
|
474
|
+
deleteNote(commitment: string): void;
|
|
475
|
+
clearAllNotes(): void;
|
|
476
|
+
saveKeys(keys: CloakKeyPair): void;
|
|
477
|
+
loadKeys(): CloakKeyPair | null;
|
|
478
|
+
deleteKeys(): void;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Browser localStorage adapter (optional, for browser environments)
|
|
482
|
+
*
|
|
483
|
+
* Only use this if you're in a browser environment and want localStorage persistence.
|
|
484
|
+
* Import from a separate browser-specific module.
|
|
485
|
+
*/
|
|
486
|
+
declare class LocalStorageAdapter implements StorageAdapter {
|
|
487
|
+
private notesKey;
|
|
488
|
+
private keysKey;
|
|
489
|
+
constructor(notesKey?: string, keysKey?: string);
|
|
490
|
+
private getStorage;
|
|
491
|
+
saveNote(note: CloakNote): void;
|
|
492
|
+
loadAllNotes(): CloakNote[];
|
|
493
|
+
updateNote(commitment: string, updates: Partial<CloakNote>): void;
|
|
494
|
+
deleteNote(commitment: string): void;
|
|
495
|
+
clearAllNotes(): void;
|
|
496
|
+
saveKeys(keys: CloakKeyPair): void;
|
|
497
|
+
loadKeys(): CloakKeyPair | null;
|
|
498
|
+
deleteKeys(): void;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** Default Cloak Program ID on Solana */
|
|
502
|
+
declare const CLOAK_PROGRAM_ID: PublicKey;
|
|
503
|
+
/**
|
|
504
|
+
* Main Cloak SDK
|
|
505
|
+
*
|
|
506
|
+
* Provides high-level API for interacting with the Cloak protocol,
|
|
507
|
+
* including deposits, withdrawals, and private transfers.
|
|
508
|
+
*/
|
|
509
|
+
declare class CloakSDK {
|
|
510
|
+
private config;
|
|
511
|
+
private keypair;
|
|
512
|
+
private cloakKeys?;
|
|
513
|
+
private indexer;
|
|
514
|
+
private artifactProver;
|
|
515
|
+
private relay;
|
|
516
|
+
private depositRecovery;
|
|
517
|
+
private storage;
|
|
518
|
+
/**
|
|
519
|
+
* Create a new Cloak SDK client
|
|
520
|
+
*
|
|
521
|
+
* @param config - Client configuration
|
|
522
|
+
*
|
|
523
|
+
* @example
|
|
524
|
+
* ```typescript
|
|
525
|
+
* const sdk = new CloakSDK({
|
|
526
|
+
* keypairBytes: keypair.secretKey,
|
|
527
|
+
* network: "devnet"
|
|
528
|
+
* });
|
|
529
|
+
*/
|
|
530
|
+
constructor(config: {
|
|
531
|
+
keypairBytes: Uint8Array;
|
|
532
|
+
network?: Network;
|
|
533
|
+
cloakKeys?: CloakKeyPair;
|
|
534
|
+
storage?: StorageAdapter;
|
|
535
|
+
programId?: PublicKey;
|
|
536
|
+
indexerUrl?: string;
|
|
537
|
+
relayUrl?: string;
|
|
538
|
+
});
|
|
539
|
+
/**
|
|
540
|
+
* Deposit SOL into the Cloak protocol
|
|
541
|
+
*
|
|
542
|
+
* Creates a new note (or uses a provided one), submits a deposit transaction,
|
|
543
|
+
* and registers with the indexer.
|
|
544
|
+
*
|
|
545
|
+
* @param connection - Solana connection
|
|
546
|
+
* @param payer - Payer wallet with sendTransaction method
|
|
547
|
+
* @param amountOrNote - Amount in lamports OR an existing note to deposit
|
|
548
|
+
* @param options - Optional configuration
|
|
549
|
+
* @returns Deposit result with note and transaction info
|
|
550
|
+
*
|
|
551
|
+
* @example
|
|
552
|
+
* ```typescript
|
|
553
|
+
* // Generate and deposit in one step
|
|
554
|
+
* const result = await client.deposit(
|
|
555
|
+
* connection,
|
|
556
|
+
* wallet,
|
|
557
|
+
* 1_000_000_000,
|
|
558
|
+
* {
|
|
559
|
+
* onProgress: (status) => console.log(status)
|
|
560
|
+
* }
|
|
561
|
+
* );
|
|
562
|
+
*
|
|
563
|
+
* // Or deposit a pre-generated note
|
|
564
|
+
* const note = client.generateNote(1_000_000_000);
|
|
565
|
+
* const result = await client.deposit(connection, wallet, note);
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
568
|
+
deposit(connection: Connection, amountOrNote: number | CloakNote, options?: DepositOptions): Promise<DepositResult>;
|
|
569
|
+
/**
|
|
570
|
+
* Private transfer with up to 5 recipients
|
|
571
|
+
*
|
|
572
|
+
* Handles the complete private transfer flow:
|
|
573
|
+
* 1. If note is not deposited, deposits it first and waits for confirmation
|
|
574
|
+
* 2. Generates a zero-knowledge proof
|
|
575
|
+
* 3. Submits the withdrawal via relay service to recipients
|
|
576
|
+
*
|
|
577
|
+
* This is the main method for performing private transfers - it handles everything!
|
|
578
|
+
*
|
|
579
|
+
* @param connection - Solana connection (required for deposit if not already deposited)
|
|
580
|
+
* @param payer - Payer wallet (required for deposit if not already deposited)
|
|
581
|
+
* @param note - Note to spend (can be deposited or not)
|
|
582
|
+
* @param recipients - Array of 1-5 recipients with amounts
|
|
583
|
+
* @param options - Optional configuration
|
|
584
|
+
* @returns Transfer result with signature and outputs
|
|
585
|
+
*
|
|
586
|
+
* @example
|
|
587
|
+
* ```typescript
|
|
588
|
+
* // Create a note (not deposited yet)
|
|
589
|
+
* const note = client.generateNote(1_000_000_000);
|
|
590
|
+
*
|
|
591
|
+
* // privateTransfer handles the full flow: deposit + withdraw
|
|
592
|
+
* const result = await client.privateTransfer(
|
|
593
|
+
* connection,
|
|
594
|
+
* wallet,
|
|
595
|
+
* note,
|
|
596
|
+
* [
|
|
597
|
+
* { recipient: new PublicKey("..."), amount: 500_000_000 },
|
|
598
|
+
* { recipient: new PublicKey("..."), amount: 492_500_000 }
|
|
599
|
+
* ],
|
|
600
|
+
* {
|
|
601
|
+
* relayFeeBps: 50, // 0.5%
|
|
602
|
+
* onProgress: (status) => console.log(status),
|
|
603
|
+
* onProofProgress: (pct) => console.log(`Proof: ${pct}%`)
|
|
604
|
+
* }
|
|
605
|
+
* );
|
|
606
|
+
* console.log(`Success! TX: ${result.signature}`);
|
|
607
|
+
* ```
|
|
608
|
+
*/
|
|
609
|
+
privateTransfer(connection: Connection, note: CloakNote, recipients: MaxLengthArray<Transfer, 5>, options?: TransferOptions): Promise<TransferResult>;
|
|
610
|
+
/**
|
|
611
|
+
* Withdraw to a single recipient
|
|
612
|
+
*
|
|
613
|
+
* Convenience method for withdrawing to one address.
|
|
614
|
+
* Handles the complete flow: deposits if needed, then withdraws.
|
|
615
|
+
*
|
|
616
|
+
* @param connection - Solana connection
|
|
617
|
+
* @param payer - Payer wallet
|
|
618
|
+
* @param note - Note to spend
|
|
619
|
+
* @param recipient - Recipient address
|
|
620
|
+
* @param options - Optional configuration
|
|
621
|
+
* @returns Transfer result
|
|
622
|
+
*
|
|
623
|
+
* @example
|
|
624
|
+
* ```typescript
|
|
625
|
+
* const note = client.generateNote(1_000_000_000);
|
|
626
|
+
* const result = await client.withdraw(
|
|
627
|
+
* connection,
|
|
628
|
+
* wallet,
|
|
629
|
+
* note,
|
|
630
|
+
* new PublicKey("..."),
|
|
631
|
+
* { withdrawAll: true }
|
|
632
|
+
* );
|
|
633
|
+
* ```
|
|
634
|
+
*/
|
|
635
|
+
withdraw(connection: Connection, note: CloakNote, recipient: PublicKey, options?: WithdrawOptions): Promise<TransferResult>;
|
|
636
|
+
/**
|
|
637
|
+
* Send SOL privately to multiple recipients
|
|
638
|
+
*
|
|
639
|
+
* Convenience method that wraps privateTransfer with a simpler API.
|
|
640
|
+
* Handles the complete flow: deposits if needed, then sends to recipients.
|
|
641
|
+
*
|
|
642
|
+
* @param connection - Solana connection
|
|
643
|
+
* @param note - Note to spend
|
|
644
|
+
* @param recipients - Array of 1-5 recipients with amounts
|
|
645
|
+
* @param options - Optional configuration
|
|
646
|
+
* @returns Transfer result
|
|
647
|
+
*
|
|
648
|
+
* @example
|
|
649
|
+
* ```typescript
|
|
650
|
+
* const note = client.generateNote(1_000_000_000);
|
|
651
|
+
* const result = await client.send(
|
|
652
|
+
* connection,
|
|
653
|
+
* note,
|
|
654
|
+
* [
|
|
655
|
+
* { recipient: new PublicKey("..."), amount: 500_000_000 },
|
|
656
|
+
* { recipient: new PublicKey("..."), amount: 492_500_000 }
|
|
657
|
+
* ]
|
|
658
|
+
* );
|
|
659
|
+
* ```
|
|
660
|
+
*/
|
|
661
|
+
send(connection: Connection, note: CloakNote, recipients: MaxLengthArray<Transfer, 5>, options?: TransferOptions): Promise<TransferResult>;
|
|
662
|
+
/**
|
|
663
|
+
* Swap SOL for tokens privately
|
|
664
|
+
*
|
|
665
|
+
* Withdraws SOL from a note and swaps it for tokens via the relay service.
|
|
666
|
+
* Handles the complete flow: deposits if needed, generates proof, and submits swap.
|
|
667
|
+
*
|
|
668
|
+
* @param connection - Solana connection
|
|
669
|
+
* @param note - Note to spend
|
|
670
|
+
* @param recipient - Recipient address (will receive tokens)
|
|
671
|
+
* @param options - Swap configuration
|
|
672
|
+
* @returns Swap result with transaction signature
|
|
673
|
+
*
|
|
674
|
+
* @example
|
|
675
|
+
* ```typescript
|
|
676
|
+
* const note = client.generateNote(1_000_000_000);
|
|
677
|
+
* const result = await client.swap(
|
|
678
|
+
* connection,
|
|
679
|
+
* note,
|
|
680
|
+
* new PublicKey("..."), // recipient
|
|
681
|
+
* {
|
|
682
|
+
* outputMint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
|
|
683
|
+
* slippageBps: 100, // 1%
|
|
684
|
+
* getQuote: async (amount, mint, slippage) => {
|
|
685
|
+
* // Fetch quote from your swap API
|
|
686
|
+
* const quote = await fetchSwapQuote(amount, mint, slippage);
|
|
687
|
+
* return {
|
|
688
|
+
* outAmount: quote.outAmount,
|
|
689
|
+
* minOutputAmount: quote.minOutputAmount
|
|
690
|
+
* };
|
|
691
|
+
* }
|
|
692
|
+
* }
|
|
693
|
+
* );
|
|
694
|
+
* ```
|
|
695
|
+
*/
|
|
696
|
+
swap(connection: Connection, note: CloakNote, recipient: PublicKey, options: SwapOptions): Promise<SwapResult>;
|
|
697
|
+
/**
|
|
698
|
+
* Generate a new note without depositing
|
|
699
|
+
*
|
|
700
|
+
* @param amountLamports - Amount for the note
|
|
701
|
+
* @param useWalletKeys - Whether to use wallet keys (v2.0 recommended)
|
|
702
|
+
* @returns New note (not yet deposited)
|
|
703
|
+
*/
|
|
704
|
+
generateNote(amountLamports: number, useWalletKeys?: boolean): Promise<CloakNote>;
|
|
705
|
+
/**
|
|
706
|
+
* Parse a note from JSON string
|
|
707
|
+
*
|
|
708
|
+
* @param jsonString - JSON representation
|
|
709
|
+
* @returns Parsed note
|
|
710
|
+
*/
|
|
711
|
+
parseNote(jsonString: string): CloakNote;
|
|
712
|
+
/**
|
|
713
|
+
* Export a note to JSON string
|
|
714
|
+
*
|
|
715
|
+
* @param note - Note to export
|
|
716
|
+
* @param pretty - Format with indentation
|
|
717
|
+
* @returns JSON string
|
|
718
|
+
*/
|
|
719
|
+
exportNote(note: CloakNote, pretty?: boolean): string;
|
|
720
|
+
/**
|
|
721
|
+
* Check if a note is ready for withdrawal
|
|
722
|
+
*
|
|
723
|
+
* @param note - Note to check
|
|
724
|
+
* @returns True if withdrawable
|
|
725
|
+
*/
|
|
726
|
+
isWithdrawable(note: CloakNote): boolean;
|
|
727
|
+
/**
|
|
728
|
+
* Get Merkle proof for a leaf index
|
|
729
|
+
*
|
|
730
|
+
* @param leafIndex - Leaf index in tree
|
|
731
|
+
* @returns Merkle proof
|
|
732
|
+
*/
|
|
733
|
+
getMerkleProof(leafIndex: number): Promise<MerkleProof>;
|
|
734
|
+
/**
|
|
735
|
+
* Get current Merkle root
|
|
736
|
+
*
|
|
737
|
+
* @returns Current root hash
|
|
738
|
+
*/
|
|
739
|
+
getCurrentRoot(): Promise<string>;
|
|
740
|
+
/**
|
|
741
|
+
* Get transaction status from relay service
|
|
742
|
+
*
|
|
743
|
+
* @param requestId - Request ID from previous submission
|
|
744
|
+
* @returns Current status
|
|
745
|
+
*/
|
|
746
|
+
getTransactionStatus(requestId: string): Promise<TxStatus>;
|
|
747
|
+
/**
|
|
748
|
+
* Recover a deposit that completed on-chain but failed to register
|
|
749
|
+
*
|
|
750
|
+
* Use this when a deposit transaction succeeded but the browser crashed
|
|
751
|
+
* or lost connection before the indexer registration completed.
|
|
752
|
+
*
|
|
753
|
+
* @param signature - Transaction signature
|
|
754
|
+
* @param commitment - Note commitment hash
|
|
755
|
+
* @param note - Optional: The full note if available
|
|
756
|
+
* @returns Recovery result with updated note
|
|
757
|
+
*
|
|
758
|
+
* @example
|
|
759
|
+
* ```typescript
|
|
760
|
+
* const result = await sdk.recoverDeposit({
|
|
761
|
+
* signature: "5Kn4...",
|
|
762
|
+
* commitment: "abc123...",
|
|
763
|
+
* note: myNote // optional if you have it
|
|
764
|
+
* });
|
|
765
|
+
*
|
|
766
|
+
* if (result.success) {
|
|
767
|
+
* console.log(`Recovered! Leaf index: ${result.leafIndex}`);
|
|
768
|
+
* }
|
|
769
|
+
* ```
|
|
770
|
+
*/
|
|
771
|
+
recoverDeposit(options: {
|
|
772
|
+
signature: string;
|
|
773
|
+
commitment: string;
|
|
774
|
+
note?: CloakNote;
|
|
775
|
+
onProgress?: (status: string) => void;
|
|
776
|
+
}): Promise<{
|
|
777
|
+
success: boolean;
|
|
778
|
+
leafIndex?: number;
|
|
779
|
+
root?: string;
|
|
780
|
+
slot?: number;
|
|
781
|
+
merkleProof?: {
|
|
782
|
+
pathElements: string[];
|
|
783
|
+
pathIndices: number[];
|
|
784
|
+
};
|
|
785
|
+
note?: CloakNote;
|
|
786
|
+
error?: string;
|
|
787
|
+
}>;
|
|
788
|
+
/**
|
|
789
|
+
* Load all notes from storage
|
|
790
|
+
*
|
|
791
|
+
* @returns Array of saved notes
|
|
792
|
+
*/
|
|
793
|
+
loadNotes(): Promise<CloakNote[]>;
|
|
794
|
+
/**
|
|
795
|
+
* Save a note to storage
|
|
796
|
+
*
|
|
797
|
+
* @param note - Note to save
|
|
798
|
+
*/
|
|
799
|
+
saveNote(note: CloakNote): Promise<void>;
|
|
800
|
+
/**
|
|
801
|
+
* Find a note by its commitment
|
|
802
|
+
*
|
|
803
|
+
* @param commitment - Commitment hash
|
|
804
|
+
* @returns Note if found
|
|
805
|
+
*/
|
|
806
|
+
findNote(commitment: string): Promise<CloakNote | undefined>;
|
|
807
|
+
/**
|
|
808
|
+
* Import wallet keys from JSON
|
|
809
|
+
*
|
|
810
|
+
* @param keysJson - JSON string containing keys
|
|
811
|
+
*/
|
|
812
|
+
importWalletKeys(keysJson: string): Promise<void>;
|
|
813
|
+
/**
|
|
814
|
+
* Export wallet keys to JSON
|
|
815
|
+
*
|
|
816
|
+
* WARNING: This exports secret keys! Store securely.
|
|
817
|
+
*
|
|
818
|
+
* @returns JSON string with keys
|
|
819
|
+
*/
|
|
820
|
+
exportWalletKeys(): string;
|
|
821
|
+
/**
|
|
822
|
+
* Get the configuration
|
|
823
|
+
*/
|
|
824
|
+
getConfig(): CloakConfig;
|
|
825
|
+
/**
|
|
826
|
+
* Scan blockchain for notes belonging to this wallet (v2.0 feature)
|
|
827
|
+
*
|
|
828
|
+
* Requires Cloak keys to be configured in the SDK.
|
|
829
|
+
* Fetches encrypted outputs from the indexer and decrypts notes
|
|
830
|
+
* that belong to this wallet.
|
|
831
|
+
*
|
|
832
|
+
* @param options - Scanning options
|
|
833
|
+
* @returns Array of discovered notes with metadata
|
|
834
|
+
*
|
|
835
|
+
* @example
|
|
836
|
+
* ```typescript
|
|
837
|
+
* const notes = await sdk.scanNotes({
|
|
838
|
+
* onProgress: (current, total) => {
|
|
839
|
+
* console.log(`Scanning: ${current}/${total}`);
|
|
840
|
+
* }
|
|
841
|
+
* });
|
|
842
|
+
*
|
|
843
|
+
* console.log(`Found ${notes.length} notes!`);
|
|
844
|
+
* const totalBalance = notes.reduce((sum, n) => sum + n.amount, 0);
|
|
845
|
+
* ```
|
|
846
|
+
*/
|
|
847
|
+
scanNotes(options?: ScanNotesOptions): Promise<ScannedNote[]>;
|
|
848
|
+
/**
|
|
849
|
+
* Wrap errors with better categorization and user-friendly messages
|
|
850
|
+
*
|
|
851
|
+
* @private
|
|
852
|
+
*/
|
|
853
|
+
private wrapError;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* Serialize a note to JSON string
|
|
858
|
+
*
|
|
859
|
+
* @param note - Note to serialize
|
|
860
|
+
* @param pretty - Whether to format with indentation (default: false)
|
|
861
|
+
* @returns JSON string
|
|
862
|
+
*
|
|
863
|
+
* @example
|
|
864
|
+
* ```typescript
|
|
865
|
+
* const json = serializeNote(note, true);
|
|
866
|
+
* console.log(json);
|
|
867
|
+
* // Or save to file, copy to clipboard, etc.
|
|
868
|
+
* ```
|
|
869
|
+
*/
|
|
870
|
+
declare function serializeNote(note: CloakNote, pretty?: boolean): string;
|
|
871
|
+
/**
|
|
872
|
+
* Export note as downloadable JSON (browser only)
|
|
873
|
+
*
|
|
874
|
+
* @param note - Note to export
|
|
875
|
+
* @param filename - Optional custom filename
|
|
876
|
+
*/
|
|
877
|
+
declare function downloadNote(note: CloakNote, filename?: string): void;
|
|
878
|
+
/**
|
|
879
|
+
* Copy note to clipboard as JSON (browser only)
|
|
880
|
+
*
|
|
881
|
+
* @param note - Note to copy
|
|
882
|
+
* @returns Promise that resolves when copied
|
|
883
|
+
*/
|
|
884
|
+
declare function copyNoteToClipboard(note: CloakNote): Promise<void>;
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* Note Manager
|
|
888
|
+
*
|
|
889
|
+
* Standalone note management - no browser dependencies.
|
|
890
|
+
* Storage is handled externally via StorageAdapter.
|
|
891
|
+
*
|
|
892
|
+
* Core functionality:
|
|
893
|
+
* - Generate notes (v1.0 and v2.0)
|
|
894
|
+
* - Parse and validate notes
|
|
895
|
+
* - Note utilities (formatting, fees, etc.)
|
|
896
|
+
* - Key management (without storage)
|
|
897
|
+
*/
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* Generate a new note without wallet keys (legacy v1.0)
|
|
901
|
+
* Uses Poseidon hashing to match circuit implementation
|
|
902
|
+
* @deprecated Use generateNoteFromWallet instead for enhanced security
|
|
903
|
+
*/
|
|
904
|
+
declare function generateNote(amountLamports: number, network?: Network): Promise<CloakNote>;
|
|
905
|
+
/**
|
|
906
|
+
* Generate a note using wallet's spend key (v2.0 recommended)
|
|
907
|
+
* Uses Poseidon hashing to match circuit implementation
|
|
908
|
+
*/
|
|
909
|
+
declare function generateNoteFromWallet(amountLamports: number, keys: CloakKeyPair, network?: Network): Promise<CloakNote>;
|
|
910
|
+
/**
|
|
911
|
+
* Parse and validate a note from JSON string
|
|
912
|
+
*/
|
|
913
|
+
declare function parseNote(jsonString: string): CloakNote;
|
|
914
|
+
/**
|
|
915
|
+
* Export note to JSON string
|
|
916
|
+
*/
|
|
917
|
+
declare function exportNote(note: CloakNote, pretty?: boolean): string;
|
|
918
|
+
/**
|
|
919
|
+
* Check if a note is withdrawable (has been deposited)
|
|
920
|
+
*/
|
|
921
|
+
declare function isWithdrawable(note: CloakNote): boolean;
|
|
922
|
+
/**
|
|
923
|
+
* Update note with deposit information
|
|
924
|
+
* Returns a new note object with deposit info added
|
|
925
|
+
*/
|
|
926
|
+
declare function updateNoteWithDeposit(note: CloakNote, depositInfo: {
|
|
927
|
+
signature: string;
|
|
928
|
+
slot: number;
|
|
929
|
+
leafIndex: number;
|
|
930
|
+
root: string;
|
|
931
|
+
merkleProof: {
|
|
932
|
+
pathElements: string[];
|
|
933
|
+
pathIndices: number[];
|
|
934
|
+
};
|
|
935
|
+
}): CloakNote;
|
|
936
|
+
/**
|
|
937
|
+
* Find note by commitment from an array
|
|
938
|
+
*/
|
|
939
|
+
declare function findNoteByCommitment(notes: CloakNote[], commitment: string): CloakNote | undefined;
|
|
940
|
+
/**
|
|
941
|
+
* Filter notes by network
|
|
942
|
+
*/
|
|
943
|
+
declare function filterNotesByNetwork(notes: CloakNote[], network: Network): CloakNote[];
|
|
944
|
+
/**
|
|
945
|
+
* Filter notes that can be withdrawn
|
|
946
|
+
*/
|
|
947
|
+
declare function filterWithdrawableNotes(notes: CloakNote[]): CloakNote[];
|
|
948
|
+
/**
|
|
949
|
+
* Export keys to JSON string
|
|
950
|
+
* WARNING: This exports secret keys! Store securely.
|
|
951
|
+
*/
|
|
952
|
+
declare function exportWalletKeys(keys: CloakKeyPair): string;
|
|
953
|
+
/**
|
|
954
|
+
* Import keys from JSON string
|
|
955
|
+
*/
|
|
956
|
+
declare function importWalletKeys(keysJson: string): CloakKeyPair;
|
|
957
|
+
/**
|
|
958
|
+
* Get public view key from keys
|
|
959
|
+
*/
|
|
960
|
+
declare function getPublicViewKey(keys: CloakKeyPair): string;
|
|
961
|
+
/**
|
|
962
|
+
* Get view key from keys
|
|
963
|
+
*/
|
|
964
|
+
declare function getViewKey(keys: CloakKeyPair): ViewKey;
|
|
965
|
+
/**
|
|
966
|
+
* Get recipient amount after fees
|
|
967
|
+
*/
|
|
968
|
+
declare function getRecipientAmount(amountLamports: number): number;
|
|
969
|
+
|
|
970
|
+
/**
|
|
971
|
+
* Compute Poseidon hash of field elements
|
|
972
|
+
* Uses circomlibjs Poseidon which matches the circuit implementation
|
|
973
|
+
*/
|
|
974
|
+
declare function poseidonHash(inputs: bigint[]): Promise<bigint>;
|
|
975
|
+
/**
|
|
976
|
+
* Split a 256-bit value into two 128-bit limbs
|
|
977
|
+
* Returns [lo, hi] where lo is the lower 128 bits
|
|
978
|
+
*/
|
|
979
|
+
declare function splitTo2Limbs(value: bigint): [bigint, bigint];
|
|
980
|
+
/**
|
|
981
|
+
* Convert a PublicKey (32 bytes) to two 128-bit limbs
|
|
982
|
+
*/
|
|
983
|
+
declare function pubkeyToLimbs(pubkey: Uint8Array | PublicKey): [bigint, bigint];
|
|
984
|
+
/**
|
|
985
|
+
* Compute Merkle root from leaf and path
|
|
986
|
+
*
|
|
987
|
+
* This matches the test implementation
|
|
988
|
+
*
|
|
989
|
+
* @param leaf - Leaf value as bigint
|
|
990
|
+
* @param pathElements - Sibling hashes along the path
|
|
991
|
+
* @param pathIndices - Path directions (0 = left, 1 = right)
|
|
992
|
+
* @returns Merkle root as bigint
|
|
993
|
+
*/
|
|
994
|
+
declare function computeMerkleRoot(leaf: bigint, pathElements: bigint[], pathIndices: number[]): Promise<bigint>;
|
|
995
|
+
/**
|
|
996
|
+
* Convert hex string to bigint
|
|
997
|
+
*/
|
|
998
|
+
declare function hexToBigint(hex: string): bigint;
|
|
999
|
+
/**
|
|
1000
|
+
* Compute commitment = Poseidon(amount, r0, r1, pk_spend)
|
|
1001
|
+
* where pk_spend = Poseidon(sk0, sk1)
|
|
1002
|
+
* (matching withdraw_regular.circom)
|
|
1003
|
+
*
|
|
1004
|
+
* This is the test-style function that takes bigints directly
|
|
1005
|
+
*
|
|
1006
|
+
* @param amount - Amount as bigint
|
|
1007
|
+
* @param r - Randomness as bigint
|
|
1008
|
+
* @param sk_spend - Spending secret key as bigint
|
|
1009
|
+
* @returns Commitment hash as bigint
|
|
1010
|
+
*/
|
|
1011
|
+
declare function computeCommitment(amount: bigint, r: bigint, sk_spend: bigint): Promise<bigint>;
|
|
1012
|
+
/**
|
|
1013
|
+
* Generate a Poseidon commitment for a note
|
|
1014
|
+
*
|
|
1015
|
+
* Formula: Poseidon(amount, r0, r1, pk_spend)
|
|
1016
|
+
* where pk_spend = Poseidon(sk0, sk1)
|
|
1017
|
+
*
|
|
1018
|
+
* This matches the withdraw_regular.circom circuit
|
|
1019
|
+
*
|
|
1020
|
+
* @param amountLamports - Amount in lamports
|
|
1021
|
+
* @param r - Randomness bytes (32 bytes)
|
|
1022
|
+
* @param skSpend - Spending secret key bytes (32 bytes)
|
|
1023
|
+
* @returns Commitment hash as bigint
|
|
1024
|
+
*/
|
|
1025
|
+
declare function generateCommitmentAsync(amountLamports: number, r: Uint8Array, skSpend: Uint8Array): Promise<bigint>;
|
|
1026
|
+
/**
|
|
1027
|
+
* Generate a Poseidon commitment for a note (sync wrapper)
|
|
1028
|
+
* Returns bytes instead of bigint for backward compatibility
|
|
1029
|
+
*
|
|
1030
|
+
* @deprecated Use generateCommitmentAsync instead
|
|
1031
|
+
*/
|
|
1032
|
+
declare function generateCommitment(_amountLamports: number, _r: Uint8Array, _skSpend: Uint8Array): Uint8Array;
|
|
1033
|
+
/**
|
|
1034
|
+
* Compute nullifier = Poseidon(sk0, sk1, leaf_index)
|
|
1035
|
+
* (matching withdraw_regular.circom)
|
|
1036
|
+
*
|
|
1037
|
+
* This is the test-style function that takes bigint directly
|
|
1038
|
+
*
|
|
1039
|
+
* @param sk_spend - Spending secret key as bigint
|
|
1040
|
+
* @param leafIndex - Index in the Merkle tree as bigint
|
|
1041
|
+
* @returns Nullifier as bigint
|
|
1042
|
+
*/
|
|
1043
|
+
declare function computeNullifier(sk_spend: bigint, leafIndex: bigint): Promise<bigint>;
|
|
1044
|
+
/**
|
|
1045
|
+
* Compute nullifier from spending key and leaf index
|
|
1046
|
+
*
|
|
1047
|
+
* Formula: Poseidon(sk0, sk1, leaf_index)
|
|
1048
|
+
*
|
|
1049
|
+
* This matches the circuit's nullifier computation
|
|
1050
|
+
*
|
|
1051
|
+
* @param skSpend - Spending secret key bytes (32 bytes) or hex string
|
|
1052
|
+
* @param leafIndex - Index in the Merkle tree
|
|
1053
|
+
* @returns Nullifier as bigint
|
|
1054
|
+
*/
|
|
1055
|
+
declare function computeNullifierAsync(skSpend: Uint8Array | string, leafIndex: number): Promise<bigint>;
|
|
1056
|
+
/**
|
|
1057
|
+
* Compute nullifier (sync wrapper for backward compatibility)
|
|
1058
|
+
* @deprecated Use computeNullifierAsync instead
|
|
1059
|
+
*/
|
|
1060
|
+
declare function computeNullifierSync(_skSpend: Uint8Array, _leafIndex: number): Uint8Array;
|
|
1061
|
+
/**
|
|
1062
|
+
* Compute outputs hash from recipients and amounts
|
|
1063
|
+
*
|
|
1064
|
+
* Formula: Chain of Poseidon(prev_hash, addr_lo, addr_hi, amount) for each active output
|
|
1065
|
+
*
|
|
1066
|
+
* This matches the withdraw_regular.circom circuit's outputs hash computation
|
|
1067
|
+
*
|
|
1068
|
+
* @param outputs - Array of {recipient: PublicKey, amount: number}
|
|
1069
|
+
* @returns Outputs hash as bigint
|
|
1070
|
+
*/
|
|
1071
|
+
declare function computeOutputsHashAsync(outputs: Array<{
|
|
1072
|
+
recipient: PublicKey;
|
|
1073
|
+
amount: number;
|
|
1074
|
+
}>): Promise<bigint>;
|
|
1075
|
+
/**
|
|
1076
|
+
* Compute outputs hash for withdraw_regular circuit
|
|
1077
|
+
* outputs_hash = chain of Poseidon(prev_hash, addr_lo, addr_hi, amount) for each active output
|
|
1078
|
+
*
|
|
1079
|
+
* This is the test-style function that takes raw limbs
|
|
1080
|
+
*
|
|
1081
|
+
* @param outAddr - Array of [lo, hi] limb pairs for each address (5x2 array)
|
|
1082
|
+
* @param outAmount - Array of amounts as bigints (5 elements)
|
|
1083
|
+
* @param outFlags - Array of flags (1 = active, 0 = inactive) (5 elements)
|
|
1084
|
+
* @returns Outputs hash as bigint
|
|
1085
|
+
*/
|
|
1086
|
+
declare function computeOutputsHash(outAddr: bigint[][], outAmount: bigint[], outFlags: number[]): Promise<bigint>;
|
|
1087
|
+
/**
|
|
1088
|
+
* Compute outputs hash (sync wrapper for backward compatibility)
|
|
1089
|
+
* @deprecated Use computeOutputsHashAsync instead
|
|
1090
|
+
*/
|
|
1091
|
+
declare function computeOutputsHashSync(_outputs: Array<{
|
|
1092
|
+
recipient: PublicKey;
|
|
1093
|
+
amount: number;
|
|
1094
|
+
}>): Uint8Array;
|
|
1095
|
+
/**
|
|
1096
|
+
* Compute outputs hash for withdraw_swap circuit
|
|
1097
|
+
* outputs_hash = Poseidon(input_mint limbs, output_mint limbs, recipient_ata limbs, min_output_amount, public_amount)
|
|
1098
|
+
*
|
|
1099
|
+
* This is the test-style function that takes raw limbs
|
|
1100
|
+
*
|
|
1101
|
+
* @param inputMintLimbs - Input mint address as [lo, hi] limbs
|
|
1102
|
+
* @param outputMintLimbs - Output mint address as [lo, hi] limbs
|
|
1103
|
+
* @param recipientAtaLimbs - Recipient ATA as [lo, hi] limbs
|
|
1104
|
+
* @param minOutputAmount - Minimum output amount as bigint
|
|
1105
|
+
* @param publicAmount - Public amount as bigint
|
|
1106
|
+
* @returns Outputs hash as bigint
|
|
1107
|
+
*/
|
|
1108
|
+
declare function computeSwapOutputsHash(inputMintLimbs: [bigint, bigint], outputMintLimbs: [bigint, bigint], recipientAtaLimbs: [bigint, bigint], minOutputAmount: bigint, publicAmount: bigint): Promise<bigint>;
|
|
1109
|
+
/**
|
|
1110
|
+
* Compute outputs hash for swap transactions
|
|
1111
|
+
*
|
|
1112
|
+
* Formula: Poseidon(input_mint_lo, input_mint_hi, output_mint_lo, output_mint_hi,
|
|
1113
|
+
* recipient_ata_lo, recipient_ata_hi, min_output_amount, public_amount)
|
|
1114
|
+
*
|
|
1115
|
+
* This matches the withdraw_swap.circom circuit
|
|
1116
|
+
*
|
|
1117
|
+
* @param inputMint - Input token mint address (SOL = SystemProgram)
|
|
1118
|
+
* @param outputMint - Output token mint address
|
|
1119
|
+
* @param recipientAta - Recipient's associated token account
|
|
1120
|
+
* @param minOutputAmount - Minimum output amount in token's smallest unit
|
|
1121
|
+
* @param amount - Note amount in lamports (public_amount)
|
|
1122
|
+
* @returns Outputs hash as bigint
|
|
1123
|
+
*/
|
|
1124
|
+
declare function computeSwapOutputsHashAsync(inputMint: PublicKey, outputMint: PublicKey, recipientAta: PublicKey, minOutputAmount: number, amount: number): Promise<bigint>;
|
|
1125
|
+
/**
|
|
1126
|
+
* Compute swap outputs hash (sync wrapper for backward compatibility)
|
|
1127
|
+
* @deprecated Use computeSwapOutputsHashAsync instead
|
|
1128
|
+
*/
|
|
1129
|
+
declare function computeSwapOutputsHashSync(_outputMint: PublicKey, _recipientAta: PublicKey, _minOutputAmount: number, _amount: number): Uint8Array;
|
|
1130
|
+
/**
|
|
1131
|
+
* Convert bigint to 32-byte big-endian Uint8Array
|
|
1132
|
+
*/
|
|
1133
|
+
declare function bigintToBytes32(n: bigint): Uint8Array;
|
|
1134
|
+
/**
|
|
1135
|
+
* Convert hex string to Uint8Array
|
|
1136
|
+
*
|
|
1137
|
+
* @param hex - Hex string (with or without 0x prefix)
|
|
1138
|
+
* @returns Decoded bytes
|
|
1139
|
+
*/
|
|
1140
|
+
declare function hexToBytes(hex: string): Uint8Array;
|
|
1141
|
+
/**
|
|
1142
|
+
* Convert Uint8Array to hex string
|
|
1143
|
+
*
|
|
1144
|
+
* @param bytes - Bytes to encode
|
|
1145
|
+
* @param prefix - Whether to include 0x prefix (default: false)
|
|
1146
|
+
* @returns Hex-encoded string
|
|
1147
|
+
*/
|
|
1148
|
+
declare function bytesToHex(bytes: Uint8Array, prefix?: boolean): string;
|
|
1149
|
+
/**
|
|
1150
|
+
* Generate random bytes using Web Crypto API
|
|
1151
|
+
*
|
|
1152
|
+
* @param length - Number of bytes to generate
|
|
1153
|
+
* @returns Random bytes
|
|
1154
|
+
*/
|
|
1155
|
+
declare function randomBytes(length: number): Uint8Array;
|
|
1156
|
+
/**
|
|
1157
|
+
* Validate a hex string format
|
|
1158
|
+
*
|
|
1159
|
+
* @param hex - Hex string to validate
|
|
1160
|
+
* @param expectedLength - Expected length in bytes (optional)
|
|
1161
|
+
* @returns True if valid hex string
|
|
1162
|
+
*/
|
|
1163
|
+
declare function isValidHex(hex: string, expectedLength?: number): boolean;
|
|
1164
|
+
/**
|
|
1165
|
+
* Groth16 proof structure from snarkjs
|
|
1166
|
+
*/
|
|
1167
|
+
interface Groth16Proof {
|
|
1168
|
+
pi_a: [string, string, string];
|
|
1169
|
+
pi_b: [[string, string], [string, string], [string, string]];
|
|
1170
|
+
pi_c: [string, string, string];
|
|
1171
|
+
protocol: string;
|
|
1172
|
+
curve: string;
|
|
1173
|
+
}
|
|
1174
|
+
/**
|
|
1175
|
+
* Convert snarkjs Groth16 proof to 256-byte format for Solana (pinocchio-groth16 format)
|
|
1176
|
+
*
|
|
1177
|
+
* Based on pinocchio-groth16/src/proof_parser.rs convert_proof function:
|
|
1178
|
+
* - Proof_a: Must be NEGATED (as G1 point) and converted from LE to BE
|
|
1179
|
+
* - Proof_b: Converted from LE to BE (reversing each 64-byte chunk)
|
|
1180
|
+
* - Proof_c: Converted from LE to BE (reversing each 32-byte chunk)
|
|
1181
|
+
*
|
|
1182
|
+
* Format: pi_a (64) + pi_b (128) + pi_c (64) = 256 bytes
|
|
1183
|
+
*/
|
|
1184
|
+
declare function proofToBytes(proof: Groth16Proof): Uint8Array;
|
|
1185
|
+
/**
|
|
1186
|
+
* Build public inputs bytes for on-chain verification
|
|
1187
|
+
* Format: root (32) + nullifier (32) + outputs_hash (32) + public_amount (8) = 104 bytes
|
|
1188
|
+
*/
|
|
1189
|
+
declare function buildPublicInputsBytes(root: bigint, nullifier: bigint, outputsHash: bigint, publicAmount: bigint): Uint8Array;
|
|
1190
|
+
|
|
1191
|
+
/**
|
|
1192
|
+
* Fee calculation utilities for Cloak Protocol
|
|
1193
|
+
*
|
|
1194
|
+
* The protocol charges a fixed fee plus a variable percentage fee
|
|
1195
|
+
* to prevent sybil attacks and cover operational costs.
|
|
1196
|
+
*/
|
|
1197
|
+
/** Fixed fee: 0.0025 SOL (2.5M lamports) */
|
|
1198
|
+
declare const FIXED_FEE_LAMPORTS = 2500000;
|
|
1199
|
+
/** Variable fee rate: 0.5% (5 basis points per 1000) */
|
|
1200
|
+
declare const VARIABLE_FEE_RATE: number;
|
|
1201
|
+
/** Lamports per SOL */
|
|
1202
|
+
declare const LAMPORTS_PER_SOL = 1000000000;
|
|
1203
|
+
/**
|
|
1204
|
+
* Calculate the total protocol fee for a given amount
|
|
1205
|
+
*
|
|
1206
|
+
* Formula: FIXED_FEE + floor((amount * 5) / 1000)
|
|
1207
|
+
*
|
|
1208
|
+
* @param amountLamports - Amount in lamports
|
|
1209
|
+
* @returns Total fee in lamports
|
|
1210
|
+
*
|
|
1211
|
+
* @example
|
|
1212
|
+
* ```typescript
|
|
1213
|
+
* const fee = calculateFee(1_000_000_000); // 1 SOL
|
|
1214
|
+
* // Returns: 2_500_000 (fixed) + 5_000_000 (0.5%) = 7_500_000 lamports
|
|
1215
|
+
* ```
|
|
1216
|
+
*/
|
|
1217
|
+
declare function calculateFee(amountLamports: number): number;
|
|
1218
|
+
/**
|
|
1219
|
+
* Calculate the distributable amount after protocol fees
|
|
1220
|
+
*
|
|
1221
|
+
* This is the amount available to send to recipients.
|
|
1222
|
+
*
|
|
1223
|
+
* @param amountLamports - Total note amount in lamports
|
|
1224
|
+
* @returns Amount available for recipients in lamports
|
|
1225
|
+
*
|
|
1226
|
+
* @example
|
|
1227
|
+
* ```typescript
|
|
1228
|
+
* const distributable = getDistributableAmount(1_000_000_000);
|
|
1229
|
+
* // Returns: 1_000_000_000 - 7_500_000 = 992_500_000 lamports
|
|
1230
|
+
* ```
|
|
1231
|
+
*/
|
|
1232
|
+
declare function getDistributableAmount(amountLamports: number): number;
|
|
1233
|
+
/**
|
|
1234
|
+
* Format lamports as SOL string
|
|
1235
|
+
*
|
|
1236
|
+
* @param lamports - Amount in lamports
|
|
1237
|
+
* @param decimals - Number of decimal places (default: 9)
|
|
1238
|
+
* @returns Formatted string (e.g., "1.000000000")
|
|
1239
|
+
*
|
|
1240
|
+
* @example
|
|
1241
|
+
* ```typescript
|
|
1242
|
+
* formatAmount(1_000_000_000); // "1.000000000"
|
|
1243
|
+
* formatAmount(1_500_000_000); // "1.500000000"
|
|
1244
|
+
* formatAmount(123_456_789, 4); // "0.1235"
|
|
1245
|
+
* ```
|
|
1246
|
+
*/
|
|
1247
|
+
declare function formatAmount(lamports: number, decimals?: number): string;
|
|
1248
|
+
/**
|
|
1249
|
+
* Parse SOL string to lamports
|
|
1250
|
+
*
|
|
1251
|
+
* @param sol - SOL amount as string (e.g., "1.5")
|
|
1252
|
+
* @returns Amount in lamports
|
|
1253
|
+
* @throws Error if invalid format
|
|
1254
|
+
*
|
|
1255
|
+
* @example
|
|
1256
|
+
* ```typescript
|
|
1257
|
+
* parseAmount("1.5"); // 1_500_000_000
|
|
1258
|
+
* parseAmount("0.001"); // 1_000_000
|
|
1259
|
+
* ```
|
|
1260
|
+
*/
|
|
1261
|
+
declare function parseAmount(sol: string): number;
|
|
1262
|
+
/**
|
|
1263
|
+
* Validate that outputs sum equals expected amount
|
|
1264
|
+
*
|
|
1265
|
+
* @param outputs - Array of output amounts
|
|
1266
|
+
* @param expectedTotal - Expected total amount
|
|
1267
|
+
* @returns True if amounts match
|
|
1268
|
+
*/
|
|
1269
|
+
declare function validateOutputsSum(outputs: Array<{
|
|
1270
|
+
amount: number;
|
|
1271
|
+
}>, expectedTotal: number): boolean;
|
|
1272
|
+
/**
|
|
1273
|
+
* Calculate relay fee from basis points
|
|
1274
|
+
*
|
|
1275
|
+
* @param amountLamports - Amount in lamports
|
|
1276
|
+
* @param feeBps - Fee in basis points (100 bps = 1%)
|
|
1277
|
+
* @returns Relay fee in lamports
|
|
1278
|
+
*
|
|
1279
|
+
* @example
|
|
1280
|
+
* ```typescript
|
|
1281
|
+
* calculateRelayFee(1_000_000_000, 50); // 0.5% = 5_000_000 lamports
|
|
1282
|
+
* ```
|
|
1283
|
+
*/
|
|
1284
|
+
declare function calculateRelayFee(amountLamports: number, feeBps: number): number;
|
|
1285
|
+
|
|
1286
|
+
/**
|
|
1287
|
+
* Validate a Solana public key
|
|
1288
|
+
*
|
|
1289
|
+
* @param address - Address string to validate
|
|
1290
|
+
* @returns True if valid Solana address
|
|
1291
|
+
*/
|
|
1292
|
+
declare function isValidSolanaAddress(address: string): boolean;
|
|
1293
|
+
/**
|
|
1294
|
+
* Validate a Cloak note structure
|
|
1295
|
+
*
|
|
1296
|
+
* @param note - Note object to validate
|
|
1297
|
+
* @throws Error if invalid
|
|
1298
|
+
*/
|
|
1299
|
+
declare function validateNote(note: any): asserts note is CloakNote;
|
|
1300
|
+
/**
|
|
1301
|
+
* Validate that a note is ready for withdrawal
|
|
1302
|
+
*
|
|
1303
|
+
* @param note - Note to validate
|
|
1304
|
+
* @throws Error if note cannot be used for withdrawal
|
|
1305
|
+
*/
|
|
1306
|
+
declare function validateWithdrawableNote(note: CloakNote): void;
|
|
1307
|
+
/**
|
|
1308
|
+
* Validate transfer recipients
|
|
1309
|
+
*
|
|
1310
|
+
* @param recipients - Array of transfers to validate
|
|
1311
|
+
* @param totalAmount - Total amount available
|
|
1312
|
+
* @throws Error if invalid
|
|
1313
|
+
*/
|
|
1314
|
+
declare function validateTransfers(recipients: Array<{
|
|
1315
|
+
recipient: PublicKey;
|
|
1316
|
+
amount: number;
|
|
1317
|
+
}>, totalAmount: number): void;
|
|
1318
|
+
|
|
1319
|
+
/**
|
|
1320
|
+
* Network Utilities
|
|
1321
|
+
*
|
|
1322
|
+
* Helper functions for network detection and configuration
|
|
1323
|
+
*/
|
|
1324
|
+
|
|
1325
|
+
/**
|
|
1326
|
+
* Detect network from RPC URL
|
|
1327
|
+
*
|
|
1328
|
+
* Attempts to detect the Solana network from common RPC URL patterns.
|
|
1329
|
+
* Falls back to devnet if unable to detect.
|
|
1330
|
+
*/
|
|
1331
|
+
declare function detectNetworkFromRpcUrl(rpcUrl?: string): Network;
|
|
1332
|
+
/**
|
|
1333
|
+
* Get the standard RPC URL for a network
|
|
1334
|
+
*/
|
|
1335
|
+
declare function getRpcUrlForNetwork(network: Network): string;
|
|
1336
|
+
/**
|
|
1337
|
+
* Validate RPC URL format
|
|
1338
|
+
*/
|
|
1339
|
+
declare function isValidRpcUrl(url: string): boolean;
|
|
1340
|
+
/**
|
|
1341
|
+
* Get explorer URL for a transaction
|
|
1342
|
+
*/
|
|
1343
|
+
declare function getExplorerUrl(signature: string, network?: Network): string;
|
|
1344
|
+
/**
|
|
1345
|
+
* Get explorer URL for an address
|
|
1346
|
+
*/
|
|
1347
|
+
declare function getAddressExplorerUrl(address: string, network?: Network): string;
|
|
1348
|
+
|
|
1349
|
+
/**
|
|
1350
|
+
* Error Utilities
|
|
1351
|
+
*
|
|
1352
|
+
* Helper functions for parsing and presenting user-friendly error messages
|
|
1353
|
+
*/
|
|
1354
|
+
|
|
1355
|
+
/**
|
|
1356
|
+
* Parse transaction error and return user-friendly message
|
|
1357
|
+
*
|
|
1358
|
+
* Attempts to extract meaningful error information from various
|
|
1359
|
+
* error formats (program errors, RPC errors, custom errors)
|
|
1360
|
+
*/
|
|
1361
|
+
declare function parseTransactionError(error: any): string;
|
|
1362
|
+
/**
|
|
1363
|
+
* Create a CloakError with appropriate categorization
|
|
1364
|
+
*/
|
|
1365
|
+
declare function createCloakError(error: unknown, _context: string): CloakError;
|
|
1366
|
+
/**
|
|
1367
|
+
* Format error for logging
|
|
1368
|
+
*/
|
|
1369
|
+
declare function formatErrorForLogging(error: unknown): string;
|
|
1370
|
+
|
|
1371
|
+
/**
|
|
1372
|
+
* Response from notes range query
|
|
1373
|
+
*/
|
|
1374
|
+
interface NotesRangeResponse {
|
|
1375
|
+
notes: string[];
|
|
1376
|
+
has_more: boolean;
|
|
1377
|
+
total: number;
|
|
1378
|
+
start: number;
|
|
1379
|
+
end: number;
|
|
1380
|
+
}
|
|
1381
|
+
/**
|
|
1382
|
+
* Indexer Service Client
|
|
1383
|
+
*
|
|
1384
|
+
* Provides access to the Cloak Indexer API for querying the Merkle tree
|
|
1385
|
+
* and registering deposits.
|
|
1386
|
+
*/
|
|
1387
|
+
declare class IndexerService {
|
|
1388
|
+
private baseUrl;
|
|
1389
|
+
/**
|
|
1390
|
+
* Create a new Indexer Service client
|
|
1391
|
+
*
|
|
1392
|
+
* @param baseUrl - Indexer API base URL
|
|
1393
|
+
*/
|
|
1394
|
+
constructor(baseUrl: string);
|
|
1395
|
+
/**
|
|
1396
|
+
* Get current Merkle root and next available index
|
|
1397
|
+
*
|
|
1398
|
+
* @returns Current root and next index
|
|
1399
|
+
*
|
|
1400
|
+
* @example
|
|
1401
|
+
* ```typescript
|
|
1402
|
+
* const { root, next_index } = await indexer.getMerkleRoot();
|
|
1403
|
+
* console.log(`Current root: ${root}, Next index: ${next_index}`);
|
|
1404
|
+
* ```
|
|
1405
|
+
*/
|
|
1406
|
+
getMerkleRoot(): Promise<MerkleRootResponse>;
|
|
1407
|
+
/**
|
|
1408
|
+
* Get Merkle proof for a specific leaf
|
|
1409
|
+
*
|
|
1410
|
+
* @param leafIndex - Index of the leaf in the tree
|
|
1411
|
+
* @returns Merkle proof with path elements and indices
|
|
1412
|
+
*
|
|
1413
|
+
* @example
|
|
1414
|
+
* ```typescript
|
|
1415
|
+
* const proof = await indexer.getMerkleProof(42);
|
|
1416
|
+
* console.log(`Proof has ${proof.pathElements.length} siblings`);
|
|
1417
|
+
* ```
|
|
1418
|
+
*/
|
|
1419
|
+
getMerkleProof(leafIndex: number): Promise<MerkleProof>;
|
|
1420
|
+
/**
|
|
1421
|
+
* Get notes in a specific range
|
|
1422
|
+
*
|
|
1423
|
+
* Useful for scanning the tree or fetching notes in batches.
|
|
1424
|
+
*
|
|
1425
|
+
* @param start - Start index (inclusive)
|
|
1426
|
+
* @param end - End index (inclusive)
|
|
1427
|
+
* @param limit - Maximum number of notes to return (default: 100)
|
|
1428
|
+
* @returns Notes in the range
|
|
1429
|
+
*
|
|
1430
|
+
* @example
|
|
1431
|
+
* ```typescript
|
|
1432
|
+
* const { notes, has_more } = await indexer.getNotesRange(0, 99, 100);
|
|
1433
|
+
* console.log(`Fetched ${notes.length} notes`);
|
|
1434
|
+
* ```
|
|
1435
|
+
*/
|
|
1436
|
+
getNotesRange(start: number, end: number, limit?: number): Promise<NotesRangeResponse>;
|
|
1437
|
+
/**
|
|
1438
|
+
* Get all notes from the tree
|
|
1439
|
+
*
|
|
1440
|
+
* Fetches all notes in batches. Use with caution for large trees.
|
|
1441
|
+
*
|
|
1442
|
+
* @param batchSize - Size of each batch (default: 100)
|
|
1443
|
+
* @returns All encrypted notes
|
|
1444
|
+
*
|
|
1445
|
+
* @example
|
|
1446
|
+
* ```typescript
|
|
1447
|
+
* const allNotes = await indexer.getAllNotes();
|
|
1448
|
+
* console.log(`Total notes: ${allNotes.length}`);
|
|
1449
|
+
* ```
|
|
1450
|
+
*/
|
|
1451
|
+
getAllNotes(batchSize?: number): Promise<string[]>;
|
|
1452
|
+
/**
|
|
1453
|
+
* Submit a deposit to the indexer
|
|
1454
|
+
*
|
|
1455
|
+
* Registers a new deposit transaction with the indexer, which will
|
|
1456
|
+
* return the leaf index and current root.
|
|
1457
|
+
*
|
|
1458
|
+
* @param params - Deposit parameters
|
|
1459
|
+
* @returns Success response with leaf index and root
|
|
1460
|
+
*
|
|
1461
|
+
* @example
|
|
1462
|
+
* ```typescript
|
|
1463
|
+
* const result = await indexer.submitDeposit({
|
|
1464
|
+
* leafCommit: note.commitment,
|
|
1465
|
+
* encryptedOutput: btoa(JSON.stringify(noteData)),
|
|
1466
|
+
* txSignature: signature,
|
|
1467
|
+
* slot: txSlot
|
|
1468
|
+
* });
|
|
1469
|
+
* console.log(`Leaf index: ${result.leafIndex}`);
|
|
1470
|
+
* ```
|
|
1471
|
+
*/
|
|
1472
|
+
submitDeposit(params: {
|
|
1473
|
+
leafCommit: string;
|
|
1474
|
+
encryptedOutput: string;
|
|
1475
|
+
txSignature: string;
|
|
1476
|
+
slot: number;
|
|
1477
|
+
}): Promise<{
|
|
1478
|
+
success: boolean;
|
|
1479
|
+
leafIndex?: number;
|
|
1480
|
+
root?: string;
|
|
1481
|
+
}>;
|
|
1482
|
+
/**
|
|
1483
|
+
* Check indexer health
|
|
1484
|
+
*
|
|
1485
|
+
* @returns Health status
|
|
1486
|
+
*/
|
|
1487
|
+
healthCheck(): Promise<{
|
|
1488
|
+
status: string;
|
|
1489
|
+
}>;
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
/**
|
|
1493
|
+
* Options for artifact-based proof generation
|
|
1494
|
+
*/
|
|
1495
|
+
interface ArtifactProofGenerationOptions {
|
|
1496
|
+
/** Progress callback - called with percentage (0-100) */
|
|
1497
|
+
onProgress?: (progress: number) => void;
|
|
1498
|
+
/** Called when proof generation starts */
|
|
1499
|
+
onStart?: () => void;
|
|
1500
|
+
/** Called on successful proof generation */
|
|
1501
|
+
onSuccess?: (result: SP1ProofResult) => void;
|
|
1502
|
+
/** Called on error */
|
|
1503
|
+
onError?: (error: string) => void;
|
|
1504
|
+
/** Custom timeout in milliseconds */
|
|
1505
|
+
timeout?: number;
|
|
1506
|
+
/** Polling interval for proof status (default: 2000ms) */
|
|
1507
|
+
pollInterval?: number;
|
|
1508
|
+
}
|
|
1509
|
+
/**
|
|
1510
|
+
* Artifact-based Prover Service
|
|
1511
|
+
*
|
|
1512
|
+
* Implements the artifact-based proof generation flow where private inputs
|
|
1513
|
+
* are uploaded directly to the TEE, never passing through the backend in plaintext.
|
|
1514
|
+
*
|
|
1515
|
+
* Flow:
|
|
1516
|
+
* 1. Create artifact → get artifact_id and upload_url
|
|
1517
|
+
* 2. Upload stdin directly to TEE (bypassing backend)
|
|
1518
|
+
* 3. Request proof generation (backend only gets artifact_id)
|
|
1519
|
+
* 4. Poll for proof status until ready
|
|
1520
|
+
*
|
|
1521
|
+
* ✅ PRIVACY: Private inputs never pass through backend in plaintext
|
|
1522
|
+
*/
|
|
1523
|
+
declare class ArtifactProverService {
|
|
1524
|
+
private indexerUrl;
|
|
1525
|
+
private timeout;
|
|
1526
|
+
private pollInterval;
|
|
1527
|
+
/**
|
|
1528
|
+
* Create a new Artifact Prover Service client
|
|
1529
|
+
*
|
|
1530
|
+
* @param indexerUrl - Indexer service base URL
|
|
1531
|
+
* @param timeout - Proof generation timeout in ms (default: 5 minutes)
|
|
1532
|
+
* @param pollInterval - Polling interval for status checks (default: 2 seconds)
|
|
1533
|
+
*/
|
|
1534
|
+
constructor(indexerUrl: string, timeout?: number, pollInterval?: number);
|
|
1535
|
+
/**
|
|
1536
|
+
* Generate a zero-knowledge proof using artifact-based flow
|
|
1537
|
+
*
|
|
1538
|
+
* This process typically takes 30-180 seconds depending on the TEE.
|
|
1539
|
+
* Private inputs are uploaded directly to TEE, never passing through backend.
|
|
1540
|
+
*
|
|
1541
|
+
* @param inputs - Circuit inputs (private + public + outputs)
|
|
1542
|
+
* @param options - Optional progress tracking and callbacks
|
|
1543
|
+
* @returns Proof result with hex-encoded proof and public inputs
|
|
1544
|
+
*
|
|
1545
|
+
* @example
|
|
1546
|
+
* ```typescript
|
|
1547
|
+
* const result = await prover.generateProof(inputs);
|
|
1548
|
+
* if (result.success) {
|
|
1549
|
+
* console.log(`Proof: ${result.proof}`);
|
|
1550
|
+
* }
|
|
1551
|
+
* ```
|
|
1552
|
+
*/
|
|
1553
|
+
generateProof(inputs: SP1ProofInputs, options?: ArtifactProofGenerationOptions): Promise<SP1ProofResult>;
|
|
1554
|
+
/**
|
|
1555
|
+
* Check if the artifact prover service is available
|
|
1556
|
+
*
|
|
1557
|
+
* @returns True if service is healthy
|
|
1558
|
+
*/
|
|
1559
|
+
healthCheck(): Promise<boolean>;
|
|
1560
|
+
/**
|
|
1561
|
+
* Get the configured timeout
|
|
1562
|
+
*/
|
|
1563
|
+
getTimeout(): number;
|
|
1564
|
+
/**
|
|
1565
|
+
* Set a new timeout
|
|
1566
|
+
*/
|
|
1567
|
+
setTimeout(timeout: number): void;
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
/**
|
|
1571
|
+
* Options for proof generation
|
|
1572
|
+
*/
|
|
1573
|
+
interface ProofGenerationOptions {
|
|
1574
|
+
/** Progress callback - called with percentage (0-100) */
|
|
1575
|
+
onProgress?: (progress: number) => void;
|
|
1576
|
+
/** Called when proof generation starts */
|
|
1577
|
+
onStart?: () => void;
|
|
1578
|
+
/** Called on successful proof generation */
|
|
1579
|
+
onSuccess?: (result: SP1ProofResult) => void;
|
|
1580
|
+
/** Called on error */
|
|
1581
|
+
onError?: (error: string) => void;
|
|
1582
|
+
/** Custom timeout in milliseconds */
|
|
1583
|
+
timeout?: number;
|
|
1584
|
+
}
|
|
1585
|
+
/**
|
|
1586
|
+
* Prover Service Client (Legacy)
|
|
1587
|
+
*
|
|
1588
|
+
* Handles zero-knowledge proof generation via the backend prover service.
|
|
1589
|
+
*
|
|
1590
|
+
* ⚠️ DEPRECATED: This implementation sends private inputs to a backend service.
|
|
1591
|
+
*
|
|
1592
|
+
* **Use ArtifactProverService instead** for privacy-preserving proof generation
|
|
1593
|
+
* where private inputs are uploaded directly to TEE, never passing through backend.
|
|
1594
|
+
*
|
|
1595
|
+
* This class is kept for backward compatibility but is not used by CloakSDK anymore.
|
|
1596
|
+
*/
|
|
1597
|
+
declare class ProverService {
|
|
1598
|
+
private indexerUrl;
|
|
1599
|
+
private timeout;
|
|
1600
|
+
/**
|
|
1601
|
+
* Create a new Prover Service client
|
|
1602
|
+
*
|
|
1603
|
+
* @param indexerUrl - Indexer/Prover service base URL
|
|
1604
|
+
* @param timeout - Proof generation timeout in ms (default: 5 minutes)
|
|
1605
|
+
*/
|
|
1606
|
+
constructor(indexerUrl: string, timeout?: number);
|
|
1607
|
+
/**
|
|
1608
|
+
* Generate a zero-knowledge proof for withdrawal
|
|
1609
|
+
*
|
|
1610
|
+
* This process typically takes 30-180 seconds depending on the backend.
|
|
1611
|
+
*
|
|
1612
|
+
* @param inputs - Circuit inputs (private + public + outputs)
|
|
1613
|
+
* @param options - Optional progress tracking and callbacks
|
|
1614
|
+
* @returns Proof result with hex-encoded proof and public inputs
|
|
1615
|
+
*
|
|
1616
|
+
* @example
|
|
1617
|
+
* ```typescript
|
|
1618
|
+
* const result = await prover.generateProof(inputs);
|
|
1619
|
+
* if (result.success) {
|
|
1620
|
+
* console.log(`Proof: ${result.proof}`);
|
|
1621
|
+
* }
|
|
1622
|
+
* ```
|
|
1623
|
+
*
|
|
1624
|
+
* @example
|
|
1625
|
+
* ```typescript
|
|
1626
|
+
* // With progress tracking
|
|
1627
|
+
* const result = await prover.generateProof(inputs, {
|
|
1628
|
+
* onProgress: (progress) => console.log(`Progress: ${progress}%`),
|
|
1629
|
+
* onStart: () => console.log("Starting proof generation..."),
|
|
1630
|
+
* onSuccess: (result) => console.log("Proof generated!"),
|
|
1631
|
+
* onError: (error) => console.error("Failed:", error)
|
|
1632
|
+
* });
|
|
1633
|
+
* ```
|
|
1634
|
+
*/
|
|
1635
|
+
generateProof(inputs: SP1ProofInputs, options?: ProofGenerationOptions): Promise<SP1ProofResult>;
|
|
1636
|
+
/**
|
|
1637
|
+
* Check if the prover service is available
|
|
1638
|
+
*
|
|
1639
|
+
* @returns True if service is healthy
|
|
1640
|
+
*/
|
|
1641
|
+
healthCheck(): Promise<boolean>;
|
|
1642
|
+
/**
|
|
1643
|
+
* Get the configured timeout
|
|
1644
|
+
*/
|
|
1645
|
+
getTimeout(): number;
|
|
1646
|
+
/**
|
|
1647
|
+
* Set a new timeout
|
|
1648
|
+
*/
|
|
1649
|
+
setTimeout(timeout: number): void;
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
/**
|
|
1653
|
+
* Relay Service Client
|
|
1654
|
+
*
|
|
1655
|
+
* Handles submission of withdrawal transactions through a relay service
|
|
1656
|
+
* that pays for transaction fees and submits the transaction on-chain.
|
|
1657
|
+
*/
|
|
1658
|
+
declare class RelayService {
|
|
1659
|
+
private baseUrl;
|
|
1660
|
+
/**
|
|
1661
|
+
* Create a new Relay Service client
|
|
1662
|
+
*
|
|
1663
|
+
* @param baseUrl - Relay service base URL
|
|
1664
|
+
*/
|
|
1665
|
+
constructor(baseUrl: string);
|
|
1666
|
+
/**
|
|
1667
|
+
* Submit a withdrawal transaction via relay
|
|
1668
|
+
*
|
|
1669
|
+
* The relay service will validate the proof, pay for transaction fees,
|
|
1670
|
+
* and submit the transaction on-chain.
|
|
1671
|
+
*
|
|
1672
|
+
* @param params - Withdrawal parameters
|
|
1673
|
+
* @param onStatusUpdate - Optional callback for status updates
|
|
1674
|
+
* @returns Transaction signature when completed
|
|
1675
|
+
*
|
|
1676
|
+
* @example
|
|
1677
|
+
* ```typescript
|
|
1678
|
+
* const signature = await relay.submitWithdraw({
|
|
1679
|
+
* proof: proofHex,
|
|
1680
|
+
* publicInputs: { root, nf, outputs_hash, amount },
|
|
1681
|
+
* outputs: [{ recipient: addr, amount: lamports }],
|
|
1682
|
+
* feeBps: 50
|
|
1683
|
+
* }, (status) => console.log(`Status: ${status}`));
|
|
1684
|
+
* console.log(`Transaction: ${signature}`);
|
|
1685
|
+
* ```
|
|
1686
|
+
*/
|
|
1687
|
+
submitWithdraw(params: {
|
|
1688
|
+
proof: string;
|
|
1689
|
+
publicInputs: {
|
|
1690
|
+
root: string;
|
|
1691
|
+
nf: string;
|
|
1692
|
+
outputs_hash: string;
|
|
1693
|
+
amount: number;
|
|
1694
|
+
};
|
|
1695
|
+
outputs: Array<{
|
|
1696
|
+
recipient: string;
|
|
1697
|
+
amount: number;
|
|
1698
|
+
}>;
|
|
1699
|
+
feeBps: number;
|
|
1700
|
+
}, onStatusUpdate?: (status: string) => void): Promise<string>;
|
|
1701
|
+
/**
|
|
1702
|
+
* Poll for withdrawal completion
|
|
1703
|
+
*
|
|
1704
|
+
* @param requestId - Request ID from relay service
|
|
1705
|
+
* @param onStatusUpdate - Optional callback for status updates
|
|
1706
|
+
* @returns Transaction signature when completed
|
|
1707
|
+
*/
|
|
1708
|
+
private pollForCompletion;
|
|
1709
|
+
/**
|
|
1710
|
+
* Submit a swap transaction via relay
|
|
1711
|
+
*
|
|
1712
|
+
* Similar to submitWithdraw but includes swap parameters for token swaps.
|
|
1713
|
+
* The relay service will validate the proof, execute the swap, pay for fees,
|
|
1714
|
+
* and submit the transaction on-chain.
|
|
1715
|
+
*
|
|
1716
|
+
* @param params - Swap parameters
|
|
1717
|
+
* @param onStatusUpdate - Optional callback for status updates
|
|
1718
|
+
* @returns Transaction signature when completed
|
|
1719
|
+
*
|
|
1720
|
+
* @example
|
|
1721
|
+
* ```typescript
|
|
1722
|
+
* const signature = await relay.submitSwap({
|
|
1723
|
+
* proof: proofHex,
|
|
1724
|
+
* publicInputs: { root, nf, outputs_hash, amount },
|
|
1725
|
+
* outputs: [{ recipient: addr, amount: lamports }],
|
|
1726
|
+
* feeBps: 50,
|
|
1727
|
+
* swap: {
|
|
1728
|
+
* output_mint: tokenMint.toBase58(),
|
|
1729
|
+
* slippage_bps: 100,
|
|
1730
|
+
* min_output_amount: minAmount
|
|
1731
|
+
* }
|
|
1732
|
+
* }, (status) => console.log(`Status: ${status}`));
|
|
1733
|
+
* console.log(`Transaction: ${signature}`);
|
|
1734
|
+
* ```
|
|
1735
|
+
*/
|
|
1736
|
+
submitSwap(params: {
|
|
1737
|
+
proof: string;
|
|
1738
|
+
publicInputs: {
|
|
1739
|
+
root: string;
|
|
1740
|
+
nf: string;
|
|
1741
|
+
outputs_hash: string;
|
|
1742
|
+
amount: number;
|
|
1743
|
+
};
|
|
1744
|
+
outputs: Array<{
|
|
1745
|
+
recipient: string;
|
|
1746
|
+
amount: number;
|
|
1747
|
+
}>;
|
|
1748
|
+
feeBps: number;
|
|
1749
|
+
swap: {
|
|
1750
|
+
output_mint: string;
|
|
1751
|
+
slippage_bps: number;
|
|
1752
|
+
min_output_amount: number;
|
|
1753
|
+
};
|
|
1754
|
+
}, onStatusUpdate?: (status: string) => void): Promise<string>;
|
|
1755
|
+
/**
|
|
1756
|
+
* Get transaction status
|
|
1757
|
+
*
|
|
1758
|
+
* @param requestId - Request ID from previous submission
|
|
1759
|
+
* @returns Current status
|
|
1760
|
+
*
|
|
1761
|
+
* @example
|
|
1762
|
+
* ```typescript
|
|
1763
|
+
* const status = await relay.getStatus(requestId);
|
|
1764
|
+
* console.log(`Status: ${status.status}`);
|
|
1765
|
+
* if (status.status === 'completed') {
|
|
1766
|
+
* console.log(`TX: ${status.txId}`);
|
|
1767
|
+
* }
|
|
1768
|
+
* ```
|
|
1769
|
+
*/
|
|
1770
|
+
getStatus(requestId: string): Promise<TxStatus>;
|
|
1771
|
+
/**
|
|
1772
|
+
* Convert bytes to base64 string
|
|
1773
|
+
*/
|
|
1774
|
+
private bytesToBase64;
|
|
1775
|
+
/**
|
|
1776
|
+
* Sleep utility
|
|
1777
|
+
*/
|
|
1778
|
+
private sleep;
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1781
|
+
/**
|
|
1782
|
+
* Deposit Recovery Service
|
|
1783
|
+
*
|
|
1784
|
+
* Handles recovery of deposits that completed on-chain but failed
|
|
1785
|
+
* to finalize with the indexer (e.g., browser crash, network failure)
|
|
1786
|
+
*/
|
|
1787
|
+
|
|
1788
|
+
interface RecoveryOptions {
|
|
1789
|
+
/** Transaction signature to recover */
|
|
1790
|
+
signature: string;
|
|
1791
|
+
/** Note commitment hash */
|
|
1792
|
+
commitment: string;
|
|
1793
|
+
/** Optional: The full note if available */
|
|
1794
|
+
note?: CloakNote;
|
|
1795
|
+
/** Callback for progress updates */
|
|
1796
|
+
onProgress?: (status: string) => void;
|
|
1797
|
+
}
|
|
1798
|
+
interface RecoveryResult {
|
|
1799
|
+
success: boolean;
|
|
1800
|
+
leafIndex?: number;
|
|
1801
|
+
root?: string;
|
|
1802
|
+
slot?: number;
|
|
1803
|
+
merkleProof?: {
|
|
1804
|
+
pathElements: string[];
|
|
1805
|
+
pathIndices: number[];
|
|
1806
|
+
};
|
|
1807
|
+
note?: CloakNote;
|
|
1808
|
+
error?: string;
|
|
1809
|
+
}
|
|
1810
|
+
/**
|
|
1811
|
+
* Service for recovering incomplete deposits
|
|
1812
|
+
*/
|
|
1813
|
+
declare class DepositRecoveryService {
|
|
1814
|
+
private indexer;
|
|
1815
|
+
private apiUrl;
|
|
1816
|
+
constructor(indexer: IndexerService, apiUrl: string);
|
|
1817
|
+
/**
|
|
1818
|
+
* Recover a deposit that completed on-chain but failed to register
|
|
1819
|
+
*
|
|
1820
|
+
* @param options Recovery options
|
|
1821
|
+
* @returns Recovery result with updated note
|
|
1822
|
+
*/
|
|
1823
|
+
recoverDeposit(options: RecoveryOptions): Promise<RecoveryResult>;
|
|
1824
|
+
/**
|
|
1825
|
+
* Check if a deposit already exists in the indexer
|
|
1826
|
+
*
|
|
1827
|
+
* @private
|
|
1828
|
+
*/
|
|
1829
|
+
private checkExistingDeposit;
|
|
1830
|
+
/**
|
|
1831
|
+
* Finalize a deposit via server API (alternative recovery method)
|
|
1832
|
+
*
|
|
1833
|
+
* This method calls a server-side endpoint that can handle
|
|
1834
|
+
* the recovery process with elevated permissions.
|
|
1835
|
+
*/
|
|
1836
|
+
finalizeDepositViaServer(signature: string, commitment: string, encryptedOutput?: string): Promise<RecoveryResult>;
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1839
|
+
/**
|
|
1840
|
+
* Encrypted Output Helpers
|
|
1841
|
+
*
|
|
1842
|
+
* Functions for creating encrypted outputs that enable note scanning
|
|
1843
|
+
*/
|
|
1844
|
+
|
|
1845
|
+
/**
|
|
1846
|
+
* Prepare encrypted output for scanning by wallet owner
|
|
1847
|
+
*
|
|
1848
|
+
* @param note - Note to encrypt
|
|
1849
|
+
* @param cloakKeys - Wallet's Cloak keys (for self-encryption)
|
|
1850
|
+
* @returns Base64-encoded encrypted output
|
|
1851
|
+
*/
|
|
1852
|
+
declare function prepareEncryptedOutput(note: CloakNote, cloakKeys: CloakKeyPair): string;
|
|
1853
|
+
/**
|
|
1854
|
+
* Prepare encrypted output for a specific recipient
|
|
1855
|
+
*
|
|
1856
|
+
* @param note - Note to encrypt
|
|
1857
|
+
* @param recipientPvkHex - Recipient's public view key (hex)
|
|
1858
|
+
* @returns Base64-encoded encrypted output
|
|
1859
|
+
*/
|
|
1860
|
+
declare function prepareEncryptedOutputForRecipient(note: CloakNote, recipientPvkHex: string): string;
|
|
1861
|
+
/**
|
|
1862
|
+
* Simple base64 encoding for v1.0 notes (no encryption)
|
|
1863
|
+
*
|
|
1864
|
+
* @param note - Note to encode
|
|
1865
|
+
* @returns Base64-encoded note data
|
|
1866
|
+
*/
|
|
1867
|
+
declare function encodeNoteSimple(note: CloakNote): string;
|
|
1868
|
+
|
|
1869
|
+
/**
|
|
1870
|
+
* Wallet Integration Helpers
|
|
1871
|
+
*
|
|
1872
|
+
* Helper functions for working with Solana Wallet Adapters
|
|
1873
|
+
*/
|
|
1874
|
+
|
|
1875
|
+
/**
|
|
1876
|
+
* Validate wallet is connected and has public key
|
|
1877
|
+
*/
|
|
1878
|
+
declare function validateWalletConnected(wallet: WalletAdapter | Keypair): void;
|
|
1879
|
+
/**
|
|
1880
|
+
* Get public key from wallet or keypair
|
|
1881
|
+
*/
|
|
1882
|
+
declare function getPublicKey(wallet: WalletAdapter | Keypair): PublicKey;
|
|
1883
|
+
/**
|
|
1884
|
+
* Send transaction using wallet adapter or keypair
|
|
1885
|
+
*/
|
|
1886
|
+
declare function sendTransaction(transaction: Transaction, wallet: WalletAdapter | Keypair, connection: Connection, options?: SendOptions): Promise<string>;
|
|
1887
|
+
/**
|
|
1888
|
+
* Sign transaction using wallet adapter or keypair
|
|
1889
|
+
*/
|
|
1890
|
+
declare function signTransaction<T extends Transaction>(transaction: T, wallet: WalletAdapter | Keypair): Promise<T>;
|
|
1891
|
+
/**
|
|
1892
|
+
* Create a keypair adapter for testing
|
|
1893
|
+
*/
|
|
1894
|
+
declare function keypairToAdapter(keypair: Keypair): WalletAdapter;
|
|
1895
|
+
|
|
1896
|
+
/**
|
|
1897
|
+
* Create a deposit instruction
|
|
1898
|
+
*
|
|
1899
|
+
* Deposits SOL into the Cloak protocol by creating a commitment.
|
|
1900
|
+
*
|
|
1901
|
+
* Instruction format:
|
|
1902
|
+
* - Byte 0: Discriminant (0x00 for deposit)
|
|
1903
|
+
* - Bytes 1-8: Amount (u64, little-endian)
|
|
1904
|
+
* - Bytes 9-40: Commitment (32 bytes)
|
|
1905
|
+
*
|
|
1906
|
+
* @param params - Deposit parameters
|
|
1907
|
+
* @returns Transaction instruction
|
|
1908
|
+
*
|
|
1909
|
+
* @example
|
|
1910
|
+
* ```typescript
|
|
1911
|
+
* const instruction = createDepositInstruction({
|
|
1912
|
+
* programId: CLOAK_PROGRAM_ID,
|
|
1913
|
+
* payer: wallet.publicKey,
|
|
1914
|
+
* pool: POOL_ADDRESS,
|
|
1915
|
+
* commitments: COMMITMENTS_ADDRESS,
|
|
1916
|
+
* amount: 1_000_000_000, // 1 SOL
|
|
1917
|
+
* commitment: commitmentBytes
|
|
1918
|
+
* });
|
|
1919
|
+
* ```
|
|
1920
|
+
*/
|
|
1921
|
+
declare function createDepositInstruction(params: {
|
|
1922
|
+
programId: PublicKey;
|
|
1923
|
+
payer: PublicKey;
|
|
1924
|
+
pool: PublicKey;
|
|
1925
|
+
merkleTree: PublicKey;
|
|
1926
|
+
amount: number;
|
|
1927
|
+
commitment: Uint8Array;
|
|
1928
|
+
}): TransactionInstruction;
|
|
1929
|
+
/**
|
|
1930
|
+
* Deposit instruction parameters for type safety
|
|
1931
|
+
*/
|
|
1932
|
+
interface DepositInstructionParams {
|
|
1933
|
+
programId: PublicKey;
|
|
1934
|
+
payer: PublicKey;
|
|
1935
|
+
pool: PublicKey;
|
|
1936
|
+
merkleTree: PublicKey;
|
|
1937
|
+
amount: number;
|
|
1938
|
+
commitment: Uint8Array;
|
|
1939
|
+
}
|
|
1940
|
+
/**
|
|
1941
|
+
* Validate deposit instruction parameters
|
|
1942
|
+
*
|
|
1943
|
+
* @param params - Parameters to validate
|
|
1944
|
+
* @throws Error if invalid
|
|
1945
|
+
*/
|
|
1946
|
+
declare function validateDepositParams(params: DepositInstructionParams): void;
|
|
1947
|
+
|
|
1948
|
+
/**
|
|
1949
|
+
* Program Derived Address (PDA) utilities for Shield Pool
|
|
1950
|
+
*
|
|
1951
|
+
* These functions derive deterministic addresses from the program ID and seeds.
|
|
1952
|
+
* This matches the behavior in tooling/test/src/shared.rs::get_pda_addresses()
|
|
1953
|
+
*/
|
|
1954
|
+
|
|
1955
|
+
interface ShieldPoolPDAs {
|
|
1956
|
+
pool: PublicKey;
|
|
1957
|
+
merkleTree: PublicKey;
|
|
1958
|
+
commitments: PublicKey;
|
|
1959
|
+
rootsRing: PublicKey;
|
|
1960
|
+
nullifierShard: PublicKey;
|
|
1961
|
+
treasury: PublicKey;
|
|
1962
|
+
}
|
|
1963
|
+
/**
|
|
1964
|
+
* Derive all Shield Pool PDAs from the program ID
|
|
1965
|
+
*
|
|
1966
|
+
* Seeds must match the on-chain program and Rust helper:
|
|
1967
|
+
* - pool: b"pool", mint
|
|
1968
|
+
* - commitments: b"commitments", mint
|
|
1969
|
+
* - roots_ring: b"roots_ring", mint
|
|
1970
|
+
* - nullifier_shard: b"nullifier_shard", mint
|
|
1971
|
+
* - treasury: b"treasury", mint
|
|
1972
|
+
*
|
|
1973
|
+
* For native SOL, the Rust tests use `Pubkey::default()` (32 zero bytes) as the
|
|
1974
|
+
* mint. We mirror that here by accepting an optional mint and defaulting to a
|
|
1975
|
+
* zeroed public key to stay in sync with `tooling/test/src/shared.rs`.
|
|
1976
|
+
*
|
|
1977
|
+
* @param programId - Optional program ID (defaults to CLOAK_PROGRAM_ID)
|
|
1978
|
+
* @param mint - Optional mint address (defaults to 32 zero bytes for native SOL)
|
|
1979
|
+
*/
|
|
1980
|
+
declare function getShieldPoolPDAs(programId?: PublicKey, mint?: PublicKey): ShieldPoolPDAs;
|
|
1981
|
+
|
|
1982
|
+
/**
|
|
1983
|
+
* Cloak SDK - TypeScript SDK for Private Transactions on Solana
|
|
1984
|
+
*
|
|
1985
|
+
* @packageDocumentation
|
|
1986
|
+
*/
|
|
1987
|
+
|
|
1988
|
+
declare const VERSION = "1.0.0";
|
|
1989
|
+
|
|
1990
|
+
export { type ArtifactProofGenerationOptions, ArtifactProverService, CLOAK_PROGRAM_ID, type CloakConfig, CloakError, type CloakKeyPair, type CloakNote, CloakSDK, type DepositInstructionParams, type DepositOptions, DepositRecoveryService, type DepositResult, type DepositStatus, type EncryptedNote, FIXED_FEE_LAMPORTS, type Groth16Proof, IndexerService, LAMPORTS_PER_SOL, LocalStorageAdapter, type MasterKey, type MaxLengthArray, MemoryStorageAdapter, type MerkleProof, type MerkleRootResponse, type Network, type NoteData, type NotesRangeResponse, type ProofGenerationOptions, ProverService, type RecoveryOptions, type RecoveryResult, RelayService, type SP1ProofInputs, type SP1ProofResult, type ScanNotesOptions, type ScannedNote, type ShieldPoolPDAs, type SpendKey, type StorageAdapter, type SwapOptions, type SwapParams, type SwapResult, type Transfer, type TransferOptions, type TransferResult, type TxStatus, VARIABLE_FEE_RATE, VERSION, type ViewKey, type WalletAdapter, type WithdrawOptions, bigintToBytes32, buildPublicInputsBytes, bytesToHex, calculateFee, calculateRelayFee, computeCommitment, computeMerkleRoot, computeNullifier, computeNullifierAsync, computeNullifierSync, computeOutputsHash, computeOutputsHashAsync, computeOutputsHashSync, computeSwapOutputsHash, computeSwapOutputsHashAsync, computeSwapOutputsHashSync, copyNoteToClipboard, createCloakError, createDepositInstruction, deriveSpendKey, deriveViewKey, detectNetworkFromRpcUrl, downloadNote, encodeNoteSimple, encryptNoteForRecipient, exportKeys, exportNote, exportWalletKeys, filterNotesByNetwork, filterWithdrawableNotes, findNoteByCommitment, formatAmount, formatErrorForLogging, generateCloakKeys, generateCommitment, generateCommitmentAsync, generateMasterSeed, generateNote, generateNoteFromWallet, getAddressExplorerUrl, getDistributableAmount, getExplorerUrl, getPublicKey, getPublicViewKey, getRecipientAmount, getRpcUrlForNetwork, getShieldPoolPDAs, getViewKey, hexToBigint, hexToBytes, importKeys, importWalletKeys, isValidHex, isValidRpcUrl, isValidSolanaAddress, isWithdrawable, keypairToAdapter, parseAmount, parseNote, parseTransactionError, poseidonHash, prepareEncryptedOutput, prepareEncryptedOutputForRecipient, proofToBytes, pubkeyToLimbs, randomBytes, scanNotesForWallet, sendTransaction, serializeNote, signTransaction, splitTo2Limbs, tryDecryptNote, updateNoteWithDeposit, validateDepositParams, validateNote, validateOutputsSum, validateTransfers, validateWalletConnected, validateWithdrawableNote };
|