@cofhe/sdk 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/adapters/ethers6.ts +28 -28
  3. package/adapters/hardhat.ts +0 -1
  4. package/adapters/index.test.ts +14 -19
  5. package/adapters/smartWallet.ts +81 -73
  6. package/adapters/test-utils.ts +45 -45
  7. package/adapters/types.ts +3 -3
  8. package/chains/chains/localcofhe.ts +14 -0
  9. package/chains/chains.test.ts +2 -1
  10. package/chains/index.ts +3 -1
  11. package/core/baseBuilder.ts +30 -49
  12. package/core/client.test.ts +94 -77
  13. package/core/client.ts +133 -149
  14. package/core/clientTypes.ts +108 -0
  15. package/core/config.test.ts +22 -11
  16. package/core/config.ts +16 -9
  17. package/core/decrypt/decryptHandleBuilder.ts +51 -45
  18. package/core/decrypt/{tnSealOutput.ts → tnSealOutputV1.ts} +1 -1
  19. package/core/decrypt/tnSealOutputV2.ts +298 -0
  20. package/core/encrypt/cofheMocksZkVerifySign.ts +16 -10
  21. package/core/encrypt/encryptInputsBuilder.test.ts +132 -116
  22. package/core/encrypt/encryptInputsBuilder.ts +159 -111
  23. package/core/encrypt/encryptUtils.ts +6 -3
  24. package/core/encrypt/zkPackProveVerify.ts +70 -8
  25. package/core/error.ts +0 -2
  26. package/core/fetchKeys.test.ts +1 -18
  27. package/core/fetchKeys.ts +0 -26
  28. package/core/index.ts +29 -17
  29. package/core/keyStore.ts +65 -38
  30. package/core/permits.test.ts +253 -1
  31. package/core/permits.ts +80 -16
  32. package/core/types.ts +198 -152
  33. package/core/utils.ts +43 -1
  34. package/dist/adapters.d.cts +38 -20
  35. package/dist/adapters.d.ts +38 -20
  36. package/dist/chains.cjs +14 -1
  37. package/dist/chains.d.cts +23 -1
  38. package/dist/chains.d.ts +23 -1
  39. package/dist/chains.js +1 -1
  40. package/dist/{chunk-LU7BMUUT.js → chunk-UGBVZNRT.js} +39 -25
  41. package/dist/{chunk-GZCQQYVI.js → chunk-WEAZ25JO.js} +14 -2
  42. package/dist/{chunk-KFGPTJ6X.js → chunk-WGCRJCBR.js} +1920 -1692
  43. package/dist/{types-bB7wLj0q.d.cts → clientTypes-5_1nwtUe.d.cts} +308 -347
  44. package/dist/{types-PhwGgQvs.d.ts → clientTypes-Es7fyi65.d.ts} +308 -347
  45. package/dist/core.cjs +2872 -2632
  46. package/dist/core.d.cts +101 -6
  47. package/dist/core.d.ts +101 -6
  48. package/dist/core.js +3 -3
  49. package/dist/node.cjs +2716 -2520
  50. package/dist/node.d.cts +3 -3
  51. package/dist/node.d.ts +3 -3
  52. package/dist/node.js +4 -3
  53. package/dist/{permit-S9CnI6MF.d.cts → permit-fUSe6KKq.d.cts} +31 -15
  54. package/dist/{permit-S9CnI6MF.d.ts → permit-fUSe6KKq.d.ts} +31 -15
  55. package/dist/permits.cjs +39 -24
  56. package/dist/permits.d.cts +137 -148
  57. package/dist/permits.d.ts +137 -148
  58. package/dist/permits.js +1 -1
  59. package/dist/web.cjs +2929 -2518
  60. package/dist/web.d.cts +21 -5
  61. package/dist/web.d.ts +21 -5
  62. package/dist/web.js +185 -9
  63. package/dist/zkProve.worker.cjs +93 -0
  64. package/dist/zkProve.worker.d.cts +2 -0
  65. package/dist/zkProve.worker.d.ts +2 -0
  66. package/dist/zkProve.worker.js +91 -0
  67. package/node/client.test.ts +20 -25
  68. package/node/encryptInputs.test.ts +18 -38
  69. package/node/index.ts +1 -0
  70. package/package.json +14 -14
  71. package/permits/index.ts +1 -0
  72. package/permits/localstorage.test.ts +0 -1
  73. package/permits/permit.test.ts +25 -22
  74. package/permits/permit.ts +30 -21
  75. package/permits/sealing.test.ts +3 -3
  76. package/permits/sealing.ts +2 -2
  77. package/permits/store.ts +5 -7
  78. package/permits/test-utils.ts +1 -1
  79. package/permits/types.ts +17 -0
  80. package/permits/utils.ts +0 -1
  81. package/permits/validation.ts +24 -4
  82. package/web/client.web.test.ts +20 -25
  83. package/web/config.web.test.ts +0 -2
  84. package/web/encryptInputs.web.test.ts +31 -54
  85. package/web/index.ts +65 -1
  86. package/web/storage.ts +19 -5
  87. package/web/worker.builder.web.test.ts +148 -0
  88. package/web/worker.config.web.test.ts +329 -0
  89. package/web/worker.output.web.test.ts +84 -0
  90. package/web/workerManager.test.ts +80 -0
  91. package/web/workerManager.ts +214 -0
  92. package/web/workerManager.web.test.ts +114 -0
  93. package/web/zkProve.worker.ts +133 -0
  94. package/core/result.test.ts +0 -180
  95. package/core/result.ts +0 -67
  96. package/core/test-utils.ts +0 -45
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-unused-vars */
2
1
  import { describe, it, expect, vi, beforeEach } from 'vitest';
