@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
package/core/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // Client (base implementations)
2
- export { createCofhesdkClientBase } from './client.js';
2
+ export { createCofhesdkClientBase, InitialConnectStore as CONNECT_STORE_DEFAULTS } from './client.js';
3
3
 
4
4
  // Configuration (base implementations)
5
5
  export { createCofhesdkConfigBase, getCofhesdkConfigItem } from './config.js';
@@ -12,6 +12,9 @@ export type {
12
12
  CofhesdkClientParams,
13
13
  CofhesdkClientConnectionState,
14
14
  CofhesdkClientPermits,
15
+ } from './clientTypes.js';
16
+
17
+ export type {
15
18
  IStorage,
16
19
  // Primitive types
17
20
  Primitive,
@@ -34,35 +37,33 @@ export type {
34
37
  EncryptedUint32Input,
35
38
  EncryptedUint64Input,
36
39
  EncryptedUint128Input,
37
- EncryptedUint256Input,
38
40
  EncryptedAddressInput,
39
41
  EncryptedItemInputs,
40
42
  EncryptableToEncryptedItemInputMap,
43
+ FheTypeValue,
41
44
  // Decryption types
42
45
  UnsealedItem,
43
46
  // Util types
44
47
  EncryptStepCallbackFunction as EncryptSetStateFn,
48
+ EncryptStepCallbackContext,
49
+ } from './types.js';
50
+ export {
51
+ FheTypes,
52
+ FheUintUTypes,
53
+ FheAllUTypes,
54
+ Encryptable,
55
+ isEncryptableItem,
56
+ EncryptStep,
57
+ isLastEncryptionStep,
58
+ assertCorrectEncryptedItemInput,
45
59
  } from './types.js';
46
- export { FheTypes, FheUintUTypes, FheAllUTypes, Encryptable, isEncryptableItem, EncryptStep } from './types.js';
47
60
 
48
61
  // Error handling
49
62
  export { CofhesdkError, CofhesdkErrorCode, isCofhesdkError } from './error.js';
50
63
  export type { CofhesdkErrorParams } from './error.js';
51
64
 
52
- // Result types
53
- export {
54
- ResultErr,
55
- ResultOk,
56
- ResultErrOrInternal,
57
- ResultHttpError,
58
- ResultValidationError,
59
- resultWrapper,
60
- resultWrapperSync,
61
- } from './result.js';
62
- export type { Result } from './result.js';
63
-
64
65
  // Key fetching
65
- export { fetchKeys, fetchMultichainKeys } from './fetchKeys.js';
66
+ export { fetchKeys } from './fetchKeys.js';
66
67
  export type { FheKeyDeserializer } from './fetchKeys.js';
67
68
 
68
69
  // Key storage
@@ -74,4 +75,15 @@ export { EncryptInputsBuilder } from './encrypt/encryptInputsBuilder.js';
74
75
  export { DecryptHandlesBuilder } from './decrypt/decryptHandleBuilder.js';
75
76
 
76
77
  // ZK utilities
77
- export type { ZkBuilderAndCrsGenerator } from './encrypt/zkPackProveVerify.js';
78
+ export type {
79
+ ZkBuilderAndCrsGenerator,
80
+ ZkProveWorkerFunction,
81
+ ZkProveWorkerRequest,
82
+ ZkProveWorkerResponse,
83
+ } from './encrypt/zkPackProveVerify.js';
84
+ export { zkProveWithWorker } from './encrypt/zkPackProveVerify.js';
85
+
86
+ export { MOCKS_ZK_VERIFIER_SIGNER_ADDRESS } from './encrypt/cofheMocksZkVerifySign.js';
87
+
88
+ // Utils
89
+ export { fheTypeToString } from './utils.js';
package/core/keyStore.ts CHANGED
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-unused-vars */
2
1
  import { createStore, type StoreApi } from 'zustand/vanilla';
3
2
  import { persist, createJSONStorage } from 'zustand/middleware';
4
3
  import { produce } from 'immer';
@@ -24,6 +23,30 @@ export type KeysStorage = {
24
23
  rehydrateKeysStore: () => Promise<void>;
25
24
  };
26
25
 
26
+ function isValidPersistedState(state: unknown): state is KeysStore {
27
+ if (state && typeof state === 'object') {
28
+ if ('fhe' in state && 'crs' in state) {
29
+ return true;
30
+ } else {
31
+ throw new Error(
32
+ "Invalid persisted state structure for KeysStore. Is object but doesn't contain required fields 'fhe' and 'crs'."
33
+ );
34
+ }
35
+ }
36
+
37
+ return false;
38
+ }
39
+
40
+ const DEFAULT_KEYS_STORE: KeysStore = {
41
+ fhe: {},
42
+ crs: {},
43
+ };
44
+
45
+ type StoreWithPersist = ReturnType<typeof createStoreWithPersit>;
46
+
47
+ function isStoreWithPersist(store: StoreApi<KeysStore> | StoreWithPersist): store is StoreWithPersist {
48
+ return 'persist' in store;
49
+ }
27
50
  /**
28
51
  * Creates a keys storage instance using the provided storage implementation
29
52
  * @param storage - The storage implementation to use (IStorage interface), or null for non-persisted store
@@ -32,39 +55,7 @@ export type KeysStorage = {
32
55
  export function createKeysStore(storage: IStorage | null): KeysStorage {
33
56
  // Conditionally create store with or without persist wrapper
34
57
  const keysStore = storage
35
- ? createStore<KeysStore>()(
36
- persist(
37
- () => ({
38
- fhe: {},
39
- crs: {},
40
- }),
41
- {
42
- name: 'cofhesdk-keys',
43
- storage: createJSONStorage(() => storage),
44
- merge: (persistedState, currentState) => {
45
- const persisted = persistedState as KeysStore;
46
- const current = currentState as KeysStore;
47
-
48
- // Deep merge for fhe
49
- const mergedFhe: KeysStore['fhe'] = { ...persisted.fhe };
50
- const allChainIds = new Set([...Object.keys(current.fhe), ...Object.keys(persisted.fhe)]);
51
- for (const chainId of allChainIds) {
52
- const persistedZones = persisted.fhe[chainId] || {};
53
- const currentZones = current.fhe[chainId] || {};
54
- mergedFhe[chainId] = { ...persistedZones, ...currentZones };
55
- }
56
-
57
- // Deep merge for crs
58
- const mergedCrs: KeysStore['crs'] = { ...persisted.crs, ...current.crs };
59
-
60
- return {
61
- fhe: mergedFhe,
62
- crs: mergedCrs,
63
- };
64
- },
65
- }
66
- )
67
- )
58
+ ? createStoreWithPersit(storage)
68
59
  : createStore<KeysStore>()(() => ({
69
60
  fhe: {},
70
61
  crs: {},
@@ -109,10 +100,9 @@ export function createKeysStore(storage: IStorage | null): KeysStorage {
109
100
  };
110
101
 
111
102
  const rehydrateKeysStore = async () => {
112
- if ('persist' in keysStore) {
113
- if ((keysStore.persist as any).hasHydrated()) return;
114
- await (keysStore.persist as any).rehydrate();
115
- }
103
+ if (!isStoreWithPersist(keysStore)) return;
104
+ if (keysStore.persist.hasHydrated()) return;
105
+ await keysStore.persist.rehydrate();
116
106
  };
117
107
 
118
108
  return {
@@ -125,3 +115,40 @@ export function createKeysStore(storage: IStorage | null): KeysStorage {
125
115
  rehydrateKeysStore,
126
116
  };
127
117
  }
118
+
119
+ function createStoreWithPersit(storage: IStorage) {
120
+ const result = createStore<KeysStore>()(
121
+ persist(() => DEFAULT_KEYS_STORE, {
122
+ // because earleir tests were written with on-init hydration skipped (due to the error suppression in zustand), returning this flag to fix test (i.e. KeyStore > Storage Utilities > should rehydrate keys store)
123
+ skipHydration: true,
124
+ // if onRehydrateStorage is not passed here, the errors thrown by storage layer are swallowed by zustand here: https://github.com/pmndrs/zustand/blob/39a391b6c1ff9aa89b81694d9bdb21da37dd4ac6/src/middleware/persist.ts#L321
125
+ onRehydrateStorage: () => (_state?, _error?) => {
126
+ if (_error) throw new Error(`onRehydrateStorage: Error rehydrating keys store: ${_error}`);
127
+ },
128
+ name: 'cofhesdk-keys',
129
+ storage: createJSONStorage(() => storage),
130
+ merge: (persistedState, currentState) => {
131
+ const persisted = isValidPersistedState(persistedState) ? persistedState : DEFAULT_KEYS_STORE;
132
+ const current = currentState as KeysStore;
133
+
134
+ // Deep merge for fhe
135
+ const mergedFhe: KeysStore['fhe'] = { ...persisted.fhe };
136
+ const allChainIds = new Set([...Object.keys(current.fhe), ...Object.keys(persisted.fhe)]);
137
+ for (const chainId of allChainIds) {
138
+ const persistedZones = persisted.fhe[chainId] || {};
139
+ const currentZones = current.fhe[chainId] || {};
140
+ mergedFhe[chainId] = { ...persistedZones, ...currentZones };
141
+ }
142
+
143
+ // Deep merge for crs
144
+ const mergedCrs: KeysStore['crs'] = { ...persisted.crs, ...current.crs };
145
+
146
+ return {
147
+ fhe: mergedFhe,
148
+ crs: mergedCrs,
149
+ };
150
+ },
151
+ })
152
+ );
153
+ return result;
154
+ }
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * @vitest-environment happy-dom
3
3
  */
4
- /* eslint-disable no-unused-vars */
5
4
  import { permitStore } from '@/permits';
6
5
 
7
6
  import { describe, it, expect, beforeEach, afterEach } from 'vitest';
@@ -239,4 +238,257 @@ describe('Core Permits Tests', () => {
239
238
  expect(activePermit?.name).toBe('Permit 2');
240
239
  });
241
240
  });
