zkenclave-sdk 0.1.12 → 0.1.17

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,33 +426,28 @@ var ZKProofClient = class {
426
426
  wasmReady = false;
427
427
  constructor(config) {
428
428
  this.config = config ?? { useRealProofs: true };
429
- if (this.config.useRealProofs) {
430
- this.loadWasm();
431
- }
429
+ this.loadWasm();
432
430
  }
433
431
  async loadWasm() {
434
432
  if (wasmModule) {
435
433
  this.wasmReady = true;
436
434
  return;
437
435
  }
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
- }
436
+ const wasmPath = this.config.wasmPath ?? "zkenclave-circuits";
437
+ const module2 = await import(
438
+ /* webpackIgnore: true */
439
+ wasmPath
440
+ );
441
+ wasmModule = module2;
442
+ this.wasmReady = true;
450
443
  }
451
444
  async generateWithdrawalProof(request) {
452
- if (this.config.useRealProofs && this.wasmReady && wasmModule) {
453
- return this.generateRealProof(request);
445
+ if (!this.wasmReady || !wasmModule) {
446
+ throw new Error(
447
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
448
+ );
454
449
  }
455
- return this.generateFallbackProof(request);
450
+ return this.generateRealProof(request);
456
451
  }
457
452
  async generateRealProof(request) {
458
453
  const wasmRequest = {
@@ -480,81 +475,51 @@ var ZKProofClient = class {
480
475
  timestamp: Date.now()
481
476
  };
482
477
  }
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
478
  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)
479
+ if (!this.wasmReady || !wasmModule) {
480
+ throw new Error(
481
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
515
482
  );
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
- };
483
+ }
484
+ const request = {
485
+ commitment: Array.from(commitment),
486
+ association_path: associationPath.map((p) => Array.from(p)),
487
+ path_indices: pathIndices,
488
+ association_root: Array.from(associationRoot)
489
+ };
490
+ const resultJson = wasmModule.generate_compliance_proof(
491
+ JSON.stringify(request)
492
+ );
493
+ const result = JSON.parse(resultJson);
494
+ if (!result.success) {
495
+ throw new Error(`Compliance proof generation failed: ${result.error}`);
527
496
  }
528
497
  return {
529
- id: "mock-compliance-proof",
498
+ id: (0, import_ethers2.keccak256)(new Uint8Array(result.proof)),
530
499
  associationRoot,
531
500
  timestamp: Date.now(),
532
501
  valid: true,
533
- proof: new Uint8Array(64).fill(1)
502
+ proof: new Uint8Array(result.proof)
534
503
  };
535
504
  }
536
505
  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);
506
+ if (!this.wasmReady || !wasmModule) {
507
+ throw new Error(
508
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
509
+ );
546
510
  }
547
- return proofResult.success && proofResult.zkProof.length > 0 && proofResult.zkProof[250] === 90 && proofResult.zkProof[251] === 75;
511
+ const proofJson = JSON.stringify({
512
+ success: proofResult.success,
513
+ proof: Array.from(proofResult.zkProof),
514
+ nullifier_hash: Array.from(proofResult.nullifierHash),
515
+ public_inputs: [],
516
+ error: null
517
+ });
518
+ return wasmModule.verify_withdrawal_proof(proofJson);
548
519
  }
549
520
  isWasmReady() {
550
521
  return this.wasmReady;
551
522
  }
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
523
  addressToBytes(address) {
559
524
  const clean = address.startsWith("0x") ? address.slice(2) : address;
560
525
  const bytes = [];
package/dist/index.mjs CHANGED
@@ -365,40 +365,35 @@ 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) {
376
- this.loadWasm();
377
- }
375
+ this.loadWasm();
378
376
  }
379
377
  async loadWasm() {
380
378
  if (wasmModule) {
381
379
  this.wasmReady = true;
382
380
  return;
383
381
  }
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
- }
382
+ const wasmPath = this.config.wasmPath ?? "zkenclave-circuits";
383
+ const module = await import(
384
+ /* webpackIgnore: true */
385
+ wasmPath
386
+ );
387
+ wasmModule = module;
388
+ this.wasmReady = true;
396
389
  }
397
390
  async generateWithdrawalProof(request) {
398
- if (this.config.useRealProofs && this.wasmReady && wasmModule) {
399
- return this.generateRealProof(request);
391
+ if (!this.wasmReady || !wasmModule) {
392
+ throw new Error(
393
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
394
+ );
400
395
  }
401
- return this.generateFallbackProof(request);
396
+ return this.generateRealProof(request);
402
397
  }
403
398
  async generateRealProof(request) {
404
399
  const wasmRequest = {
@@ -426,81 +421,51 @@ var ZKProofClient = class {
426
421
  timestamp: Date.now()
427
422
  };
428
423
  }
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
424
  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)
425
+ if (!this.wasmReady || !wasmModule) {
426
+ throw new Error(
427
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
461
428
  );
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
- };
429
+ }
430
+ const request = {
431
+ commitment: Array.from(commitment),
432
+ association_path: associationPath.map((p) => Array.from(p)),
433
+ path_indices: pathIndices,
434
+ association_root: Array.from(associationRoot)
435
+ };
436
+ const resultJson = wasmModule.generate_compliance_proof(
437
+ JSON.stringify(request)
438
+ );
439
+ const result = JSON.parse(resultJson);
440
+ if (!result.success) {
441
+ throw new Error(`Compliance proof generation failed: ${result.error}`);
473
442
  }
474
443
  return {
475
- id: "mock-compliance-proof",
444
+ id: keccak2562(new Uint8Array(result.proof)),
476
445
  associationRoot,
477
446
  timestamp: Date.now(),
478
447
  valid: true,
479
- proof: new Uint8Array(64).fill(1)
448
+ proof: new Uint8Array(result.proof)
480
449
  };
481
450
  }
482
451
  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);
452
+ if (!this.wasmReady || !wasmModule) {
453
+ throw new Error(
454
+ "WASM module not loaded. Make sure zkenclave-circuits is properly installed and configured."
455
+ );
492
456
  }
493
- return proofResult.success && proofResult.zkProof.length > 0 && proofResult.zkProof[250] === 90 && proofResult.zkProof[251] === 75;
457
+ const proofJson = JSON.stringify({
458
+ success: proofResult.success,
459
+ proof: Array.from(proofResult.zkProof),
460
+ nullifier_hash: Array.from(proofResult.nullifierHash),
461
+ public_inputs: [],
462
+ error: null
463
+ });
464
+ return wasmModule.verify_withdrawal_proof(proofJson);
494
465
  }
495
466
  isWasmReady() {
496
467
  return this.wasmReady;
497
468
  }
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
469
  addressToBytes(address) {
505
470
  const clean = address.startsWith("0x") ? address.slice(2) : address;
506
471
  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.17",
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",