zkenclave-sdk 0.1.12 → 0.1.18

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.d.mts CHANGED
@@ -93,11 +93,9 @@ declare class ZKProofClient {
93
93
  private loadWasm;
94
94
  generateWithdrawalProof(request: WithdrawalRequest): Promise<WithdrawalResult>;
95
95
  private generateRealProof;
96
- private generateFallbackProof;
97
96
  generateComplianceProof(commitment: Uint8Array, associationPath: Uint8Array[], pathIndices: boolean[], associationRoot: Uint8Array): Promise<ComplianceProof>;
98
97
  verifyProof(proofResult: WithdrawalResult): Promise<boolean>;
99
98
  isWasmReady(): boolean;
100
- private computeNullifierHash;
101
99
  private addressToBytes;
102
100
  private hexToBytes;
103
101
  }
package/dist/index.d.ts CHANGED
@@ -93,11 +93,9 @@ declare class ZKProofClient {
93
93
  private loadWasm;
94
94
  generateWithdrawalProof(request: WithdrawalRequest): Promise<WithdrawalResult>;
95
95
  private generateRealProof;
96
- private generateFallbackProof;
97
96
  generateComplianceProof(commitment: Uint8Array, associationPath: Uint8Array[], pathIndices: boolean[], associationRoot: Uint8Array): Promise<ComplianceProof>;
98
97
  verifyProof(proofResult: WithdrawalResult): Promise<boolean>;
99
98
  isWasmReady(): boolean;
100
- private computeNullifierHash;
101
99
  private addressToBytes;
102
100
  private hexToBytes;
103
101
  }
