@cofhe/sdk 0.0.0-alpha-20260409113701

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 (132) hide show
  1. package/CHANGELOG.md +146 -0
  2. package/adapters/ethers5.test.ts +174 -0
  3. package/adapters/ethers5.ts +36 -0
  4. package/adapters/ethers6.test.ts +169 -0
  5. package/adapters/ethers6.ts +36 -0
  6. package/adapters/hardhat-node.ts +167 -0
  7. package/adapters/hardhat.hh2.test.ts +159 -0
  8. package/adapters/hardhat.ts +36 -0
  9. package/adapters/index.test.ts +20 -0
  10. package/adapters/index.ts +5 -0
  11. package/adapters/smartWallet.ts +99 -0
  12. package/adapters/test-utils.ts +53 -0
  13. package/adapters/types.ts +6 -0
  14. package/adapters/wagmi.test.ts +156 -0
  15. package/adapters/wagmi.ts +17 -0
  16. package/chains/chains/arbSepolia.ts +14 -0
  17. package/chains/chains/baseSepolia.ts +14 -0
  18. package/chains/chains/hardhat.ts +15 -0
  19. package/chains/chains/localcofhe.ts +14 -0
  20. package/chains/chains/sepolia.ts +14 -0
  21. package/chains/chains.test.ts +50 -0
  22. package/chains/defineChain.ts +18 -0
  23. package/chains/index.ts +35 -0
  24. package/chains/types.ts +32 -0
  25. package/core/baseBuilder.ts +119 -0
  26. package/core/client.test.ts +429 -0
  27. package/core/client.ts +341 -0
  28. package/core/clientTypes.ts +119 -0
  29. package/core/config.test.ts +242 -0
  30. package/core/config.ts +225 -0
  31. package/core/consts.ts +22 -0
  32. package/core/decrypt/MockThresholdNetworkAbi.ts +179 -0
  33. package/core/decrypt/cofheMocksDecryptForTx.ts +84 -0
  34. package/core/decrypt/cofheMocksDecryptForView.ts +48 -0
  35. package/core/decrypt/decryptForTxBuilder.ts +359 -0
  36. package/core/decrypt/decryptForViewBuilder.ts +332 -0
  37. package/core/decrypt/decryptUtils.ts +28 -0
  38. package/core/decrypt/pollCallbacks.test.ts +194 -0
  39. package/core/decrypt/polling.ts +14 -0
  40. package/core/decrypt/tnDecryptUtils.ts +65 -0
  41. package/core/decrypt/tnDecryptV1.ts +171 -0
  42. package/core/decrypt/tnDecryptV2.ts +365 -0
  43. package/core/decrypt/tnSealOutputV1.ts +59 -0
  44. package/core/decrypt/tnSealOutputV2.ts +324 -0
  45. package/core/decrypt/verifyDecryptResult.ts +52 -0
  46. package/core/encrypt/MockZkVerifierAbi.ts +106 -0
  47. package/core/encrypt/cofheMocksZkVerifySign.ts +281 -0
  48. package/core/encrypt/encryptInputsBuilder.test.ts +747 -0
  49. package/core/encrypt/encryptInputsBuilder.ts +583 -0
  50. package/core/encrypt/encryptUtils.ts +67 -0
  51. package/core/encrypt/zkPackProveVerify.ts +335 -0
  52. package/core/error.ts +168 -0
  53. package/core/fetchKeys.test.ts +195 -0
  54. package/core/fetchKeys.ts +144 -0
  55. package/core/index.ts +106 -0
  56. package/core/keyStore.test.ts +226 -0
  57. package/core/keyStore.ts +154 -0
  58. package/core/permits.test.ts +493 -0
  59. package/core/permits.ts +201 -0
  60. package/core/types.ts +419 -0
  61. package/core/utils.ts +130 -0
  62. package/dist/adapters.cjs +88 -0
  63. package/dist/adapters.d.cts +14576 -0
  64. package/dist/adapters.d.ts +14576 -0
  65. package/dist/adapters.js +83 -0
  66. package/dist/chains.cjs +111 -0
  67. package/dist/chains.d.cts +121 -0
  68. package/dist/chains.d.ts +121 -0
  69. package/dist/chains.js +1 -0
  70. package/dist/chunk-36FBWLUS.js +3310 -0
  71. package/dist/chunk-7HLGHV67.js +990 -0
  72. package/dist/chunk-TBLR7NNE.js +102 -0
  73. package/dist/clientTypes-AVSCBet7.d.cts +998 -0
  74. package/dist/clientTypes-flH1ju82.d.ts +998 -0
  75. package/dist/core.cjs +4362 -0
  76. package/dist/core.d.cts +138 -0
  77. package/dist/core.d.ts +138 -0
  78. package/dist/core.js +3 -0
  79. package/dist/node.cjs +4225 -0
  80. package/dist/node.d.cts +22 -0
  81. package/dist/node.d.ts +22 -0
  82. package/dist/node.js +91 -0
  83. package/dist/permit-jRirYqFt.d.cts +376 -0
  84. package/dist/permit-jRirYqFt.d.ts +376 -0
  85. package/dist/permits.cjs +1025 -0
  86. package/dist/permits.d.cts +353 -0
  87. package/dist/permits.d.ts +353 -0
  88. package/dist/permits.js +1 -0
  89. package/dist/types-YiAC4gig.d.cts +33 -0
  90. package/dist/types-YiAC4gig.d.ts +33 -0
  91. package/dist/web.cjs +4434 -0
  92. package/dist/web.d.cts +42 -0
  93. package/dist/web.d.ts +42 -0
  94. package/dist/web.js +256 -0
  95. package/dist/zkProve.worker.cjs +93 -0
  96. package/dist/zkProve.worker.d.cts +2 -0
  97. package/dist/zkProve.worker.d.ts +2 -0
  98. package/dist/zkProve.worker.js +91 -0
  99. package/node/client.test.ts +159 -0
  100. package/node/config.test.ts +68 -0
  101. package/node/encryptInputs.test.ts +155 -0
  102. package/node/index.ts +97 -0
  103. package/node/storage.ts +51 -0
  104. package/package.json +121 -0
  105. package/permits/index.ts +68 -0
  106. package/permits/localstorage.test.ts +113 -0
  107. package/permits/onchain-utils.ts +221 -0
  108. package/permits/permit.test.ts +534 -0
  109. package/permits/permit.ts +386 -0
  110. package/permits/sealing.test.ts +84 -0
  111. package/permits/sealing.ts +131 -0
  112. package/permits/signature.ts +79 -0
  113. package/permits/store.test.ts +88 -0
  114. package/permits/store.ts +156 -0
  115. package/permits/test-utils.ts +28 -0
  116. package/permits/types.ts +204 -0
  117. package/permits/utils.ts +58 -0
  118. package/permits/validation.test.ts +361 -0
  119. package/permits/validation.ts +327 -0
  120. package/web/client.web.test.ts +159 -0
  121. package/web/config.web.test.ts +69 -0
  122. package/web/const.ts +2 -0
  123. package/web/encryptInputs.web.test.ts +172 -0
  124. package/web/index.ts +166 -0
  125. package/web/storage.ts +49 -0
  126. package/web/worker.builder.web.test.ts +148 -0
  127. package/web/worker.config.web.test.ts +329 -0
  128. package/web/worker.output.web.test.ts +84 -0
  129. package/web/workerManager.test.ts +80 -0
  130. package/web/workerManager.ts +214 -0
  131. package/web/workerManager.web.test.ts +114 -0
  132. package/web/zkProve.worker.ts +133 -0
