@zkproofport-app/sdk 0.1.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,2364 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var QRCode = require('qrcode');
6
+ var ethers = require('ethers');
7
+
8
+ /**
9
+ * ZKProofport SDK Constants
10
+ */
11
+ /**
12
+ * Pre-configured relay server URLs for each environment.
13
+ * Used by `ProofportSDK.create('production')` for zero-config initialization.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const sdk = ProofportSDK.create('production');
18
+ * // Uses RELAY_URLS.production = 'https://relay.zkproofport.app'
19
+ * ```
20
+ */
21
+ const RELAY_URLS = {
22
+ production: 'https://relay.zkproofport.app',
23
+ staging: 'https://stg-relay.zkproofport.app',
24
+ local: 'http://localhost:4001',
25
+ };
26
+ /**
27
+ * Default deep link URL scheme for ZKProofport mobile app.
28
+ * Used to construct deep link URLs that open the mobile app.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * const deepLink = `${DEFAULT_SCHEME}://proof-request?...`;
33
+ * // Results in: zkproofport://proof-request?...
34
+ * ```
35
+ */
36
+ const DEFAULT_SCHEME = 'zkproofport';
37
+ /**
38
+ * Deep link URL hosts for different proof request flows.
39
+ * Used as the host component in deep link URLs.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const requestUrl = `zkproofport://${DEEP_LINK_HOSTS.PROOF_REQUEST}`;
44
+ * const responseUrl = `zkproofport://${DEEP_LINK_HOSTS.PROOF_RESPONSE}`;
45
+ * ```
46
+ */
47
+ const DEEP_LINK_HOSTS = {
48
+ /** Host for proof requests sent to mobile app */
49
+ PROOF_REQUEST: 'proof-request'};
50
+ /**
51
+ * Circuit metadata containing display names, descriptions, and public input specifications.
52
+ * Each circuit has a defined number and layout of public inputs that must match
53
+ * the Noir circuit definition.
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * const metadata = CIRCUIT_METADATA['coinbase_attestation'];
58
+ * console.log(metadata.name); // "Coinbase KYC"
59
+ * console.log(metadata.publicInputsCount); // 2
60
+ * ```
61
+ */
62
+ const CIRCUIT_METADATA = {
63
+ coinbase_attestation: {
64
+ name: 'Coinbase KYC',
65
+ description: 'Prove Coinbase identity verification',
66
+ publicInputsCount: 2,
67
+ publicInputNames: ['signal_hash', 'signer_list_merkle_root'],
68
+ },
69
+ coinbase_country_attestation: {
70
+ name: 'Coinbase Country',
71
+ description: 'Prove Coinbase country verification',
72
+ publicInputsCount: 14,
73
+ publicInputNames: ['signal_hash', 'signer_list_merkle_root', 'country_list', 'country_list_length', 'is_included'],
74
+ },
75
+ };
76
+ /**
77
+ * Standard verifier contract ABI shared across all Barretenberg-generated verifiers.
78
+ * This ABI defines the interface for calling the verify function on deployed verifier contracts.
79
+ *
80
+ * Uses ethers v6 human-readable ABI format.
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * import { Contract } from 'ethers';
85
+ *
86
+ * const verifier = new Contract(verifierAddress, VERIFIER_ABI, provider);
87
+ * const isValid = await verifier.verify(proofBytes, publicInputs);
88
+ * ```
89
+ */
90
+ const VERIFIER_ABI = [
91
+ 'function verify(bytes calldata _proof, bytes32[] calldata _publicInputs) external view returns (bool)',
92
+ ];
93
+ /**
94
+ * Public RPC endpoint URLs for supported blockchain networks.
95
+ * Used as fallback when no custom provider is supplied.
96
+ *
97
+ * Supported networks:
98
+ * - 84532: Base Sepolia (testnet)
99
+ * - 8453: Base Mainnet (production)
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * import { JsonRpcProvider } from 'ethers';
104
+ *
105
+ * const provider = new JsonRpcProvider(RPC_ENDPOINTS[84532]);
106
+ * ```
107
+ */
108
+ const RPC_ENDPOINTS = {
109
+ 84532: 'https://sepolia.base.org', // Base Sepolia
110
+ 8453: 'https://mainnet.base.org', // Base Mainnet
111
+ };
112
+ /**
113
+ * Default proof request expiration time in milliseconds.
114
+ * Requests older than this are considered expired and should not be processed.
115
+ *
116
+ * Default: 10 minutes (600,000 ms)
117
+ *
118
+ * @example
119
+ * ```typescript
120
+ * const request: ProofRequest = {
121
+ * // ...
122
+ * createdAt: Date.now(),
123
+ * expiresAt: Date.now() + DEFAULT_REQUEST_EXPIRY_MS
124
+ * };
125
+ * ```
126
+ */
127
+ const DEFAULT_REQUEST_EXPIRY_MS = 10 * 60 * 1000;
128
+ /**
129
+ * Maximum data size (in bytes) that can be encoded in a QR code.
130
+ * Based on QR Code Version 40 with L (Low) error correction level.
131
+ *
132
+ * Requests exceeding this size should use alternative methods (HTTP, WebSocket).
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * const deepLinkUrl = generateDeepLink(request);
137
+ * if (deepLinkUrl.length > MAX_QR_DATA_SIZE) {
138
+ * console.warn('Request too large for QR code');
139
+ * }
140
+ * ```
141
+ */
142
+ const MAX_QR_DATA_SIZE = 2953; // Version 40 with L error correction
143
+ /**
144
+ * ZKProofportNullifierRegistry contract ABI (V2).
145
+ *
146
+ * This is the current nullifier registry interface with relayer-only registration.
147
+ * Public view functions allow checking nullifier status and verifying proofs without registration.
148
+ *
149
+ * Key functions:
150
+ * - `isNullifierRegistered`: Check if a nullifier has been used
151
+ * - `getNullifierInfo`: Get registration details for a nullifier
152
+ * - `verifyOnly`: Verify a proof without registering the nullifier
153
+ *
154
+ * Note: Registration functions (verifyAndRegister) are relayer-only and not exposed in this ABI.
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * import { Contract } from 'ethers';
159
+ *
160
+ * const registry = new Contract(
161
+ * registryAddress,
162
+ * ZKPROOFPORT_NULLIFIER_REGISTRY_ABI,
163
+ * provider
164
+ * );
165
+ *
166
+ * const isUsed = await registry.isNullifierRegistered(nullifier);
167
+ * ```
168
+ */
169
+ const ZKPROOFPORT_NULLIFIER_REGISTRY_ABI = [
170
+ 'function isNullifierRegistered(bytes32 _nullifier) external view returns (bool)',
171
+ 'function getNullifierInfo(bytes32 _nullifier) external view returns (uint64 registeredAt, bytes32 scope, bytes32 circuitId)',
172
+ 'function verifyOnly(bytes32 _circuitId, bytes calldata _proof, bytes32[] calldata _publicInputs) external view returns (bool)',
173
+ 'event NullifierRegistered(bytes32 indexed nullifier, bytes32 indexed scope, bytes32 indexed circuitId)',
174
+ ];
175
+
176
+ var constants = /*#__PURE__*/Object.freeze({
177
+ __proto__: null,
178
+ CIRCUIT_METADATA: CIRCUIT_METADATA,
179
+ DEEP_LINK_HOSTS: DEEP_LINK_HOSTS,
180
+ DEFAULT_REQUEST_EXPIRY_MS: DEFAULT_REQUEST_EXPIRY_MS,
181
+ DEFAULT_SCHEME: DEFAULT_SCHEME,
182
+ MAX_QR_DATA_SIZE: MAX_QR_DATA_SIZE,
183
+ RELAY_URLS: RELAY_URLS,
184
+ RPC_ENDPOINTS: RPC_ENDPOINTS,
185
+ VERIFIER_ABI: VERIFIER_ABI,
186
+ ZKPROOFPORT_NULLIFIER_REGISTRY_ABI: ZKPROOFPORT_NULLIFIER_REGISTRY_ABI
187
+ });
188
+
189
+ /**
190
+ * Deep Link utilities for ZKProofport SDK
191
+ */
192
+ /**
193
+ * Generates a unique request ID for proof requests.
194
+ *
195
+ * Creates an ID by combining a base36-encoded timestamp with a random string,
196
+ * prefixed with "req-". This ensures uniqueness across concurrent requests.
197
+ *
198
+ * @returns A unique string identifier in the format "req-{timestamp}-{random}"
199
+ *
200
+ * @example
201
+ * ```typescript
202
+ * const id = generateRequestId();
203
+ * // "req-lh8k3f2g-a9b7c4d2"
204
+ * ```
205
+ */
206
+ function generateRequestId() {
207
+ const timestamp = Date.now().toString(36);
208
+ const random = Math.random().toString(36).substring(2, 10);
209
+ return `req-${timestamp}-${random}`;
210
+ }
211
+ /**
212
+ * Encodes an object into a URL-safe base64url string with UTF-8 support.
213
+ *
214
+ * Converts the input object to JSON, then encodes it using base64url format
215
+ * (RFC 4648 §5) which replaces '+' with '-', '/' with '_', and removes padding.
216
+ * Works in both browser and Node.js environments.
217
+ *
218
+ * @param data - The object to encode (will be JSON stringified)
219
+ * @returns Base64url-encoded string safe for URL parameters
220
+ *
221
+ * @example
222
+ * ```typescript
223
+ * const encoded = encodeData({ circuit: 'coinbase_attestation', requestId: 'req-123' });
224
+ * // "eyJjaXJjdWl0IjoiY29pbmJhc2VfYXR0ZXN0YXRpb24iLCJyZXF1ZXN0SWQiOiJyZXEtMTIzIn0"
225
+ * ```
226
+ */
227
+ function encodeData(data) {
228
+ const json = JSON.stringify(data);
229
+ // Use base64url encoding (URL-safe) with UTF-8 support
230
+ if (typeof btoa === 'function') {
231
+ // Browser: UTF-8 encode first, then base64
232
+ const utf8Encoded = encodeURIComponent(json).replace(/%([0-9A-F]{2})/g, (_, p1) => String.fromCharCode(parseInt(p1, 16)));
233
+ return btoa(utf8Encoded)
234
+ .replace(/\+/g, '-')
235
+ .replace(/\//g, '_')
236
+ .replace(/=+$/, '');
237
+ }
238
+ // Node.js environment
239
+ return Buffer.from(json, 'utf-8')
240
+ .toString('base64')
241
+ .replace(/\+/g, '-')
242
+ .replace(/\//g, '_')
243
+ .replace(/=+$/, '');
244
+ }
245
+ /**
246
+ * Decodes a base64url-encoded string back into a typed object.
247
+ *
248
+ * Reverses the encoding process by converting base64url to standard base64,
249
+ * decoding it, and parsing the resulting JSON. Handles UTF-8 correctly in
250
+ * both browser and Node.js environments.
251
+ *
252
+ * @typeParam T - The expected type of the decoded object
253
+ * @param encoded - Base64url-encoded string (from encodeData)
254
+ * @returns Decoded and parsed object of type T
255
+ *
256
+ * @throws {SyntaxError} If the decoded string is not valid JSON
257
+ *
258
+ * @example
259
+ * ```typescript
260
+ * const request = decodeData<ProofRequest>(encodedString);
261
+ * console.log(request.circuit); // "coinbase_attestation"
262
+ * ```
263
+ */
264
+ function decodeData(encoded) {
265
+ // Restore base64 padding
266
+ let base64 = encoded.replace(/-/g, '+').replace(/_/g, '/');
267
+ while (base64.length % 4) {
268
+ base64 += '=';
269
+ }
270
+ let json;
271
+ if (typeof atob === 'function') {
272
+ // Browser: decode base64, then UTF-8 decode
273
+ const decoded = atob(base64);
274
+ json = decodeURIComponent(decoded.split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''));
275
+ }
276
+ else {
277
+ json = Buffer.from(base64, 'base64').toString('utf-8');
278
+ }
279
+ return JSON.parse(json);
280
+ }
281
+ /**
282
+ * Builds a deep link URL for a proof request.
283
+ *
284
+ * Encodes the proof request and constructs a deep link URL that can be opened
285
+ * by the ZKProofport mobile app. The URL format is:
286
+ * `{scheme}://proof-request?data={encodedRequest}`
287
+ *
288
+ * @param request - The proof request to encode in the deep link
289
+ * @param scheme - Custom URL scheme (defaults to "zkproofport")
290
+ * @returns Complete deep link URL ready to be opened or embedded in a QR code
291
+ *
292
+ * @example
293
+ * ```typescript
294
+ * const request: ProofRequest = {
295
+ * requestId: generateRequestId(),
296
+ * circuit: 'coinbase_attestation',
297
+ * inputs: { scope: 'myapp.com' },
298
+ * createdAt: Date.now()
299
+ * };
300
+ * const url = buildProofRequestUrl(request);
301
+ * // "zkproofport://proof-request?data=eyJyZXF1ZXN0SWQi..."
302
+ * ```
303
+ */
304
+ function buildProofRequestUrl(request, scheme = DEFAULT_SCHEME) {
305
+ const encodedRequest = encodeData(request);
306
+ return `${scheme}://${DEEP_LINK_HOSTS.PROOF_REQUEST}?data=${encodedRequest}`;
307
+ }
308
+ /**
309
+ * Parses a proof request from a deep link URL.
310
+ *
311
+ * Extracts and decodes the proof request data from a ZKProofport deep link URL.
312
+ * Returns null if the URL is invalid or missing required parameters.
313
+ *
314
+ * @param url - Deep link URL (e.g., "zkproofport://proof-request?data=...")
315
+ * @returns Decoded ProofRequest object, or null if parsing fails
316
+ *
317
+ * @example
318
+ * ```typescript
319
+ * const url = "zkproofport://proof-request?data=eyJyZXF1ZXN0SWQi...";
320
+ * const request = parseProofRequestUrl(url);
321
+ * if (request) {
322
+ * console.log(request.circuit); // "coinbase_attestation"
323
+ * }
324
+ * ```
325
+ */
326
+ function parseProofRequestUrl(url) {
327
+ try {
328
+ const urlObj = new URL(url);
329
+ const data = urlObj.searchParams.get('data');
330
+ if (!data) {
331
+ return null;
332
+ }
333
+ return decodeData(data);
334
+ }
335
+ catch (error) {
336
+ console.error('Failed to parse proof request URL:', error);
337
+ return null;
338
+ }
339
+ }
340
+ /**
341
+ * Parses a proof response from a callback URL.
342
+ *
343
+ * Extracts proof response data from query parameters in a callback URL.
344
+ * Handles both successful proof completions and error responses.
345
+ * Returns null if required parameters (requestId, status) are missing.
346
+ *
347
+ * @param url - Callback URL with proof response query parameters
348
+ * @returns Decoded ProofResponse object, or null if parsing fails
349
+ *
350
+ * @example
351
+ * ```typescript
352
+ * const callbackUrl = "https://example.com/callback?requestId=req-123&status=completed&proof=0x...";
353
+ * const response = parseProofResponseUrl(callbackUrl);
354
+ * if (response && response.status === 'completed') {
355
+ * console.log(response.proof); // "0x..."
356
+ * }
357
+ * ```
358
+ */
359
+ function parseProofResponseUrl(url) {
360
+ try {
361
+ const urlObj = new URL(url);
362
+ const requestId = urlObj.searchParams.get('requestId');
363
+ const status = urlObj.searchParams.get('status');
364
+ if (!requestId || !status) {
365
+ return null;
366
+ }
367
+ const response = {
368
+ requestId,
369
+ circuit: urlObj.searchParams.get('circuit') || 'coinbase_attestation',
370
+ status,
371
+ };
372
+ if (status === 'completed') {
373
+ response.proof = urlObj.searchParams.get('proof') || undefined;
374
+ const publicInputsStr = urlObj.searchParams.get('publicInputs');
375
+ if (publicInputsStr) {
376
+ response.publicInputs = publicInputsStr.split(',');
377
+ }
378
+ const numPublicInputs = urlObj.searchParams.get('numPublicInputs');
379
+ if (numPublicInputs) {
380
+ response.numPublicInputs = parseInt(numPublicInputs, 10);
381
+ }
382
+ const timestamp = urlObj.searchParams.get('timestamp');
383
+ if (timestamp) {
384
+ response.timestamp = parseInt(timestamp, 10);
385
+ }
386
+ response.nullifier = urlObj.searchParams.get('nullifier') || undefined;
387
+ }
388
+ else if (status === 'error') {
389
+ response.error = urlObj.searchParams.get('error') || undefined;
390
+ }
391
+ return response;
392
+ }
393
+ catch (error) {
394
+ console.error('Failed to parse proof response URL:', error);
395
+ return null;
396
+ }
397
+ }
398
+ /**
399
+ * Checks if a URL is a valid ZKProofport deep link.
400
+ *
401
+ * Performs a case-insensitive check to see if the URL starts with the
402
+ * ZKProofport scheme. Does not validate the URL structure beyond the scheme.
403
+ *
404
+ * @param url - URL to check
405
+ * @param scheme - Expected URL scheme (defaults to "zkproofport")
406
+ * @returns True if the URL starts with the specified scheme
407
+ *
408
+ * @example
409
+ * ```typescript
410
+ * isProofportDeepLink("zkproofport://proof-request?data=..."); // true
411
+ * isProofportDeepLink("https://example.com"); // false
412
+ * isProofportDeepLink("ZKPROOFPORT://proof-request"); // true (case-insensitive)
413
+ * ```
414
+ */
415
+ function isProofportDeepLink(url, scheme = DEFAULT_SCHEME) {
416
+ return url.toLowerCase().startsWith(`${scheme.toLowerCase()}://`);
417
+ }
418
+ /**
419
+ * @internal
420
+ * Validates a proof request for completeness and correctness.
421
+ *
422
+ * Performs comprehensive validation including:
423
+ * - Required fields (requestId, circuit, callbackUrl)
424
+ * - Circuit type validity (must be a supported circuit)
425
+ * - Circuit-specific input validation (e.g., userAddress format, countryList structure)
426
+ * - Expiration check (if expiresAt is provided)
427
+ *
428
+ * Note: userAddress is optional for both circuits - the mobile app will prompt
429
+ * for wallet connection if not provided.
430
+ *
431
+ * @param request - Proof request to validate
432
+ * @returns Validation result with `valid` flag and optional `error` message
433
+ */
434
+ function validateProofRequest(request) {
435
+ if (!request.requestId) {
436
+ return { valid: false, error: 'Missing requestId' };
437
+ }
438
+ if (!request.circuit) {
439
+ return { valid: false, error: 'Missing circuit type' };
440
+ }
441
+ if (!['coinbase_attestation', 'coinbase_country_attestation'].includes(request.circuit)) {
442
+ return { valid: false, error: `Invalid circuit type: ${request.circuit}` };
443
+ }
444
+ if (!request.callbackUrl) {
445
+ return { valid: false, error: 'Missing callbackUrl' };
446
+ }
447
+ // Validate circuit-specific inputs
448
+ if (request.circuit === 'coinbase_attestation') {
449
+ // Coinbase KYC: userAddress is optional - app will connect wallet if not provided
450
+ const inputs = request.inputs;
451
+ if (inputs.userAddress && !/^0x[a-fA-F0-9]{40}$/.test(inputs.userAddress)) {
452
+ return { valid: false, error: 'Invalid userAddress format' };
453
+ }
454
+ // If userAddress is not provided, app will prompt wallet connection - this is valid
455
+ }
456
+ else if (request.circuit === 'coinbase_country_attestation') {
457
+ const inputs = request.inputs;
458
+ if (inputs.userAddress && !/^0x[a-fA-F0-9]{40}$/.test(inputs.userAddress)) {
459
+ return { valid: false, error: 'Invalid userAddress format' };
460
+ }
461
+ if (!inputs.countryList || !Array.isArray(inputs.countryList) || inputs.countryList.length === 0) {
462
+ return { valid: false, error: 'countryList is required and must be a non-empty array' };
463
+ }
464
+ if (!inputs.countryList.every(c => typeof c === 'string')) {
465
+ return { valid: false, error: 'countryList must contain only strings' };
466
+ }
467
+ if (typeof inputs.isIncluded !== 'boolean') {
468
+ return { valid: false, error: 'isIncluded is required and must be a boolean' };
469
+ }
470
+ }
471
+ // Check expiry
472
+ if (request.expiresAt && Date.now() > request.expiresAt) {
473
+ return { valid: false, error: 'Request has expired' };
474
+ }
475
+ return { valid: true };
476
+ }
477
+
478
+ /**
479
+ * QR Code utilities for ZKProofport SDK
480
+ */
481
+ /**
482
+ * Default QR code options
483
+ */
484
+ const DEFAULT_QR_OPTIONS = {
485
+ width: 300,
486
+ errorCorrectionLevel: 'M',
487
+ margin: 2,
488
+ darkColor: '#000000',
489
+ lightColor: '#ffffff',
490
+ };
491
+ /**
492
+ * Generates a QR code as a base64-encoded PNG data URL.
493
+ *
494
+ * Creates a scannable QR code image from a proof request or deep link URL.
495
+ * The returned data URL can be used directly in HTML img src attributes.
496
+ * Validates that the encoded data doesn't exceed the maximum QR code size.
497
+ *
498
+ * @param requestOrUrl - Proof request object or pre-built deep link URL
499
+ * @param options - QR code appearance options (width, colors, error correction)
500
+ * @param scheme - Custom URL scheme (only used if requestOrUrl is a ProofRequest)
501
+ * @returns Promise resolving to base64 PNG data URL (e.g., "data:image/png;base64,...")
502
+ *
503
+ * @throws {Error} If the URL exceeds MAX_QR_DATA_SIZE (2953 bytes)
504
+ *
505
+ * @example
506
+ * ```typescript
507
+ * const request: ProofRequest = {
508
+ * requestId: generateRequestId(),
509
+ * circuit: 'coinbase_attestation',
510
+ * inputs: { scope: 'myapp.com' },
511
+ * createdAt: Date.now()
512
+ * };
513
+ *
514
+ * const dataUrl = await generateQRCodeDataUrl(request, {
515
+ * width: 400,
516
+ * darkColor: '#000000',
517
+ * lightColor: '#ffffff'
518
+ * });
519
+ *
520
+ * // Use in HTML: <img src={dataUrl} alt="Scan to prove" />
521
+ * ```
522
+ */
523
+ async function generateQRCodeDataUrl(requestOrUrl, options = {}, scheme = DEFAULT_SCHEME) {
524
+ const url = typeof requestOrUrl === 'string'
525
+ ? requestOrUrl
526
+ : buildProofRequestUrl(requestOrUrl, scheme);
527
+ // Check data size
528
+ if (url.length > MAX_QR_DATA_SIZE) {
529
+ throw new Error(`QR code data too large (${url.length} bytes). Maximum is ${MAX_QR_DATA_SIZE} bytes.`);
530
+ }
531
+ const mergedOptions = { ...DEFAULT_QR_OPTIONS, ...options };
532
+ return QRCode.toDataURL(url, {
533
+ width: mergedOptions.width,
534
+ errorCorrectionLevel: mergedOptions.errorCorrectionLevel,
535
+ margin: mergedOptions.margin,
536
+ color: {
537
+ dark: mergedOptions.darkColor,
538
+ light: mergedOptions.lightColor,
539
+ },
540
+ });
541
+ }
542
+ /**
543
+ * Generates a QR code as an SVG string.
544
+ *
545
+ * Creates a scalable vector graphics QR code that can be embedded directly in HTML
546
+ * or saved to a file. SVG QR codes scale perfectly at any size and have smaller
547
+ * file sizes than raster formats.
548
+ *
549
+ * @param requestOrUrl - Proof request object or pre-built deep link URL
550
+ * @param options - QR code appearance options (width, colors, error correction)
551
+ * @param scheme - Custom URL scheme (only used if requestOrUrl is a ProofRequest)
552
+ * @returns Promise resolving to SVG markup string
553
+ *
554
+ * @throws {Error} If the URL exceeds MAX_QR_DATA_SIZE (2953 bytes)
555
+ *
556
+ * @example
557
+ * ```typescript
558
+ * const request: ProofRequest = { ... };
559
+ * const svg = await generateQRCodeSVG(request, { width: 300 });
560
+ *
561
+ * // Embed directly: <div dangerouslySetInnerHTML={{ __html: svg }} />
562
+ * // Or save to file: fs.writeFileSync('qr.svg', svg);
563
+ * ```
564
+ */
565
+ async function generateQRCodeSVG(requestOrUrl, options = {}, scheme = DEFAULT_SCHEME) {
566
+ const url = typeof requestOrUrl === 'string'
567
+ ? requestOrUrl
568
+ : buildProofRequestUrl(requestOrUrl, scheme);
569
+ if (url.length > MAX_QR_DATA_SIZE) {
570
+ throw new Error(`QR code data too large (${url.length} bytes). Maximum is ${MAX_QR_DATA_SIZE} bytes.`);
571
+ }
572
+ const mergedOptions = { ...DEFAULT_QR_OPTIONS, ...options };
573
+ return QRCode.toString(url, {
574
+ type: 'svg',
575
+ width: mergedOptions.width,
576
+ errorCorrectionLevel: mergedOptions.errorCorrectionLevel,
577
+ margin: mergedOptions.margin,
578
+ color: {
579
+ dark: mergedOptions.darkColor,
580
+ light: mergedOptions.lightColor,
581
+ },
582
+ });
583
+ }
584
+ /**
585
+ * Renders a QR code directly to an HTML canvas element.
586
+ *
587
+ * Browser-only function that draws a QR code onto an existing canvas element.
588
+ * Useful for interactive applications or when you need direct canvas manipulation.
589
+ * The canvas dimensions will be set automatically based on the width option.
590
+ *
591
+ * @param canvas - HTML canvas element to render to (must exist in DOM)
592
+ * @param requestOrUrl - Proof request object or pre-built deep link URL
593
+ * @param options - QR code appearance options (width, colors, error correction)
594
+ * @param scheme - Custom URL scheme (only used if requestOrUrl is a ProofRequest)
595
+ * @returns Promise that resolves when rendering is complete
596
+ *
597
+ * @throws {Error} If the URL exceeds MAX_QR_DATA_SIZE (2953 bytes)
598
+ * @throws {Error} If canvas is not a valid HTMLCanvasElement (browser only)
599
+ *
600
+ * @example
601
+ * ```typescript
602
+ * // In a React component:
603
+ * const canvasRef = useRef<HTMLCanvasElement>(null);
604
+ *
605
+ * useEffect(() => {
606
+ * if (canvasRef.current) {
607
+ * generateQRCodeToCanvas(canvasRef.current, request, { width: 400 });
608
+ * }
609
+ * }, [request]);
610
+ *
611
+ * return <canvas ref={canvasRef} />;
612
+ * ```
613
+ */
614
+ async function generateQRCodeToCanvas(canvas, requestOrUrl, options = {}, scheme = DEFAULT_SCHEME) {
615
+ const url = typeof requestOrUrl === 'string'
616
+ ? requestOrUrl
617
+ : buildProofRequestUrl(requestOrUrl, scheme);
618
+ if (url.length > MAX_QR_DATA_SIZE) {
619
+ throw new Error(`QR code data too large (${url.length} bytes). Maximum is ${MAX_QR_DATA_SIZE} bytes.`);
620
+ }
621
+ const mergedOptions = { ...DEFAULT_QR_OPTIONS, ...options };
622
+ await QRCode.toCanvas(canvas, url, {
623
+ width: mergedOptions.width,
624
+ errorCorrectionLevel: mergedOptions.errorCorrectionLevel,
625
+ margin: mergedOptions.margin,
626
+ color: {
627
+ dark: mergedOptions.darkColor,
628
+ light: mergedOptions.lightColor,
629
+ },
630
+ });
631
+ }
632
+ /**
633
+ * Estimates the byte size of a QR code's encoded data.
634
+ *
635
+ * Calculates the size of the deep link URL that will be encoded in the QR code,
636
+ * and checks if it's within the maximum supported size (2953 bytes for QR version 40
637
+ * with medium error correction). Use this before generating QR codes to avoid errors.
638
+ *
639
+ * @param requestOrUrl - Proof request object or pre-built deep link URL
640
+ * @param scheme - Custom URL scheme (only used if requestOrUrl is a ProofRequest)
641
+ * @returns Object with `size` in bytes and `withinLimit` boolean flag
642
+ *
643
+ * @example
644
+ * ```typescript
645
+ * const request: ProofRequest = { ... };
646
+ * const { size, withinLimit } = estimateQRDataSize(request);
647
+ *
648
+ * if (!withinLimit) {
649
+ * console.error(`QR code too large: ${size} bytes (max 2953)`);
650
+ * // Consider reducing request data or splitting into multiple QR codes
651
+ * }
652
+ *
653
+ * // Example output: { size: 384, withinLimit: true }
654
+ * ```
655
+ */
656
+ function estimateQRDataSize(requestOrUrl, scheme = DEFAULT_SCHEME) {
657
+ const url = typeof requestOrUrl === 'string'
658
+ ? requestOrUrl
659
+ : buildProofRequestUrl(requestOrUrl, scheme);
660
+ return {
661
+ size: url.length,
662
+ withinLimit: url.length <= MAX_QR_DATA_SIZE,
663
+ };
664
+ }
665
+
666
+ /**
667
+ * On-chain verification utilities for ZKProofport SDK
668
+ *
669
+ * Compatible with both ethers v5 and v6.
670
+ */
671
+ // ethers v5/v6 compatibility shims
672
+ const _ethers = ethers.ethers;
673
+ /** @internal ethers v5/v6 compatibility shim */
674
+ function hexZeroPad(value, length) {
675
+ // v6: ethers.zeroPadValue, v5: ethers.utils.hexZeroPad
676
+ if (typeof _ethers.zeroPadValue === 'function')
677
+ return _ethers.zeroPadValue(value, length);
678
+ if (_ethers.utils?.hexZeroPad)
679
+ return _ethers.utils.hexZeroPad(value, length);
680
+ // manual fallback
681
+ const hex = value.startsWith('0x') ? value.slice(2) : value;
682
+ return '0x' + hex.padStart(length * 2, '0');
683
+ }
684
+ /** @internal ethers v5/v6 compatibility shim */
685
+ function createJsonRpcProvider(url) {
686
+ // v6: ethers.JsonRpcProvider, v5: ethers.providers.JsonRpcProvider
687
+ if (typeof _ethers.JsonRpcProvider === 'function')
688
+ return new _ethers.JsonRpcProvider(url);
689
+ if (_ethers.providers?.JsonRpcProvider)
690
+ return new _ethers.providers.JsonRpcProvider(url);
691
+ throw new Error('No JsonRpcProvider found in ethers');
692
+ }
693
+ /**
694
+ * @internal Resolve verifier from SDK config or proof response.
695
+ * SDK config (customVerifier) takes priority over response-provided verifier.
696
+ */
697
+ function resolveVerifier(customVerifier, responseVerifier) {
698
+ if (customVerifier)
699
+ return customVerifier;
700
+ if (responseVerifier?.verifierAddress) {
701
+ return {
702
+ address: responseVerifier.verifierAddress,
703
+ chainId: responseVerifier.chainId ?? 0,
704
+ abi: VERIFIER_ABI,
705
+ };
706
+ }
707
+ return null;
708
+ }
709
+ /**
710
+ * Get verifier contract instance for interacting with on-chain verifier contracts.
711
+ *
712
+ * @param providerOrSigner - ethers.js Provider or Signer instance (v5 or v6 compatible)
713
+ * @param verifier - Verifier contract configuration containing address and ABI
714
+ * @returns ethers.Contract instance connected to the verifier
715
+ *
716
+ * @example
717
+ * ```typescript
718
+ * const provider = new ethers.JsonRpcProvider(rpcUrl);
719
+ * const contract = getVerifierContract(provider, {
720
+ * address: '0x...',
721
+ * chainId: 11155111,
722
+ * abi: VERIFIER_ABI
723
+ * });
724
+ * ```
725
+ */
726
+ function getVerifierContract(providerOrSigner, verifier) {
727
+ return new ethers.ethers.Contract(verifier.address, verifier.abi, providerOrSigner);
728
+ }
729
+ /**
730
+ * Get default JSON-RPC provider for a chain using pre-configured RPC endpoints.
731
+ *
732
+ * @param chainId - The chain ID (e.g., 11155111 for Sepolia, 84532 for Base Sepolia)
733
+ * @returns ethers.JsonRpcProvider instance for the specified chain
734
+ * @throws Error if no RPC endpoint is configured for the chain
735
+ *
736
+ * @example
737
+ * ```typescript
738
+ * const provider = getDefaultProvider(11155111); // Sepolia
739
+ * ```
740
+ */
741
+ function getDefaultProvider(chainId) {
742
+ const rpcUrl = RPC_ENDPOINTS[chainId];
743
+ if (!rpcUrl) {
744
+ throw new Error(`No RPC endpoint configured for chain ${chainId}`);
745
+ }
746
+ return createJsonRpcProvider(rpcUrl);
747
+ }
748
+ /**
749
+ * Verify a zero-knowledge proof on-chain by calling the verifier smart contract.
750
+ *
751
+ * This function resolves the verifier contract from SDK config or proof response,
752
+ * connects to the blockchain, and calls the verify() method with the proof and public inputs.
753
+ *
754
+ * @param circuit - The canonical circuit identifier (e.g., "coinbase_attestation")
755
+ * @param parsedProof - Parsed proof object containing proofHex and publicInputsHex
756
+ * @param providerOrSigner - Optional ethers.js Provider or Signer instance. If not provided, uses default RPC for the chain
757
+ * @param customVerifier - Optional custom verifier contract config (takes priority over responseVerifier)
758
+ * @param responseVerifier - Optional verifier info from proof generation response
759
+ * @returns Promise resolving to verification result with valid flag and optional error message
760
+ *
761
+ * @example
762
+ * ```typescript
763
+ * const parsed = parseProofForOnChain(proof, publicInputs, numPublicInputs);
764
+ * const result = await verifyProofOnChain(
765
+ * 'coinbase_attestation',
766
+ * parsed,
767
+ * provider,
768
+ * { address: '0x...', chainId: 11155111, abi: VERIFIER_ABI }
769
+ * );
770
+ *
771
+ * if (result.valid) {
772
+ * console.log('Proof is valid!');
773
+ * } else {
774
+ * console.error('Verification failed:', result.error);
775
+ * }
776
+ * ```
777
+ */
778
+ async function verifyProofOnChain(circuit, parsedProof, providerOrSigner, customVerifier, responseVerifier) {
779
+ const verifier = resolveVerifier(customVerifier, responseVerifier);
780
+ if (!verifier) {
781
+ return {
782
+ valid: false,
783
+ error: 'No verifier address provided. Configure via SDK or ensure proof response includes verifierAddress.',
784
+ };
785
+ }
786
+ const provider = providerOrSigner || (verifier.chainId > 0 ? getDefaultProvider(verifier.chainId) : null);
787
+ if (!provider) {
788
+ return {
789
+ valid: false,
790
+ error: 'No provider available. Provide a provider or ensure chainId is set for RPC lookup.',
791
+ };
792
+ }
793
+ const contract = getVerifierContract(provider, verifier);
794
+ try {
795
+ const isValid = await contract.verify(parsedProof.proofHex, parsedProof.publicInputsHex);
796
+ return { valid: isValid };
797
+ }
798
+ catch (error) {
799
+ const errorMessage = error instanceof Error ? error.message : String(error);
800
+ return { valid: false, error: errorMessage };
801
+ }
802
+ }
803
+ /** @internal Ensure a hex string has the 0x prefix */
804
+ function ensureHexPrefix(hex) {
805
+ return hex.startsWith('0x') ? hex : `0x${hex}`;
806
+ }
807
+ /**
808
+ * Parse proof response into format suitable for on-chain verification.
809
+ *
810
+ * Converts proof and public inputs from relay response format to the format
811
+ * expected by Solidity verifier contracts. Public inputs are zero-padded to
812
+ * 32 bytes (bytes32) to match Solidity's bytes32[] type.
813
+ *
814
+ * @param proof - Proof bytes as hex string (with or without 0x prefix)
815
+ * @param publicInputs - Array of public input values as hex strings
816
+ * @param numPublicInputs - Number of public inputs (for validation)
817
+ * @returns Parsed proof object ready for on-chain verification
818
+ *
819
+ * @example
820
+ * ```typescript
821
+ * const parsed = parseProofForOnChain(
822
+ * '0x1a2b3c...',
823
+ * ['0x01', '0x02', '0x03'],
824
+ * 3
825
+ * );
826
+ *
827
+ * // parsed.proofHex: '0x1a2b3c...'
828
+ * // parsed.publicInputsHex: ['0x0000...01', '0x0000...02', '0x0000...03']
829
+ * ```
830
+ */
831
+ function parseProofForOnChain(proof, publicInputs, numPublicInputs) {
832
+ const proofHex = ensureHexPrefix(proof);
833
+ const publicInputsHex = publicInputs.map((input) => {
834
+ return hexZeroPad(ensureHexPrefix(input), 32);
835
+ });
836
+ return {
837
+ proofHex,
838
+ publicInputsHex,
839
+ numPublicInputs,
840
+ };
841
+ }
842
+ /** @internal Require a verifier or throw with a helpful message */
843
+ function requireVerifier(circuit, verifier) {
844
+ if (!verifier) {
845
+ throw new Error(`No verifier configured for circuit '${circuit}'. Configure via SDK verifiers option.`);
846
+ }
847
+ return verifier;
848
+ }
849
+ /**
850
+ * Get verifier contract address for a circuit.
851
+ *
852
+ * @param circuit - The canonical circuit identifier (e.g., "coinbase_attestation")
853
+ * @param customVerifier - Optional custom verifier contract config
854
+ * @returns Verifier contract address as hex string
855
+ * @throws Error if no verifier is configured for the circuit
856
+ *
857
+ * @example
858
+ * ```typescript
859
+ * const address = getVerifierAddress('coinbase_attestation', verifierConfig);
860
+ * console.log(address); // '0x1234...'
861
+ * ```
862
+ */
863
+ function getVerifierAddress(circuit, customVerifier) {
864
+ return requireVerifier(circuit, customVerifier).address;
865
+ }
866
+ /**
867
+ * Get chain ID for a circuit's verifier contract.
868
+ *
869
+ * @param circuit - The canonical circuit identifier (e.g., "coinbase_attestation")
870
+ * @param customVerifier - Optional custom verifier contract config
871
+ * @returns Chain ID number (e.g., 11155111 for Sepolia, 84532 for Base Sepolia)
872
+ * @throws Error if no verifier is configured for the circuit
873
+ *
874
+ * @example
875
+ * ```typescript
876
+ * const chainId = getVerifierChainId('coinbase_attestation', verifierConfig);
877
+ * console.log(chainId); // 11155111
878
+ * ```
879
+ */
880
+ function getVerifierChainId(circuit, customVerifier) {
881
+ return requireVerifier(circuit, customVerifier).chainId;
882
+ }
883
+ /**
884
+ * Extract scope value from public inputs array.
885
+ *
886
+ * The scope is a bytes32 value encoded across 32 consecutive field elements
887
+ * in the public inputs. The exact position depends on the circuit type.
888
+ *
889
+ * @param publicInputsHex - Array of public input hex strings (zero-padded to 32 bytes)
890
+ * @param circuit - Optional circuit identifier to determine field positions
891
+ * @returns Reconstructed scope as hex string with 0x prefix, or null if inputs are insufficient
892
+ *
893
+ * @example
894
+ * ```typescript
895
+ * const scope = extractScopeFromPublicInputs(publicInputsHex, 'coinbase_attestation');
896
+ * console.log(scope); // '0x7a6b70726f6f66706f72742e636f6d...'
897
+ * ```
898
+ */
899
+ function extractScopeFromPublicInputs(publicInputsHex, circuit) {
900
+ let start, end;
901
+ if (circuit === 'coinbase_country_attestation') {
902
+ start = 86;
903
+ end = 117;
904
+ }
905
+ else {
906
+ start = 64;
907
+ end = 95;
908
+ }
909
+ if (publicInputsHex.length <= end)
910
+ return null;
911
+ const scopeFields = publicInputsHex.slice(start, end + 1);
912
+ return reconstructBytes32FromFields(scopeFields);
913
+ }
914
+ /**
915
+ * Extract nullifier value from public inputs array.
916
+ *
917
+ * The nullifier is a bytes32 value encoded across 32 consecutive field elements
918
+ * in the public inputs. The exact position depends on the circuit type.
919
+ * Nullifiers are used for duplicate proof detection and must be unique per user+scope.
920
+ *
921
+ * @param publicInputsHex - Array of public input hex strings (zero-padded to 32 bytes)
922
+ * @param circuit - Optional circuit identifier to determine field positions
923
+ * @returns Reconstructed nullifier as hex string with 0x prefix, or null if inputs are insufficient
924
+ *
925
+ * @example
926
+ * ```typescript
927
+ * const nullifier = extractNullifierFromPublicInputs(publicInputsHex, 'coinbase_attestation');
928
+ * console.log(nullifier); // '0xabcd1234...'
929
+ *
930
+ * // Check if nullifier is already registered
931
+ * const isRegistered = await isNullifierRegistered(nullifier, registryAddress, provider);
932
+ * ```
933
+ */
934
+ function extractNullifierFromPublicInputs(publicInputsHex, circuit) {
935
+ let start, end;
936
+ if (circuit === 'coinbase_country_attestation') {
937
+ start = 118;
938
+ end = 149;
939
+ }
940
+ else {
941
+ start = 96;
942
+ end = 127;
943
+ }
944
+ if (publicInputsHex.length <= end)
945
+ return null;
946
+ const nullifierFields = publicInputsHex.slice(start, end + 1);
947
+ return reconstructBytes32FromFields(nullifierFields);
948
+ }
949
+ /** @internal Reconstruct a bytes32 value from 32 individual field elements */
950
+ function reconstructBytes32FromFields(fields) {
951
+ if (fields.length !== 32) {
952
+ throw new Error(`Expected 32 fields, got ${fields.length}`);
953
+ }
954
+ const bytes = fields.map(f => {
955
+ const byte = BigInt(f) & 0xffn;
956
+ return byte.toString(16).padStart(2, '0');
957
+ }).join('');
958
+ return '0x' + bytes;
959
+ }
960
+ /**
961
+ * Check if a nullifier is already registered on-chain in the ZKProofport nullifier registry.
962
+ *
963
+ * This function queries the on-chain nullifier registry contract to determine if a nullifier
964
+ * has been used before. This is used to prevent duplicate proof submissions from the same user
965
+ * for the same scope.
966
+ *
967
+ * @param nullifier - The nullifier hash as hex string with 0x prefix
968
+ * @param registryAddress - ZKProofportNullifierRegistry contract address
969
+ * @param provider - ethers.js Provider instance (v5 or v6 compatible)
970
+ * @returns Promise resolving to true if nullifier is registered, false otherwise
971
+ *
972
+ * @example
973
+ * ```typescript
974
+ * const nullifier = extractNullifierFromPublicInputs(publicInputsHex, circuit);
975
+ * const isRegistered = await isNullifierRegistered(
976
+ * nullifier,
977
+ * '0x...',
978
+ * provider
979
+ * );
980
+ *
981
+ * if (isRegistered) {
982
+ * console.log('This nullifier has already been used');
983
+ * }
984
+ * ```
985
+ */
986
+ async function isNullifierRegistered(nullifier, registryAddress, provider) {
987
+ const { ZKPROOFPORT_NULLIFIER_REGISTRY_ABI } = await Promise.resolve().then(function () { return constants; });
988
+ const contract = new ethers.ethers.Contract(registryAddress, ZKPROOFPORT_NULLIFIER_REGISTRY_ABI, provider);
989
+ try {
990
+ return await contract.isNullifierRegistered(nullifier);
991
+ }
992
+ catch {
993
+ return false;
994
+ }
995
+ }
996
+ /**
997
+ * Get detailed information about a registered nullifier from the on-chain registry.
998
+ *
999
+ * This function retrieves the registration timestamp, scope, and circuit ID for a nullifier
1000
+ * that has been registered on-chain. This metadata is useful for auditing and analytics.
1001
+ *
1002
+ * @param nullifier - The nullifier hash as hex string with 0x prefix
1003
+ * @param registryAddress - ZKProofportNullifierRegistry contract address
1004
+ * @param provider - ethers.js Provider instance (v5 or v6 compatible)
1005
+ * @returns Promise resolving to nullifier info object, or null if not registered
1006
+ *
1007
+ * @example
1008
+ * ```typescript
1009
+ * const info = await getNullifierInfo(nullifier, registryAddress, provider);
1010
+ *
1011
+ * if (info) {
1012
+ * console.log('Registered at:', new Date(info.registeredAt * 1000));
1013
+ * console.log('Scope:', info.scope);
1014
+ * console.log('Circuit:', info.circuitId);
1015
+ * } else {
1016
+ * console.log('Nullifier not registered');
1017
+ * }
1018
+ * ```
1019
+ */
1020
+ async function getNullifierInfo(nullifier, registryAddress, provider) {
1021
+ const { ZKPROOFPORT_NULLIFIER_REGISTRY_ABI } = await Promise.resolve().then(function () { return constants; });
1022
+ const contract = new ethers.ethers.Contract(registryAddress, ZKPROOFPORT_NULLIFIER_REGISTRY_ABI, provider);
1023
+ try {
1024
+ const [registeredAt, scope, circuitId] = await contract.getNullifierInfo(nullifier);
1025
+ if (BigInt(registeredAt) === 0n)
1026
+ return null;
1027
+ return {
1028
+ registeredAt: Number(registeredAt),
1029
+ scope: scope,
1030
+ circuitId: circuitId,
1031
+ };
1032
+ }
1033
+ catch {
1034
+ return null;
1035
+ }
1036
+ }
1037
+
1038
+ /**
1039
+ * Proofport SDK - Main class
1040
+ */
1041
+ /**
1042
+ * Main SDK class for interacting with the ZKProofport mobile app.
1043
+ *
1044
+ * Provides methods for creating proof requests, generating QR codes and deep links,
1045
+ * verifying proofs on-chain, and handling proof responses from the mobile app.
1046
+ *
1047
+ * @example
1048
+ * ```typescript
1049
+ * import { ProofportSDK } from '@zkproofport-app/sdk';
1050
+ *
1051
+ * // Initialize SDK (uses production relay by default)
1052
+ * const sdk = ProofportSDK.create();
1053
+ *
1054
+ * // Authenticate
1055
+ * await sdk.login({ clientId: 'your-id', apiKey: 'your-key' });
1056
+ *
1057
+ * // Create proof request via relay
1058
+ * const relay = await sdk.createRelayRequest('coinbase_attestation', {
1059
+ * scope: 'myapp.com'
1060
+ * });
1061
+ *
1062
+ * // Generate QR code for desktop users
1063
+ * const qrDataUrl = await sdk.generateQRCode(relay.deepLink);
1064
+ *
1065
+ * // Wait for proof via WebSocket (primary) or polling (fallback)
1066
+ * const result = await sdk.waitForProof(relay.requestId);
1067
+ * if (result.status === 'completed') {
1068
+ * console.log('Proof received:', result.proof);
1069
+ * }
1070
+ * ```
1071
+ */
1072
+ class ProofportSDK {
1073
+ /**
1074
+ * Creates a new ProofportSDK instance.
1075
+ *
1076
+ * For most use cases, prefer the static factory with environment presets:
1077
+ * ```typescript
1078
+ * const sdk = ProofportSDK.create('production');
1079
+ * ```
1080
+ *
1081
+ * @param config - SDK configuration options
1082
+ * @param config.scheme - Custom deep link scheme (default: 'zkproofport')
1083
+ * @param config.relayUrl - Relay server URL (required for relay features)
1084
+ * @param config.verifiers - Custom verifier contract addresses per circuit
1085
+ *
1086
+ * @example
1087
+ * ```typescript
1088
+ * const sdk = new ProofportSDK({
1089
+ * relayUrl: 'https://relay.zkproofport.app',
1090
+ * verifiers: {
1091
+ * coinbase_attestation: {
1092
+ * verifierAddress: '0x...',
1093
+ * chainId: 1
1094
+ * }
1095
+ * }
1096
+ * });
1097
+ * ```
1098
+ */
1099
+ constructor(config = {}) {
1100
+ this.pendingRequests = new Map();
1101
+ this.authToken = null;
1102
+ this.socket = null;
1103
+ this.config = {
1104
+ scheme: config.scheme || DEFAULT_SCHEME,
1105
+ verifiers: config.verifiers || {},
1106
+ };
1107
+ this.relayUrl = config.relayUrl || '';
1108
+ this.nullifierRegistry = config.nullifierRegistry;
1109
+ }
1110
+ // ============ Request Creation ============
1111
+ /**
1112
+ * @internal
1113
+ * Creates a Coinbase KYC verification proof request.
1114
+ *
1115
+ * Generates a proof request for verifying Coinbase KYC status without revealing
1116
+ * the actual attestation data. The proof confirms the user has passed Coinbase KYC
1117
+ * while maintaining privacy through zero-knowledge proofs.
1118
+ *
1119
+ * @param inputs - Circuit inputs for Coinbase KYC
1120
+ * @param inputs.scope - Application-specific scope (e.g., domain name)
1121
+ * @param options - Request configuration options
1122
+
1123
+ * @param options.message - Custom message to display to user
1124
+ * @param options.dappName - Application name shown in ZKProofport app
1125
+ * @param options.dappIcon - Application icon URL shown in ZKProofport app
1126
+ * @param options.expiresInMs - Request expiration time in milliseconds (default: 10 minutes)
1127
+ *
1128
+ * @returns ProofRequest object with unique requestId and all request details
1129
+ *
1130
+ * @throws Error if scope is not provided
1131
+ *
1132
+ * @example
1133
+ * ```typescript
1134
+ * const request = sdk.createCoinbaseKycRequest({
1135
+ * scope: 'myapp.com'
1136
+ * }, {
1137
+ * dappName: 'My DApp',
1138
+ * message: 'Verify your KYC status to access this feature'
1139
+ * });
1140
+ *
1141
+ * const deepLink = sdk.getDeepLinkUrl(request);
1142
+ * ```
1143
+ */
1144
+ createCoinbaseKycRequest(inputs, options = {}) {
1145
+ if (!inputs.scope) {
1146
+ throw new Error('scope is required for coinbase_attestation circuit');
1147
+ }
1148
+ const request = {
1149
+ requestId: generateRequestId(),
1150
+ circuit: 'coinbase_attestation',
1151
+ inputs,
1152
+ message: options.message,
1153
+ dappName: options.dappName,
1154
+ dappIcon: options.dappIcon,
1155
+ createdAt: Date.now(),
1156
+ expiresAt: Date.now() + (options.expiresInMs || DEFAULT_REQUEST_EXPIRY_MS),
1157
+ };
1158
+ this.pendingRequests.set(request.requestId, request);
1159
+ return request;
1160
+ }
1161
+ /**
1162
+ * @internal
1163
+ * Creates a Coinbase Country attestation proof request.
1164
+ *
1165
+ * Generates a proof request for verifying country eligibility through Coinbase
1166
+ * attestation without revealing the actual country data. The proof confirms the
1167
+ * user's country status while maintaining privacy through zero-knowledge proofs.
1168
+ *
1169
+ * @param inputs - Circuit inputs for Coinbase Country attestation
1170
+ * @param inputs.scope - Application-specific scope (e.g., domain name)
1171
+ * @param options - Request configuration options
1172
+
1173
+ * @param options.message - Custom message to display to user
1174
+ * @param options.dappName - Application name shown in ZKProofport app
1175
+ * @param options.dappIcon - Application icon URL shown in ZKProofport app
1176
+ * @param options.expiresInMs - Request expiration time in milliseconds (default: 10 minutes)
1177
+ *
1178
+ * @returns ProofRequest object with unique requestId and all request details
1179
+ *
1180
+ * @throws Error if scope is not provided
1181
+ *
1182
+ * @example
1183
+ * ```typescript
1184
+ * const request = sdk.createCoinbaseCountryRequest({
1185
+ * scope: 'myapp.com'
1186
+ * }, {
1187
+ * dappName: 'My DApp',
1188
+ * message: 'Verify your country eligibility'
1189
+ * });
1190
+ * ```
1191
+ */
1192
+ createCoinbaseCountryRequest(inputs, options = {}) {
1193
+ if (!inputs.scope) {
1194
+ throw new Error('scope is required for coinbase_country_attestation circuit');
1195
+ }
1196
+ if (!inputs.countryList || !Array.isArray(inputs.countryList) || inputs.countryList.length === 0) {
1197
+ throw new Error('countryList is required for coinbase_country_attestation circuit');
1198
+ }
1199
+ if (typeof inputs.isIncluded !== 'boolean') {
1200
+ throw new Error('isIncluded is required for coinbase_country_attestation circuit');
1201
+ }
1202
+ const request = {
1203
+ requestId: generateRequestId(),
1204
+ circuit: 'coinbase_country_attestation',
1205
+ inputs,
1206
+ message: options.message,
1207
+ dappName: options.dappName,
1208
+ dappIcon: options.dappIcon,
1209
+ createdAt: Date.now(),
1210
+ expiresAt: Date.now() + (options.expiresInMs || DEFAULT_REQUEST_EXPIRY_MS),
1211
+ };
1212
+ this.pendingRequests.set(request.requestId, request);
1213
+ return request;
1214
+ }
1215
+ /**
1216
+ * @internal
1217
+ * Creates a generic proof request for any supported circuit type.
1218
+ *
1219
+ * Routes to the appropriate circuit-specific request creation method based on
1220
+ * the circuit type. Use this for dynamic circuit selection or when the circuit
1221
+ * type is determined at runtime.
1222
+ *
1223
+ * @param circuit - Circuit type identifier ('coinbase_attestation' | 'coinbase_country_attestation')
1224
+ * @param inputs - Circuit-specific inputs
1225
+ * @param options - Request configuration options
1226
+
1227
+ * @param options.message - Custom message to display to user
1228
+ * @param options.dappName - Application name shown in ZKProofport app
1229
+ * @param options.dappIcon - Application icon URL shown in ZKProofport app
1230
+ * @param options.expiresInMs - Request expiration time in milliseconds (default: 15 minutes)
1231
+ *
1232
+ * @returns ProofRequest object with unique requestId and all request details
1233
+ *
1234
+ * @throws Error if required inputs are missing for the specified circuit
1235
+ *
1236
+ * @example
1237
+ * ```typescript
1238
+ * const circuitType = userSelection; // Dynamic selection
1239
+ * const request = sdk.createProofRequest(
1240
+ * circuitType,
1241
+ * { scope: 'myapp.com' },
1242
+ * { dappName: 'My DApp' }
1243
+ * );
1244
+ * ```
1245
+ */
1246
+ createProofRequest(circuit, inputs, options = {}) {
1247
+ if (circuit === 'coinbase_country_attestation') {
1248
+ return this.createCoinbaseCountryRequest(inputs, options);
1249
+ }
1250
+ else {
1251
+ return this.createCoinbaseKycRequest(inputs, options);
1252
+ }
1253
+ }
1254
+ // ============ Deep Link Generation ============
1255
+ /**
1256
+ * @internal
1257
+ * Generates a deep link URL for a proof request.
1258
+ *
1259
+ * Creates a zkproofport:// URL that opens the ZKProofport mobile app with the
1260
+ * specified proof request. The URL contains all request details encoded as query
1261
+ * parameters.
1262
+ *
1263
+ * @param request - ProofRequest object to encode as deep link
1264
+ *
1265
+ * @returns Deep link URL string (e.g., 'zkproofport://proof?requestId=...')
1266
+ *
1267
+ * @example
1268
+ * ```typescript
1269
+ * const request = sdk.createCoinbaseKycRequest({ scope: 'myapp.com' });
1270
+ * const url = sdk.getDeepLinkUrl(request);
1271
+ * // url: 'zkproofport://proof?requestId=abc123&circuit=coinbase_attestation&...'
1272
+ * ```
1273
+ */
1274
+ getDeepLinkUrl(request) {
1275
+ return buildProofRequestUrl(request, this.config.scheme);
1276
+ }
1277
+ /**
1278
+ * @internal
1279
+ * Opens the ZKProofport mobile app with a proof request.
1280
+ *
1281
+ * Redirects the browser to the deep link URL, which opens the ZKProofport app
1282
+ * if installed. Only works in mobile browser environments. For desktop, use
1283
+ * requestProof() to display a QR code instead.
1284
+ *
1285
+ * @param request - ProofRequest object to send to the app
1286
+ *
1287
+ * @example
1288
+ * ```typescript
1289
+ * const request = sdk.createCoinbaseKycRequest({ scope: 'myapp.com' });
1290
+ * if (ProofportSDK.isMobile()) {
1291
+ * sdk.openProofRequest(request); // Opens app directly
1292
+ * }
1293
+ * ```
1294
+ */
1295
+ openProofRequest(request) {
1296
+ const url = this.getDeepLinkUrl(request);
1297
+ window.location.href = url;
1298
+ }
1299
+ /**
1300
+ * @internal
1301
+ * Requests a proof with automatic platform detection.
1302
+ *
1303
+ * Detects whether the user is on mobile or desktop and automatically chooses
1304
+ * the appropriate flow:
1305
+ * - Mobile: Opens the deep link directly to launch the ZKProofport app
1306
+ * - Desktop: Generates a QR code data URL for the user to scan
1307
+ *
1308
+ * @param request - ProofRequest object to send
1309
+ * @param qrOptions - QR code generation options (size, colors, logo) for desktop
1310
+ *
1311
+ * @returns Object containing deep link URL, optional QR code data URL, and platform detection result
1312
+ *
1313
+ * @example
1314
+ * ```typescript
1315
+ * const request = sdk.createCoinbaseKycRequest({ scope: 'myapp.com' });
1316
+ * const result = await sdk.requestProof(request);
1317
+ *
1318
+ * if (result.mobile) {
1319
+ * console.log('Opening app directly');
1320
+ * } else {
1321
+ * // Display QR code for desktop users
1322
+ * document.getElementById('qr').src = result.qrDataUrl;
1323
+ * }
1324
+ * ```
1325
+ */
1326
+ async requestProof(request, qrOptions) {
1327
+ const deepLink = this.getDeepLinkUrl(request);
1328
+ const mobile = ProofportSDK.isMobile();
1329
+ if (mobile) {
1330
+ window.location.href = deepLink;
1331
+ return { deepLink, mobile: true };
1332
+ }
1333
+ const qrDataUrl = await this.generateQRCode(request, qrOptions);
1334
+ return { deepLink, qrDataUrl, mobile: false };
1335
+ }
1336
+ // ============ QR Code Generation ============
1337
+ /**
1338
+ * Generates a QR code as a data URL for a proof request.
1339
+ *
1340
+ * Creates a PNG image data URL that can be directly set as an img src attribute.
1341
+ * Suitable for embedding QR codes in web pages for desktop users to scan with
1342
+ * their mobile device.
1343
+ *
1344
+ * @param requestOrUrl - ProofRequest object or deep link URL string
1345
+ * @param options - QR code customization options
1346
+ * @param options.size - QR code size in pixels (default: 256)
1347
+ * @param options.margin - Quiet zone margin in modules (default: 4)
1348
+ * @param options.darkColor - Color for dark modules (default: '#000000')
1349
+ * @param options.lightColor - Color for light modules (default: '#ffffff')
1350
+ * @param options.logoUrl - Optional center logo image URL
1351
+ * @param options.logoSize - Logo size as fraction of QR code (default: 0.2)
1352
+ *
1353
+ * @returns Promise resolving to PNG data URL (e.g., 'data:image/png;base64,...')
1354
+ *
1355
+ * @example
1356
+ * ```typescript
1357
+ * const request = sdk.createCoinbaseKycRequest({ scope: 'myapp.com' });
1358
+ * const qrUrl = await sdk.generateQRCode(request, {
1359
+ * size: 400,
1360
+ * darkColor: '#1a1a1a',
1361
+ * logoUrl: 'https://myapp.com/logo.png'
1362
+ * });
1363
+ * document.getElementById('qr-image').src = qrUrl;
1364
+ * ```
1365
+ */
1366
+ async generateQRCode(requestOrUrl, options) {
1367
+ return generateQRCodeDataUrl(requestOrUrl, options, this.config.scheme);
1368
+ }
1369
+ /**
1370
+ * Generates a QR code as an SVG string for a proof request.
1371
+ *
1372
+ * Creates a scalable vector graphics representation of the QR code, ideal for
1373
+ * high-resolution displays or when vector format is preferred over raster images.
1374
+ *
1375
+ * @param requestOrUrl - ProofRequest object or deep link URL string
1376
+ * @param options - QR code customization options
1377
+ * @param options.size - QR code size in pixels (default: 256)
1378
+ * @param options.margin - Quiet zone margin in modules (default: 4)
1379
+ * @param options.darkColor - Color for dark modules (default: '#000000')
1380
+ * @param options.lightColor - Color for light modules (default: '#ffffff')
1381
+ *
1382
+ * @returns Promise resolving to SVG string
1383
+ *
1384
+ * @example
1385
+ * ```typescript
1386
+ * const request = sdk.createCoinbaseKycRequest({ scope: 'myapp.com' });
1387
+ * const svgString = await sdk.generateQRCodeSVG(request);
1388
+ * document.getElementById('qr-container').innerHTML = svgString;
1389
+ * ```
1390
+ */
1391
+ async generateQRCodeSVG(requestOrUrl, options) {
1392
+ return generateQRCodeSVG(requestOrUrl, options, this.config.scheme);
1393
+ }
1394
+ /**
1395
+ * Renders a QR code directly to an HTML canvas element.
1396
+ *
1397
+ * Draws the QR code onto the provided canvas element, useful for custom
1398
+ * rendering workflows or when you need direct canvas manipulation.
1399
+ *
1400
+ * @param canvas - HTML canvas element to render to
1401
+ * @param requestOrUrl - ProofRequest object or deep link URL string
1402
+ * @param options - QR code customization options
1403
+ * @param options.size - QR code size in pixels (default: 256)
1404
+ * @param options.margin - Quiet zone margin in modules (default: 4)
1405
+ * @param options.darkColor - Color for dark modules (default: '#000000')
1406
+ * @param options.lightColor - Color for light modules (default: '#ffffff')
1407
+ *
1408
+ * @returns Promise that resolves when rendering is complete
1409
+ *
1410
+ * @example
1411
+ * ```typescript
1412
+ * const canvas = document.getElementById('qr-canvas') as HTMLCanvasElement;
1413
+ * const request = sdk.createCoinbaseKycRequest({ scope: 'myapp.com' });
1414
+ * await sdk.renderQRCodeToCanvas(canvas, request, { size: 400 });
1415
+ * ```
1416
+ */
1417
+ async renderQRCodeToCanvas(canvas, requestOrUrl, options) {
1418
+ return generateQRCodeToCanvas(canvas, requestOrUrl, options, this.config.scheme);
1419
+ }
1420
+ /**
1421
+ * Checks if a proof request fits within QR code size limits.
1422
+ *
1423
+ * Estimates the encoded data size and validates it against QR code capacity limits.
1424
+ * Useful for validating requests before generating QR codes, especially when
1425
+ * including large custom messages or metadata.
1426
+ *
1427
+ * @param requestOrUrl - ProofRequest object or deep link URL string
1428
+ *
1429
+ * @returns Object with size in bytes and whether it fits within limits
1430
+ *
1431
+ * @example
1432
+ * ```typescript
1433
+ * const request = sdk.createCoinbaseKycRequest({ scope: 'myapp.com' });
1434
+ * const check = sdk.checkQRCodeSize(request);
1435
+ * if (!check.withinLimit) {
1436
+ * console.warn(`Request too large: ${check.size} bytes`);
1437
+ * }
1438
+ * ```
1439
+ */
1440
+ checkQRCodeSize(requestOrUrl) {
1441
+ return estimateQRDataSize(requestOrUrl, this.config.scheme);
1442
+ }
1443
+ // ============ Response Handling ============
1444
+ /**
1445
+ * @internal
1446
+ * Parses a proof response from a callback URL.
1447
+ *
1448
+ * Extracts and decodes proof response data from the callback URL query parameters
1449
+ * after the user completes proof generation in the ZKProofport app. The app
1450
+ * redirects to your callback URL with the proof data encoded as query parameters.
1451
+ *
1452
+ * @param url - Callback URL containing proof response data
1453
+ *
1454
+ * @returns ProofResponse object with status, proof data, and public inputs, or null if invalid
1455
+ *
1456
+ * @example
1457
+ * ```typescript
1458
+ * // In your callback endpoint handler
1459
+ * app.get('/callback', (req, res) => {
1460
+ * const callbackUrl = req.url;
1461
+ * const response = sdk.parseResponse(callbackUrl);
1462
+ *
1463
+ * if (response?.status === 'completed') {
1464
+ * console.log('Proof received:', response.proof);
1465
+ * console.log('Public inputs:', response.publicInputs);
1466
+ * } else if (response?.status === 'rejected') {
1467
+ * console.log('User rejected the request');
1468
+ * }
1469
+ * });
1470
+ * ```
1471
+ */
1472
+ parseResponse(url) {
1473
+ return parseProofResponseUrl(url);
1474
+ }
1475
+ /**
1476
+ * @internal
1477
+ * Checks if a URL is a ZKProofport proof response callback.
1478
+ *
1479
+ * Validates whether the given URL contains the required query parameters for a
1480
+ * proof response. Useful for filtering and routing callback requests.
1481
+ *
1482
+ * @param url - URL to check
1483
+ *
1484
+ * @returns True if the URL appears to be a ZKProofport response callback
1485
+ *
1486
+ * @example
1487
+ * ```typescript
1488
+ * app.get('/callback', (req, res) => {
1489
+ * const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
1490
+ * if (sdk.isProofportResponse(fullUrl)) {
1491
+ * const response = sdk.parseResponse(fullUrl);
1492
+ * // Handle proof response
1493
+ * }
1494
+ * });
1495
+ * ```
1496
+ */
1497
+ isProofportResponse(url) {
1498
+ try {
1499
+ const urlObj = new URL(url);
1500
+ return urlObj.searchParams.has('requestId') && urlObj.searchParams.has('status');
1501
+ }
1502
+ catch {
1503
+ return false;
1504
+ }
1505
+ }
1506
+ /**
1507
+ * @internal
1508
+ * Retrieves a pending proof request by its ID.
1509
+ *
1510
+ * Looks up a previously created proof request from the SDK's internal cache.
1511
+ * Useful for validating callback responses and maintaining request state.
1512
+ *
1513
+ * @param requestId - Unique request identifier
1514
+ *
1515
+ * @returns ProofRequest object if found, undefined otherwise
1516
+ *
1517
+ * @example
1518
+ * ```typescript
1519
+ * app.get('/callback', (req, res) => {
1520
+ * const response = sdk.parseResponse(req.url);
1521
+ * const originalRequest = sdk.getPendingRequest(response.requestId);
1522
+ *
1523
+ * if (originalRequest) {
1524
+ * console.log('Original request:', originalRequest);
1525
+ * sdk.clearPendingRequest(response.requestId);
1526
+ * }
1527
+ * });
1528
+ * ```
1529
+ */
1530
+ getPendingRequest(requestId) {
1531
+ return this.pendingRequests.get(requestId);
1532
+ }
1533
+ /**
1534
+ * @internal
1535
+ * Clears a pending proof request from the internal cache.
1536
+ *
1537
+ * Removes a proof request from the SDK's pending requests map. Should be called
1538
+ * after successfully processing a proof response to prevent memory leaks.
1539
+ *
1540
+ * @param requestId - Unique request identifier to remove
1541
+ *
1542
+ * @example
1543
+ * ```typescript
1544
+ * app.get('/callback', (req, res) => {
1545
+ * const response = sdk.parseResponse(req.url);
1546
+ * if (response?.status === 'completed') {
1547
+ * // Process proof...
1548
+ * sdk.clearPendingRequest(response.requestId);
1549
+ * }
1550
+ * });
1551
+ * ```
1552
+ */
1553
+ clearPendingRequest(requestId) {
1554
+ this.pendingRequests.delete(requestId);
1555
+ }
1556
+ // ============ Verification ============
1557
+ /**
1558
+ * Verifies a zero-knowledge proof on-chain using the deployed verifier contract.
1559
+ *
1560
+ * Calls the Solidity verifier contract to cryptographically verify the proof's
1561
+ * validity. This ensures the proof was generated correctly and the public inputs
1562
+ * match the claimed values.
1563
+ *
1564
+ * @param circuit - Circuit type identifier
1565
+ * @param proof - Hex-encoded proof string from the ZKProofport app
1566
+ * @param publicInputs - Array of hex-encoded public input strings
1567
+ * @param providerOrSigner - ethers v6 Provider or Signer (defaults to base mainnet public RPC)
1568
+ *
1569
+ * @returns Promise resolving to verification result with valid boolean and optional error message
1570
+ *
1571
+ * @example
1572
+ * ```typescript
1573
+ * import { ethers } from 'ethers';
1574
+ *
1575
+ * const response = sdk.parseResponse(callbackUrl);
1576
+ * if (response?.status === 'completed') {
1577
+ * const provider = new ethers.JsonRpcProvider('https://mainnet.base.org');
1578
+ * const result = await sdk.verifyOnChain(
1579
+ * response.circuit,
1580
+ * response.proof,
1581
+ * response.publicInputs,
1582
+ * provider
1583
+ * );
1584
+ *
1585
+ * if (result.valid) {
1586
+ * console.log('Proof verified on-chain!');
1587
+ * } else {
1588
+ * console.error('Verification failed:', result.error);
1589
+ * }
1590
+ * }
1591
+ * ```
1592
+ */
1593
+ async verifyOnChain(circuit, proof, publicInputs, providerOrSigner) {
1594
+ const parsedProof = parseProofForOnChain(proof, publicInputs, publicInputs.length);
1595
+ const customVerifier = this.config.verifiers[circuit];
1596
+ return verifyProofOnChain(circuit, parsedProof, providerOrSigner, customVerifier);
1597
+ }
1598
+ /**
1599
+ * Verifies a proof response on-chain using the deployed verifier contract.
1600
+ *
1601
+ * Convenience method that extracts proof data from a ProofResponse object and
1602
+ * verifies it on-chain. Automatically handles incomplete or rejected responses.
1603
+ *
1604
+ * @param response - ProofResponse object from parseResponse()
1605
+ * @param providerOrSigner - ethers v6 Provider or Signer (defaults to base mainnet public RPC)
1606
+ *
1607
+ * @returns Promise resolving to verification result with valid boolean and optional error message
1608
+ *
1609
+ * @example
1610
+ * ```typescript
1611
+ * import { ethers } from 'ethers';
1612
+ *
1613
+ * app.get('/callback', async (req, res) => {
1614
+ * const response = sdk.parseResponse(req.url);
1615
+ *
1616
+ * if (response?.status === 'completed') {
1617
+ * const provider = new ethers.JsonRpcProvider('https://mainnet.base.org');
1618
+ * const result = await sdk.verifyResponseOnChain(response, provider);
1619
+ *
1620
+ * if (result.valid) {
1621
+ * // Grant access to user
1622
+ * res.json({ success: true, verified: true });
1623
+ * } else {
1624
+ * res.status(400).json({ error: result.error });
1625
+ * }
1626
+ * } else {
1627
+ * res.status(400).json({ error: 'Proof generation failed or rejected' });
1628
+ * }
1629
+ * });
1630
+ * ```
1631
+ */
1632
+ async verifyResponseOnChain(response, providerOrSigner) {
1633
+ if (response.status !== 'completed' || !response.proof || !response.publicInputs) {
1634
+ return { valid: false, error: 'Invalid or incomplete response' };
1635
+ }
1636
+ const parsedProof = parseProofForOnChain(response.proof, response.publicInputs, response.publicInputs.length);
1637
+ const customVerifier = this.config.verifiers[response.circuit];
1638
+ const responseVerifier = {
1639
+ verifierAddress: response.verifierAddress,
1640
+ chainId: response.chainId,
1641
+ };
1642
+ return verifyProofOnChain(response.circuit, parsedProof, providerOrSigner, customVerifier, responseVerifier);
1643
+ }
1644
+ // ============ Utility Methods ============
1645
+ /**
1646
+ * Gets the deployed verifier contract address for a circuit.
1647
+ *
1648
+ * Returns the Ethereum address of the Solidity verifier contract for the
1649
+ * specified circuit. Uses custom verifier if configured, otherwise returns
1650
+ * the default deployed address.
1651
+ *
1652
+ * @param circuit - Circuit type identifier
1653
+ *
1654
+ * @returns Ethereum address of the verifier contract (0x...)
1655
+ *
1656
+ * @example
1657
+ * ```typescript
1658
+ * const address = sdk.getVerifierAddress('coinbase_attestation');
1659
+ * console.log('Verifier deployed at:', address);
1660
+ * ```
1661
+ */
1662
+ getVerifierAddress(circuit) {
1663
+ const customVerifier = this.config.verifiers[circuit];
1664
+ return getVerifierAddress(circuit, customVerifier);
1665
+ }
1666
+ /**
1667
+ * Gets the blockchain chain ID where the verifier contract is deployed.
1668
+ *
1669
+ * Returns the EIP-155 chain ID for the network where the verifier contract
1670
+ * is deployed. Uses custom verifier if configured, otherwise returns the
1671
+ * default chain ID (Base mainnet: 8453).
1672
+ *
1673
+ * @param circuit - Circuit type identifier
1674
+ *
1675
+ * @returns Chain ID number (e.g., 8453 for Base mainnet)
1676
+ *
1677
+ * @example
1678
+ * ```typescript
1679
+ * const chainId = sdk.getVerifierChainId('coinbase_attestation');
1680
+ * console.log('Verifier on chain:', chainId); // 8453 (Base mainnet)
1681
+ * ```
1682
+ */
1683
+ getVerifierChainId(circuit) {
1684
+ const customVerifier = this.config.verifiers[circuit];
1685
+ return getVerifierChainId(circuit, customVerifier);
1686
+ }
1687
+ /**
1688
+ * Gets metadata for a specific circuit type.
1689
+ *
1690
+ * Returns circuit metadata including display name, description, required inputs,
1691
+ * and other configuration details.
1692
+ *
1693
+ * @param circuit - Circuit type identifier
1694
+ *
1695
+ * @returns Circuit metadata object with name, description, and configuration
1696
+ *
1697
+ * @example
1698
+ * ```typescript
1699
+ * const metadata = sdk.getCircuitMetadata('coinbase_attestation');
1700
+ * console.log('Circuit name:', metadata.name);
1701
+ * console.log('Description:', metadata.description);
1702
+ * ```
1703
+ */
1704
+ getCircuitMetadata(circuit) {
1705
+ return CIRCUIT_METADATA[circuit];
1706
+ }
1707
+ /**
1708
+ * Gets all supported circuit types.
1709
+ *
1710
+ * Returns an array of all circuit type identifiers that are currently supported
1711
+ * by the SDK and ZKProofport app.
1712
+ *
1713
+ * @returns Array of supported circuit type identifiers
1714
+ *
1715
+ * @example
1716
+ * ```typescript
1717
+ * const circuits = sdk.getSupportedCircuits();
1718
+ * console.log('Supported circuits:', circuits);
1719
+ * // ['coinbase_attestation', 'coinbase_country_attestation']
1720
+ * ```
1721
+ */
1722
+ getSupportedCircuits() {
1723
+ return Object.keys(CIRCUIT_METADATA);
1724
+ }
1725
+ /**
1726
+ * @internal
1727
+ * Validates a proof request for completeness and correctness.
1728
+ *
1729
+ * Checks that the proof request contains all required fields and that the
1730
+ * circuit-specific inputs are valid. Useful for catching configuration errors
1731
+ * before generating QR codes or sending requests to users.
1732
+ *
1733
+ * @param request - ProofRequest object to validate
1734
+ *
1735
+ * @returns Validation result with valid boolean and optional error message
1736
+ *
1737
+ * @example
1738
+ * ```typescript
1739
+ * const request = sdk.createCoinbaseKycRequest({ scope: 'myapp.com' });
1740
+ * const validation = sdk.validateRequest(request);
1741
+ *
1742
+ * if (!validation.valid) {
1743
+ * console.error('Invalid request:', validation.error);
1744
+ * }
1745
+ * ```
1746
+ */
1747
+ validateRequest(request) {
1748
+ return validateProofRequest(request);
1749
+ }
1750
+ /**
1751
+ * @internal
1752
+ * Checks if a URL is a ZKProofport deep link.
1753
+ *
1754
+ * Validates whether the given URL uses the ZKProofport deep link scheme
1755
+ * (zkproofport:// by default) and has the correct format for a proof request.
1756
+ *
1757
+ * @param url - URL string to check
1758
+ *
1759
+ * @returns True if the URL is a valid ZKProofport deep link
1760
+ *
1761
+ * @example
1762
+ * ```typescript
1763
+ * const url = 'zkproofport://proof?requestId=abc123&circuit=coinbase_attestation';
1764
+ * if (sdk.isProofportDeepLink(url)) {
1765
+ * const request = sdk.parseDeepLink(url);
1766
+ * console.log('Parsed request:', request);
1767
+ * }
1768
+ * ```
1769
+ */
1770
+ isProofportDeepLink(url) {
1771
+ return isProofportDeepLink(url, this.config.scheme);
1772
+ }
1773
+ /**
1774
+ * @internal
1775
+ * Parses a proof request from a deep link URL.
1776
+ *
1777
+ * Extracts and decodes the proof request data from a zkproofport:// deep link URL.
1778
+ * Useful for handling deep link navigation in web applications or validating
1779
+ * deep link URLs before displaying QR codes.
1780
+ *
1781
+ * @param url - ZKProofport deep link URL string
1782
+ *
1783
+ * @returns ProofRequest object with all request details, or null if invalid
1784
+ *
1785
+ * @example
1786
+ * ```typescript
1787
+ * const deepLink = 'zkproofport://proof?requestId=abc123&circuit=coinbase_attestation&...';
1788
+ * const request = sdk.parseDeepLink(deepLink);
1789
+ *
1790
+ * if (request) {
1791
+ * console.log('Request ID:', request.requestId);
1792
+ * console.log('Circuit:', request.circuit);
1793
+ * }
1794
+ * ```
1795
+ */
1796
+ parseDeepLink(url) {
1797
+ return parseProofRequestUrl(url);
1798
+ }
1799
+ // ============ Static Factory ============
1800
+ /**
1801
+ * Creates a new ProofportSDK instance with environment preset or custom config.
1802
+ * Defaults to `'production'` if no argument is provided.
1803
+ *
1804
+ * **Recommended usage** — use the default production relay:
1805
+ * ```typescript
1806
+ * const sdk = ProofportSDK.create();
1807
+ * ```
1808
+ *
1809
+ * Environment presets:
1810
+ * - `'production'` — relay.zkproofport.app (default)
1811
+ * - `'staging'` — stg-relay.zkproofport.app
1812
+ * - `'local'` — localhost:4001
1813
+ *
1814
+ * @param envOrConfig - Environment name or custom SDK configuration
1815
+ *
1816
+ * @returns New ProofportSDK instance
1817
+ *
1818
+ * @example
1819
+ * ```typescript
1820
+ * // Environment preset (recommended)
1821
+ * const sdk = ProofportSDK.create(); // production (default)
1822
+ * const sdk = ProofportSDK.create('staging');
1823
+ *
1824
+ * // Custom config
1825
+ * const sdk = ProofportSDK.create({
1826
+ * relayUrl: 'https://my-custom-relay.example.com',
1827
+ * });
1828
+ * ```
1829
+ */
1830
+ static create(envOrConfig) {
1831
+ if (typeof envOrConfig === 'undefined') {
1832
+ return new ProofportSDK({ relayUrl: RELAY_URLS.production });
1833
+ }
1834
+ if (typeof envOrConfig === 'string') {
1835
+ const relayUrl = RELAY_URLS[envOrConfig];
1836
+ if (!relayUrl) {
1837
+ throw new Error(`Unknown environment: ${envOrConfig}. Use 'production', 'staging', or 'local'.`);
1838
+ }
1839
+ return new ProofportSDK({ relayUrl });
1840
+ }
1841
+ return new ProofportSDK(envOrConfig);
1842
+ }
1843
+ /**
1844
+ * Detects if the code is running on a mobile device.
1845
+ *
1846
+ * Uses user agent detection to determine if the current environment is a mobile
1847
+ * browser. This helps the SDK automatically choose between direct deep link
1848
+ * navigation (mobile) and QR code display (desktop).
1849
+ *
1850
+ * @returns True if running on a mobile device (iOS, Android, etc.)
1851
+ *
1852
+ * @example
1853
+ * ```typescript
1854
+ * if (ProofportSDK.isMobile()) {
1855
+ * // Open deep link directly
1856
+ * sdk.openProofRequest(request);
1857
+ * } else {
1858
+ * // Show QR code
1859
+ * const qr = await sdk.generateQRCode(request);
1860
+ * displayQRCode(qr);
1861
+ * }
1862
+ * ```
1863
+ */
1864
+ static isMobile() {
1865
+ if (typeof navigator === 'undefined')
1866
+ return false;
1867
+ return /Android|iPhone|iPad|iPod|webOS|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
1868
+ }
1869
+ /**
1870
+ * Authenticates with ZKProofport using client credentials via the relay server.
1871
+ *
1872
+ * Exchanges a client_id and api_key pair for a short-lived JWT token
1873
+ * that can be used to authenticate relay requests.
1874
+ *
1875
+ * @param credentials - Client ID and API key
1876
+ * @param relayUrl - Relay server URL (e.g., 'https://relay.zkproofport.app')
1877
+ * @returns Promise resolving to AuthToken with JWT token and metadata
1878
+ * @throws Error if authentication fails
1879
+ *
1880
+ * @example
1881
+ * ```typescript
1882
+ * const auth = await ProofportSDK.authenticate(
1883
+ * { clientId: 'your-client-id', apiKey: 'your-api-key' },
1884
+ * 'https://relay.zkproofport.app'
1885
+ * );
1886
+ * console.log('Token:', auth.token);
1887
+ * console.log('Expires in:', auth.expiresIn, 'seconds');
1888
+ * ```
1889
+ */
1890
+ static async authenticate(credentials, relayUrl) {
1891
+ if (!credentials.clientId || !credentials.apiKey) {
1892
+ throw new Error('clientId and apiKey are required');
1893
+ }
1894
+ if (!relayUrl) {
1895
+ throw new Error('relayUrl is required');
1896
+ }
1897
+ const response = await fetch(`${relayUrl}/api/v1/auth/token`, {
1898
+ method: 'POST',
1899
+ headers: { 'Content-Type': 'application/json' },
1900
+ body: JSON.stringify({
1901
+ client_id: credentials.clientId,
1902
+ api_key: credentials.apiKey,
1903
+ }),
1904
+ });
1905
+ if (!response.ok) {
1906
+ const error = await response.json().catch(() => ({ error: `HTTP ${response.status}` }));
1907
+ throw new Error(error.error || `Authentication failed: HTTP ${response.status}`);
1908
+ }
1909
+ const data = await response.json();
1910
+ return {
1911
+ token: data.token,
1912
+ clientId: data.client_id,
1913
+ dappId: data.dapp_id,
1914
+ tier: data.tier,
1915
+ expiresIn: data.expires_in,
1916
+ expiresAt: Date.now() + (data.expires_in * 1000),
1917
+ };
1918
+ }
1919
+ /**
1920
+ * Checks if an auth token is still valid (not expired).
1921
+ *
1922
+ * @param auth - AuthToken to check
1923
+ * @returns True if the token has not expired
1924
+ *
1925
+ * @example
1926
+ * ```typescript
1927
+ * if (!ProofportSDK.isTokenValid(auth)) {
1928
+ * auth = await ProofportSDK.authenticate(credentials, relayUrl);
1929
+ * }
1930
+ * ```
1931
+ */
1932
+ static isTokenValid(auth) {
1933
+ return Date.now() < auth.expiresAt - 30000; // 30s buffer
1934
+ }
1935
+ // ============ Relay Integration ============
1936
+ /**
1937
+ * Authenticates with ZKProofport and stores the token for relay requests.
1938
+ *
1939
+ * Instance method that authenticates via the relay server and stores
1940
+ * the JWT token internally, so subsequent relay requests are automatically authenticated.
1941
+ *
1942
+ * @param credentials - Client ID and API key
1943
+ * @returns Promise resolving to AuthToken
1944
+ * @throws Error if authentication fails or relayUrl is not configured
1945
+ *
1946
+ * @example
1947
+ * ```typescript
1948
+ * const sdk = ProofportSDK.create('production');
1949
+ *
1950
+ * await sdk.login({ clientId: 'your-id', apiKey: 'your-key' });
1951
+ * // SDK is now authenticated for relay requests
1952
+ * ```
1953
+ */
1954
+ async login(credentials) {
1955
+ if (!this.relayUrl) {
1956
+ throw new Error('relayUrl is required for authentication. Use ProofportSDK.create(\'production\') or set relayUrl in config.');
1957
+ }
1958
+ this.authToken = await ProofportSDK.authenticate(credentials, this.relayUrl);
1959
+ return this.authToken;
1960
+ }
1961
+ /**
1962
+ * Logs out by clearing the stored authentication token.
1963
+ */
1964
+ logout() {
1965
+ this.authToken = null;
1966
+ }
1967
+ /**
1968
+ * Returns whether the SDK instance is currently authenticated with a valid token.
1969
+ */
1970
+ isAuthenticated() {
1971
+ return this.authToken !== null && ProofportSDK.isTokenValid(this.authToken);
1972
+ }
1973
+ /**
1974
+ * Returns the current auth token, or null if not authenticated.
1975
+ */
1976
+ getAuthToken() {
1977
+ return this.authToken;
1978
+ }
1979
+ /**
1980
+ * Creates a proof request through the relay server.
1981
+ *
1982
+ * This is the recommended way to create proof requests. The relay server:
1983
+ * - Issues a server-side requestId (validated by the mobile app)
1984
+ * - Tracks request status in Redis
1985
+ * - Handles credit deduction and tier enforcement
1986
+ * - Builds the deep link with relay callback URL
1987
+ *
1988
+ * @param circuit - Circuit type identifier
1989
+ * @param inputs - Circuit-specific inputs
1990
+ * @param options - Request options (message, dappName, dappIcon, nonce)
1991
+ * @returns Promise resolving to RelayProofRequest with requestId, deepLink, pollUrl
1992
+ * @throws Error if not authenticated or relay request fails
1993
+ *
1994
+ * @example
1995
+ * ```typescript
1996
+ * const sdk = ProofportSDK.create();
1997
+ * await sdk.login({ clientId: 'id', apiKey: 'key' });
1998
+ *
1999
+ * const relay = await sdk.createRelayRequest('coinbase_attestation', {
2000
+ * scope: 'myapp.com'
2001
+ * }, { dappName: 'My DApp' });
2002
+ *
2003
+ * // Generate QR code from relay deep link
2004
+ * const qr = await sdk.generateQRCode(relay.deepLink);
2005
+ *
2006
+ * // Wait for proof (WebSocket primary, polling fallback)
2007
+ * const result = await sdk.waitForProof(relay.requestId);
2008
+ * ```
2009
+ */
2010
+ async createRelayRequest(circuit, inputs, options = {}) {
2011
+ if (!this.authToken || !ProofportSDK.isTokenValid(this.authToken)) {
2012
+ throw new Error('Not authenticated. Call login() first.');
2013
+ }
2014
+ if (!this.relayUrl) {
2015
+ throw new Error('relayUrl is required. Set it in ProofportSDK config.');
2016
+ }
2017
+ const body = {
2018
+ circuitId: circuit,
2019
+ inputs,
2020
+ };
2021
+ if (options.message)
2022
+ body.message = options.message;
2023
+ if (options.dappName)
2024
+ body.dappName = options.dappName;
2025
+ if (options.dappIcon)
2026
+ body.dappIcon = options.dappIcon;
2027
+ if (options.nonce)
2028
+ body.nonce = options.nonce;
2029
+ const response = await fetch(`${this.relayUrl}/api/v1/proof/request`, {
2030
+ method: 'POST',
2031
+ headers: {
2032
+ 'Content-Type': 'application/json',
2033
+ 'Authorization': `Bearer ${this.authToken.token}`,
2034
+ },
2035
+ body: JSON.stringify(body),
2036
+ });
2037
+ if (!response.ok) {
2038
+ const error = await response.json().catch(() => ({ error: `HTTP ${response.status}` }));
2039
+ throw new Error(error.error || `Relay request failed: HTTP ${response.status}`);
2040
+ }
2041
+ return await response.json();
2042
+ }
2043
+ /**
2044
+ * Polls the relay for proof result status.
2045
+ *
2046
+ * @param requestId - The relay-issued request ID
2047
+ * @returns Promise resolving to RelayProofResult
2048
+ * @throws Error if relay URL not configured or request not found
2049
+ *
2050
+ * @example
2051
+ * ```typescript
2052
+ * const result = await sdk.pollResult(relay.requestId);
2053
+ * if (result.status === 'completed') {
2054
+ * console.log('Proof:', result.proof);
2055
+ * console.log('Public inputs:', result.publicInputs);
2056
+ * }
2057
+ * ```
2058
+ */
2059
+ async pollResult(requestId) {
2060
+ if (!this.relayUrl) {
2061
+ throw new Error('relayUrl is required. Set it in ProofportSDK config.');
2062
+ }
2063
+ const response = await fetch(`${this.relayUrl}/api/v1/proof/${requestId}`);
2064
+ if (!response.ok) {
2065
+ if (response.status === 404) {
2066
+ throw new Error('Request not found or expired');
2067
+ }
2068
+ const error = await response.json().catch(() => ({ error: `HTTP ${response.status}` }));
2069
+ throw new Error(error.error || `Poll failed: HTTP ${response.status}`);
2070
+ }
2071
+ return await response.json();
2072
+ }
2073
+ /**
2074
+ * Polls the relay until proof is completed or failed, with configurable interval and timeout.
2075
+ *
2076
+ * @param requestId - The relay-issued request ID
2077
+ * @param options - Polling options
2078
+ * @param options.intervalMs - Polling interval in milliseconds (default: 2000)
2079
+ * @param options.timeoutMs - Maximum polling time in milliseconds (default: 300000 = 5 min)
2080
+ * @param options.onStatusChange - Callback when status changes
2081
+ * @returns Promise resolving to final RelayProofResult
2082
+ * @throws Error if timeout or relay error
2083
+ */
2084
+ async waitForResult(requestId, options = {}) {
2085
+ const interval = options.intervalMs || 2000;
2086
+ const timeout = options.timeoutMs || 300000;
2087
+ const startTime = Date.now();
2088
+ let lastStatus = '';
2089
+ while (Date.now() - startTime < timeout) {
2090
+ const result = await this.pollResult(requestId);
2091
+ if (result.status !== lastStatus) {
2092
+ lastStatus = result.status;
2093
+ options.onStatusChange?.(result);
2094
+ }
2095
+ if (result.status === 'completed' || result.status === 'failed') {
2096
+ return result;
2097
+ }
2098
+ await new Promise(resolve => setTimeout(resolve, interval));
2099
+ }
2100
+ throw new Error(`Polling timed out after ${timeout}ms`);
2101
+ }
2102
+ /**
2103
+ * Subscribes to real-time proof status updates via Socket.IO.
2104
+ *
2105
+ * This is the recommended way to receive proof results. Uses WebSocket
2106
+ * connection for instant delivery instead of polling.
2107
+ *
2108
+ * Requires `socket.io-client` package: `npm install socket.io-client`
2109
+ *
2110
+ * @param requestId - The relay-issued request ID to subscribe to
2111
+ * @param callbacks - Event callbacks for status changes and results
2112
+ * @param callbacks.onStatus - Called on status updates (pending, generating)
2113
+ * @param callbacks.onResult - Called when proof is completed or failed
2114
+ * @param callbacks.onError - Called on errors
2115
+ * @returns Unsubscribe function to clean up the connection
2116
+ * @throws Error if not authenticated, relayUrl not set, or socket.io-client not installed
2117
+ *
2118
+ * @example
2119
+ * ```typescript
2120
+ * const relay = await sdk.createRelayRequest('coinbase_attestation', { scope: 'myapp.com' });
2121
+ * const qr = await sdk.generateQRCode(relay.deepLink);
2122
+ *
2123
+ * const unsubscribe = await sdk.subscribe(relay.requestId, {
2124
+ * onResult: (result) => {
2125
+ * if (result.status === 'completed') {
2126
+ * console.log('Proof received!', result.proof);
2127
+ * }
2128
+ * },
2129
+ * onError: (error) => console.error('Error:', error),
2130
+ * });
2131
+ *
2132
+ * // Later: clean up
2133
+ * unsubscribe();
2134
+ * ```
2135
+ */
2136
+ async subscribe(requestId, callbacks) {
2137
+ if (!this.authToken || !ProofportSDK.isTokenValid(this.authToken)) {
2138
+ throw new Error('Not authenticated. Call login() first.');
2139
+ }
2140
+ if (!this.relayUrl) {
2141
+ throw new Error('relayUrl is required. Set it in ProofportSDK config.');
2142
+ }
2143
+ let ioConnect;
2144
+ try {
2145
+ const mod = await import('socket.io-client');
2146
+ ioConnect = mod.io ?? mod.connect ?? mod.default;
2147
+ }
2148
+ catch {
2149
+ throw new Error('socket.io-client is required for real-time updates. Install it: npm install socket.io-client');
2150
+ }
2151
+ if (typeof ioConnect !== 'function') {
2152
+ throw new Error('Failed to load socket.io-client: io function not found');
2153
+ }
2154
+ // Connect to relay /proof namespace
2155
+ const socket = ioConnect(`${this.relayUrl}/proof`, {
2156
+ path: '/socket.io',
2157
+ auth: { token: this.authToken.token },
2158
+ transports: ['websocket', 'polling'],
2159
+ });
2160
+ this.socket = socket;
2161
+ // Subscribe to the request room
2162
+ socket.on('connect', () => {
2163
+ socket.emit('proof:subscribe', { requestId });
2164
+ });
2165
+ // Handle connection errors (e.g. rejected by relay middleware)
2166
+ socket.on('connect_error', (err) => {
2167
+ console.error(`[ProofportSDK] Socket.IO connect_error for requestId=${requestId}: ${err.message}`);
2168
+ callbacks.onError?.({ error: err.message, requestId });
2169
+ });
2170
+ // Listen for events
2171
+ if (callbacks.onStatus) {
2172
+ socket.on('proof:status', callbacks.onStatus);
2173
+ }
2174
+ if (callbacks.onResult) {
2175
+ socket.on('proof:result', callbacks.onResult);
2176
+ }
2177
+ if (callbacks.onError) {
2178
+ socket.on('proof:error', callbacks.onError);
2179
+ }
2180
+ // Return unsubscribe function
2181
+ return () => {
2182
+ socket.off('proof:status');
2183
+ socket.off('proof:result');
2184
+ socket.off('proof:error');
2185
+ socket.disconnect();
2186
+ if (this.socket === socket) {
2187
+ this.socket = null;
2188
+ }
2189
+ };
2190
+ }
2191
+ /**
2192
+ * Waits for a proof result using Socket.IO (primary) with polling fallback.
2193
+ *
2194
+ * Tries Socket.IO first for real-time delivery. If socket.io-client is not
2195
+ * installed or connection fails, automatically falls back to HTTP polling.
2196
+ *
2197
+ * @param requestId - The relay-issued request ID
2198
+ * @param options - Configuration options
2199
+ * @param options.timeoutMs - Maximum wait time in ms (default: 300000 = 5 min)
2200
+ * @param options.onStatusChange - Callback for status updates
2201
+ * @returns Promise resolving to final RelayProofResult
2202
+ */
2203
+ async waitForProof(requestId, options = {}) {
2204
+ const timeout = options.timeoutMs || 300000;
2205
+ // Try Socket.IO first
2206
+ if (this.authToken && ProofportSDK.isTokenValid(this.authToken) && this.relayUrl) {
2207
+ try {
2208
+ return await new Promise((resolve, reject) => {
2209
+ const timer = setTimeout(() => {
2210
+ unsubscribePromise?.then(fn => fn());
2211
+ reject(new Error(`Waiting for proof timed out after ${timeout}ms`));
2212
+ }, timeout);
2213
+ const unsubscribePromise = this.subscribe(requestId, {
2214
+ onStatus: (data) => {
2215
+ options.onStatusChange?.(data);
2216
+ },
2217
+ onResult: (result) => {
2218
+ clearTimeout(timer);
2219
+ unsubscribePromise?.then(fn => fn());
2220
+ resolve(result);
2221
+ },
2222
+ onError: (error) => {
2223
+ clearTimeout(timer);
2224
+ unsubscribePromise?.then(fn => fn());
2225
+ reject(new Error(error.error));
2226
+ },
2227
+ });
2228
+ });
2229
+ }
2230
+ catch (err) {
2231
+ // Re-throw timeout — that's a real failure the caller should handle
2232
+ if (err.message?.includes('timed out')) {
2233
+ throw err;
2234
+ }
2235
+ // socket.io-client missing or connection failed → fall back to polling silently
2236
+ console.warn('Socket.IO unavailable, falling back to HTTP polling:', err.message);
2237
+ }
2238
+ }
2239
+ // Fallback: HTTP polling
2240
+ return this.waitForResult(requestId, {
2241
+ timeoutMs: timeout,
2242
+ onStatusChange: options.onStatusChange,
2243
+ });
2244
+ }
2245
+ /**
2246
+ * Disconnects the Socket.IO connection if active.
2247
+ */
2248
+ disconnect() {
2249
+ if (this.socket) {
2250
+ this.socket.disconnect();
2251
+ this.socket = null;
2252
+ }
2253
+ }
2254
+ // ============ Nullifier Utilities ============
2255
+ /**
2256
+ * Extracts the nullifier from proof public inputs.
2257
+ *
2258
+ * The nullifier is a bytes32 value derived from the user's address and scope,
2259
+ * used to prevent duplicate proof submissions. Each user+scope combination
2260
+ * produces a unique nullifier.
2261
+ *
2262
+ * @param publicInputs - Array of public input hex strings from proof response
2263
+ * @param circuit - Circuit type to determine field positions
2264
+ * @returns Nullifier as hex string (0x...), or null if inputs are insufficient
2265
+ *
2266
+ * @example
2267
+ * ```typescript
2268
+ * const result = await sdk.waitForProof(relay.requestId);
2269
+ * if (result.status === 'completed') {
2270
+ * const nullifier = sdk.extractNullifier(result.publicInputs, result.circuit);
2271
+ * console.log('Nullifier:', nullifier);
2272
+ * }
2273
+ * ```
2274
+ */
2275
+ extractNullifier(publicInputs, circuit) {
2276
+ return extractNullifierFromPublicInputs(publicInputs, circuit);
2277
+ }
2278
+ /**
2279
+ * Extracts the scope from proof public inputs.
2280
+ *
2281
+ * The scope is an application-specific identifier (e.g., domain name) encoded
2282
+ * as bytes32 in the proof's public inputs.
2283
+ *
2284
+ * @param publicInputs - Array of public input hex strings from proof response
2285
+ * @param circuit - Circuit type to determine field positions
2286
+ * @returns Scope as hex string (0x...), or null if inputs are insufficient
2287
+ *
2288
+ * @example
2289
+ * ```typescript
2290
+ * const result = await sdk.waitForProof(relay.requestId);
2291
+ * if (result.status === 'completed') {
2292
+ * const scope = sdk.extractScope(result.publicInputs, result.circuit);
2293
+ * console.log('Scope:', scope);
2294
+ * }
2295
+ * ```
2296
+ */
2297
+ extractScope(publicInputs, circuit) {
2298
+ return extractScopeFromPublicInputs(publicInputs, circuit);
2299
+ }
2300
+ /**
2301
+ * Checks if a nullifier is already registered on-chain.
2302
+ *
2303
+ * Queries the ZKProofportNullifierRegistry contract to determine if the
2304
+ * nullifier has been used before. Used to prevent duplicate proof submissions.
2305
+ *
2306
+ * Requires `nullifierRegistry` in SDK config.
2307
+ *
2308
+ * @param nullifier - Nullifier hex string from extractNullifier()
2309
+ * @param provider - Optional ethers provider (defaults to public RPC for configured chain)
2310
+ * @returns True if nullifier is already registered
2311
+ * @throws Error if nullifierRegistry is not configured
2312
+ *
2313
+ * @example
2314
+ * ```typescript
2315
+ * const sdk = ProofportSDK.create({
2316
+ * relayUrl: 'https://relay.zkproofport.app',
2317
+ * nullifierRegistry: { address: '0x...', chainId: 8453 }
2318
+ * });
2319
+ *
2320
+ * const nullifier = sdk.extractNullifier(publicInputs, circuit);
2321
+ * const isDuplicate = await sdk.checkNullifier(nullifier);
2322
+ * ```
2323
+ */
2324
+ async checkNullifier(nullifier, provider) {
2325
+ if (!this.nullifierRegistry) {
2326
+ throw new Error('nullifierRegistry is required. Set it in ProofportSDK config.');
2327
+ }
2328
+ const p = provider || getDefaultProvider(this.nullifierRegistry.chainId);
2329
+ return isNullifierRegistered(nullifier, this.nullifierRegistry.address, p);
2330
+ }
2331
+ /**
2332
+ * Gets detailed information about a registered nullifier from on-chain registry.
2333
+ *
2334
+ * Retrieves the registration timestamp, scope, and circuit ID for a nullifier.
2335
+ * Returns null if the nullifier is not registered.
2336
+ *
2337
+ * Requires `nullifierRegistry` in SDK config.
2338
+ *
2339
+ * @param nullifier - Nullifier hex string from extractNullifier()
2340
+ * @param provider - Optional ethers provider (defaults to public RPC for configured chain)
2341
+ * @returns Nullifier info or null if not registered
2342
+ * @throws Error if nullifierRegistry is not configured
2343
+ *
2344
+ * @example
2345
+ * ```typescript
2346
+ * const info = await sdk.getNullifierDetails(nullifier);
2347
+ * if (info) {
2348
+ * console.log('Registered at:', new Date(info.registeredAt * 1000));
2349
+ * console.log('Circuit:', info.circuitId);
2350
+ * }
2351
+ * ```
2352
+ */
2353
+ async getNullifierDetails(nullifier, provider) {
2354
+ if (!this.nullifierRegistry) {
2355
+ throw new Error('nullifierRegistry is required. Set it in ProofportSDK config.');
2356
+ }
2357
+ const p = provider || getDefaultProvider(this.nullifierRegistry.chainId);
2358
+ return getNullifierInfo(nullifier, this.nullifierRegistry.address, p);
2359
+ }
2360
+ }
2361
+
2362
+ exports.ProofportSDK = ProofportSDK;
2363
+ exports.default = ProofportSDK;
2364
+ //# sourceMappingURL=index.js.map