3
2
  import { EncryptInputsBuilder } from './encryptInputsBuilder.js';
4
3
  import {
@@ -18,7 +17,6 @@ import { type CofhesdkConfig, createCofhesdkConfigBase } from '../config.js';
18
17
  import { type ZkBuilderAndCrsGenerator } from './zkPackProveVerify.js';
19
18
  import { type KeysStorage, createKeysStore } from '../keyStore.js';
20
19
  import { type FheKeyDeserializer } from '../fetchKeys.js';
21
- import { expectResultSuccess, expectResultError } from '../test-utils.js';
22
20
 
23
21
  const MockZkVerifierUrl = 'http://localhost:3001';
24
22
 
@@ -93,25 +91,25 @@ class MockZkListBuilder {
93
91
  this.items = items;
94
92
  }
95
93
  push_boolean(data: boolean): void {
96
- this.items.push({ utype: FheTypes.Bool, data });
94
+ this.items.push({ utype: FheTypes.Bool, data, securityZone: 0 });
97
95
  }
98
96
  push_u8(data: number): void {
99
- this.items.push({ utype: FheTypes.Uint8, data: BigInt(data) });
97
+ this.items.push({ utype: FheTypes.Uint8, data: BigInt(data), securityZone: 0 });
100
98
  }
101
99
  push_u16(data: number): void {
102
- this.items.push({ utype: FheTypes.Uint16, data: BigInt(data) });
100
+ this.items.push({ utype: FheTypes.Uint16, data: BigInt(data), securityZone: 0 });
103
101
  }
104
102
  push_u32(data: number): void {
105
- this.items.push({ utype: FheTypes.Uint32, data: BigInt(data) });
103
+ this.items.push({ utype: FheTypes.Uint32, data: BigInt(data), securityZone: 0 });
106
104
  }
107
105
  push_u64(data: bigint): void {
108
- this.items.push({ utype: FheTypes.Uint64, data });
106
+ this.items.push({ utype: FheTypes.Uint64, data, securityZone: 0 });
109
107
  }
110
108
  push_u128(data: bigint): void {
111
- this.items.push({ utype: FheTypes.Uint128, data });
109
+ this.items.push({ utype: FheTypes.Uint128, data, securityZone: 0 });
112
110
  }
113
111
  push_u160(data: bigint): void {
114
- this.items.push({ utype: FheTypes.Uint160, data });
112
+ this.items.push({ utype: FheTypes.Uint160, data, securityZone: 0 });
115
113
  }
116
114
  build_with_proof_packed(_crs: any, metadata: Uint8Array, _computeLoad: 1): MockZkProvenList {
117
115
  // Clear items to prevent persisting items between tests
@@ -249,6 +247,7 @@ describe('EncryptInputsBuilder', () => {
249
247
  compactPkeCrsDeserializer: mockCompactPkeCrsDeserializer,
250
248
  zkBuilderAndCrsGenerator: mockZkBuilderAndCrsGenerator,
251
249
  initTfhe: mockInitTfhe,
250
+ zkProveWorkerFn: undefined,
252
251
  keysStorage: keysStorage,
253
252
  requireConnected: vi.fn(),
254
253
  };
@@ -279,58 +278,62 @@ describe('EncryptInputsBuilder', () => {
279
278
  });
280
279
 
281
280
  it('should throw an error if config is not set', async () => {
282
- const builder = new EncryptInputsBuilder({
283
- ...createDefaultParams(),
284
- config: undefined as unknown as CofhesdkConfig,
285
- });
286
- const result = await builder.encrypt();
287
- expectResultError(result, CofhesdkErrorCode.MissingConfig);
281
+ // Should throw before .encrypt() is called
282
+ try {
283
+ new EncryptInputsBuilder({
284
+ ...createDefaultParams(),
285
+ config: undefined as unknown as CofhesdkConfig,
286
+ });
287
+ } catch (error) {
288
+ expect(error).toBeInstanceOf(CofhesdkError);
289
+ expect((error as CofhesdkError).code).toBe(CofhesdkErrorCode.MissingConfig);
290
+ }
288
291
  });
289
292
 
290
293
  it('should throw an error if tfhePublicKeyDeserializer is not set', async () => {
291
- const builder = new EncryptInputsBuilder({
292
- ...createDefaultParams(),
293
- tfhePublicKeyDeserializer: undefined as unknown as FheKeyDeserializer,
294
- });
295
- const result = await builder.encrypt();
296
- expectResultError(result, CofhesdkErrorCode.MissingTfhePublicKeyDeserializer);
294
+ // Should throw before .encrypt() is called
295
+ try {
296
+ new EncryptInputsBuilder({
297
+ ...createDefaultParams(),
298
+ tfhePublicKeyDeserializer: undefined as unknown as FheKeyDeserializer,
299
+ });
300
+ } catch (error) {
301
+ expect(error).toBeInstanceOf(CofhesdkError);
302
+ expect((error as CofhesdkError).code).toBe(CofhesdkErrorCode.MissingTfhePublicKeyDeserializer);
303
+ }
297
304
  });
298
305
 
299
306
  it('should throw an error if compactPkeCrsDeserializer is not set', async () => {
300
- const builder = new EncryptInputsBuilder({
301
- ...createDefaultParams(),
302
- compactPkeCrsDeserializer: undefined as unknown as FheKeyDeserializer,
303
- });
304
- const result = await builder.encrypt();
305
- expectResultError(result, CofhesdkErrorCode.MissingCompactPkeCrsDeserializer);
307
+ // Should throw before .encrypt() is called
308
+ try {
309
+ new EncryptInputsBuilder({
310
+ ...createDefaultParams(),
311
+ compactPkeCrsDeserializer: undefined as unknown as FheKeyDeserializer,
312
+ });
313
+ } catch (error) {
314
+ expect(error).toBeInstanceOf(CofhesdkError);
315
+ expect((error as CofhesdkError).code).toBe(CofhesdkErrorCode.MissingCompactPkeCrsDeserializer);
316
+ }
306
317
  });
307
318
 
308
319
  it('should throw an error if initTfhe throws an error', async () => {
309
- const builder = new EncryptInputsBuilder({
310
- ...createDefaultParams(),
311
- initTfhe: vi.fn().mockRejectedValue(new Error('Failed to initialize TFHE')),
312
- });
313
- const result = await builder.encrypt();
314
- expectResultError(result, CofhesdkErrorCode.InitTfheFailed);
315
- });
316
-
317
- it('should throw an error if initTfhe throws', async () => {
318
- const mockInitTfhe = vi.fn().mockRejectedValue(new Error('Failed to initialize TFHE'));
319
- const builder = new EncryptInputsBuilder({
320
- ...createDefaultParams(),
321
- initTfhe: mockInitTfhe,
322
- });
323
- const result = await builder.encrypt();
324
- expectResultError(result, CofhesdkErrorCode.InitTfheFailed);
320
+ try {
321
+ await new EncryptInputsBuilder({
322
+ ...createDefaultParams(),
323
+ initTfhe: vi.fn().mockRejectedValue(new Error('Failed to initialize TFHE')),
324
+ }).encrypt();
325
+ } catch (error) {
326
+ expect(error).toBeInstanceOf(CofhesdkError);
327
+ expect((error as CofhesdkError).code).toBe(CofhesdkErrorCode.InitTfheFailed);
328
+ }
325
329
  });
326
330
 
327
331
  it('should not throw an error if initTfhe is set', async () => {
328
- const builder = new EncryptInputsBuilder({
332
+ const result = await new EncryptInputsBuilder({
329
333
  ...createDefaultParams(),
330
334
  initTfhe: mockInitTfhe,
331
- });
332
- const result = await builder.encrypt();
333
- expectResultSuccess(result);
335
+ }).encrypt();
336
+ expect(result).toBeDefined();
334
337
  });
335
338
  });
336
339
 
@@ -359,12 +362,15 @@ describe('EncryptInputsBuilder', () => {
359
362
  });
360
363
 
361
364
  it('should throw an error if account is not set', async () => {
362
- const builder = new EncryptInputsBuilder({
363
- ...createDefaultParams(),
364
- account: undefined,
365
- });
366
- const result = await builder.encrypt();
367
- expectResultError(result, CofhesdkErrorCode.AccountUninitialized);
365
+ try {
366
+ await new EncryptInputsBuilder({
367
+ ...createDefaultParams(),
368
+ account: undefined,
369
+ }).encrypt();
370
+ } catch (error) {
371
+ expect(error).toBeInstanceOf(CofhesdkError);
372
+ expect((error as CofhesdkError).code).toBe(CofhesdkErrorCode.AccountUninitialized);
373
+ }
368
374
  });
369
375
  });