@@ -0,0 +1,493 @@
1
+ /**
2
+ * @vitest-environment happy-dom
3
+ */
4
+ import { permitStore } from '@/permits';
5
+
6
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
7
+ import { createPublicClient, createWalletClient, http, type PublicClient, type WalletClient } from 'viem';
8
+ import { arbitrumSepolia } from 'viem/chains';
9
+ import { privateKeyToAccount } from 'viem/accounts';
10
+ import { permits } from './permits.js';
11
+
12
+ // Type declarations for happy-dom environment
13
+ declare const localStorage: {
14
+ clear: () => void;
15
+ getItem: (name: string) => string | null;
16
+ setItem: (name: string, value: string) => void;
17
+ };
18
+
19
+ // Test private keys (well-known test keys from Anvil/Hardhat)
20
+ const BOB_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'; // Bob - always issuer
21
+ const ALICE_PRIVATE_KEY = '0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d'; // Alice - always recipient
22
+
23
+ // Create real viem clients for Arbitrum Sepolia
24
+ const publicClient: PublicClient = createPublicClient({
25
+ chain: arbitrumSepolia,
26
+ transport: http(),
27
+ });
28
+
29
+ const bobWalletClient: WalletClient = createWalletClient({
30
+ chain: arbitrumSepolia,
31
+ transport: http(),
32
+ account: privateKeyToAccount(BOB_PRIVATE_KEY),
33
+ });
34
+
35
+ const aliceWalletClient: WalletClient = createWalletClient({
36
+ chain: arbitrumSepolia,
37
+ transport: http(),
38
+ account: privateKeyToAccount(ALICE_PRIVATE_KEY),
39
+ });
40
+
41
+ // Helper to get the wallet addresses
42
+ const bobAddress = bobWalletClient.account!.address;
43
+ const aliceAddress = aliceWalletClient.account!.address;
44
+ const chainId = 421614; // Arbitrum Sepolia
45
+
46
+ describe('Core Permits Tests', () => {
47
+ beforeEach(() => {
48
+ // Clear localStorage and reset stores
49
+ localStorage.clear();
50
+ permitStore.store.setState({ permits: {}, activePermitHash: {} });
51
+ });
52
+
53
+ afterEach(() => {
54
+ localStorage.clear();
55
+ permitStore.store.setState({ permits: {}, activePermitHash: {} });
56
+ });
57
+
58
+ describe('Permit Creation', () => {
59
+ it('should create and store self permit', async () => {
60
+ const permit = await permits.createSelf(
61
+ { name: 'Test Self Permit', issuer: bobAddress },
62
+ publicClient,
63
+ bobWalletClient
64
+ );
65
+
66
+ expect(permit).toBeDefined();
67
+ expect(permit.name).toBe('Test Self Permit');
68
+ expect(permit.type).toBe('self');
69
+ expect(permit.issuer).toBe(bobAddress);
70
+ expect(permit.issuerSignature).toBeDefined();
71
+ expect(permit.issuerSignature).not.toBe('0x');
72
+
73
+ // Verify localStorage
74
+ const storedData = localStorage.getItem('cofhesdk-permits');
75
+ expect(storedData).toBeDefined();
76
+ const parsedData = JSON.parse(storedData!);
77
+ expect(parsedData.state.permits[chainId][bobAddress]).toBeDefined();
78
+ expect(parsedData.state.activePermitHash[chainId][bobAddress]).toBeDefined();
79
+ });
80
+
81
+ it('should create and store sharing permit', async () => {
82
+ const permit = await permits.createSharing(
83
+ {
84
+ name: 'Test Sharing Permit',
85
+ issuer: bobAddress,
86
+ recipient: aliceAddress,
87
+ },
88
+ publicClient,
89
+ bobWalletClient
90
+ );
91
+
92
+ expect(permit.name).toBe('Test Sharing Permit');
93
+ expect(permit.type).toBe('sharing');
94
+ expect(permit.issuer).toBe(bobAddress);
95
+ expect(permit.recipient).toBe(aliceAddress);
96
+ expect(permit.issuerSignature).toBeDefined();
97
+ expect(permit.issuerSignature).not.toBe('0x');
98
+
99
+ // Verify localStorage
100
+ const storedData = localStorage.getItem('cofhesdk-permits');
101
+ expect(storedData).toBeDefined();
102
+ const parsedData = JSON.parse(storedData!);
103
+ expect(parsedData.state.permits[chainId][bobAddress]).toBeDefined();
104
+ expect(parsedData.state.activePermitHash[chainId][bobAddress]).toBeDefined();
105
+ });
106
+
107
+ it('should import shared permit from JSON string', async () => {
108
+ // First create a sharing permit to import
109
+ const sharingPermit = await permits.createSharing(
110
+ {
111
+ name: 'Original Sharing Permit',
112
+ issuer: bobAddress,
113
+ recipient: aliceAddress,
114
+ },
115
+ publicClient,
116
+ bobWalletClient
117
+ );
118
+
119
+ // Export the permit as JSON string
120
+ const permitJson = JSON.stringify({
121
+ name: sharingPermit.name,
122
+ type: sharingPermit.type,
123
+ issuer: sharingPermit.issuer,
124
+ expiration: sharingPermit.expiration,
125
+ recipient: sharingPermit.recipient,
126
+ validatorId: sharingPermit.validatorId,
127
+ validatorContract: sharingPermit.validatorContract,
128
+ issuerSignature: sharingPermit.issuerSignature,
129
+ });
130
+
131
+ // Import the permit as Alice (recipient)
132
+ const permit = await permits.importShared(permitJson, publicClient, aliceWalletClient);
133
+
134
+ expect(permit.name).toBe('Original Sharing Permit');
135
+ expect(permit.type).toBe('recipient');
136
+ expect(permit.issuer).toBe(bobAddress);
137
+ expect(permit.recipient).toBe(aliceAddress);
138
+ expect(permit.recipientSignature).toBeDefined();
139
+ expect(permit.recipientSignature).not.toBe('0x');
140
+ });
141
+ });
142
+
143
+ describe('Permit Retrieval', () => {
144
+ let createdPermit: any;
145
+ let permitHash: string;
146
+
147
+ beforeEach(async () => {
148
+ // Create a real permit for testing
149
+ createdPermit = await permits.createSelf(
150
+ { name: 'Test Permit', issuer: bobAddress },
151
+ publicClient,
152
+ bobWalletClient
153
+ );
154
+ permitHash = createdPermit.hash;
155
+ });
156
+
157
+ it('should get permit by hash', async () => {
158
+ const permit = await permits.getPermit(chainId, bobAddress, permitHash);
159
+ expect(permit?.name).toBe('Test Permit');
160
+ expect(permit?.type).toBe('self');
161
+ });
162
+
163
+ it('should get all permits', async () => {
164
+ const allPermits = await permits.getPermits(chainId, bobAddress);
165
+ expect(Object.keys(allPermits).length).toBeGreaterThan(0);
166
+ });
167
+
168
+ it('should get active permit', async () => {
169
+ const permit = await permits.getActivePermit(chainId, bobAddress);
170
+ expect(permit?.name).toBe('Test Permit');
171
+ });
172
+
173
+ it('should get active permit hash', async () => {
174
+ const hash = await permits.getActivePermitHash(chainId, bobAddress);
175
+ expect(typeof hash).toBe('string');
176
+ });
177
+ });
178
+
179
+ describe('localStorage Integration', () => {
180
+ it('should persist permits to localStorage', async () => {
181
+ const createdPermit = await permits.createSelf(
182
+ { name: 'Test Permit', issuer: bobAddress },
183
+ publicClient,
184
+ bobWalletClient
185
+ );
186
+
187
+ const storedData = localStorage.getItem('cofhesdk-permits');
188
+ expect(storedData).toBeDefined();
189
+
190
+ const parsedData = JSON.parse(storedData!);
191
+ expect(parsedData.state.permits[chainId][bobAddress]).toBeDefined();
192
+ expect(parsedData.state.activePermitHash[chainId][bobAddress]).toBeDefined();
193
+
194
+ // Verify the permit data structure
195
+ const permitKeys = Object.keys(parsedData.state.permits[chainId][bobAddress]);
196
+ expect(permitKeys.length).toBeGreaterThan(0);
197
+
198
+ const serializedPermit = permits.serialize(createdPermit);
199
+ expect(parsedData.state.permits[chainId][bobAddress][createdPermit.hash]).toEqual(serializedPermit);
200
+ });
201
+ });
202
+
203
+ describe('Real Network Integration', () => {
204
+ it('should create permit with real EIP712 domain from Arbitrum Sepolia', async () => {
205
+ const permit = await permits.createSelf(
206
+ { name: 'Real Network Permit', issuer: bobAddress },
207
+ publicClient,
208
+ bobWalletClient
209
+ );
210
+
211
+ expect(permit._signedDomain).toBeDefined();
212
+ expect(permit._signedDomain?.chainId).toBe(chainId);
213
+ expect(permit._signedDomain?.name).toBeDefined();
214
+ expect(permit._signedDomain?.version).toBeDefined();
215
+ expect(permit._signedDomain?.verifyingContract).toBeDefined();
216
+ });
217
+
218
+ it('should handle multiple permits on real network', async () => {
219
+ // Create multiple permits
220
+ await permits.createSelf({ name: 'Permit 1', issuer: bobAddress }, publicClient, bobWalletClient);
221
+ await permits.createSharing(
222
+ {
223
+ name: 'Permit 2',
224
+ issuer: bobAddress,
225
+ recipient: aliceAddress,
226
+ },
227
+ publicClient,
228
+ bobWalletClient
229
+ );
230
+
231
+ // Verify both permits exist
232
+ const allPermits = await permits.getPermits(chainId, bobAddress);
233
+ expect(Object.keys(allPermits).length).toBeGreaterThanOrEqual(2);
234
+
235
+ // Verify active permit is the last created one
236
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
237
+ expect(activePermit?.name).toBe('Permit 2');
238
+ });
239
+ });
240
+
241
+ describe('getOrCreateSelfPermit', () => {
242
+ it('should create a new self permit when none exists', async () => {
243
+ const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
244
+ issuer: bobAddress,
245
+ name: 'New Self Permit',
246
+ });
247
+
248
+ expect(permit).toBeDefined();
249
+ expect(permit.name).toBe('New Self Permit');
250
+ expect(permit.type).toBe('self');
251
+ expect(permit.issuer).toBe(bobAddress);
252
+ expect(permit.issuerSignature).toBeDefined();
253
+
254
+ // Verify it was stored and set as active
255
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
256
+ expect(activePermit?.name).toBe('New Self Permit');
257
+ });
258
+
259
+ it('should return existing self permit when one exists', async () => {
260
+ // Create an initial self permit
261
+ const firstPermit = await permits.createSelf(
262
+ { name: 'First Self Permit', issuer: bobAddress },
263
+ publicClient,
264
+ bobWalletClient
265
+ );
266
+
267
+ // Call getOrCreateSelfPermit - should return existing
268
+ const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
269
+ issuer: bobAddress,
270
+ name: 'Should Not Create This',
271
+ });
272
+
273
+ expect(permit.name).toBe('First Self Permit');
274
+ expect(permit.hash).toBe(firstPermit.hash);
275
+
276
+ // Verify no new permit was created
277
+ const allPermits = await permits.getPermits(chainId, bobAddress);
278
+ expect(Object.keys(allPermits).length).toBe(1);
279
+ });
280
+
281
+ it('should create new self permit when active permit is sharing type', async () => {
282
+ // Create a sharing permit first
283
+ await permits.createSharing(
284
+ {
285
+ name: 'Sharing Permit',
286
+ issuer: bobAddress,
287
+ recipient: aliceAddress,
288
+ },
289
+ publicClient,
290
+ bobWalletClient
291
+ );
292
+
293
+ // Call getOrCreateSelfPermit - should create new since active is sharing type
294
+ const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
295
+ issuer: bobAddress,
296
+ name: 'New Self Permit',
297
+ });
298
+
299
+ expect(permit.name).toBe('New Self Permit');
300
+ expect(permit.type).toBe('self');
301
+
302
+ // Verify two permits exist now
303
+ const allPermits = await permits.getPermits(chainId, bobAddress);
304
+ expect(Object.keys(allPermits).length).toBe(2);
305
+ });
306
+
307
+ it('should use default options when none provided', async () => {
308
+ const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress);
309
+
310
+ expect(permit).toBeDefined();
311
+ expect(permit.type).toBe('self');
312
+ expect(permit.issuer).toBe(bobAddress);
313
+ expect(permit.name).toBe('Autogenerated Self Permit');
314
+ });
315
+
316
+ it('should use default chainId and account when not provided', async () => {
317
+ const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, undefined, undefined, {
318
+ issuer: bobAddress,
319
+ name: 'Test Permit',
320
+ });
321
+
322
+ expect(permit).toBeDefined();
323
+ expect(permit.issuer).toBe(bobAddress);
324
+
325
+ // Verify it was stored with the chain's actual chainId
326
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
327
+ expect(activePermit?.name).toBe('Test Permit');
328
+ });
329
+ });
330
+
331
+ describe('getOrCreateSharingPermit', () => {
332
+ it('should create a new sharing permit when none exists', async () => {
333
+ const permit = await permits.getOrCreateSharingPermit(
334
+ publicClient,
335
+ bobWalletClient,
336
+ {
337
+ issuer: bobAddress,
338
+ recipient: aliceAddress,
339
+ name: 'New Sharing Permit',
340
+ },
341
+ chainId,
342
+ bobAddress
343
+ );
344
+
345
+ expect(permit).toBeDefined();
346
+ expect(permit.name).toBe('New Sharing Permit');
347
+ expect(permit.type).toBe('sharing');
348
+ expect(permit.issuer).toBe(bobAddress);
349
+ expect(permit.recipient).toBe(aliceAddress);
350
+ expect(permit.issuerSignature).toBeDefined();
351
+
352
+ // Verify it was stored and set as active
353
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
354
+ expect(activePermit?.name).toBe('New Sharing Permit');
355
+ });
356
+
357
+ it('should return existing sharing permit when one exists', async () => {
358
+ // Create an initial sharing permit
359
+ const firstPermit = await permits.createSharing(
360
+ {
361
+ name: 'First Sharing Permit',
362
+ issuer: bobAddress,
363
+ recipient: aliceAddress,
364
+ },
365
+ publicClient,
366
+ bobWalletClient
367
+ );
368
+
369
+ // Call getOrCreateSharingPermit - should return existing
370
+ const permit = await permits.getOrCreateSharingPermit(
371
+ publicClient,
372
+ bobWalletClient,
373
+ {
374
+ issuer: bobAddress,
375
+ recipient: aliceAddress,
376
+ name: 'Should Not Create This',
377
+ },
378
+ chainId,
379
+ bobAddress
380
+ );
381
+
382
+ expect(permit.name).toBe('First Sharing Permit');
383
+ expect(permit.hash).toBe(firstPermit.hash);
384
+
385
+ // Verify no new permit was created
386
+ const allPermits = await permits.getPermits(chainId, bobAddress);
387
+ expect(Object.keys(allPermits).length).toBe(1);
388
+ });
389
+
390
+ it('should create new sharing permit when active permit is self type', async () => {
391
+ // Create a self permit first
392
+ await permits.createSelf({ name: 'Self Permit', issuer: bobAddress }, publicClient, bobWalletClient);
393
+
394
+ // Call getOrCreateSharingPermit - should create new since active is self type
395
+ const permit = await permits.getOrCreateSharingPermit(
396
+ publicClient,
397
+ bobWalletClient,
398
+ {
399
+ issuer: bobAddress,
400
+ recipient: aliceAddress,
401
+ name: 'New Sharing Permit',
402
+ },
403
+ chainId,
404
+ bobAddress
405
+ );
406
+
407
+ expect(permit.name).toBe('New Sharing Permit');
408
+ expect(permit.type).toBe('sharing');
409
+
410
+ // Verify two permits exist now
411
+ const allPermits = await permits.getPermits(chainId, bobAddress);
412
+ expect(Object.keys(allPermits).length).toBe(2);
413
+ });
414
+
415
+ it('should use default chainId and account when not provided', async () => {
416
+ const permit = await permits.getOrCreateSharingPermit(
417
+ publicClient,
418
+ bobWalletClient,
419
+ {
420
+ issuer: bobAddress,
421
+ recipient: aliceAddress,
422
+ name: 'Test Sharing Permit',
423
+ },
424
+ undefined,
425
+ undefined
426
+ );
427
+
428
+ expect(permit).toBeDefined();
429
+ expect(permit.issuer).toBe(bobAddress);
430
+ expect(permit.recipient).toBe(aliceAddress);
431
+
432
+ // Verify it was stored with the chain's actual chainId
433
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
434
+ expect(activePermit?.name).toBe('Test Sharing Permit');
435
+ });
436
+ });
437
+
438
+ describe('getOrCreate - Multiple Types Scenarios', () => {
439
+ it('should handle switching between self and sharing permits', async () => {
440
+ // Create self permit
441
+ const selfPermit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
442
+ issuer: bobAddress,
443
+ name: 'Self Permit',
444
+ });
445
+ expect(selfPermit.type).toBe('self');
446
+
447
+ // Create sharing permit (should create new one)
448
+ const sharingPermit = await permits.getOrCreateSharingPermit(
449
+ publicClient,
450
+ bobWalletClient,
451
+ {
452
+ issuer: bobAddress,
453
+ recipient: aliceAddress,
454
+ name: 'Sharing Permit',
455
+ },
456
+ chainId,
457
+ bobAddress
458
+ );
459
+ expect(sharingPermit.type).toBe('sharing');
460
+
461
+ // Both should exist
462
+ const allPermits = await permits.getPermits(chainId, bobAddress);
463
+ expect(Object.keys(allPermits).length).toBe(2);
464
+
465
+ // Active permit should be the sharing one
466
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
467
+ expect(activePermit?.type).toBe('sharing');
468
+ expect(activePermit?.name).toBe('Sharing Permit');
469
+ });
470
+
471
+ it('should correctly handle sequential getOrCreate calls', async () => {
472
+ // First call - creates new
473
+ const permit1 = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
474
+ issuer: bobAddress,
475
+ name: 'Permit 1',
476
+ });
477
+
478
+ // Second call - returns existing
479
+ const permit2 = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
480
+ issuer: bobAddress,
481
+ name: 'Permit 2',
482
+ });
483
+
484
+ // Should be the same permit
485
+ expect(permit1.hash).toBe(permit2.hash);
486
+ expect(permit2.name).toBe('Permit 1'); // Original name
487
+
488
+ // Only one permit should exist
489
+ const allPermits = await permits.getPermits(chainId, bobAddress);
490
+ expect(Object.keys(allPermits).length).toBe(1);
491
+ });
492
+ });
493
+ });
@@ -0,0 +1,201 @@
1
+ import {
2
+ type ImportSharedPermitOptions,
3
+ PermitUtils,
4
+ type CreateSelfPermitOptions,
5
+ type CreateSharingPermitOptions,
6
+ type Permit,
7
+ permitStore,
8
+ type SerializedPermit,
9
+ type SelfPermit,
10
+ type RecipientPermit,
11
+ type SharingPermit,
12
+ type PermitHashFields,
13
+ } from '@/permits';
14
+
15
+ import { type PublicClient, type WalletClient } from 'viem';
16
+
17
+ // HELPERS
18
+
19
+ // Helper function to store permit as active permit
20
+ const storeActivePermit = async (permit: Permit, publicClient: any, walletClient: any) => {
21
+ const chainId = await publicClient.getChainId();
22
+ const account = walletClient.account!.address;
23
+
24
+ permitStore.setPermit(chainId, account, permit);
25
+ permitStore.setActivePermitHash(chainId, account, permit.hash);
26
+ };
27
+
28
+ // Generic function to handle permit creation with error handling
29
+ const createPermitWithSign = async <T, TPermit extends Permit>(
30
+ options: T,
31
+ publicClient: PublicClient,
32
+ walletClient: WalletClient,
33
+ permitMethod: (options: T, publicClient: PublicClient, walletClient: WalletClient) => Promise<TPermit>
34
+ ): Promise<TPermit> => {
35
+ const permit = await permitMethod(options, publicClient, walletClient);
36
+ await storeActivePermit(permit, publicClient, walletClient);
37
+ return permit;
38
+ };
39
+
40
+ // CREATE
41
+
42
+ /**
43
+ * Create a permit usable by the connected user
44
+ * Stores the permit and selects it as the active permit
45
+ * @param options - The options for creating a self permit
46
+ * @returns The created permit or error
47
+ */
48
+ const createSelf = async (
49
+ options: CreateSelfPermitOptions,
50
+ publicClient: PublicClient,
51
+ walletClient: WalletClient
52
+ ): Promise<SelfPermit> => {
53
+ return createPermitWithSign(options, publicClient, walletClient, PermitUtils.createSelfAndSign);
54
+ };
55
+
56
+ const createSharing = async (
57
+ options: CreateSharingPermitOptions,
58
+ publicClient: PublicClient,
59
+ walletClient: WalletClient
60
+ ): Promise<SharingPermit> => {
61
+ return createPermitWithSign(options, publicClient, walletClient, PermitUtils.createSharingAndSign);
62
+ };
63
+
64
+ const importShared = async (
65
+ options: ImportSharedPermitOptions | string,
66
+ publicClient: PublicClient,
67
+ walletClient: WalletClient
68
+ ): Promise<RecipientPermit> => {
69
+ return createPermitWithSign(options, publicClient, walletClient, PermitUtils.importSharedAndSign);
70
+ };
71
+
72
+ // PERMIT UTILS
73
+
74
+ const getHash = (permit: PermitHashFields) => {
75
+ return PermitUtils.getHash(permit);
76
+ };
77
+
78
+ const serialize = (permit: Permit) => {
79
+ return PermitUtils.serialize(permit);
80
+ };
81
+
82
+ const deserialize = (serialized: SerializedPermit) => {
83
+ return PermitUtils.deserialize(serialized);
84
+ };
85
+
86
+ // GET
87
+
88
+ const getPermit = (chainId: number, account: string, hash: string): Permit | undefined => {
89
+ return permitStore.getPermit(chainId, account, hash);
90
+ };
91
+
92
+ const getPermits = (chainId: number, account: string): Record<string, Permit> => {
93
+ return permitStore.getPermits(chainId, account);
94
+ };
95
+
96
+ const getActivePermit = (chainId: number, account: string): Permit | undefined => {
97
+ return permitStore.getActivePermit(chainId, account);
98
+ };
99
+
100
+ const getActivePermitHash = (chainId: number, account: string): string | undefined => {
101
+ return permitStore.getActivePermitHash(chainId, account);
102
+ };
103
+
104
+ const selectActivePermit = (chainId: number, account: string, hash: string): void => {
105
+ permitStore.setActivePermitHash(chainId, account, hash);
106
+ };
107
+
108
+ // GET OR CREATE
109
+
110
+ /**
111
+ * Get the active self permit or create a new one if it doesn't exist
112
+ * @param publicClient - The public client
113
+ * @param walletClient - The wallet client
114
+ * @param chainId - Optional chain ID (will use publicClient if not provided)
115
+ * @param account - Optional account (will use walletClient if not provided)
116
+ * @param options - The options for creating a self permit
117
+ * @returns The existing or newly created permit
118
+ */
119
+ const getOrCreateSelfPermit = async (
120
+ publicClient: PublicClient,
121
+ walletClient: WalletClient,
122
+ chainId?: number,
123
+ account?: string,
124
+ options?: CreateSelfPermitOptions
125
+ ): Promise<Permit> => {
126
+ const _chainId = chainId ?? (await publicClient.getChainId());
127
+ const _account = account ?? walletClient.account!.address;
128
+
129
+ // Try to get active permit first
130
+ const activePermit = await getActivePermit(_chainId, _account);
131
+
132
+ if (activePermit && activePermit.type === 'self') {
133
+ return activePermit;
134
+ }
135
+
136
+ // No active permit or wrong type, create new one
137
+ return createSelf(options ?? { issuer: _account, name: 'Autogenerated Self Permit' }, publicClient, walletClient);
138
+ };
139
+
140
+ /**
141
+ * Get the active sharing permit or create a new one if it doesn't exist
142
+ * @param publicClient - The public client
143
+ * @param walletClient - The wallet client
144
+ * @param options - The options for creating a sharing permit (required)
145
+ * @param chainId - Optional chain ID (will use publicClient if not provided)
146
+ * @param account - Optional account (will use walletClient if not provided)
147
+ * @returns The existing or newly created permit
148
+ */
149
+ const getOrCreateSharingPermit = async (
150
+ publicClient: PublicClient,
151
+ walletClient: WalletClient,
152
+ options: CreateSharingPermitOptions,
153
+ chainId?: number,
154
+ account?: string
155
+ ): Promise<Permit> => {
156
+ const _chainId = chainId ?? (await publicClient.getChainId());
157
+ const _account = account ?? walletClient.account!.address;
158
+
159
+ // Try to get active permit first
160
+ const activePermit = await getActivePermit(_chainId, _account);
161
+
162
+ if (activePermit && activePermit.type === 'sharing') {
163
+ return activePermit;
164
+ }
165
+
166
+ return createSharing(options, publicClient, walletClient);
167
+ };
168
+
169
+ // REMOVE
170
+
171
+ const removePermit = async (chainId: number, account: string, hash: string): Promise<void> =>
172
+ permitStore.removePermit(chainId, account, hash);
173
+
174
+ const removeActivePermit = async (chainId: number, account: string): Promise<void> =>
175
+ permitStore.removeActivePermitHash(chainId, account);
176
+
177
+ // EXPORT
178
+
179
+ export const permits = {
180
+ getSnapshot: permitStore.store.getState,
181
+ subscribe: permitStore.store.subscribe,
182
+
183
+ createSelf,
184
+ createSharing,
185
+ importShared,
186
+
187
+ getOrCreateSelfPermit,
188
+ getOrCreateSharingPermit,
189
+
190
+ getHash,
191
+ serialize,
192
+ deserialize,
193
+
194
+ getPermit,
195
+ getPermits,
196
+ getActivePermit,
197
+ getActivePermitHash,
198
+ removePermit,
199
+ selectActivePermit,
200
+ removeActivePermit,
201
+ };