@cofhe/sdk 0.4.0 → 0.5.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 (95) hide show
  1. package/CHANGELOG.md +32 -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 +588 -251
  56. package/dist/web.d.cts +8 -4
  57. package/dist/web.d.ts +8 -4
  58. package/dist/web.js +34 -11
  59. package/dist/zkProve.worker.cjs +6 -3
  60. package/dist/zkProve.worker.js +5 -3
  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 +20 -6
  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 +4 -3
  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,12 +1,11 @@
1
- import { isAddress, getAddress, zeroAddress, isHex, keccak256, toHex, parseAbi, BaseError, ContractFunctionRevertedError, decodeErrorResult } from 'viem';
2
- import * as nacl from 'tweetnacl';
1
+ import { TASK_MANAGER_ADDRESS } from './chunk-4FP4V35O.js';
2
+ import { isAddress, getAddress, zeroAddress, isHex, keccak256, toHex, BaseError, ContractFunctionRevertedError, decodeErrorResult, parseAbi } from 'viem';
3
+ import nacl from 'tweetnacl';
3
4
  import { z } from 'zod';
4
5
  import { createStore } from 'zustand/vanilla';
5
6
  import { persist } from 'zustand/middleware';
6
7
  import { produce } from 'immer';
7
8
 
8
- // permits/permit.ts
9
-
10
9
  // permits/utils.ts
11
10
  var fromHexString = (hexString) => {
12
11
  const cleanString = hexString.length % 2 === 1 ? `0${hexString}` : hexString;
@@ -292,9 +291,9 @@ var ValidationUtils = {
292
291
  return false;
293
292
  },
294
293
  /**
295
- * Overall validity checker of a permit
294
+ * Checks that a permit is signed and not expired.
296
295
  */
297
- isValid: (permit) => {
296
+ isSignedAndNotExpired: (permit) => {
298
297
  if (ValidationUtils.isExpired(permit)) {
299
298
  return { valid: false, error: "expired" };
300
299
  }
@@ -302,6 +301,34 @@ var ValidationUtils = {
302
301
  return { valid: false, error: "not-signed" };
303
302
  }
304
303
  return { valid: true, error: null };
304
+ },
305
+ /**
306
+ * Asserts that a permit is signed and not expired.
307
+ *
308
+ * Throws `Error` with message:
309
+ * - `Permit is expired`
310
+ * - `Permit is not signed`
311
+ */
312
+ assertSignedAndNotExpired: (permit) => {
313
+ const result = ValidationUtils.isSignedAndNotExpired(permit);
314
+ if (result.valid)
315
+ return;
316
+ if (result.error === "expired") {
317
+ throw new Error("Permit is expired");
318
+ }
319
+ if (result.error === "not-signed") {
320
+ throw new Error("Permit is not signed");
321
+ }
322
+ throw new Error("Permit is invalid");
323
+ },
324
+ isValid: (permit) => {
325
+ const schema = permit.type === "self" ? SelfPermitValidator : permit.type === "sharing" ? SharingPermitValidator : permit.type === "recipient" ? ImportPermitValidator : null;
326
+ if (schema == null)
327
+ return { valid: false, error: "invalid-schema" };
328
+ const schemaResult = schema.safeParse(permit);
329
+ if (!schemaResult.success)
330
+ return { valid: false, error: "invalid-schema" };
331
+ return ValidationUtils.isSignedAndNotExpired(permit);
305
332
  }
306
333
  };
307
334
 
@@ -365,17 +392,6 @@ var SignatureUtils = {
365
392
  throw new Error(`Unknown permit type: ${permitType}`);
366
393
  }
367
394
  };
368
-
369
- // core/consts.ts
370
- var TASK_MANAGER_ADDRESS = "0xeA30c4B8b44078Bbf8a6ef5b9f1eC1626C7848D9";
371
- var MOCKS_ZK_VERIFIER_ADDRESS = "0x0000000000000000000000000000000000005001";
372
- var MOCKS_THRESHOLD_NETWORK_ADDRESS = "0x0000000000000000000000000000000000005002";
373
- var TEST_BED_ADDRESS = "0x0000000000000000000000000000000000005003";
374
- var MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY = "0x6C8D7F768A6BB4AAFE85E8A2F5A9680355239C7E14646ED62B044E39DE154512";
375
- var MOCKS_ZK_VERIFIER_SIGNER_ADDRESS = "0x6E12D8C87503D4287c294f2Fdef96ACd9DFf6bd2";
376
- var MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY = "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d";
377
-
378
- // permits/onchain-utils.ts
379
395
  var getAclAddress = async (publicClient) => {
380
396
  const ACL_IFACE = "function acl() view returns (address)";
381
397
  const aclAbi = parseAbi([ACL_IFACE]);
@@ -694,9 +710,9 @@ var PermitUtils = {
694
710
  };
695
711
  },