package/dist/index.js CHANGED
@@ -426,7 +426,7 @@ var ZKProofClient = class {
426
426
  wasmReady = false;
427
427
  constructor(config) {
428
428
  this.config = config ?? { useRealProofs: true };
429
- if (this.config.useRealProofs) {
429
+ if (this.config.useRealProofs !== false) {
430
430
  this.loadWasm();
431
431
  }
432
432
  }
@@ -435,24 +435,21 @@ var ZKProofClient = class {
435
435
  this.wasmReady = true;
436
436
  return;
437
437
  }
438
- try {
439
- const wasmPath = this.config.wasmPath ?? "zkenclave-circuits";
440
- const module2 = await import(
441
- /* webpackIgnore: true */
442
- wasmPath
443
- );
444
- wasmModule = module2;
445
- this.wasmReady = true;
446
- } catch {
447
- console.warn("WASM module not available, falling back to mock proofs");
448
- this.wasmReady = false;
449
- }
438
+ const wasmPath = this.config.wasmPath ?? "zkenclave-circuits";
439
+ const module2 = await import(
440
+ /* webpackIgnore: true */
441
+ wasmPath
442
+ );
443
+ wasmModule = module2;
444
+ this.wasmReady = true;
450
445
  }
451
446
  async generateWithdrawalProof(request) {
452
- if (this.config.useRealProofs && this.wasmReady && wasmModule) {
453
- return this.generateRealProof(request);
447
+ if (!this.wasmReady || !wasmModule) {
448
+ throw new Error(
449
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
450
+ );
454
451
  }
455
- return this.generateFallbackProof(request);
452
+ return this.generateRealProof(request);
456
453
  }
457
454
  async generateRealProof(request) {
458
455
  const wasmRequest = {
@@ -480,81 +477,51 @@ var ZKProofClient = class {
480
477
  timestamp: Date.now()
481
478
  };
482
479
  }
483
- async generateFallbackProof(request) {
484
- const nullifierHash = this.computeNullifierHash(
485
- request.nullifier,
486
- request.leafIndex
487
- );
488
- const merkleRoot = request.merkleRoot ?? new Uint8Array(32);
489
- const proof = new Uint8Array(256);
490
- proof[0] = 1;
491
- const amountHex = (0, import_ethers2.toBeHex)(request.amount, 32);
492
- const amountBytes = this.hexToBytes(amountHex);
493
- proof.set(amountBytes.slice(0, 32), 1);
494
- proof.set(request.commitment.slice(0, 32), 33);
495
- proof[250] = 90;
496
- proof[251] = 75;
497
- return {
498
- success: true,
499
- zkProof: proof,
500
- nullifierHash,
501
- merkleRoot,
502
- timestamp: Date.now()
503
- };
504
- }
505
480
  async generateComplianceProof(commitment, associationPath, pathIndices, associationRoot) {
506
- if (this.config.useRealProofs && this.wasmReady && wasmModule) {
507
- const request = {
508
- commitment: Array.from(commitment),
509
- association_path: associationPath.map((p) => Array.from(p)),
510
- path_indices: pathIndices,
511
- association_root: Array.from(associationRoot)
512
- };
513
- const resultJson = wasmModule.generate_compliance_proof(
514
- JSON.stringify(request)
481
+ if (!this.wasmReady || !wasmModule) {
482
+ throw new Error(
483
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
515
484
  );
516
- const result = JSON.parse(resultJson);
517
- if (!result.success) {
518
- throw new Error(`Compliance proof generation failed: ${result.error}`);
519
- }
520
- return {
521
- id: (0, import_ethers2.keccak256)(new Uint8Array(result.proof)),
522
- associationRoot,
523
- timestamp: Date.now(),
524
- valid: true,
525
- proof: new Uint8Array(result.proof)
526
- };
485
+ }
486
+ const request = {
487
+ commitment: Array.from(commitment),
488
+ association_path: associationPath.map((p) => Array.from(p)),
489
+ path_indices: pathIndices,
490
+ association_root: Array.from(associationRoot)
491
+ };
492
+ const resultJson = wasmModule.generate_compliance_proof(
493
+ JSON.stringify(request)
494
+ );
495
+ const result = JSON.parse(resultJson);
496
+ if (!result.success) {
497
+ throw new Error(`Compliance proof generation failed: ${result.error}`);
527
498
  }
528
499
  return {
529
- id: "mock-compliance-proof",
500
+ id: (0, import_ethers2.keccak256)(new Uint8Array(result.proof)),
530
501
  associationRoot,
531
502
  timestamp: Date.now(),
532
503
  valid: true,
533
- proof: new Uint8Array(64).fill(1)
504
+ proof: new Uint8Array(result.proof)
534
505
  };
535
506
  }
536
507
  async verifyProof(proofResult) {
537
- if (this.wasmReady && wasmModule) {
538
- const proofJson = JSON.stringify({
539
- success: proofResult.success,
540
- proof: Array.from(proofResult.zkProof),
541
- nullifier_hash: Array.from(proofResult.nullifierHash),
542
- public_inputs: [],
543
- error: null
544
- });
545
- return wasmModule.verify_withdrawal_proof(proofJson);
508
+ if (!this.wasmReady || !wasmModule) {
509
+ throw new Error(
510
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
511
+ );
546
512
  }
547
- return proofResult.success && proofResult.zkProof.length > 0 && proofResult.zkProof[250] === 90 && proofResult.zkProof[251] === 75;
513
+ const proofJson = JSON.stringify({
514
+ success: proofResult.success,
515
+ proof: Array.from(proofResult.zkProof),
516
+ nullifier_hash: Array.from(proofResult.nullifierHash),
517
+ public_inputs: [],
518
+ error: null
519
+ });
520
+ return wasmModule.verify_withdrawal_proof(proofJson);
548
521
  }
549
522
  isWasmReady() {
550
523
  return this.wasmReady;
551
524
  }
552
- computeNullifierHash(nullifier, leafIndex) {
553
- const indexBytes = new TextEncoder().encode(leafIndex.toString());
554
- const combined = new Uint8Array([...nullifier, ...indexBytes]);
555
- const hash = (0, import_ethers2.keccak256)(combined);
556
- return this.hexToBytes(hash);
557
- }
558
525
  addressToBytes(address) {
559
526
  const clean = address.startsWith("0x") ? address.slice(2) : address;
560
527
  const bytes = [];
package/dist/index.mjs CHANGED
@@ -365,14 +365,14 @@ var MerkleTree = class {
365
365
  };
366
366
 
367
367
  // src/zk-client.ts
368
- import { keccak256 as keccak2562, toBeHex } from "ethers";
368
+ import { keccak256 as keccak2562 } from "ethers";
369
369
  var wasmModule = null;
370
370
  var ZKProofClient = class {
371
371
  config;
372
372
  wasmReady = false;
373
373
  constructor(config) {
374
374
  this.config = config ?? { useRealProofs: true };
375
- if (this.config.useRealProofs) {
375
+ if (this.config.useRealProofs !== false) {
376
376
  this.loadWasm();
377
377
  }
378
378
  }
@@ -381,24 +381,21 @@ var ZKProofClient = class {
381
381
  this.wasmReady = true;
382
382
  return;
383
383
  }
384
- try {
385
- const wasmPath = this.config.wasmPath ?? "zkenclave-circuits";
386
- const module = await import(
387
- /* webpackIgnore: true */
388
- wasmPath
389
- );
390
- wasmModule = module;
391
- this.wasmReady = true;
392
- } catch {
393
- console.warn("WASM module not available, falling back to mock proofs");
394
- this.wasmReady = false;
395
- }
384
+ const wasmPath = this.config.wasmPath ?? "zkenclave-circuits";
385
+ const module = await import(
386
+ /* webpackIgnore: true */
387
+ wasmPath
388
+ );
389
+ wasmModule = module;
390
+ this.wasmReady = true;
396
391
  }
397
392
  async generateWithdrawalProof(request) {
398
- if (this.config.useRealProofs && this.wasmReady && wasmModule) {
399
- return this.generateRealProof(request);
393
+ if (!this.wasmReady || !wasmModule) {
394
+ throw new Error(
395
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
396
+ );
400
397
  }
401
- return this.generateFallbackProof(request);
398
+ return this.generateRealProof(request);
402
399
  }
403
400
  async generateRealProof(request) {
404
401
  const wasmRequest = {
@@ -426,81 +423,51 @@ var ZKProofClient = class {
426
423
  timestamp: Date.now()
427
424
  };
428
425
  }
429
- async generateFallbackProof(request) {
430
- const nullifierHash = this.computeNullifierHash(
431
- request.nullifier,
432
- request.leafIndex
433
- );
434
- const merkleRoot = request.merkleRoot ?? new Uint8Array(32);
435
- const proof = new Uint8Array(256);
436
- proof[0] = 1;
437
- const amountHex = toBeHex(request.amount, 32);
438
- const amountBytes = this.hexToBytes(amountHex);
439
- proof.set(amountBytes.slice(0, 32), 1);
440
- proof.set(request.commitment.slice(0, 32), 33);
441
- proof[250] = 90;
442
- proof[251] = 75;
443
- return {
444
- success: true,
445
- zkProof: proof,
446
- nullifierHash,
447
- merkleRoot,
448
- timestamp: Date.now()
449
- };
450
- }
451
426
  async generateComplianceProof(commitment, associationPath, pathIndices, associationRoot) {
452
- if (this.config.useRealProofs && this.wasmReady && wasmModule) {
453
- const request = {
454
- commitment: Array.from(commitment),
455
- association_path: associationPath.map((p) => Array.from(p)),
456
- path_indices: pathIndices,
457
- association_root: Array.from(associationRoot)
458
- };
459
- const resultJson = wasmModule.generate_compliance_proof(
460
- JSON.stringify(request)
427
+ if (!this.wasmReady || !wasmModule) {
428
+ throw new Error(
429
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
461
430
  );
462
- const result = JSON.parse(resultJson);
463
- if (!result.success) {
464
- throw new Error(`Compliance proof generation failed: ${result.error}`);
465
- }
466
- return {
467
- id: keccak2562(new Uint8Array(result.proof)),
468
- associationRoot,
469
- timestamp: Date.now(),
470
- valid: true,
471
- proof: new Uint8Array(result.proof)
472
- };
431
+ }
432
+ const request = {
433
+ commitment: Array.from(commitment),
434
+ association_path: associationPath.map((p) => Array.from(p)),
435
+ path_indices: pathIndices,
436
+ association_root: Array.from(associationRoot)
437
+ };
438
+ const resultJson = wasmModule.generate_compliance_proof(
439
+ JSON.stringify(request)
440
+ );
441
+ const result = JSON.parse(resultJson);
442
+ if (!result.success) {
443
+ throw new Error(`Compliance proof generation failed: ${result.error}`);
473
444
  }
474
445
  return {
475
- id: "mock-compliance-proof",
446
+ id: keccak2562(new Uint8Array(result.proof)),
476
447
  associationRoot,
477
448
  timestamp: Date.now(),
478
449
  valid: true,
479
- proof: new Uint8Array(64).fill(1)
450
+ proof: new Uint8Array(result.proof)
480
451
  };
481
452
  }
482
453
  async verifyProof(proofResult) {
483
- if (this.wasmReady && wasmModule) {
484
- const proofJson = JSON.stringify({
485
- success: proofResult.success,
486
- proof: Array.from(proofResult.zkProof),
487
- nullifier_hash: Array.from(proofResult.nullifierHash),
488
- public_inputs: [],
489
- error: null
490
- });
491
- return wasmModule.verify_withdrawal_proof(proofJson);
454
+ if (!this.wasmReady || !wasmModule) {
455
+ throw new Error(
456
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
457
+ );
492
458
  }
493
- return proofResult.success && proofResult.zkProof.length > 0 && proofResult.zkProof[250] === 90 && proofResult.zkProof[251] === 75;
459
+ const proofJson = JSON.stringify({
460
+ success: proofResult.success,
461
+ proof: Array.from(proofResult.zkProof),
462
+ nullifier_hash: Array.from(proofResult.nullifierHash),
463
+ public_inputs: [],
464
+ error: null
465
+ });
466
+ return wasmModule.verify_withdrawal_proof(proofJson);
494
467
  }
495
468
  isWasmReady() {
496
469
  return this.wasmReady;
497
470
  }
498
- computeNullifierHash(nullifier, leafIndex) {
499
- const indexBytes = new TextEncoder().encode(leafIndex.toString());
500
- const combined = new Uint8Array([...nullifier, ...indexBytes]);
501
- const hash = keccak2562(combined);
502
- return this.hexToBytes(hash);
503
- }
504
471
  addressToBytes(address) {
505
472
  const clean = address.startsWith("0x") ? address.slice(2) : address;
506
473
  const bytes = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zkenclave-sdk",
3
- "version": "0.1.12",
3
+ "version": "0.1.18",
4
4
  "description": "TypeScript SDK for privacy-preserving vault withdrawals with ZK proofs",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",