@cofhe/sdk 0.2.0 → 0.2.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 (57) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/chains/defineChain.ts +2 -2
  3. package/chains/types.ts +3 -3
  4. package/core/client.test.ts +111 -0
  5. package/core/client.ts +22 -2
  6. package/core/clientTypes.ts +7 -1
  7. package/core/config.test.ts +8 -0
  8. package/core/config.ts +10 -4
  9. package/core/consts.ts +18 -0
  10. package/core/decrypt/cofheMocksSealOutput.ts +2 -4
  11. package/core/encrypt/cofheMocksZkVerifySign.ts +4 -11
  12. package/core/index.ts +9 -1
  13. package/core/permits.test.ts +5 -6
  14. package/core/permits.ts +5 -4
  15. package/dist/chains.cjs +4 -7
  16. package/dist/chains.d.cts +12 -12
  17. package/dist/chains.d.ts +12 -12
  18. package/dist/chains.js +1 -1
  19. package/dist/{chunk-WGCRJCBR.js → chunk-I5WFEYXX.js} +33 -19
  20. package/dist/{chunk-UGBVZNRT.js → chunk-R3B5TMVX.js} +308 -189
  21. package/dist/{chunk-WEAZ25JO.js → chunk-TBLR7NNE.js} +4 -7
  22. package/dist/{clientTypes-Es7fyi65.d.ts → clientTypes-RqkgkV2i.d.ts} +34 -93
  23. package/dist/{clientTypes-5_1nwtUe.d.cts → clientTypes-e4filDzK.d.cts} +34 -93
  24. package/dist/core.cjs +343 -208
  25. package/dist/core.d.cts +17 -6
  26. package/dist/core.d.ts +17 -6
  27. package/dist/core.js +3 -3
  28. package/dist/node.cjs +337 -208
  29. package/dist/node.d.cts +3 -3
  30. package/dist/node.d.ts +3 -3
  31. package/dist/node.js +3 -3
  32. package/dist/{permit-fUSe6KKq.d.cts → permit-MZ502UBl.d.cts} +30 -33
  33. package/dist/{permit-fUSe6KKq.d.ts → permit-MZ502UBl.d.ts} +30 -33
  34. package/dist/permits.cjs +305 -187
  35. package/dist/permits.d.cts +111 -812
  36. package/dist/permits.d.ts +111 -812
  37. package/dist/permits.js +1 -1
  38. package/dist/types-YiAC4gig.d.cts +33 -0
  39. package/dist/types-YiAC4gig.d.ts +33 -0
  40. package/dist/web.cjs +337 -208
  41. package/dist/web.d.cts +3 -3
  42. package/dist/web.d.ts +3 -3
  43. package/dist/web.js +3 -3
  44. package/package.json +3 -3
  45. package/permits/localstorage.test.ts +9 -13
  46. package/permits/onchain-utils.ts +221 -0
  47. package/permits/permit.test.ts +51 -5
  48. package/permits/permit.ts +28 -74
  49. package/permits/store.test.ts +10 -50
  50. package/permits/store.ts +4 -14
  51. package/permits/test-utils.ts +10 -2
  52. package/permits/types.ts +22 -9
  53. package/permits/utils.ts +0 -4
  54. package/permits/validation.test.ts +29 -32
  55. package/permits/validation.ts +112 -194
  56. package/dist/types-KImPrEIe.d.cts +0 -48
  57. package/dist/types-KImPrEIe.d.ts +0 -48
package/dist/core.cjs CHANGED
@@ -661,9 +661,16 @@ var MockZkVerifierAbi = [
661
661
  },
662
662
  { type: "error", name: "InvalidInputs", inputs: [] }
663
663
  ];
664
- var MocksZkVerifierAddress = "0x0000000000000000000000000000000000000100";
664
+
665
+ // core/consts.ts
666
+ var TASK_MANAGER_ADDRESS = "0xeA30c4B8b44078Bbf8a6ef5b9f1eC1626C7848D9";
667
+ var MOCKS_ZK_VERIFIER_ADDRESS = "0x0000000000000000000000000000000000005001";
668
+ var MOCKS_QUERY_DECRYPTER_ADDRESS = "0x0000000000000000000000000000000000005002";
669
+ var TEST_BED_ADDRESS = "0x0000000000000000000000000000000000005003";
665
670
  var MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY = "0x6C8D7F768A6BB4AAFE85E8A2F5A9680355239C7E14646ED62B044E39DE154512";
666
671
  var MOCKS_ZK_VERIFIER_SIGNER_ADDRESS = "0x6E12D8C87503D4287c294f2Fdef96ACd9DFf6bd2";
