@cofhe/sdk 0.4.0 → 0.5.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.
Files changed (95) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/adapters/{ethers5.test.ts → test/ethers5.test.ts} +2 -2
  3. package/adapters/{ethers6.test.ts → test/ethers6.test.ts} +2 -2
  4. package/adapters/{hardhat.hh2.test.ts → test/hardhat.hh2.test.ts} +2 -2
  5. package/adapters/{index.test.ts → test/index.test.ts} +1 -1
  6. package/adapters/{wagmi.test.ts → test/wagmi.test.ts} +1 -1
  7. package/chains/{chains.test.ts → test/chains.test.ts} +1 -1
  8. package/core/client.ts +11 -1
  9. package/core/clientTypes.ts +3 -1
  10. package/core/consts.ts +9 -0
  11. package/core/decrypt/cofheMocksDecryptForTx.ts +14 -3
  12. package/core/decrypt/decryptForTxBuilder.ts +16 -2
  13. package/core/decrypt/decryptForViewBuilder.ts +14 -7
  14. package/core/decrypt/polling.ts +14 -0
  15. package/core/decrypt/tnDecryptV2.ts +250 -110
  16. package/core/decrypt/tnSealOutputV2.ts +245 -104
  17. package/core/decrypt/verifyDecryptResult.ts +65 -0
  18. package/core/encrypt/cofheMocksZkVerifySign.ts +6 -6
  19. package/core/encrypt/zkPackProveVerify.ts +10 -19
  20. package/core/fetchKeys.ts +0 -2
  21. package/core/index.ts +9 -1
  22. package/core/keyStore.ts +5 -2
  23. package/core/permits.ts +5 -0
  24. package/core/{client.test.ts → test/client.test.ts} +7 -7
  25. package/core/{config.test.ts → test/config.test.ts} +1 -1
  26. package/core/test/decrypt.test.ts +252 -0
  27. package/core/test/decryptBuilders.test.ts +390 -0
  28. package/core/{encrypt → test}/encryptInputsBuilder.test.ts +61 -6
  29. package/core/{fetchKeys.test.ts → test/fetchKeys.test.ts} +3 -3
  30. package/core/{keyStore.test.ts → test/keyStore.test.ts} +5 -3
  31. package/core/{permits.test.ts → test/permits.test.ts} +42 -1
  32. package/core/test/pollCallbacks.test.ts +563 -0
  33. package/core/types.ts +13 -0
  34. package/dist/chains.d.cts +2 -2
  35. package/dist/chains.d.ts +2 -2
  36. package/dist/chunk-4FP4V35O.js +13 -0
  37. package/dist/{chunk-NWDKXBIP.js → chunk-MRCKUMOS.js} +62 -22
  38. package/dist/{chunk-MXND5SVN.js → chunk-S7OKGLFD.js} +485 -207
  39. package/dist/{clientTypes-kkrRdawm.d.ts → clientTypes-BSbwairE.d.cts} +23 -6
  40. package/dist/{clientTypes-ACVWbrXL.d.cts → clientTypes-DDmcgZ0a.d.ts} +23 -6
  41. package/dist/core.cjs +561 -244
  42. package/dist/core.d.cts +24 -6
  43. package/dist/core.d.ts +24 -6
  44. package/dist/core.js +3 -2
  45. package/dist/node.cjs +566 -246
  46. package/dist/node.d.cts +3 -3
  47. package/dist/node.d.ts +3 -3
  48. package/dist/node.js +14 -7
  49. package/dist/{permit-MZ502UBl.d.cts → permit-DnVMDT5h.d.cts} +34 -4
  50. package/dist/{permit-MZ502UBl.d.ts → permit-DnVMDT5h.d.ts} +34 -4
  51. package/dist/permits.cjs +66 -29
  52. package/dist/permits.d.cts +18 -13
  53. package/dist/permits.d.ts +18 -13
  54. package/dist/permits.js +2 -1
  55. package/dist/web.cjs +604 -256
  56. package/dist/web.d.cts +8 -4
  57. package/dist/web.d.ts +8 -4
  58. package/dist/web.js +49 -14
  59. package/dist/zkProve.worker.cjs +72 -64
  60. package/dist/zkProve.worker.js +71 -64
  61. package/node/index.ts +13 -4
  62. package/node/test/client.test.ts +25 -0
  63. package/node/test/config.test.ts +16 -0
  64. package/node/test/inherited.test.ts +244 -0
  65. package/node/test/tfheinit.test.ts +56 -0
  66. package/package.json +24 -22
  67. package/permits/permit.ts +31 -5
  68. package/permits/sealing.ts +1 -1
  69. package/permits/{localstorage.test.ts → test/localstorage.test.ts} +2 -2
  70. package/permits/{permit.test.ts → test/permit.test.ts} +35 -1
  71. package/permits/{sealing.test.ts → test/sealing.test.ts} +1 -1
  72. package/permits/{store.test.ts → test/store.test.ts} +2 -2
  73. package/permits/{validation.test.ts → test/validation.test.ts} +82 -6
  74. package/permits/types.ts +1 -1
  75. package/permits/validation.ts +42 -2
  76. package/web/const.ts +2 -0
  77. package/web/index.ts +40 -11
  78. package/web/storage.ts +18 -3
  79. package/web/{client.web.test.ts → test/client.web.test.ts} +13 -1
  80. package/web/test/config.web.test.ts +16 -0
  81. package/web/test/inherited.web.test.ts +245 -0
  82. package/web/test/tfheinit.web.test.ts +62 -0
  83. package/web/{worker.config.web.test.ts → test/worker.config.web.test.ts} +1 -1
  84. package/web/{worker.output.web.test.ts → test/worker.output.web.test.ts} +1 -1
  85. package/web/{workerManager.test.ts → test/workerManager.test.ts} +1 -1
  86. package/web/{workerManager.web.test.ts → test/workerManager.web.test.ts} +1 -1
  87. package/web/zkProve.worker.ts +94 -84
  88. package/node/client.test.ts +0 -147
  89. package/node/config.test.ts +0 -68
  90. package/node/encryptInputs.test.ts +0 -155
  91. package/web/config.web.test.ts +0 -69
  92. package/web/encryptInputs.web.test.ts +0 -172
  93. package/web/worker.builder.web.test.ts +0 -148
  94. /package/dist/{types-YiAC4gig.d.cts → types-C07FK-cL.d.cts} +0 -0
  95. /package/dist/{types-YiAC4gig.d.ts → types-C07FK-cL.d.ts} +0 -0