696
712
  /**
697
- * Validate a permit
713
+ * Validate a permit (schema-level validation)
698
714
  */
699
- validate: (permit) => {
715
+ validateSchema: (permit) => {
700
716
  if (permit.type === "self") {
701
717
  return validateSelfPermit(permit);
702
718
  } else if (permit.type === "sharing") {
@@ -707,12 +723,27 @@ var PermitUtils = {
707
723
  throw new Error("Invalid permit type");
708
724
  }
709
725
  },
726
+ /**
727
+ * Validate a permit (holistic validation).
728
+ *
729
+ * This validates:
730
+ * - Permit schema (shape + invariants)
731
+ * - Permit is signed
732
+ * - Permit is not expired
733
+ *
734
+ * For schema-only validation, use `validateSchema(permit)`.
735
+ */
736
+ validate: (permit) => {
737
+ const validated = PermitUtils.validateSchema(permit);
738
+ ValidationUtils.assertSignedAndNotExpired(validated);
739
+ return validated;
740
+ },
710
741
  /**
711
742
  * Get the permission object from a permit (for use in contracts)
712
743
  */
713
744
  getPermission: (permit, skipValidation = false) => {
714
745
  if (!skipValidation) {
715
- PermitUtils.validate(permit);
746
+ PermitUtils.validateSchema(permit);
716
747
  }
717
748
  return {
718
749
  issuer: permit.issuer,
@@ -778,8 +809,17 @@ var PermitUtils = {
778
809
  return ValidationUtils.isSigned(permit);
779
810
  },
780
811
  /**
781
- * Check if permit is valid
812
+ * Check if permit is signed and not expired
782
813
  */
814
+ isSignedAndNotExpired: (permit) => {
815
+ return ValidationUtils.isSignedAndNotExpired(permit);
816
+ },
817
+ /**
818
+ * Assert that permit is signed and not expired
819
+ */
820
+ assertSignedAndNotExpired: (permit) => {
821
+ return ValidationUtils.assertSignedAndNotExpired(permit);
822
+ },
783
823
  isValid: (permit) => {
784
824
  return ValidationUtils.isValid(permit);
785
825
  },
@@ -935,4 +975,4 @@ var permitStore = {
935
975
  resetStore
936
976
  };
937
977
 
938
- export { GenerateSealingKey, ImportPermitOptionsValidator, ImportPermitValidator, MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY, MOCKS_THRESHOLD_NETWORK_ADDRESS, MOCKS_ZK_VERIFIER_ADDRESS, MOCKS_ZK_VERIFIER_SIGNER_ADDRESS, MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY, PERMIT_STORE_DEFAULTS, PermitUtils, SealingKey, SelfPermitOptionsValidator, SelfPermitValidator, SharingPermitOptionsValidator, SharingPermitValidator, SignatureTypes, SignatureUtils, TASK_MANAGER_ADDRESS, TEST_BED_ADDRESS, ValidationUtils, _permitStore, addressNotZeroSchema, addressSchema, bytesNotEmptySchema, bytesSchema, clearStaleStore, getActivePermit, getActivePermitHash, getPermit, getPermits, getSignatureTypesAndMessage, permitStore, removeActivePermitHash, removePermit, resetStore, setActivePermitHash, setPermit, validateImportPermit, validateImportPermitOptions, validateSelfPermit, validateSelfPermitOptions, validateSharingPermit, validateSharingPermitOptions };
978
+ export { GenerateSealingKey, ImportPermitOptionsValidator, ImportPermitValidator, PERMIT_STORE_DEFAULTS, PermitUtils, SealingKey, SelfPermitOptionsValidator, SelfPermitValidator, SharingPermitOptionsValidator, SharingPermitValidator, SignatureTypes, SignatureUtils, ValidationUtils, _permitStore, addressNotZeroSchema, addressSchema, bytesNotEmptySchema, bytesSchema, clearStaleStore, getActivePermit, getActivePermitHash, getPermit, getPermits, getSignatureTypesAndMessage, permitStore, removeActivePermitHash, removePermit, resetStore, setActivePermitHash, setPermit, validateImportPermit, validateImportPermitOptions, validateSelfPermit, validateSelfPermitOptions, validateSharingPermit, validateSharingPermitOptions };