dop-wallet-v6 1.3.39 → 1.3.41

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.
@@ -0,0 +1,338 @@
1
+ /**
2
+ * React Native Prover - Complete ZK Proof Solution
3
+ *
4
+ * This module provides a complete proof generation solution for React Native
5
+ * that works exactly like snarkjs in Node.js:
6
+ *
7
+ * Node.js snarkjs flow:
8
+ * 1. groth16.fullProve(inputs, wasmPath, zkeyPath)
9
+ * 2. Internally: Load WASM -> Create WitnessCalculator -> Calculate witness -> Generate proof
10
+ *
11
+ * React Native rapidsnark flow (this module):
12
+ * 1. setReactNativeProver(config) - Initialize with rapidsnark + file system
13
+ * 2. DOP Engine calls groth16.fullProveDop(inputs, wasm, zkey)
14
+ * 3. Internally: Calculate witness using WASM -> Save files -> Call rapidsnark -> Return proof
15
+ *
16
+ * Key components:
17
+ * - CircuitArtifactManager: Downloads and stores circuit files (WASM, zkey) locally
18
+ * - WitnessGenerator: Calculates witness from inputs using WASM (like circom_runtime)
19
+ * - RapidsnarkProver: Generates proof using witness and zkey
20
+ */
21
+ /// <reference types="node" />
22
+ /// <reference types="node" />
23
+ import { FormattedCircuitInputsDop, Proof } from 'dop-engine-v3';
24
+ /**
25
+ * Result from rapidsnark groth16Prove
26
+ */
27
+ export interface RNRapidsnarkProofResult {
28
+ proof: {
29
+ a: string[];
30
+ b: string[][];
31
+ c: string[];
32
+ };
33
+ pub_signals: string[];
34
+ }
35
+ /**
36
+ * Rapidsnark's groth16Prove function signature
37
+ * From: import { groth16Prove } from 'react-native-rapidsnark'
38
+ */
39
+ export type RNGroth16ProveFunction = (zkeyPath: string, witnessBase64: string) => Promise<RNRapidsnarkProofResult>;
40
+ /**
41
+ * Rapidsnark's groth16ProveWithZKeyFilePath for file-based proving
42
+ * Some versions of rapidsnark support this
43
+ */
44
+ export type RNGroth16ProveWithFilesFunction = (zkeyPath: string, witnessPath: string) => Promise<RNRapidsnarkProofResult>;
45
+ /**
46
+ * File system interface for React Native
47
+ * Implement using react-native-fs or expo-file-system
48
+ */
49
+ export interface RNFileSystem {
50
+ writeFile(path: string, data: string, encoding: 'base64' | 'utf8'): Promise<void>;
51
+ readFile(path: string, encoding: 'base64' | 'utf8'): Promise<string>;
52
+ exists(path: string): Promise<boolean>;
53
+ unlink(path: string): Promise<void>;
54
+ mkdir(path: string): Promise<void>;
55
+ documentDirectory: string;
56
+ cacheDirectory: string;
57
+ }
58
+ /**
59
+ * Witness calculation result
60
+ */
61
+ export interface WitnessResult {
62
+ witnessBin: Uint8Array;
63
+ witnessBase64: string;
64
+ }
65
+ /**
66
+ * Rapidsnark's groth16Verify function signature
67
+ * From: import { groth16Verify } from 'react-native-rapidsnark'
68
+ */
69
+ export type RNGroth16VerifyFunction = (proof: string, inputs: string, verificationKey: string) => Promise<boolean>;
70
+ /**
71
+ * Circuit artifacts stored on device
72
+ */
73
+ export interface StoredCircuitArtifacts {
74
+ wasmPath: string;
75
+ zkeyPath: string;
76
+ vkeyPath: string;
77
+ circuitId: string;
78
+ }
79
+ /**
80
+ * Configuration for React Native Prover
81
+ */
82
+ export interface ReactNativeProverConfig {
83
+ /**
84
+ * Rapidsnark's groth16Prove function
85
+ * Import: import { groth16Prove } from 'react-native-rapidsnark'
86
+ */
87
+ groth16Prove: RNGroth16ProveFunction;
88
+ /**
89
+ * Optional: Rapidsnark's groth16Verify function for local proof verification
90
+ * Import: import { groth16Verify } from 'react-native-rapidsnark'
91
+ * If not provided, verification will throw an error
92
+ */
93
+ groth16Verify?: RNGroth16VerifyFunction;
94
+ /**
95
+ * File system interface (react-native-fs or expo-file-system)
96
+ */
97
+ fs: RNFileSystem;
98
+ /**
99
+ * Optional: Custom artifact storage directory
100
+ * Default: fs.documentDirectory + '/dop-circuits'
101
+ */
102
+ artifactDir?: string;
103
+ /**
104
+ * Optional: Enable debug logging
105
+ */
106
+ debug?: boolean;
107
+ /**
108
+ * Optional: Custom witness calculator
109
+ * If not provided, will use the built-in WASM-based calculator
110
+ */
111
+ witnessCalculator?: (wasmBuffer: Uint8Array, inputs: FormattedCircuitInputsDop) => Promise<Uint8Array>;
112
+ }
113
+ /**
114
+ * Convert Uint8Array to base64 string (React Native compatible)
115
+ */
116
+ export declare function uint8ArrayToBase64(data: Uint8Array): string;
117
+ /**
118
+ * Convert base64 string to Uint8Array (React Native compatible)
119
+ */
120
+ export declare function base64ToUint8Array(base64: string): Uint8Array;
121
+ /**
122
+ * Calculate witness using WASM
123
+ * This is the core function that mimics circom_runtime's WitnessCalculator
124
+ *
125
+ * For React Native, the WASM execution needs special handling.
126
+ * This function provides the interface - the actual WASM execution
127
+ * should be done by a native module or WebAssembly polyfill.
128
+ */
129
+ export declare function calculateWitnessFromWASM(wasmBuffer: Uint8Array, inputs: FormattedCircuitInputsDop, wasmExecutor?: (wasm: Uint8Array, inputs: Record<string, unknown>) => Promise<bigint[]>): Promise<WitnessResult>;
130
+ /**
131
+ * Manages circuit artifacts (WASM, zkey, vkey) on the device
132
+ */
133
+ export declare class CircuitArtifactManager {
134
+ private fs;
135
+ private artifactDir;
136
+ private debug;
137
+ constructor(fs: RNFileSystem, artifactDir?: string, debug?: boolean);
138
+ private log;
139
+ /**
140
+ * Ensure artifact directory exists
141
+ */
142
+ ensureDir(dir: string): Promise<void>;
143
+ /**
144
+ * Get paths for circuit artifacts
145
+ */
146
+ getArtifactPaths(circuitId: string): StoredCircuitArtifacts;
147
+ /**
148
+ * Check if circuit artifacts are already stored
149
+ */
150
+ hasArtifacts(circuitId: string): Promise<boolean>;
151
+ /**
152
+ * Store circuit artifacts from buffers
153
+ * Call this after downloading artifacts
154
+ */
155
+ storeArtifacts(circuitId: string, wasmBuffer: Uint8Array, zkeyBuffer: Uint8Array, vkeyJson?: object): Promise<StoredCircuitArtifacts>;
156
+ /**
157
+ * Store artifacts from the DOP Engine's artifact getter
158
+ * This is called during proof generation when we receive buffers from the engine
159
+ */
160
+ storeArtifactsFromBuffers(circuitId: string, wasmOrDat: Uint8Array | ArrayLike<number>, zkey: ArrayLike<number>): Promise<StoredCircuitArtifacts>;
161
+ /**
162
+ * Load WASM buffer from stored file
163
+ */
164
+ loadWASM(circuitId: string): Promise<Uint8Array>;
165
+ /**
166
+ * Get zkey path for rapidsnark
167
+ */
168
+ getZkeyPath(circuitId: string): string;
169
+ /**
170
+ * Clean up artifacts for a circuit
171
+ */
172
+ removeArtifacts(circuitId: string): Promise<void>;
173
+ /**
174
+ * Get all stored circuit IDs
175
+ */
176
+ getArtifactDirectory(): string;
177
+ }
178
+ /**
179
+ * React Native Prover Instance
180
+ * Manages the complete proof generation flow
181
+ */
182
+ declare class ReactNativeProverInstance {
183
+ private config;
184
+ private artifactManager;
185
+ private initialized;
186
+ constructor(config: ReactNativeProverConfig);
187
+ private log;
188
+ /**
189
+ * Initialize the prover (ensure directories exist)
190
+ */
191
+ initialize(): Promise<void>;
192
+ /**
193
+ * Generate proof using rapidsnark
194
+ * This is the main function that mimics snarkjs.groth16.fullProve
195
+ */
196
+ generateProof(inputs: FormattedCircuitInputsDop, wasmBuffer: Uint8Array | ArrayLike<number>, zkeyBuffer: ArrayLike<number>, logger: {
197
+ debug: (log: string) => void;
198
+ }, progressCallback?: (progress: number) => void): Promise<{
199
+ proof: Proof;
200
+ publicSignals: string[];
201
+ }>;
202
+ /**
203
+ * Get the artifact manager for manual artifact management
204
+ */
205
+ getArtifactManager(): CircuitArtifactManager;
206
+ }
207
+ /**
208
+ * Set up React Native prover for DOP Engine
209
+ *
210
+ * This function configures the DOP Engine to use rapidsnark for proof generation
211
+ * on React Native. It works exactly like setSnarkJSGroth16 but uses rapidsnark
212
+ * under the hood.
213
+ *
214
+ * @example
215
+ * ```typescript
216
+ * import { groth16Prove } from 'react-native-rapidsnark';
217
+ * import RNFS from 'react-native-fs';
218
+ * import { setReactNativeProver } from 'new-dop-wallet-v3';
219
+ *
220
+ * // Create file system interface for react-native-fs
221
+ * const fs: RNFileSystem = {
222
+ * writeFile: (path, data, encoding) => RNFS.writeFile(path, data, encoding),
223
+ * readFile: (path, encoding) => RNFS.readFile(path, encoding),
224
+ * exists: RNFS.exists,
225
+ * unlink: RNFS.unlink,
226
+ * mkdir: RNFS.mkdir,
227
+ * documentDirectory: RNFS.DocumentDirectoryPath,
228
+ * cacheDirectory: RNFS.CachesDirectoryPath,
229
+ * };
230
+ *
231
+ * // Initialize React Native prover (call after initDOP)
232
+ * await setReactNativeProver({
233
+ * groth16Prove,
234
+ * fs,
235
+ * debug: true,
236
+ * });
237
+ *
238
+ * // Now all DOP SDK transactions will use rapidsnark automatically!
239
+ * ```
240
+ */
241
+ export declare function setReactNativeProver(config: ReactNativeProverConfig): Promise<void>;
242
+ /**
243
+ * Get the global React Native prover instance
244
+ * Useful for accessing the artifact manager directly
245
+ */
246
+ export declare function getReactNativeProver(): ReactNativeProverInstance | null;
247
+ /**
248
+ * Create RNFileSystem from react-native-fs (RNFS)
249
+ *
250
+ * @example
251
+ * ```typescript
252
+ * import RNFS from 'react-native-fs';
253
+ * const fs = createRNFSInterface(RNFS);
254
+ * ```
255
+ */
256
+ export declare function createRNFSInterface(RNFS: {
257
+ writeFile: (path: string, content: string, encoding: string) => Promise<void>;
258
+ readFile: (path: string, encoding: string) => Promise<string>;
259
+ exists: (path: string) => Promise<boolean>;
260
+ unlink: (path: string) => Promise<void>;
261
+ mkdir: (path: string, options?: object) => Promise<void>;
262
+ DocumentDirectoryPath: string;
263
+ CachesDirectoryPath: string;
264
+ }): RNFileSystem;
265
+ /**
266
+ * Create RNFileSystem from expo-file-system
267
+ *
268
+ * @example
269
+ * ```typescript
270
+ * import * as FileSystem from 'expo-file-system';
271
+ * const fs = createExpoFSInterface(FileSystem);
272
+ * ```
273
+ */
274
+ export declare function createExpoFSInterface(ExpoFS: {
275
+ writeAsStringAsync: (path: string, content: string, options?: {
276
+ encoding: string;
277
+ }) => Promise<void>;
278
+ readAsStringAsync: (path: string, options?: {
279
+ encoding: string;
280
+ }) => Promise<string>;
281
+ getInfoAsync: (path: string) => Promise<{
282
+ exists: boolean;
283
+ }>;
284
+ deleteAsync: (path: string, options?: {
285
+ idempotent: boolean;
286
+ }) => Promise<void>;
287
+ makeDirectoryAsync: (path: string, options?: {
288
+ intermediates: boolean;
289
+ }) => Promise<void>;
290
+ documentDirectory: string | null;
291
+ cacheDirectory: string | null;
292
+ }): RNFileSystem;
293
+ /**
294
+ * ArtifactStore type matching the SDK's artifact-store.ts
295
+ */
296
+ export interface RNArtifactStore {
297
+ get: (path: string) => Promise<string | Buffer | null>;
298
+ store: (dir: string, path: string, item: string | Uint8Array) => Promise<void>;
299
+ exists: (path: string) => Promise<boolean>;
300
+ }
301
+ /**
302
+ * Create an ArtifactStore for React Native that stores files to device storage
303
+ *
304
+ * This allows the SDK's ArtifactDownloader to automatically save artifacts
305
+ * to a location that rapidsnark can access by file path.
306
+ *
307
+ * Use this when initializing the DOP Engine to make artifacts available for rapidsnark.
308
+ *
309
+ * @example
310
+ * ```typescript
311
+ * import RNFS from 'react-native-fs';
312
+ * import { createRNArtifactStore, createRNFSInterface } from 'new-dop-wallet-v3';
313
+ *
314
+ * const fs = createRNFSInterface(RNFS);
315
+ * const artifactStore = createRNArtifactStore(fs);
316
+ *
317
+ * // Pass to ArtifactDownloader when initializing DOP Engine
318
+ * const artifactDownloader = new ArtifactDownloader(artifactStore, false);
319
+ * ```
320
+ */
321
+ export declare function createRNArtifactStore(fs: RNFileSystem, baseDir?: string): RNArtifactStore;
322
+ /**
323
+ * Get the full file path for an artifact stored via RNArtifactStore
324
+ *
325
+ * Use this to get the path for rapidsnark's groth16Prove function
326
+ *
327
+ * @example
328
+ * ```typescript
329
+ * const zkeyPath = getRNArtifactPath(fs, '3x2', 'zkey');
330
+ * const proofResult = await groth16Prove(zkeyPath, witnessBase64);
331
+ * ```
332
+ */
333
+ export declare function getRNArtifactPath(fs: RNFileSystem, circuitId: string, artifactType: 'zkey' | 'wasm' | 'vkey' | 'dat', baseDir?: string): string;
334
+ /**
335
+ * Check if all artifacts for a circuit are downloaded
336
+ */
337
+ export declare function hasRNArtifacts(fs: RNFileSystem, circuitId: string, baseDir?: string, useNativeArtifacts?: boolean): Promise<boolean>;
338
+ export {};