241
+
242
+ describe('getOrCreateSelfPermit', () => {
243
+ it('should create a new self permit when none exists', async () => {
244
+ const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
245
+ issuer: bobAddress,
246
+ name: 'New Self Permit',
247
+ });
248
+
249
+ expect(permit).toBeDefined();
250
+ expect(permit.name).toBe('New Self Permit');
251
+ expect(permit.type).toBe('self');
252
+ expect(permit.issuer).toBe(bobAddress);
253
+ expect(permit.issuerSignature).toBeDefined();
254
+
255
+ // Verify it was stored and set as active
256
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
257
+ expect(activePermit?.name).toBe('New Self Permit');
258
+ });
259
+
260
+ it('should return existing self permit when one exists', async () => {
261
+ // Create an initial self permit
262
+ const firstPermit = await permits.createSelf(
263
+ { name: 'First Self Permit', issuer: bobAddress },
264
+ publicClient,
265
+ bobWalletClient
266
+ );
267
+
268
+ // Call getOrCreateSelfPermit - should return existing
269
+ const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
270
+ issuer: bobAddress,
271
+ name: 'Should Not Create This',
272
+ });
273
+
274
+ expect(permit.name).toBe('First Self Permit');
275
+ expect(permits.getHash(permit)).toBe(permits.getHash(firstPermit));
276
+
277
+ // Verify no new permit was created
278
+ const allPermits = await permits.getPermits(chainId, bobAddress);
279
+ expect(Object.keys(allPermits).length).toBe(1);
280
+ });
281
+
282
+ it('should create new self permit when active permit is sharing type', async () => {
283
+ // Create a sharing permit first
284
+ await permits.createSharing(
285
+ {
286
+ name: 'Sharing Permit',
287
+ issuer: bobAddress,
288
+ recipient: aliceAddress,
289
+ },
290
+ publicClient,
291
+ bobWalletClient
292
+ );
293
+
294
+ // Call getOrCreateSelfPermit - should create new since active is sharing type
295
+ const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
296
+ issuer: bobAddress,
297
+ name: 'New Self Permit',
298
+ });
299
+
300
+ expect(permit.name).toBe('New Self Permit');
301
+ expect(permit.type).toBe('self');
302
+
303
+ // Verify two permits exist now
304
+ const allPermits = await permits.getPermits(chainId, bobAddress);
305
+ expect(Object.keys(allPermits).length).toBe(2);
306
+ });
307
+
308
+ it('should use default options when none provided', async () => {
309
+ const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress);
310
+
311
+ expect(permit).toBeDefined();
312
+ expect(permit.type).toBe('self');
313
+ expect(permit.issuer).toBe(bobAddress);
314
+ expect(permit.name).toBe('Autogenerated Self Permit');
315
+ });
316
+
317
+ it('should use default chainId and account when not provided', async () => {
318
+ const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, undefined, undefined, {
319
+ issuer: bobAddress,
320
+ name: 'Test Permit',
321
+ });
322
+
323
+ expect(permit).toBeDefined();
324
+ expect(permit.issuer).toBe(bobAddress);
325
+
326
+ // Verify it was stored with the chain's actual chainId
327
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
328
+ expect(activePermit?.name).toBe('Test Permit');
329
+ });
330
+ });
331
+
332
+ describe('getOrCreateSharingPermit', () => {
333
+ it('should create a new sharing permit when none exists', async () => {
334
+ const permit = await permits.getOrCreateSharingPermit(
335
+ publicClient,
336
+ bobWalletClient,
337
+ {
338
+ issuer: bobAddress,
339
+ recipient: aliceAddress,
340
+ name: 'New Sharing Permit',
341
+ },
342
+ chainId,
343
+ bobAddress
344
+ );
345
+
346
+ expect(permit).toBeDefined();
347
+ expect(permit.name).toBe('New Sharing Permit');
348
+ expect(permit.type).toBe('sharing');
349
+ expect(permit.issuer).toBe(bobAddress);
350
+ expect(permit.recipient).toBe(aliceAddress);
351
+ expect(permit.issuerSignature).toBeDefined();
352
+
353
+ // Verify it was stored and set as active
354
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
355
+ expect(activePermit?.name).toBe('New Sharing Permit');
356
+ });
357
+
358
+ it('should return existing sharing permit when one exists', async () => {
359
+ // Create an initial sharing permit
360
+ const firstPermit = await permits.createSharing(
361
+ {
362
+ name: 'First Sharing Permit',
363
+ issuer: bobAddress,
364
+ recipient: aliceAddress,
365
+ },
366
+ publicClient,
367
+ bobWalletClient
368
+ );
369
+
370
+ // Call getOrCreateSharingPermit - should return existing
371
+ const permit = await permits.getOrCreateSharingPermit(
372
+ publicClient,
373
+ bobWalletClient,
374
+ {
375
+ issuer: bobAddress,
376
+ recipient: aliceAddress,
377
+ name: 'Should Not Create This',
378
+ },
379
+ chainId,
380
+ bobAddress
381
+ );
382
+
383
+ expect(permit.name).toBe('First Sharing Permit');
384
+ expect(permits.getHash(permit)).toBe(permits.getHash(firstPermit));
385
+
386
+ // Verify no new permit was created
387
+ const allPermits = await permits.getPermits(chainId, bobAddress);
388
+ expect(Object.keys(allPermits).length).toBe(1);
389
+ });
390
+
391
+ it('should create new sharing permit when active permit is self type', async () => {
392
+ // Create a self permit first
393
+ await permits.createSelf({ name: 'Self Permit', issuer: bobAddress }, publicClient, bobWalletClient);
394
+
395
+ // Call getOrCreateSharingPermit - should create new since active is self type
396
+ const permit = await permits.getOrCreateSharingPermit(
397
+ publicClient,
398
+ bobWalletClient,
399
+ {
400
+ issuer: bobAddress,
401
+ recipient: aliceAddress,
402
+ name: 'New Sharing Permit',
403
+ },
404
+ chainId,
405
+ bobAddress
406
+ );
407
+
408
+ expect(permit.name).toBe('New Sharing Permit');
409
+ expect(permit.type).toBe('sharing');
410
+
411
+ // Verify two permits exist now
412
+ const allPermits = await permits.getPermits(chainId, bobAddress);
413
+ expect(Object.keys(allPermits).length).toBe(2);
414
+ });
415
+
416
+ it('should use default chainId and account when not provided', async () => {
417
+ const permit = await permits.getOrCreateSharingPermit(
418
+ publicClient,
419
+ bobWalletClient,
420
+ {
421
+ issuer: bobAddress,
422
+ recipient: aliceAddress,
423
+ name: 'Test Sharing Permit',
424
+ },
425
+ undefined,
426
+ undefined
427
+ );
428
+
429
+ expect(permit).toBeDefined();
430
+ expect(permit.issuer).toBe(bobAddress);
431
+ expect(permit.recipient).toBe(aliceAddress);
432
+
433
+ // Verify it was stored with the chain's actual chainId
434
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
435
+ expect(activePermit?.name).toBe('Test Sharing Permit');
436
+ });
437
+ });
438
+
439
+ describe('getOrCreate - Multiple Types Scenarios', () => {
440
+ it('should handle switching between self and sharing permits', async () => {
441
+ // Create self permit
442
+ const selfPermit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
443
+ issuer: bobAddress,
444
+ name: 'Self Permit',
445
+ });
446
+ expect(selfPermit.type).toBe('self');
447
+
448
+ // Create sharing permit (should create new one)
449
+ const sharingPermit = await permits.getOrCreateSharingPermit(
450
+ publicClient,
451
+ bobWalletClient,
452
+ {
453
+ issuer: bobAddress,
454
+ recipient: aliceAddress,
455
+ name: 'Sharing Permit',
456
+ },
457
+ chainId,
458
+ bobAddress
459
+ );
460
+ expect(sharingPermit.type).toBe('sharing');
461
+
462
+ // Both should exist
463
+ const allPermits = await permits.getPermits(chainId, bobAddress);
464
+ expect(Object.keys(allPermits).length).toBe(2);
465
+
466
+ // Active permit should be the sharing one
467
+ const activePermit = await permits.getActivePermit(chainId, bobAddress);
468
+ expect(activePermit?.type).toBe('sharing');
469
+ expect(activePermit?.name).toBe('Sharing Permit');
470
+ });
471
+
472
+ it('should correctly handle sequential getOrCreate calls', async () => {
473
+ // First call - creates new
474
+ const permit1 = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
475
+ issuer: bobAddress,
476
+ name: 'Permit 1',
477
+ });
478
+
479
+ // Second call - returns existing
480
+ const permit2 = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
481
+ issuer: bobAddress,
482
+ name: 'Permit 2',
483
+ });
484
+
485
+ // Should be the same permit
486
+ expect(permits.getHash(permit1)).toBe(permits.getHash(permit2));
487
+ expect(permit2.name).toBe('Permit 1'); // Original name
488
+
489
+ // Only one permit should exist
490
+ const allPermits = await permits.getPermits(chainId, bobAddress);
491
+ expect(Object.keys(allPermits).length).toBe(1);
492
+ });
493
+ });
242
494
  });