370
376
 
@@ -400,27 +406,32 @@ describe('EncryptInputsBuilder', () => {
400
406
  });
401
407
 
402
408
  it('should throw an error if chainId is not set', async () => {
403
- const builder = new EncryptInputsBuilder({
404
- ...createDefaultParams(),
405
- chainId: undefined,
406
- });
407
- const result = await builder.encrypt();
408
- expectResultError(result, CofhesdkErrorCode.ChainIdUninitialized);
409
+ try {
410
+ await new EncryptInputsBuilder({
411
+ ...createDefaultParams(),
412
+ chainId: undefined,
413
+ }).encrypt();
414
+ } catch (error) {
415
+ expect(error).toBeInstanceOf(CofhesdkError);
416
+ expect((error as CofhesdkError).code).toBe(CofhesdkErrorCode.ChainIdUninitialized);
417
+ }
409
418
  });
410
419
  });
411
420
 
412
421
  describe('zkVerifierUrl', () => {
413
422
  it('should throw if zkVerifierUrl is not set', async () => {
414
- builder = new EncryptInputsBuilder({
415
- ...createDefaultParams(),
416
- inputs: [Encryptable.uint128(100n)] as [EncryptableUint128],
417
- account: '0x1234567890123456789012345678901234567890',
418
- chainId: 1,
419
- config: createMockCofhesdkConfig(defaultChainId, undefined as unknown as string),
420
- });
421
-
422
- const result = await builder.encrypt();
423
- expectResultError(result, CofhesdkErrorCode.ZkVerifierUrlUninitialized);
423
+ try {
424
+ await new EncryptInputsBuilder({
425
+ ...createDefaultParams(),
426
+ inputs: [Encryptable.uint128(100n)] as [EncryptableUint128],
427
+ account: '0x1234567890123456789012345678901234567890',
428
+ chainId: 1,
429
+ config: createMockCofhesdkConfig(defaultChainId, undefined as unknown as string),
430
+ }).encrypt();
431
+ } catch (error) {
432
+ expect(error).toBeInstanceOf(CofhesdkError);
433
+ expect((error as CofhesdkError).code).toBe(CofhesdkErrorCode.ZkVerifierUrlUninitialized);
434
+ }
424
435
  });
425
436
  });
