@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,221 @@
1
+ import {
2
+ BaseError,
3
+ ContractFunctionRevertedError,
4
+ type Hex,
5
+ type PublicClient,
6
+ decodeErrorResult,
7
+ parseAbi,
8
+ } from 'viem';
9
+ import type { EIP712Domain, Permission } from './types';
10
+ import { TASK_MANAGER_ADDRESS } from '../core/consts.js';
11
+
12
+ export const getAclAddress = async (publicClient: PublicClient): Promise<Hex> => {
13
+ const ACL_IFACE = 'function acl() view returns (address)';
14
+
15
+ // Parse the ABI for the ACL function
16
+ const aclAbi = parseAbi([ACL_IFACE]);
17
+
18
+ // Get the ACL address
19
+ return (await publicClient.readContract({
20
+ address: TASK_MANAGER_ADDRESS as `0x${string}`,
21
+ abi: aclAbi,
22
+ functionName: 'acl',
23
+ })) as `0x${string}`;
24
+ };
25
+
26
+ export const getAclEIP712Domain = async (publicClient: PublicClient): Promise<EIP712Domain> => {
27
+ const aclAddress = await getAclAddress(publicClient);
28
+ const EIP712_DOMAIN_IFACE =
29
+ 'function eip712Domain() public view returns (bytes1 fields, string name, string version, uint256 chainId, address verifyingContract, bytes32 salt, uint256[] extensions)';
30
+
31
+ // Parse the ABI for the EIP712 domain function
32
+ const domainAbi = parseAbi([EIP712_DOMAIN_IFACE]);
33
+
34
+ // Get the EIP712 domain
35
+ const domain = await publicClient.readContract({
36
+ address: aclAddress,
37
+ abi: domainAbi,
38
+ functionName: 'eip712Domain',
39
+ });
40
+
41
+ // eslint-disable-next-line no-unused-vars
42
+ const [_fields, name, version, chainId, verifyingContract, _salt, _extensions] = domain;
43
+
44
+ return {
45
+ name,
46
+ version,
47
+ chainId: Number(chainId),
48
+ verifyingContract,
49
+ };
50
+ };
51
+
52
+ export const checkPermitValidityOnChain = async (
53
+ permission: Permission,
54
+ publicClient: PublicClient
55
+ ): Promise<boolean> => {
56
+ const aclAddress = await getAclAddress(publicClient);
57
+
58
+ // Check if the permit is valid
59
+ try {
60
+ await publicClient.simulateContract({
61
+ address: aclAddress,
62
+ abi: checkPermitValidityAbi,
63
+ functionName: 'checkPermitValidity',
64
+ args: [
65
+ {
66
+ issuer: permission.issuer,
67
+ expiration: BigInt(permission.expiration),
68
+ recipient: permission.recipient,
69
+ validatorId: BigInt(permission.validatorId),
70
+ validatorContract: permission.validatorContract,
71
+ sealingKey: permission.sealingKey,
72
+ issuerSignature: permission.issuerSignature,
73
+ recipientSignature: permission.recipientSignature,
74
+ },
75
+ ],
76
+ });
77
+ return true;
78
+ } catch (err: any) {
79
+ // Viem default handling
80
+ if (err instanceof BaseError) {
81
+ const revertError = err.walk((err: any) => err instanceof ContractFunctionRevertedError);
82
+ if (revertError instanceof ContractFunctionRevertedError) {
83
+ const errorName = revertError.data?.errorName ?? '';
84
+ throw new Error(errorName);
85
+ }
86
+ }
87
+
88
+ // Check details field for custom error names (e.g., from Hardhat test nodes)
89
+ const customErrorName = extractCustomErrorFromDetails(err, checkPermitValidityAbi);
90
+ if (customErrorName) {
91
+ throw new Error(customErrorName);
92
+ }
93
+
94
+ // Hardhat wrapped error will need to be unwrapped to get the return data
95
+ const hhDetailsData = extractReturnData(err);
96
+ if (hhDetailsData != null) {
97
+ const decoded = decodeErrorResult({
98
+ abi: checkPermitValidityAbi,
99
+ data: hhDetailsData,
100
+ });
101
+
102
+ throw new Error(decoded.errorName);
103
+ }
104
+
105
+ // Fallback throw the original error
106
+ throw err;
107
+ }
108
+ };
109
+
110
+ function extractCustomErrorFromDetails(err: unknown, abi: readonly any[]): string | undefined {
111
+ // Check details field for custom error names (e.g., from Hardhat test nodes)
112
+ const anyErr = err as any;
113
+ const details = anyErr?.details ?? anyErr?.cause?.details;
114
+
115
+ if (typeof details === 'string') {
116
+ // Match pattern: "reverted with custom error 'ErrorName()'"
117
+ const customErrorMatch = details.match(/reverted with custom error '(\w+)\(\)'/);
118
+ if (customErrorMatch) {
119
+ const errorName = customErrorMatch[1];
120
+ // Check if this error exists in our ABI
121
+ const errorExists = abi.some((item) => item.type === 'error' && item.name === errorName);
122
+ if (errorExists) {
123
+ return errorName;
124
+ }
125
+ }
126
+ }
127
+
128
+ return undefined;
129
+ }
130
+
131
+ function extractReturnData(err: unknown): `0x${string}` | undefined {
132
+ // viem BaseError has `details`, but fall back to any message-like string we can find
133
+ const anyErr = err as any;
134
+ const s = anyErr?.details ?? anyErr?.cause?.details ?? anyErr?.shortMessage ?? anyErr?.message ?? String(err);
135
+
136
+ return s.match(/return data:\s*(0x[a-fA-F0-9]+)/)?.[1] as `0x${string}` | undefined;
137
+ }
138
+
139
+ const checkPermitValidityAbi = [
140
+ {
141
+ type: 'function',
142
+ name: 'checkPermitValidity',
143
+ inputs: [
144
+ {
145
+ name: 'permission',
146
+ type: 'tuple',
147
+ internalType: 'struct Permission',
148
+ components: [
149
+ {
150
+ name: 'issuer',
151
+ type: 'address',
152
+ internalType: 'address',
153
+ },
154
+ {
155
+ name: 'expiration',
156
+ type: 'uint64',
157
+ internalType: 'uint64',
158
+ },
159
+ {
160
+ name: 'recipient',
161
+ type: 'address',
162
+ internalType: 'address',
163
+ },
164
+ {
165
+ name: 'validatorId',
166
+ type: 'uint256',
167
+ internalType: 'uint256',
168
+ },
169
+ {
170
+ name: 'validatorContract',
171
+ type: 'address',
172
+ internalType: 'address',
173
+ },
174
+ {
175
+ name: 'sealingKey',
176
+ type: 'bytes32',
177
+ internalType: 'bytes32',
178
+ },
179
+ {
180
+ name: 'issuerSignature',
181
+ type: 'bytes',
182
+ internalType: 'bytes',
183
+ },
184
+ {
185
+ name: 'recipientSignature',
186
+ type: 'bytes',
187
+ internalType: 'bytes',
188
+ },
189
+ ],
190
+ },
191
+ ],
192
+ outputs: [
193
+ {
194
+ name: '',
195
+ type: 'bool',
196
+ internalType: 'bool',
197
+ },
198
+ ],
199
+ stateMutability: 'view',
200
+ },
201
+ {
202
+ type: 'error',
203
+ name: 'PermissionInvalid_Disabled',
204
+ inputs: [],
205
+ },
206
+ {
207
+ type: 'error',
208
+ name: 'PermissionInvalid_Expired',
209
+ inputs: [],
210
+ },
211
+ {
212
+ type: 'error',
213
+ name: 'PermissionInvalid_IssuerSignature',
214
+ inputs: [],
215
+ },
216
+ {
217
+ type: 'error',
218
+ name: 'PermissionInvalid_RecipientSignature',
219
+ inputs: [],
220
+ },
221
+ ] as const;