package/core/permits.ts CHANGED
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-unused-vars */
2
1
  import {
3
2
  type ImportSharedPermitOptions,
4
3
  PermitUtils,
@@ -7,6 +6,9 @@ import {
7
6
  type Permit,
8
7
  permitStore,
9
8
  type SerializedPermit,
9
+ type SelfPermit,
10
+ type RecipientPermit,
11
+ type SharingPermit,
10
12
  } from '@/permits';
11
13
 
12
14
  import { type PublicClient, type WalletClient } from 'viem';
@@ -23,12 +25,12 @@ const storeActivePermit = async (permit: Permit, publicClient: any, walletClient
23
25
  };
24
26
 
25
27
  // Generic function to handle permit creation with error handling
26
- const createPermitWithSign = async <T>(
28
+ const createPermitWithSign = async <T, TPermit extends Permit>(
27
29
  options: T,
28
30
  publicClient: PublicClient,
29
31
  walletClient: WalletClient,
30
- permitMethod: (options: T, publicClient: PublicClient, walletClient: WalletClient) => Promise<Permit>
31
- ): Promise<Permit> => {
32
+ permitMethod: (options: T, publicClient: PublicClient, walletClient: WalletClient) => Promise<TPermit>
33
+ ): Promise<TPermit> => {
32
34
  const permit = await permitMethod(options, publicClient, walletClient);
33
35
  await storeActivePermit(permit, publicClient, walletClient);
34
36
  return permit;
@@ -46,7 +48,7 @@ const createSelf = async (
46
48
  options: CreateSelfPermitOptions,
47
49
  publicClient: PublicClient,
48
50
  walletClient: WalletClient
49
- ): Promise<Permit> => {
51
+ ): Promise<SelfPermit> => {
50
52
  return createPermitWithSign(options, publicClient, walletClient, PermitUtils.createSelfAndSign);
51
53
  };
52
54
 
@@ -54,15 +56,15 @@ const createSharing = async (
54
56
  options: CreateSharingPermitOptions,
55
57
  publicClient: PublicClient,
56
58
  walletClient: WalletClient
57
- ): Promise<Permit> => {
59
+ ): Promise<SharingPermit> => {
58
60
  return createPermitWithSign(options, publicClient, walletClient, PermitUtils.createSharingAndSign);
59
61
  };
60
62
 
61
63
  const importShared = async (
62
- options: ImportSharedPermitOptions | any | string,
64
+ options: ImportSharedPermitOptions | string,
63
65
  publicClient: PublicClient,
64
66
  walletClient: WalletClient
65
- ): Promise<Permit> => {
67
+ ): Promise<RecipientPermit> => {
66
68
  return createPermitWithSign(options, publicClient, walletClient, PermitUtils.importSharedAndSign);
67
69
  };
68
70
 
@@ -94,24 +96,83 @@ const getActivePermit = async (chainId: number, account: string): Promise<Permit
94
96
  return permitStore.getActivePermit(chainId, account);
95
97
  };
96
98
 
97
- const getActivePermitHash = async (chainId: number, account: string): Promise<string | undefined> => {
99
+ const getActivePermitHash = (chainId: number, account: string): string | undefined => {
98
100
  return permitStore.getActivePermitHash(chainId, account);
99
101
  };
100
102
 
101
- const selectActivePermit = async (chainId: number, account: string, hash: string): Promise<void> => {
102
- await permitStore.setActivePermitHash(chainId, account, hash);
103
+ const selectActivePermit = (chainId: number, account: string, hash: string): void => {
104
+ permitStore.setActivePermitHash(chainId, account, hash);
103
105
  };
104
106
 
105
- // REMOVE
107
+ // GET OR CREATE
108
+
109
+ /**
110
+ * Get the active self permit or create a new one if it doesn't exist
111
+ * @param publicClient - The public client
112
+ * @param walletClient - The wallet client
113
+ * @param chainId - Optional chain ID (will use publicClient if not provided)
114
+ * @param account - Optional account (will use walletClient if not provided)
115
+ * @param options - The options for creating a self permit
116
+ * @returns The existing or newly created permit
117
+ */
118
+ const getOrCreateSelfPermit = async (
119
+ publicClient: PublicClient,
120
+ walletClient: WalletClient,
121
+ chainId?: number,
122
+ account?: string,
123
+ options?: CreateSelfPermitOptions
124
+ ): Promise<Permit> => {
125
+ const _chainId = chainId ?? (await publicClient.getChainId());
126
+ const _account = account ?? walletClient.account!.address;
127
+
128
+ // Try to get active permit first
129
+ const activePermit = await getActivePermit(_chainId, _account);
130
+
131
+ if (activePermit && activePermit.type === 'self') {
132
+ return activePermit;
133
+ }
106
134
 
107
- const removePermit = async (chainId: number, account: string, hash: string): Promise<void> => {
108
- await permitStore.removePermit(chainId, account, hash);
135
+ // No active permit or wrong type, create new one
136
+ return createSelf(options ?? { issuer: _account, name: 'Autogenerated Self Permit' }, publicClient, walletClient);
109
137
  };
110
138
 
111
- const removeActivePermit = async (chainId: number, account: string): Promise<void> => {
112
- await permitStore.removeActivePermitHash(chainId, account);
139
+ /**
140
+ * Get the active sharing permit or create a new one if it doesn't exist
141
+ * @param publicClient - The public client
142
+ * @param walletClient - The wallet client
143
+ * @param options - The options for creating a sharing permit (required)
144
+ * @param chainId - Optional chain ID (will use publicClient if not provided)
145
+ * @param account - Optional account (will use walletClient if not provided)
146
+ * @returns The existing or newly created permit
147
+ */
148
+ const getOrCreateSharingPermit = async (
149
+ publicClient: PublicClient,
150
+ walletClient: WalletClient,
151
+ options: CreateSharingPermitOptions,
152
+ chainId?: number,
153
+ account?: string
154
+ ): Promise<Permit> => {
155
+ const _chainId = chainId ?? (await publicClient.getChainId());
156
+ const _account = account ?? walletClient.account!.address;
157
+
158
+ // Try to get active permit first
159
+ const activePermit = await getActivePermit(_chainId, _account);
160
+
161
+ if (activePermit && activePermit.type === 'sharing') {
162
+ return activePermit;
163
+ }
164
+
165
+ return createSharing(options, publicClient, walletClient);
113
166
  };
114
167
 
168
+ // REMOVE
169
+
170
+ const removePermit = async (chainId: number, account: string, hash: string, force?: boolean): Promise<void> =>
171
+ permitStore.removePermit(chainId, account, hash, force);
172
+
173
+ const removeActivePermit = async (chainId: number, account: string): Promise<void> =>
174
+ permitStore.removeActivePermitHash(chainId, account);
175
+
115
176
  // EXPORT
116
177
 
117
178
  export const permits = {
@@ -122,6 +183,9 @@ export const permits = {
122
183
  createSharing,
123
184
  importShared,
124
185
 
186
+ getOrCreateSelfPermit,
187
+ getOrCreateSharingPermit,
188
+
125
189
  getHash,
126
190
  serialize,
127
191
  deserialize,