426
437
 
@@ -444,7 +455,7 @@ describe('EncryptInputsBuilder', () => {
444
455
  const stepCallback = vi.fn();
445
456
  builder.setStepCallback(stepCallback);
446
457
 
447
- const result = expectResultSuccess(await builder.encrypt());
458
+ const result = await builder.encrypt();
448
459
 
449
460
  // Verify step callbacks were called in order
450
461
  expect(stepCallback).toHaveBeenCalledTimes(10);
@@ -557,7 +568,7 @@ describe('EncryptInputsBuilder', () => {
557
568
  const overriddenSender = '0x5555555555555555555555555555555555555555';
558
569
  builder.setAccount(overriddenSender);
559
570
 
560
- const result = expectResultSuccess(await builder.encrypt());
571
+ const result = await builder.encrypt();
561
572
 
562
573
  // Verify result embedded metadata
563
574
  const [encrypted] = result;
@@ -574,7 +585,7 @@ describe('EncryptInputsBuilder', () => {
574
585
 
575
586
  insertMockKeys(defaultChainId, overriddenZone);
576
587
 
577
- const result = expectResultSuccess(await builder.encrypt());
588
+ const result = await builder.encrypt();
578
589
 
579
590
  // Verify result embedded metadata
580
591
  const [encrypted] = result;
@@ -587,7 +598,7 @@ describe('EncryptInputsBuilder', () => {
587
598
 
588
599
  it('should work without step callback', async () => {
589
600
  // No step callback set
590
- const result = expectResultSuccess(await builder.encrypt());
601
+ const result = await builder.encrypt();
591
602
 
592
603
  expect(result).toBeDefined();
593
604
  expect(Array.isArray(result)).toBe(true);
@@ -603,54 +614,60 @@ describe('EncryptInputsBuilder', () => {
603
614
  ],
604
615
  });
605
616
 
606
- const result = expectResultSuccess(await multiInputBuilder.encrypt());
617
+ const result = await multiInputBuilder.encrypt();
607
618
 
608
619
  expect(result).toBeDefined();
609
620
  expect(Array.isArray(result)).toBe(true);
610
621
  });
611
622
 
612
623
  it('should throw an error if total bits exceeds 2048', async () => {
613
- const builder = new EncryptInputsBuilder({
614
- ...createDefaultParams(),
615
- inputs: [
616
- Encryptable.uint128(100n),
617
- Encryptable.uint128(100n),
618
- Encryptable.uint128(100n),
619
- Encryptable.uint128(100n),
620
- Encryptable.uint128(100n),
621
- Encryptable.uint128(100n),
622
- Encryptable.uint128(100n),
623
- Encryptable.uint128(100n),
624
- Encryptable.uint128(100n),
625
- Encryptable.uint128(100n),
626
- Encryptable.uint128(100n),
627
- Encryptable.uint128(100n),
628
- Encryptable.uint128(100n),
629
- Encryptable.uint128(100n),
630
- Encryptable.uint128(100n),
631
- Encryptable.uint128(100n),
632
- Encryptable.uint128(100n),
633
- Encryptable.uint128(100n),
634
- Encryptable.uint128(100n),
635
- Encryptable.uint128(100n),
636
- ],
637
- });
638
- const result = await builder.encrypt();
639
- expectResultError(result, CofhesdkErrorCode.ZkPackFailed);
624
+ try {
625
+ await new EncryptInputsBuilder({
626
+ ...createDefaultParams(),
627
+ inputs: [
628
+ Encryptable.uint128(100n),
629
+ Encryptable.uint128(100n),
630
+ Encryptable.uint128(100n),
631
+ Encryptable.uint128(100n),
632
+ Encryptable.uint128(100n),
633
+ Encryptable.uint128(100n),
634
+ Encryptable.uint128(100n),
635
+ Encryptable.uint128(100n),
636
+ Encryptable.uint128(100n),
637
+ Encryptable.uint128(100n),
638
+ Encryptable.uint128(100n),
639
+ Encryptable.uint128(100n),
640
+ Encryptable.uint128(100n),
641
+ Encryptable.uint128(100n),
642
+ Encryptable.uint128(100n),
643
+ Encryptable.uint128(100n),
644
+ Encryptable.uint128(100n),
645
+ Encryptable.uint128(100n),
646
+ Encryptable.uint128(100n),
647
+ Encryptable.uint128(100n),
648
+ ],
649
+ }).encrypt();
650
+ } catch (error) {
651
+ expect(error).toBeInstanceOf(CofhesdkError);
652
+ expect((error as CofhesdkError).code).toBe(CofhesdkErrorCode.ZkPackFailed);
653
+ }
640
654
  });
641
655
 
642
656
  it('should throw an error if utype is invalid', async () => {
643
- const builder = new EncryptInputsBuilder({
644
- ...createDefaultParams(),
645
- inputs: [
646
- {
647
- data: 10n,
648
- utype: FheTypes.Uint10, // Invalid utype
649
- },
650
- ] as unknown as [EncryptableItem],
651
- });
652
- const result = await builder.encrypt();
653
- expectResultError(result, CofhesdkErrorCode.ZkPackFailed);
657
+ try {
658
+ await new EncryptInputsBuilder({
659
+ ...createDefaultParams(),
660
+ inputs: [
661
+ {
662
+ data: 10n,
663
+ utype: FheTypes.Uint10, // Invalid utype
664
+ },
665
+ ] as unknown as [EncryptableItem],
666
+ });
667
+ } catch (error) {
668
+ expect(error).toBeInstanceOf(CofhesdkError);
669
+ expect((error as CofhesdkError).code).toBe(CofhesdkErrorCode.ZkPackFailed);
670
+ }
654
671
  });
655
672
  });
656
673
 
@@ -685,13 +702,12 @@ describe('EncryptInputsBuilder', () => {
685
702
  .setSecurityZone(securityZone)
686
703
  .setStepCallback(stepCallback)
687
704
  .encrypt();
688
- const resultData = expectResultSuccess(result);
689
705
 
690
706
  expect(result).toBeDefined();
691
707
  expect(stepCallback).toHaveBeenCalledTimes(10);
692
708
 
693
709
  // Verify result embedded metadata
694
- const [encrypted] = resultData;
710
+ const [encrypted] = result;
695
711
  const encryptedMetadata = unpackMetadata(encrypted.signature);
696
712
  expect(encryptedMetadata).toBeDefined();
697
713
  expect(encryptedMetadata.signer).toBe(sender);
@@ -709,8 +725,8 @@ describe('EncryptInputsBuilder', () => {
709
725
  builder.setSecurityZone(securityZone);
710
726
 
711
727
  // Call encrypt multiple times to ensure state is maintained
712
- const result1 = expectResultSuccess(await builder.encrypt());
713
- const result2 = expectResultSuccess(await builder.encrypt());
728
+ const result1 = await builder.encrypt();
729
+ const result2 = await builder.encrypt();
714
730
 
715
731
  expect(result1).toBeDefined();
716
732
  expect(result2).toBeDefined();