672
+
673
+ // core/encrypt/cofheMocksZkVerifySign.ts
667
674
  function createMockZkVerifierSigner() {
668
675
  return viem.createWalletClient({
669
676
  chain: chains.hardhat,
@@ -729,7 +736,7 @@ async function calcCtHashes(items, account, securityZone, publicClient) {
729
736
  let ctHashes;
730
737
  try {
731
738
  ctHashes = await publicClient.readContract({
732
- address: MocksZkVerifierAddress,
739
+ address: MOCKS_ZK_VERIFIER_ADDRESS,
733
740
  abi: MockZkVerifierAbi,
734
741
  functionName: "zkVerifyCalcCtHashesPacked",
735
742
  args: calcCtHashesArgs
@@ -740,7 +747,7 @@ async function calcCtHashes(items, account, securityZone, publicClient) {
740
747
  message: `mockZkVerifySign calcCtHashes failed while calling zkVerifyCalcCtHashesPacked`,
741
748
  cause: err instanceof Error ? err : void 0,
742
749
  context: {
743
- address: MocksZkVerifierAddress,
750
+ address: MOCKS_ZK_VERIFIER_ADDRESS,
744
751
  items,
745
752
  account,
746
753
  securityZone,
@@ -773,7 +780,7 @@ async function insertCtHashes(items, walletClient) {
773
780
  try {
774
781
  const account = walletClient.account;
775
782
  await walletClient.writeContract({
776
- address: MocksZkVerifierAddress,
783
+ address: MOCKS_ZK_VERIFIER_ADDRESS,
777
784
  abi: MockZkVerifierAbi,
778
785
  functionName: "insertPackedCtHashes",
779
786
  args: insertPackedCtHashesArgs,
@@ -861,21 +868,18 @@ var CofheChainSchema = zod.z.object({
861
868
  /** Network identifier */
862
869
  network: zod.z.string().min(1),
863
870
  /** coFhe service URL */
864
- coFheUrl: zod.z.string().url(),
871
+ coFheUrl: zod.z.url(),
865
872
  /** Verifier service URL */
866
- verifierUrl: zod.z.string().url(),
873
+ verifierUrl: zod.z.url(),
867
874
  /** Threshold network service URL */
868
- thresholdNetworkUrl: zod.z.string().url(),
875
+ thresholdNetworkUrl: zod.z.url(),
869
876
  /** Environment type */
870
877
  environment: EnvironmentSchema
871
878
  });
872
-
873
- // chains/defineChain.ts
874
879
  function defineChain(chainConfig) {
875
880
  const result = CofheChainSchema.safeParse(chainConfig);
876
881
  if (!result.success) {
877
- const errorMessages = result.error.errors.map((err) => `${err.path.join(".")}: ${err.message}`);
878
- throw new Error(`Invalid chain configuration: ${errorMessages.join(", ")}`);
882
+ throw new Error(`Invalid chain configuration: ${zod.z.prettifyError(result.error)}`, { cause: result.error });
879
883
  }
880
884
  return result.data;
881
885
  }
@@ -902,9 +906,15 @@ var CofhesdkConfigSchema = zod.z.object({
902
906
  defaultPermitExpiration: zod.z.number().optional().default(60 * 60 * 24 * 30),
903
907
  /** Storage method for fhe keys (defaults to indexedDB on web, filesystem on node) */
904
908
  fheKeyStorage: zod.z.object({
905
- getItem: zod.z.function().args(zod.z.string()).returns(zod.z.promise(zod.z.any())),
906
- setItem: zod.z.function().args(zod.z.string(), zod.z.any()).returns(zod.z.promise(zod.z.void())),
907
- removeItem: zod.z.function().args(zod.z.string()).returns(zod.z.promise(zod.z.void()))
909
+ getItem: zod.z.custom((val) => typeof val === "function", {
910
+ message: "getItem must be a function"
911
+ }),
912
+ setItem: zod.z.custom((val) => typeof val === "function", {
913
+ message: "setItem must be a function"
914
+ }),
915
+ removeItem: zod.z.custom((val) => typeof val === "function", {
916
+ message: "removeItem must be a function"
917
+ })
908
918
  }).or(zod.z.null()).default(null),
909
919
  /** Whether to use Web Workers for ZK proof generation (web platform only) */
910
920
  useWorkers: zod.z.boolean().optional().default(true),
@@ -920,7 +930,7 @@ var CofhesdkConfigSchema = zod.z.object({
920
930
  function createCofhesdkConfigBase(config) {
921
931
  const result = CofhesdkConfigSchema.safeParse(config);
922
932
  if (!result.success) {
923
- throw new Error(`Invalid cofhesdk configuration: ${result.error.message}`);
933
+ throw new Error(`Invalid cofhesdk configuration: ${zod.z.prettifyError(result.error)}`, { cause: result.error });
924
934
  }
925
935
  return result.data;
926
936
  }
@@ -1733,9 +1743,6 @@ function isBigIntOrNumber(value) {
1733
1743
  }
1734
1744
  }
1735
1745
  }
1736
- function is0xPrefixed(value) {
1737
- return value.startsWith("0x");
1738
- }
1739
1746
 
1740
1747
  // permits/sealing.ts
1741
1748
  var PRIVATE_KEY_LENGTH = 64;
@@ -1823,158 +1830,137 @@ var SerializedSealingPair = zod.z.object({
1823
1830
  privateKey: zod.z.string(),
1824
1831
  publicKey: zod.z.string()
1825
1832
  });
1833
+ var addressSchema = zod.z.string().refine((val) => viem.isAddress(val), {
1834
+ error: "Invalid address"
1835
+ }).transform((val) => viem.getAddress(val));
1836
+ var addressNotZeroSchema = addressSchema.refine((val) => val !== viem.zeroAddress, {
1837
+ error: "Must not be zeroAddress"
1838
+ });
1839
+ var bytesSchema = zod.z.custom(
1840
+ (val) => {
1841
+ return typeof val === "string" && viem.isHex(val);
1842
+ },
1843
+ {
1844
+ message: "Invalid hex value"
1845
+ }
1846
+ );
1847
+ var bytesNotEmptySchema = bytesSchema.refine((val) => val !== "0x", {
1848
+ error: "Must not be empty"
1849
+ });
1826
1850
  var DEFAULT_EXPIRATION_FN = () => Math.round(Date.now() / 1e3) + 7 * 24 * 60 * 60;
1827
1851
  var zPermitWithDefaults = zod.z.object({
1828
1852
  name: zod.z.string().optional().default("Unnamed Permit"),
1829
1853
  type: zod.z.enum(["self", "sharing", "recipient"]),
1830
- issuer: zod.z.string().refine((val) => viem.isAddress(val), {
1831
- message: "Permit issuer :: invalid address"
1832
- }).refine((val) => val !== viem.zeroAddress, {
1833
- message: "Permit issuer :: must not be zeroAddress"
1834
- }),
1835
- expiration: zod.z.number().optional().default(DEFAULT_EXPIRATION_FN),
1836
- recipient: zod.z.string().optional().default(viem.zeroAddress).refine((val) => viem.isAddress(val), {
1837
- message: "Permit recipient :: invalid address"
1838
- }),
1839
- validatorId: zod.z.number().optional().default(0),
1840
- validatorContract: zod.z.string().optional().default(viem.zeroAddress).refine((val) => viem.isAddress(val), {
1841
- message: "Permit validatorContract :: invalid address"
1842
- }),
1843
- issuerSignature: zod.z.string().optional().default("0x"),
1844
- recipientSignature: zod.z.string().optional().default("0x")
1854
+ issuer: addressNotZeroSchema,
1855
+ expiration: zod.z.int().optional().default(DEFAULT_EXPIRATION_FN),
1856
+ recipient: addressSchema.optional().default(viem.zeroAddress),
1857
+ validatorId: zod.z.int().optional().default(0),
1858
+ validatorContract: addressSchema.optional().default(viem.zeroAddress),
1859
+ issuerSignature: bytesSchema.optional().default("0x"),
1860
+ recipientSignature: bytesSchema.optional().default("0x")
1845
1861
  });
1846
1862
  var zPermitWithSealingPair = zPermitWithDefaults.extend({
1847
1863
  sealingPair: SerializedSealingPair.optional()
1848
1864
  });
1849
- var ValidatorContractRefinement = [
1865
+ var ExternalValidatorRefinement = [
1850
1866
  (data) => data.validatorId !== 0 && data.validatorContract !== viem.zeroAddress || data.validatorId === 0 && data.validatorContract === viem.zeroAddress,
1851
1867
  {
1852
- message: "Permit external validator :: validatorId and validatorContract must either both be set or both be unset.",
1868
+ error: "Permit external validator :: validatorId and validatorContract must either both be set or both be unset.",
1853
1869
  path: ["validatorId", "validatorContract"]
1854
1870
  }
1855
1871
  ];
1872
+ var RecipientRefinement = [
1873
+ (data) => data.issuer !== data.recipient,
1874
+ {
1875
+ error: "Sharing permit :: issuer and recipient must not be the same",
1876
+ path: ["issuer", "recipient"]
1877
+ }
1878
+ ];
1856
1879
  var SelfPermitOptionsValidator = zod.z.object({
1857
1880
  type: zod.z.literal("self").optional().default("self"),
1858
- issuer: zod.z.string().refine((val) => viem.isAddress(val), {
1859
- message: "Self permit issuer :: invalid address"
1860
- }).refine((val) => is0xPrefixed(val), {
1861
- message: "Self permit issuer :: must be 0x prefixed"
1862
- }).refine((val) => val !== viem.zeroAddress, {
1863
- message: "Self permit issuer :: must not be zeroAddress"
1864
- }),
1881
+ issuer: addressNotZeroSchema,
1865
1882
  name: zod.z.string().optional().default("Unnamed Permit"),
1866
- expiration: zod.z.number().optional().default(DEFAULT_EXPIRATION_FN),
1867
- recipient: zod.z.string().optional().default(viem.zeroAddress).refine((val) => viem.isAddress(val), {
1868
- message: "Self permit recipient :: invalid address"
1869
- }).refine((val) => is0xPrefixed(val), {
1870
- message: "Self permit recipient :: must be 0x prefixed"
1871
- }).refine((val) => val === viem.zeroAddress, {
1872
- message: "Self permit recipient :: must be zeroAddress"
1873
- }),
1874
- validatorId: zod.z.number().optional().default(0),
1875
- validatorContract: zod.z.string().optional().default(viem.zeroAddress).refine((val) => viem.isAddress(val), {
1876
- message: "Self permit validatorContract :: invalid address"
1877
- }),
1878
- issuerSignature: zod.z.string().optional().default("0x").refine((val) => is0xPrefixed(val), {
1879
- message: "Self permit issuerSignature :: must be 0x prefixed"
1880
- }),
1881
- recipientSignature: zod.z.string().optional().default("0x").refine((val) => is0xPrefixed(val), {
1882
- message: "Self permit recipientSignature :: must be 0x prefixed"
1883
- })
1884
- }).refine(...ValidatorContractRefinement);
1883
+ expiration: zod.z.int().optional().default(DEFAULT_EXPIRATION_FN),
1884
+ recipient: addressSchema.optional().default(viem.zeroAddress),
1885
+ validatorId: zod.z.int().optional().default(0),
1886
+ validatorContract: addressSchema.optional().default(viem.zeroAddress),
1887
+ issuerSignature: bytesSchema.optional().default("0x"),
1888
+ recipientSignature: bytesSchema.optional().default("0x")
1889
+ }).refine(...ExternalValidatorRefinement);
1885
1890
  var SelfPermitValidator = zPermitWithSealingPair.refine((data) => data.type === "self", {
1886
- message: "Self permit :: type must be 'self'"
1891
+ error: "Type must be 'self'"
1887
1892
  }).refine((data) => data.recipient === viem.zeroAddress, {
1888
- message: "Self permit :: recipient must be zeroAddress"
1893
+ error: "Recipient must be zeroAddress"
1889
1894
  }).refine((data) => data.issuerSignature !== "0x", {
1890
- message: "Self permit :: issuerSignature must be populated"
1895
+ error: "IssuerSignature must be populated"
1891
1896
  }).refine((data) => data.recipientSignature === "0x", {
1892
- message: "Self permit :: recipientSignature must be empty"
1893
- }).refine(...ValidatorContractRefinement);
1897
+ error: "RecipientSignature must be empty"
1898
+ }).refine(...ExternalValidatorRefinement);
1894
1899
  var SharingPermitOptionsValidator = zod.z.object({
1895
1900
  type: zod.z.literal("sharing").optional().default("sharing"),
1896
- issuer: zod.z.string().refine((val) => viem.isAddress(val), {
1897
- message: "Sharing permit issuer :: invalid address"
1898
- }).refine((val) => is0xPrefixed(val), {
1899
- message: "Sharing permit issuer :: must be 0x prefixed"
1900
- }).refine((val) => val !== viem.zeroAddress, {
1901
- message: "Sharing permit issuer :: must not be zeroAddress"
1902
- }),
1903
- recipient: zod.z.string().refine((val) => viem.isAddress(val), {
1904
- message: "Sharing permit recipient :: invalid address"
1905
- }).refine((val) => is0xPrefixed(val), {
1906
- message: "Sharing permit recipient :: must be 0x prefixed"
1907
- }).refine((val) => val !== viem.zeroAddress, {
1908
- message: "Sharing permit recipient :: must not be zeroAddress"
1909
- }),
1901
+ issuer: addressNotZeroSchema,
1902
+ recipient: addressNotZeroSchema,
1910
1903
  name: zod.z.string().optional().default("Unnamed Permit"),
1911
- expiration: zod.z.number().optional().default(DEFAULT_EXPIRATION_FN),
1912
- validatorId: zod.z.number().optional().default(0),
1913
- validatorContract: zod.z.string().optional().default(viem.zeroAddress).refine((val) => viem.isAddress(val), {
1914
- message: "Sharing permit validatorContract :: invalid address"
1915
- }),
1916
- issuerSignature: zod.z.string().optional().default("0x").refine((val) => is0xPrefixed(val), {
1917
- message: "Sharing permit issuerSignature :: must be 0x prefixed"
1918
- }),
1919
- recipientSignature: zod.z.string().optional().default("0x").refine((val) => is0xPrefixed(val), {
1920
- message: "Sharing permit recipientSignature :: must be 0x prefixed"
1921
- })
1922
- }).refine(...ValidatorContractRefinement);
1904
+ expiration: zod.z.int().optional().default(DEFAULT_EXPIRATION_FN),
1905
+ validatorId: zod.z.int().optional().default(0),
1906
+ validatorContract: addressSchema.optional().default(viem.zeroAddress),
1907
+ issuerSignature: bytesSchema.optional().default("0x"),
1908
+ recipientSignature: bytesSchema.optional().default("0x")
1909
+ }).refine(...RecipientRefinement).refine(...ExternalValidatorRefinement);
1923
1910
  var SharingPermitValidator = zPermitWithSealingPair.refine((data) => data.type === "sharing", {
1924
- message: "Sharing permit :: type must be 'sharing'"
1911
+ error: "Type must be 'sharing'"
1925
1912
  }).refine((data) => data.recipient !== viem.zeroAddress, {
1926
- message: "Sharing permit :: recipient must not be zeroAddress"
1913
+ error: "Recipient must not be zeroAddress"
1927
1914
  }).refine((data) => data.issuerSignature !== "0x", {
1928
- message: "Sharing permit :: issuerSignature must be populated"
1915
+ error: "IssuerSignature must be populated"
1929
1916
  }).refine((data) => data.recipientSignature === "0x", {
1930
- message: "Sharing permit :: recipientSignature must be empty"
1931
- }).refine(...ValidatorContractRefinement);
1917
+ error: "RecipientSignature must be empty"
1918
+ }).refine(...ExternalValidatorRefinement);
1932
1919
  var ImportPermitOptionsValidator = zod.z.object({
1933
1920
  type: zod.z.literal("recipient").optional().default("recipient"),
1934
- issuer: zod.z.string().refine((val) => viem.isAddress(val), {
1935
- message: "Import permit issuer :: invalid address"
1936
- }).refine((val) => is0xPrefixed(val), {
1937
- message: "Import permit issuer :: must be 0x prefixed"
1938
- }).refine((val) => val !== viem.zeroAddress, {
1939
- message: "Import permit issuer :: must not be zeroAddress"
1940
- }),
1941
- recipient: zod.z.string().refine((val) => viem.isAddress(val), {
1942
- message: "Import permit recipient :: invalid address"
1943
- }).refine((val) => is0xPrefixed(val), {
1944
- message: "Import permit recipient :: must be 0x prefixed"
1945
- }).refine((val) => val !== viem.zeroAddress, {
1946
- message: "Import permit recipient :: must not be zeroAddress"
1947
- }),
1948
- issuerSignature: zod.z.string().refine((val) => is0xPrefixed(val), {
1949
- message: "Import permit issuerSignature :: must be 0x prefixed"
1950
- }).refine((val) => val !== "0x", {
1951
- message: "Import permit :: issuerSignature must be provided"
1952
- }),
1921
+ issuer: addressNotZeroSchema,
1922
+ recipient: addressNotZeroSchema,
1953
1923
  name: zod.z.string().optional().default("Unnamed Permit"),
1954
- expiration: zod.z.number().optional().default(DEFAULT_EXPIRATION_FN),
1955
- validatorId: zod.z.number().optional().default(0),
1956
- validatorContract: zod.z.string().optional().default(viem.zeroAddress).refine((val) => viem.isAddress(val), {
1957
- message: "Import permit validatorContract :: invalid address"
1958
- }),
1959
- recipientSignature: zod.z.string().optional().default("0x").refine((val) => is0xPrefixed(val), {
1960
- message: "Import permit recipientSignature :: must be 0x prefixed"
1961
- })
1962
- }).refine(...ValidatorContractRefinement);
1924
+ expiration: zod.z.int(),
1925
+ validatorId: zod.z.int().optional().default(0),
1926
+ validatorContract: addressSchema.optional().default(viem.zeroAddress),
1927
+ issuerSignature: bytesNotEmptySchema,
1928
+ recipientSignature: bytesSchema.optional().default("0x")
1929
+ }).refine(...ExternalValidatorRefinement);
1963
1930
  var ImportPermitValidator = zPermitWithSealingPair.refine((data) => data.type === "recipient", {
1964
- message: "Import permit :: type must be 'recipient'"
1931
+ error: "Type must be 'recipient'"
1965
1932
  }).refine((data) => data.recipient !== viem.zeroAddress, {
1966
- message: "Import permit :: recipient must not be zeroAddress"
1933
+ error: "Recipient must not be zeroAddress"
1967
1934
  }).refine((data) => data.issuerSignature !== "0x", {
1968
- message: "Import permit :: issuerSignature must be populated"
1935
+ error: "IssuerSignature must be populated"
1969
1936
  }).refine((data) => data.recipientSignature !== "0x", {
1970
- message: "Import permit :: recipientSignature must be populated"
1971
- }).refine(...ValidatorContractRefinement);
1972
- var validateSelfPermitOptions = (options) => SelfPermitOptionsValidator.safeParse(options);
1973
- var validateSharingPermitOptions = (options) => SharingPermitOptionsValidator.safeParse(options);
1974
- var validateImportPermitOptions = (options) => ImportPermitOptionsValidator.safeParse(options);
1975
- var validateSelfPermit = (permit) => SelfPermitValidator.safeParse(permit);
1976
- var validateSharingPermit = (permit) => SharingPermitValidator.safeParse(permit);
1977
- var validateImportPermit = (permit) => ImportPermitValidator.safeParse(permit);
1937
+ error: "RecipientSignature must be populated"
1938
+ }).refine(...ExternalValidatorRefinement);
1939
+ var safeParseAndThrowFormatted = (schema, data, message) => {
1940
+ const result = schema.safeParse(data);
1941
+ if (!result.success) {
1942
+ throw new Error(`${message}: ${zod.z.prettifyError(result.error)}`, { cause: result.error });
1943
+ }
1944
+ return result.data;
1945
+ };
1946
+ var validateSelfPermitOptions = (options) => {
1947
+ return safeParseAndThrowFormatted(SelfPermitOptionsValidator, options, "Invalid self permit options");
1948
+ };
1949
+ var validateSharingPermitOptions = (options) => {
1950
+ return safeParseAndThrowFormatted(SharingPermitOptionsValidator, options, "Invalid sharing permit options");
1951
+ };
1952
+ var validateImportPermitOptions = (options) => {
1953
+ return safeParseAndThrowFormatted(ImportPermitOptionsValidator, options, "Invalid import permit options");
1954
+ };
1955
+ var validateSelfPermit = (permit) => {
1956
+ return safeParseAndThrowFormatted(SelfPermitValidator, permit, "Invalid self permit");
1957
+ };
1958
+ var validateSharingPermit = (permit) => {
1959
+ return safeParseAndThrowFormatted(SharingPermitValidator, permit, "Invalid sharing permit");
1960
+ };
1961
+ var validateImportPermit = (permit) => {
1962
+ return safeParseAndThrowFormatted(ImportPermitValidator, permit, "Invalid import permit");
1963
+ };
1978
1964
  var ValidationUtils = {
1979
1965
  /**
1980
1966
  * Check if permit is expired
@@ -2068,6 +2054,179 @@ var SignatureUtils = {
2068
2054
  throw new Error(`Unknown permit type: ${permitType}`);
2069
2055
  }
2070
2056
  };
2057
+ var getAclAddress = async (publicClient) => {
2058
+ const ACL_IFACE = "function acl() view returns (address)";
2059
+ const aclAbi = viem.parseAbi([ACL_IFACE]);
2060
+ return await publicClient.readContract({
2061
+ address: TASK_MANAGER_ADDRESS,
2062
+ abi: aclAbi,
2063
+ functionName: "acl"
2064
+ });
2065
+ };
2066
+ var getAclEIP712Domain = async (publicClient) => {
2067
+ const aclAddress = await getAclAddress(publicClient);
2068
+ const EIP712_DOMAIN_IFACE = "function eip712Domain() public view returns (bytes1 fields, string name, string version, uint256 chainId, address verifyingContract, bytes32 salt, uint256[] extensions)";
2069
+ const domainAbi = viem.parseAbi([EIP712_DOMAIN_IFACE]);
2070
+ const domain = await publicClient.readContract({
2071
+ address: aclAddress,
2072
+ abi: domainAbi,
2073
+ functionName: "eip712Domain"
2074
+ });
2075
+ const [_fields, name, version, chainId, verifyingContract, _salt, _extensions] = domain;
2076
+ return {
2077
+ name,
2078
+ version,
2079
+ chainId: Number(chainId),
2080
+ verifyingContract
2081
+ };
2082
+ };
2083
+ var checkPermitValidityOnChain = async (permission, publicClient) => {
2084
+ const aclAddress = await getAclAddress(publicClient);
2085
+ try {
2086
+ await publicClient.simulateContract({
2087
+ address: aclAddress,
2088
+ abi: checkPermitValidityAbi,
2089
+ functionName: "checkPermitValidity",
2090
+ args: [
2091
+ {
2092
+ issuer: permission.issuer,
2093
+ expiration: BigInt(permission.expiration),
2094
+ recipient: permission.recipient,
2095
+ validatorId: BigInt(permission.validatorId),
2096
+ validatorContract: permission.validatorContract,
2097
+ sealingKey: permission.sealingKey,
2098
+ issuerSignature: permission.issuerSignature,
2099
+ recipientSignature: permission.recipientSignature
2100
+ }
2101
+ ]
2102
+ });
2103
+ return true;
2104
+ } catch (err) {
2105
+ if (err instanceof viem.BaseError) {
2106
+ const revertError = err.walk((err2) => err2 instanceof viem.ContractFunctionRevertedError);
2107
+ if (revertError instanceof viem.ContractFunctionRevertedError) {
2108
+ const errorName = revertError.data?.errorName ?? "";
2109
+ throw new Error(errorName);
2110
+ }
2111
+ }
2112
+ const customErrorName = extractCustomErrorFromDetails(err, checkPermitValidityAbi);
2113
+ if (customErrorName) {
2114
+ throw new Error(customErrorName);
2115
+ }
2116
+ const hhDetailsData = extractReturnData(err);
2117
+ if (hhDetailsData != null) {
2118
+ const decoded = viem.decodeErrorResult({
2119
+ abi: checkPermitValidityAbi,
2120
+ data: hhDetailsData
2121
+ });
2122
+ throw new Error(decoded.errorName);
2123
+ }
2124
+ throw err;
2125
+ }
2126
+ };
2127
+ function extractCustomErrorFromDetails(err, abi) {
2128
+ const anyErr = err;
2129
+ const details = anyErr?.details ?? anyErr?.cause?.details;
2130
+ if (typeof details === "string") {
2131
+ const customErrorMatch = details.match(/reverted with custom error '(\w+)\(\)'/);
2132
+ if (customErrorMatch) {
2133
+ const errorName = customErrorMatch[1];
2134
+ const errorExists = abi.some((item) => item.type === "error" && item.name === errorName);
2135
+ if (errorExists) {
2136
+ return errorName;
2137
+ }
2138
+ }
2139
+ }
2140
+ return void 0;
2141
+ }
2142
+ function extractReturnData(err) {
2143
+ const anyErr = err;
2144
+ const s = anyErr?.details ?? anyErr?.cause?.details ?? anyErr?.shortMessage ?? anyErr?.message ?? String(err);
2145
+ return s.match(/return data:\s*(0x[a-fA-F0-9]+)/)?.[1];
2146
+ }
2147
+ var checkPermitValidityAbi = [
2148
+ {
2149
+ type: "function",
2150
+ name: "checkPermitValidity",
2151
+ inputs: [
2152
+ {
2153
+ name: "permission",
2154
+ type: "tuple",
2155
+ internalType: "struct Permission",
2156
+ components: [
2157
+ {
2158
+ name: "issuer",
2159
+ type: "address",
2160
+ internalType: "address"
2161
+ },
2162
+ {
2163
+ name: "expiration",
2164
+ type: "uint64",
2165
+ internalType: "uint64"
2166
+ },
2167
+ {
2168
+ name: "recipient",
2169
+ type: "address",
2170
+ internalType: "address"
2171
+ },
2172
+ {
2173
+ name: "validatorId",
2174
+ type: "uint256",
2175
+ internalType: "uint256"
2176
+ },
2177
+ {
2178
+ name: "validatorContract",
2179
+ type: "address",
2180
+ internalType: "address"
2181
+ },
2182
+ {
2183
+ name: "sealingKey",
2184
+ type: "bytes32",
2185
+ internalType: "bytes32"
2186
+ },
2187
+ {
2188
+ name: "issuerSignature",
2189
+ type: "bytes",
2190
+ internalType: "bytes"
2191
+ },
2192
+ {
2193
+ name: "recipientSignature",
2194
+ type: "bytes",
2195
+ internalType: "bytes"
2196
+ }
2197
+ ]
2198
+ }
2199
+ ],
2200
+ outputs: [
2201
+ {
2202
+ name: "",
2203
+ type: "bool",
2204
+ internalType: "bool"
2205
+ }
2206
+ ],
2207
+ stateMutability: "view"
2208
+ },
2209
+ {
2210
+ type: "error",
2211
+ name: "PermissionInvalid_Disabled",
2212
+ inputs: []
2213
+ },
2214
+ {
2215
+ type: "error",
2216
+ name: "PermissionInvalid_Expired",
2217
+ inputs: []
2218
+ },
2219
+ {
2220
+ type: "error",
2221
+ name: "PermissionInvalid_IssuerSignature",
2222
+ inputs: []
2223
+ },
2224
+ {
2225
+ type: "error",
2226
+ name: "PermissionInvalid_RecipientSignature",
2227
+ inputs: []
2228
+ }
2229
+ ];
2071
2230
 
2072
2231
  // permits/permit.ts
2073
2232
  var PermitUtils = {
@@ -2076,14 +2235,10 @@ var PermitUtils = {
2076
2235
  */
2077
2236
  createSelf: (options) => {
2078
2237
  const validation = validateSelfPermitOptions(options);
2079
- if (!validation.success) {
2080
- throw new Error(
2081
- "PermitUtils :: createSelf :: Parsing SelfPermitOptions failed " + JSON.stringify(validation.error, null, 2)
2082
- );
2083
- }
2084
2238
  const sealingPair = GenerateSealingKey();
2085
2239
  const permit = {
2086
- ...validation.data,
2240
+ hash: PermitUtils.getHash(validation),
2241
+ ...validation,
2087
2242
  sealingPair,
2088
2243
  _signedDomain: void 0
2089
2244
  };
@@ -2094,14 +2249,10 @@ var PermitUtils = {
2094
2249
  */
2095
2250
  createSharing: (options) => {
2096
2251
  const validation = validateSharingPermitOptions(options);
2097
- if (!validation.success) {
2098
- throw new Error(
2099
- "PermitUtils :: createSharing :: Parsing SharingPermitOptions failed " + JSON.stringify(validation.error, null, 2)
2100
- );
2101
- }
2102
2252
  const sealingPair = GenerateSealingKey();
2103
2253
  const permit = {
2104
- ...validation.data,
2254
+ hash: PermitUtils.getHash(validation),
2255
+ ...validation,
2105
2256
  sealingPair,
2106
2257
  _signedDomain: void 0
2107
2258
  };
@@ -2116,27 +2267,21 @@ var PermitUtils = {
2116
2267
  try {
2117
2268
  parsedOptions = JSON.parse(options);
2118
2269
  } catch (error) {
2119
- throw new Error(`PermitUtils :: importShared :: Failed to parse JSON string: ${error}`);
2270
+ throw new Error(`Failed to parse JSON string: ${error}`);
2120
2271
  }
2121
2272
  } else if (typeof options === "object" && options !== null) {
2122
2273
  parsedOptions = options;
2123
2274
  } else {
2124
- throw new Error(
2125
- "PermitUtils :: importShared :: Invalid input type, expected ImportSharedPermitOptions, object, or string"
2126
- );
2275
+ throw new Error("Invalid input type, expected ImportSharedPermitOptions, object, or string");
2127
2276
  }
2128
2277
  if (parsedOptions.type != null && parsedOptions.type !== "sharing") {
2129
- throw new Error(`PermitUtils :: importShared :: Invalid permit type <${parsedOptions.type}>, must be "sharing"`);
2278
+ throw new Error(`Invalid permit type <${parsedOptions.type}>, must be "sharing"`);
2130
2279
  }
2131
2280
  const validation = validateImportPermitOptions({ ...parsedOptions, type: "recipient" });
2132
- if (!validation.success) {
2133
- throw new Error(
2134
- "PermitUtils :: importShared :: Parsing ImportPermitOptions failed " + JSON.stringify(validation.error, null, 2)
2135
- );
2136
- }
2137
2281
  const sealingPair = GenerateSealingKey();
2138
2282
  const permit = {
2139
- ...validation.data,
2283
+ hash: PermitUtils.getHash(validation),
2284
+ ...validation,
2140
2285
  sealingPair,
2141
2286
  _signedDomain: void 0
2142
2287
  };
@@ -2148,11 +2293,11 @@ var PermitUtils = {
2148
2293
  sign: async (permit, publicClient, walletClient) => {
2149
2294
  if (walletClient == null || walletClient.account == null) {
2150
2295
  throw new Error(
2151
- "PermitUtils :: sign - walletClient undefined, you must pass in a `walletClient` for the connected user to create a permit signature"
2296
+ "Missing walletClient, you must pass in a `walletClient` for the connected user to create a permit signature"
2152
2297
  );
2153
2298
  }
2154
2299
  const primaryType = SignatureUtils.getPrimaryType(permit.type);
2155
- const domain = await PermitUtils.fetchEIP712Domain(publicClient);
2300
+ const domain = await getAclEIP712Domain(publicClient);
2156
2301
  const { types, message } = SignatureUtils.getSignatureParams(PermitUtils.getPermission(permit, true), primaryType);
2157
2302
  const signature = await walletClient.signTypedData({
2158
2303
  domain,
@@ -2212,6 +2357,7 @@ var PermitUtils = {
2212
2357
  */
2213
2358
  serialize: (permit) => {
2214
2359
  return {
2360
+ hash: permit.hash,
2215
2361
  name: permit.name,
2216
2362
  type: permit.type,
2217
2363
  issuer: permit.issuer,
@@ -2236,7 +2382,7 @@ var PermitUtils = {
2236
2382
  } else if (permit.type === "recipient") {
2237
2383
  return validateImportPermit(permit);
2238
2384
  } else {
2239
- throw new Error("PermitUtils :: validate :: Invalid permit type");
2385
+ throw new Error("Invalid permit type");
2240
2386
  }
2241
2387
  },
2242
2388
  /**
@@ -2244,12 +2390,7 @@ var PermitUtils = {
2244
2390
  */
2245
2391
  getPermission: (permit, skipValidation = false) => {
2246
2392
  if (!skipValidation) {
2247
- const validationResult = PermitUtils.validate(permit);
2248
- if (!validationResult.success) {
2249
- throw new Error(
2250
- `PermitUtils :: getPermission :: permit validation failed - ${JSON.stringify(validationResult.error, null, 2)} ${JSON.stringify(permit, null, 2)}`
2251
- );
2252
- }
2393
+ PermitUtils.validate(permit);
2253
2394
  }
2254
2395
  return {
2255
2396
  issuer: permit.issuer,
@@ -2330,28 +2471,7 @@ var PermitUtils = {
2330
2471
  * Fetch EIP712 domain from the blockchain
2331
2472
  */
2332
2473
  fetchEIP712Domain: async (publicClient) => {
2333
- const TASK_MANAGER_ADDRESS = "0xeA30c4B8b44078Bbf8a6ef5b9f1eC1626C7848D9";
2334
- const ACL_IFACE = "function acl() view returns (address)";
2335
- const EIP712_DOMAIN_IFACE = "function eip712Domain() public view returns (bytes1 fields, string name, string version, uint256 chainId, address verifyingContract, bytes32 salt, uint256[] extensions)";
2336
- const aclAbi = viem.parseAbi([ACL_IFACE]);
2337
- const aclAddress = await publicClient.readContract({
2338
- address: TASK_MANAGER_ADDRESS,
2339
- abi: aclAbi,
2340
- functionName: "acl"
2341
- });
2342
- const domainAbi = viem.parseAbi([EIP712_DOMAIN_IFACE]);
2343
- const domain = await publicClient.readContract({
2344
- address: aclAddress,
2345
- abi: domainAbi,
2346
- functionName: "eip712Domain"
2347
- });
2348
- const [_fields, name, version, chainId, verifyingContract, _salt, _extensions] = domain;
2349
- return {
2350
- name,
2351
- version,
2352
- chainId: Number(chainId),
2353
- verifyingContract
2354
- };
2474
+ return getAclEIP712Domain(publicClient);
2355
2475
  },
2356
2476
  /**
2357
2477
  * Check if permit's signed domain matches the provided domain
@@ -2365,8 +2485,15 @@ var PermitUtils = {
2365
2485
  checkSignedDomainValid: async (permit, publicClient) => {
2366
2486
  if (permit._signedDomain == null)
2367
2487
  return false;
2368
- const domain = await PermitUtils.fetchEIP712Domain(publicClient);
2488
+ const domain = await getAclEIP712Domain(publicClient);
2369
2489
  return PermitUtils.matchesDomain(permit, domain);
2490
+ },
2491
+ /**
2492
+ * Check if permit passes the on-chain validation
2493
+ */
2494
+ checkValidityOnChain: async (permit, publicClient) => {
2495
+ const permission = PermitUtils.getPermission(permit);
2496
+ return checkPermitValidityOnChain(permission, publicClient);
2370
2497
  }
2371
2498
  };
2372
2499
  var PERMIT_STORE_DEFAULTS = {
@@ -2420,11 +2547,11 @@ var setPermit = (chainId, account, permit) => {
2420
2547
  state.permits[chainId] = {};
2421
2548
  if (state.permits[chainId][account] == null)
2422
2549
  state.permits[chainId][account] = {};
2423
- state.permits[chainId][account][PermitUtils.getHash(permit)] = PermitUtils.serialize(permit);
2550
+ state.permits[chainId][account][permit.hash] = PermitUtils.serialize(permit);
2424
2551
  })
2425
2552
  );
2426
2553
  };
2427
- var removePermit = (chainId, account, hash, force) => {
2554
+ var removePermit = (chainId, account, hash) => {
2428
2555
  clearStaleStore();
2429
2556
  _permitStore.setState(
2430
2557
  immer.produce((state) => {
@@ -2438,15 +2565,7 @@ var removePermit = (chainId, account, hash, force) => {
2438
2565
  if (accountPermits[hash] == null)
2439
2566
  return;
2440
2567
  if (state.activePermitHash[chainId][account] === hash) {
2441
- const otherPermitHash = Object.keys(accountPermits).find((key) => key !== hash && accountPermits[key] != null);
2442
- if (otherPermitHash) {
2443
- state.activePermitHash[chainId][account] = otherPermitHash;
2444
- } else {
2445
- if (!force) {
2446
- throw new Error("Cannot remove the last permit without force flag");
2447
- }
2448
- state.activePermitHash[chainId][account] = void 0;
2449
- }
2568
+ state.activePermitHash[chainId][account] = void 0;
2450
2569
  }
2451
2570
  accountPermits[hash] = void 0;
2452
2571
  })
@@ -2497,7 +2616,7 @@ var storeActivePermit = async (permit, publicClient, walletClient) => {
2497
2616
  const chainId = await publicClient.getChainId();
2498
2617
  const account = walletClient.account.address;
2499
2618
  permitStore.setPermit(chainId, account, permit);
2500
- permitStore.setActivePermitHash(chainId, account, PermitUtils.getHash(permit));
2619
+ permitStore.setActivePermitHash(chainId, account, permit.hash);
2501
2620
  };
2502
2621
  var createPermitWithSign = async (options, publicClient, walletClient, permitMethod) => {
2503
2622
  const permit = await permitMethod(options, publicClient, walletClient);
@@ -2555,7 +2674,7 @@ var getOrCreateSharingPermit = async (publicClient, walletClient, options, chain
2555
2674
  }
2556
2675
  return createSharing(options, publicClient, walletClient);
2557
2676
  };
2558
- var removePermit2 = async (chainId, account, hash, force) => permitStore.removePermit(chainId, account, hash, force);
2677
+ var removePermit2 = async (chainId, account, hash) => permitStore.removePermit(chainId, account, hash);
2559
2678
  var removeActivePermit = async (chainId, account) => permitStore.removeActivePermitHash(chainId, account);
2560
2679
  var permits = {
2561
2680
  getSnapshot: permitStore.store.getState,
@@ -2727,7 +2846,6 @@ var MockQueryDecrypterAbi = [
2727
2846
  ];
2728
2847
 
2729
2848
  // core/decrypt/cofheMocksSealOutput.ts
2730
- var MockQueryDecrypterAddress = "0x0000000000000000000000000000000000000200";
2731
2849
  async function cofheMocksSealOutput(ctHash, utype, permit, publicClient, mocksSealOutputDelay) {
2732
2850
  if (mocksSealOutputDelay > 0)
2733
2851
  await sleep(mocksSealOutputDelay);
@@ -2738,7 +2856,7 @@ async function cofheMocksSealOutput(ctHash, utype, permit, publicClient, mocksSe
2738
2856
  validatorId: BigInt(permission.validatorId)
2739
2857
  };
2740
2858
  const [allowed, error, result] = await publicClient.readContract({
2741
- address: MockQueryDecrypterAddress,
2859
+ address: MOCKS_QUERY_DECRYPTER_ADDRESS,
2742
2860
  abi: MockQueryDecrypterAbi,
2743
2861
  functionName: "querySealOutput",
2744
2862
  args: [ctHash, BigInt(utype), permissionWithBigInts]
@@ -3198,6 +3316,7 @@ var InitialConnectStore = {
3198
3316
  function createCofhesdkClientBase(opts) {
3199
3317
  const keysStorage = createKeysStore(opts.config.fheKeyStorage);
3200
3318
  const connectStore = vanilla.createStore(() => InitialConnectStore);
3319
+ let connectAttemptId = 0;
3201
3320
  const updateConnectState = (partial) => {
3202
3321
  connectStore.setState((state) => ({ ...state, ...partial }));
3203
3322
  };
@@ -3223,6 +3342,8 @@ function createCofhesdkClientBase(opts) {
3223
3342
  const state = connectStore.getState();
3224
3343
  if (state.connected && state.publicClient === publicClient && state.walletClient === walletClient)
3225
3344
  return;
3345
+ connectAttemptId += 1;
3346
+ const localAttemptId = connectAttemptId;
3226
3347
  updateConnectState({
3227
3348
  ...InitialConnectStore,
3228
3349
  connecting: true
@@ -3230,6 +3351,8 @@ function createCofhesdkClientBase(opts) {
3230
3351
  try {
3231
3352
  const chainId = await getPublicClientChainID(publicClient);
3232
3353
  const account = await getWalletClientAccount(walletClient);
3354
+ if (localAttemptId !== connectAttemptId)
3355
+ return;
3233
3356
  updateConnectState({
3234
3357
  connected: true,
3235
3358
  connecting: false,
@@ -3240,6 +3363,8 @@ function createCofhesdkClientBase(opts) {
3240
3363
  walletClient
3241
3364
  });
3242
3365
  } catch (e) {
3366
+ if (localAttemptId !== connectAttemptId)
3367
+ return;
3243
3368
  updateConnectState({
3244
3369
  ...InitialConnectStore,
3245
3370
  connectError: e
@@ -3247,6 +3372,10 @@ function createCofhesdkClientBase(opts) {
3247
3372
  throw e;
3248
3373
  }
3249
3374
  }
3375
+ function disconnect() {
3376
+ connectAttemptId += 1;
3377
+ updateConnectState({ ...InitialConnectStore });
3378
+ }
3250
3379
  function encryptInputs(inputs) {
3251
3380
  const state = connectStore.getState();
3252
3381
  return new EncryptInputsBuilder({
@@ -3351,9 +3480,9 @@ function createCofhesdkClientBase(opts) {
3351
3480
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
3352
3481
  return permits.selectActivePermit(_chainId, _account, hash);
3353
3482
  },
3354
- removePermit: async (hash, chainId, account, force) => {
3483
+ removePermit: async (hash, chainId, account) => {
3355
3484
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
3356
- return permits.removePermit(_chainId, _account, hash, force);
3485
+ return permits.removePermit(_chainId, _account, hash);
3357
3486
  },
3358
3487
  removeActivePermit: async (chainId, account) => {
3359
3488
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
@@ -3378,6 +3507,7 @@ function createCofhesdkClientBase(opts) {
3378
3507
  // config & platform-specific (read-only)
3379
3508
  config: opts.config,
3380
3509
  connect,
3510
+ disconnect,
3381
3511
  encryptInputs,
3382
3512
  decryptHandle,
3383
3513
  permits: clientPermits
@@ -3400,7 +3530,12 @@ exports.Encryptable = Encryptable;
3400
3530
  exports.FheAllUTypes = FheAllUTypes;
3401
3531
  exports.FheTypes = FheTypes;
3402
3532
  exports.FheUintUTypes = FheUintUTypes;
3533
+ exports.MOCKS_QUERY_DECRYPTER_ADDRESS = MOCKS_QUERY_DECRYPTER_ADDRESS;
3534
+ exports.MOCKS_ZK_VERIFIER_ADDRESS = MOCKS_ZK_VERIFIER_ADDRESS;
3403
3535
  exports.MOCKS_ZK_VERIFIER_SIGNER_ADDRESS = MOCKS_ZK_VERIFIER_SIGNER_ADDRESS;
3536
+ exports.MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY = MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY;
3537
+ exports.TASK_MANAGER_ADDRESS = TASK_MANAGER_ADDRESS;
3538
+ exports.TEST_BED_ADDRESS = TEST_BED_ADDRESS;
3404
3539
  exports.assertCorrectEncryptedItemInput = assertCorrectEncryptedItemInput;
3405
3540
  exports.createCofhesdkClientBase = createCofhesdkClientBase;
3406
3541
  exports.createCofhesdkConfigBase = createCofhesdkConfigBase;