@@ -1,172 +0,0 @@
1
- import { arbSepolia as cofheArbSepolia } from '@/chains';
2
- import { Encryptable, FheTypes, type CofheClient, CofheErrorCode, CofheError } from '@/core';
3
-
4
- import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
5
- import type { PublicClient, WalletClient } from 'viem';
6
- import { createPublicClient, createWalletClient, http } from 'viem';
7
- import { privateKeyToAccount } from 'viem/accounts';
8
- import { arbitrumSepolia as viemArbitrumSepolia } from 'viem/chains';
9
- import { createCofheClient, createCofheConfig } from './index.js';
10
-
11
- // Real test setup - runs in browser with real tfhe
12
- const TEST_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
13
-
14
- describe('@cofhe/web - Encrypt Inputs Browser Tests', () => {
15
- let cofheClient: CofheClient;
16
- let publicClient: PublicClient;
17
- let walletClient: WalletClient;
18
-
19
- beforeAll(() => {
20
- // Create real viem clients
21
- publicClient = createPublicClient({
22
- chain: viemArbitrumSepolia,
23
- transport: http(),
24
- });
25
-
26
- const account = privateKeyToAccount(TEST_PRIVATE_KEY);
27
- walletClient = createWalletClient({
28
- chain: viemArbitrumSepolia,
29
- transport: http(),
30
- account,
31
- });
32
- });
33
-
34
- beforeEach(() => {
35
- const config = createCofheConfig({
36
- supportedChains: [cofheArbSepolia],
37
- });
38
- cofheClient = createCofheClient(config);
39
- });
40
-
41
- describe('Browser TFHE Initialization', () => {
42
- it('should initialize tfhe on first encryption', async () => {
43
- await cofheClient.connect(publicClient, walletClient);
44
-
45
- // This will trigger real TFHE initialization in browser
46
- const result = await cofheClient.encryptInputs([Encryptable.uint128(100n)]).execute();
47
-
48
- // If we get here, TFHE was initialized successfully
49
- expect(result).toBeDefined();
50
- }, 60000); // Longer timeout for real operations
51
-
52
- it('should handle multiple encryptions without re-initializing', async () => {
53
- await cofheClient.connect(publicClient, walletClient);
54
-
55
- // First encryption
56
- expect(cofheClient.encryptInputs([Encryptable.uint128(100n)]).execute()).resolves.not.toThrow();
57
-
58
- // Second encryption should reuse initialization
59
- expect(cofheClient.encryptInputs([Encryptable.uint64(50n)]).execute()).resolves.not.toThrow();
60
- }, 60000);
61
- });
62
-
63
- describe('Browser Encryption', () => {
64
- it('should encrypt a bool with real TFHE in browser', async () => {
65
- await cofheClient.connect(publicClient, walletClient);
66
-
67
- const result = await cofheClient.encryptInputs([Encryptable.bool(true)]).execute();
68
-
69
- expect(result).toBeDefined();
70
- expect(result.length).toBe(1);
71
- expect(result[0].utype).toBe(FheTypes.Bool);
72
- expect(result[0].ctHash).toBeDefined();
73
- expect(typeof result[0].ctHash).toBe('bigint');
74
- expect(result[0].signature).toBeDefined();
75
- expect(typeof result[0].signature).toBe('string');
76
- expect(result[0].securityZone).toBe(0);
77
- }, 60000);
78
-
79
- it('should encrypt all supported types together', async () => {
80
- await cofheClient.connect(publicClient, walletClient);
81
-
82
- const inputs = [
83
- Encryptable.bool(false),
84
- Encryptable.uint8(1n),
85
- Encryptable.uint16(2n),
86
- Encryptable.uint32(3n),
87
- Encryptable.uint64(4n),
88
- Encryptable.uint128(5n),
89
- Encryptable.address('0x742d35Cc6634C0532925a3b844D16faC4c175E99'),
90
- ];
91
-
92
- const result = await cofheClient.encryptInputs(inputs).execute();
93
- expect(result).toBeDefined();
94
-
95
- expect(result.length).toBe(7);
96
- // Verify each type
97
- expect(result[0].utype).toBe(FheTypes.Bool);
98
- expect(result[1].utype).toBe(FheTypes.Uint8);
99
- expect(result[2].utype).toBe(FheTypes.Uint16);
100
- expect(result[3].utype).toBe(FheTypes.Uint32);
101
- expect(result[4].utype).toBe(FheTypes.Uint64);
102
- expect(result[5].utype).toBe(FheTypes.Uint128);
103
- expect(result[6].utype).toBe(FheTypes.Uint160);
104
- }, 90000); // Longer timeout for multiple encryptions
105
- });
106
-
107
- describe('Browser Builder Pattern', () => {
108
- it('should support chaining builder methods with real encryption', async () => {
109
- await cofheClient.connect(publicClient, walletClient);
110
-
111
- const snapshot = cofheClient.getSnapshot();
112
- const encrypted = await cofheClient
113
- .encryptInputs([Encryptable.uint128(100n)])
114
- .setChainId(snapshot.chainId!)
115
- .setAccount(snapshot.account!)
116
- .setSecurityZone(0)
117
- .execute();
118
-
119
- expect(encrypted.length).toBe(1);
120
- expect(encrypted[0].utype).toBe(FheTypes.Uint128);
121
- }, 60000);
122
- });
123
-
124
- describe('Browser Error Handling', () => {
125
- it('should fail gracefully when not connected', async () => {
126
- // Don't connect the client
127
- try {
128
- const promise = cofheClient.encryptInputs([Encryptable.uint128(100n)]).execute();
129
- } catch (error) {
130
- expect(error).toBeInstanceOf(CofheError);
131
- expect((error as CofheError).code).toBe(CofheErrorCode.NotConnected);
132
- }
133
- }, 30000);
134
-
135
- it('should handle invalid CoFHE URL', async () => {
136
- const badConfig = createCofheConfig({
137
- supportedChains: [
138
- {
139
- ...cofheArbSepolia,
140
- coFheUrl: 'http://invalid-cofhe-url.local',
141
- verifierUrl: 'http://invalid-verifier-url.local',
142
- },
143
- ],
144
- });
145
-
146
- const badClient = createCofheClient(badConfig);
147
- await badClient.connect(publicClient, walletClient);
148
-
149
- const promise = badClient.encryptInputs([Encryptable.uint128(100n)]).execute();
150
- expect(promise).rejects.toThrow();
151
- }, 60000);
152
- });
153
-
154
- describe('Browser Performance', () => {
155
- it('should handle consecutive encryptions efficiently', async () => {
156
- await cofheClient.connect(publicClient, walletClient);
157
-
158
- const start = Date.now();
159
-
160
- // Perform 5 encryptions
161
- for (let i = 0; i < 5; i++) {
162
- await cofheClient.encryptInputs([Encryptable.uint128(BigInt(i))]).execute();
163
- }
164
-
165
- const duration = Date.now() - start;
166
-
167
- // Should complete all 5 encryptions in reasonable time
168
- // This is a sanity check, not a strict benchmark
169
- expect(duration).toBeLessThan(120000); // 2 minutes max
170
- }, 180000); // 3 minute timeout
171
- });
172
- });
@@ -1,148 +0,0 @@
1
- import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
2
- import { createCofheClient, createCofheConfig } from './index.js';
3
- import { Encryptable, type CofheClient } from '@/core';
4
- import { arbSepolia as cofheArbSepolia } from '@/chains';
5
- import { arbitrumSepolia as viemArbitrumSepolia } from 'viem/chains';
6
- import { createPublicClient, createWalletClient, http, type PublicClient, type WalletClient } from 'viem';
7
- import { privateKeyToAccount } from 'viem/accounts';
8
-
9
- const TEST_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
10
-
11
- describe('@cofhe/sdk/web - EncryptInputsBuilder Worker Methods', () => {
12
- let cofheClient: CofheClient;
13
- let publicClient: PublicClient;
14
- let walletClient: WalletClient;
15
-
16
- beforeAll(() => {
17
- publicClient = createPublicClient({
18
- chain: viemArbitrumSepolia,
19
- transport: http(),
20
- });
21
-
22
- const account = privateKeyToAccount(TEST_PRIVATE_KEY);
23
- walletClient = createWalletClient({
24
- chain: viemArbitrumSepolia,
25
- transport: http(),
26
- account,
27
- });
28
- });
29
-
30
- beforeEach(async () => {
31
- const config = createCofheConfig({
32
- supportedChains: [cofheArbSepolia],
33
- });
34
- cofheClient = createCofheClient(config);
35
- await cofheClient.connect(publicClient, walletClient);
36
- });
37
-
38
- describe('setUseWorker method', () => {
39
- it('should have setUseWorker method on EncryptInputsBuilder', () => {
40
- const builder = cofheClient.encryptInputs([Encryptable.uint128(100n)]);
41
-
42
- expect(builder).toHaveProperty('setUseWorker');
43
- expect(typeof builder.setUseWorker).toBe('function');
44
- });
45
-
46
- it('should return builder for method chaining', () => {
47
- const builder = cofheClient.encryptInputs([Encryptable.uint128(100n)]);
48
- const returnedBuilder = builder.setUseWorker(false);
49
-
50
- // Should return the same builder instance (or at least same type)
51
- expect(returnedBuilder).toBe(builder);
52
- });
53
-
54
- it('should allow chaining with other builder methods', () => {
55
- // Should be able to chain setUseWorker with setStepCallback
56
- const builder = cofheClient
57
- .encryptInputs([Encryptable.uint128(100n)])
58
- .setUseWorker(false)
59
- .onStep(() => {});
60
-
61
- expect(builder).toBeDefined();
62
- expect(builder).toHaveProperty('execute');
63
- });
64
-
65
- it('should accept true parameter', () => {
66
- expect(() => {
67
- cofheClient.encryptInputs([Encryptable.uint128(100n)]).setUseWorker(true);
68
- }).not.toThrow();
69
- });
70
-
71
- it('should accept false parameter', () => {
72
- expect(() => {
73
- cofheClient.encryptInputs([Encryptable.uint128(100n)]).setUseWorker(false);
74
- }).not.toThrow();
75
- });
76
-
77
- it('should have getUseWorker method', () => {
78
- const builder = cofheClient.encryptInputs([Encryptable.uint128(100n)]);
79
-
80
- expect(builder).toHaveProperty('getUseWorker');
81
- expect(typeof builder.getUseWorker).toBe('function');
82
- });
83
-
84
- it('should return current useWorker value', () => {
85
- const builderWithWorkers = cofheClient.encryptInputs([Encryptable.uint128(100n)]).setUseWorker(true);
86
- const builderWithoutWorkers = cofheClient.encryptInputs([Encryptable.uint128(100n)]).setUseWorker(false);
87
-
88
- // Should reflect config values
89
- expect(builderWithWorkers.getUseWorker()).toBe(true);
90
- expect(builderWithoutWorkers.getUseWorker()).toBe(false);
91
- });
92
-
93
- it('should reflect changes from setUseWorker', () => {
94
- const builder = cofheClient.encryptInputs([Encryptable.uint128(100n)]).setUseWorker(true);
95
- expect(builder.getUseWorker()).toBe(true);
96
-
97
- builder.setUseWorker(false);
98
- expect(builder.getUseWorker()).toBe(false);
99
-
100
- builder.setUseWorker(true);
101
- expect(builder.getUseWorker()).toBe(true);
102
- });
103
- });
104
-
105
- describe('Worker function availability', () => {
106
- it('should initialize client without errors', () => {
107
- const config = createCofheConfig({
108
- supportedChains: [cofheArbSepolia],
109
- useWorkers: true,
110
- });
111
-
112
- expect(() => {
113
- createCofheClient(config);
114
- }).not.toThrow();
115
- });
116
-
117
- it('should handle worker function when workers enabled', async () => {
118
- const config = createCofheConfig({
119
- supportedChains: [cofheArbSepolia],
120
- useWorkers: true,
121
- });
122
-
123
- const client = createCofheClient(config);
124
- await client.connect(publicClient, walletClient);
125
- const builder = client.encryptInputs([Encryptable.uint128(100n)]);
126
-
127
- // Should not throw even though workers aren't available in Node
128
- expect(() => {
129
- builder.setUseWorker(true);
130
- }).not.toThrow();
131
- });
132
-
133
- it('should handle when workers disabled', async () => {
134
- const config = createCofheConfig({
135
- supportedChains: [cofheArbSepolia],
136
- useWorkers: false,
137
- });
138
-
139
- const client = createCofheClient(config);
140
- await client.connect(publicClient, walletClient);
141
- const builder = client.encryptInputs([Encryptable.uint128(100n)]);
142
-
143
- expect(() => {
144
- builder.setUseWorker(false);
145
- }).not.toThrow();
146
- });
147
- });
148
- });