@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,376 @@
1
+ import { Hex, PublicClient, WalletClient } from 'viem';
2
+
3
+ type EthEncryptedData = {
4
+ data: Uint8Array;
5
+ public_key: Uint8Array;
6
+ nonce: Uint8Array;
7
+ };
8
+ /**
9
+ * A class representing a SealingKey which provides cryptographic sealing (encryption)
10
+ * and unsealing (decryption) capabilities.
11
+ */
12
+ declare class SealingKey$1 {
13
+ /**
14
+ * The private key used for decryption.
15
+ */
16
+ privateKey: string;
17
+ /**
18
+ * The public key used for encryption.
19
+ */
20
+ publicKey: string;
21
+ /**
22
+ * Constructs a SealingKey instance with the given private and public keys.
23
+ *
24
+ * @param {string} privateKey - The private key used for decryption.
25
+ * @param {string} publicKey - The public key used for encryption.
26
+ * @throws Will throw an error if the provided keys lengths do not match
27
+ * the required lengths for private and public keys.
28
+ */
29
+ constructor(privateKey: string, publicKey: string);
30
+ unseal: (parsedData: EthEncryptedData) => bigint;
31
+ /**
32
+ * Serializes the SealingKey to a JSON object.
33
+ */
34
+ serialize: () => {
35
+ privateKey: string;
36
+ publicKey: string;
37
+ };
38
+ /**
39
+ * Deserializes the SealingKey from a JSON object.
40
+ */
41
+ static deserialize: (privateKey: string, publicKey: string) => SealingKey$1;
42
+ /**
43
+ * Seals (encrypts) the provided message for a receiver with the specified public key.
44
+ *
45
+ * @param {bigint | number} value - The message to be encrypted.
46
+ * @param {string} publicKey - The public key of the intended recipient.
47
+ * @returns string - The encrypted message in hexadecimal format.
48
+ * @static
49
+ * @throws Will throw if the provided publicKey or value do not meet defined preconditions.
50
+ */
51
+ static seal: (value: bigint | number, publicKey: string) => EthEncryptedData;
52
+ }
53
+ /**
54
+ * Asynchronously generates a new SealingKey.
55
+ * This function uses the 'nacl' library to create a new public/private key pair for sealing purposes.
56
+ * A sealing key is used to encrypt data such that it can only be unsealed (decrypted) by the owner of the corresponding private key.
57
+ * @returns {SealingKey} - A new SealingKey object containing the hexadecimal strings of the public and private keys.
58
+ */
59
+ declare const GenerateSealingKey: () => SealingKey$1;
60
+
61
+ /**
62
+ * EIP712 related types
63
+ */
64
+ type EIP712Type = {
65
+ name: string;
66
+ type: string;
67
+ };
68
+ type EIP712Types = Record<string, EIP712Type[]>;
69
+ type EIP712Message = Record<string, string>;
70
+ type EIP712Domain = {
71
+ chainId: number;
72
+ name: string;
73
+ verifyingContract: Hex;
74
+ version: string;
75
+ };
76
+ /**
77
+ * Sealing key type - using the actual SealingKey class
78
+ */
79
+ type SealingKey = SealingKey$1;
80
+
81
+ /**
82
+ * Core Permit interface - immutable design for React compatibility
83
+ */
84
+ interface Permit {
85
+ /**
86
+ * Stable hash of relevant permit data, used as key in storage
87
+ */
88
+ hash: string;
89
+ /**
90
+ * Name for this permit, for organization and UI usage, not included in signature.
91
+ */
92
+ name: string;
93
+ /**
94
+ * The type of the Permit (self / sharing)
95
+ * (self) Permit that will be signed and used by the issuer
96
+ * (sharing) Permit that is signed by the issuer, but intended to be shared with recipient
97
+ * (recipient) Permit that has been received, and signed by the recipient
98
+ */
99
+ type: 'self' | 'sharing' | 'recipient';
100
+ /**
101
+ * (base) User that initially created the permission, target of data fetching
102
+ */
103
+ issuer: Hex;
104
+ /**
105
+ * (base) Expiration timestamp
106
+ */
107
+ expiration: number;
108
+ /**
109
+ * (sharing) The user that this permission will be shared with
110
+ * ** optional, use `address(0)` to disable **
111
+ */
112
+ recipient: Hex;
113
+ /**
114
+ * (issuer defined validation) An id used to query a contract to check this permissions validity
115
+ * ** optional, use `0` to disable **
116
+ */
117
+ validatorId: number;
118
+ /**
119
+ * (issuer defined validation) The contract to query to determine permission validity
120
+ * ** optional, user `address(0)` to disable **
121
+ */
122
+ validatorContract: Hex;
123
+ /**
124
+ * (base) The publicKey of a sealingPair used to re-encrypt `issuer`s confidential data
125
+ * (non-sharing) Populated by `issuer`
126
+ * (sharing) Populated by `recipient`
127
+ */
128
+ sealingPair: SealingKey;
129
+ /**
130
+ * (base) `signTypedData` signature created by `issuer`.
131
+ * (base) Shared- and Self- permissions differ in signature format: (`sealingKey` absent in shared signature)
132
+ * (non-sharing) < issuer, expiration, recipient, validatorId, validatorContract, sealingKey >
133
+ * (sharing) < issuer, expiration, recipient, validatorId, validatorContract >
134
+ */
135
+ issuerSignature: Hex;
136
+ /**
137
+ * (sharing) `signTypedData` signature created by `recipient` with format:
138
+ * (sharing) < sealingKey, issuerSignature>
139
+ * ** required for shared permits **
140
+ */
141
+ recipientSignature: Hex;
142
+ /**
143
+ * EIP712 domain used to sign this permit.
144
+ * Should not be set manually, included in metadata as part of serialization flows.
145
+ */
146
+ _signedDomain?: EIP712Domain;
147
+ }
148
+ /**
149
+ * Permit discriminant helpers
150
+ */
151
+ type PermitType = Permit['type'];
152
+ /**
153
+ * Utility type to narrow a permit to a specific discriminant.
154
+ *
155
+ * Note: this only narrows the `type` field. Runtime/validation constraints
156
+ * (e.g. recipient == zeroAddress for self permits) are enforced elsewhere.
157
+ */
158
+ type PermitOf<T extends PermitType> = Expand<Omit<Permit, 'type'> & {
159
+ type: T;
160
+ }>;
161
+ type SelfPermit = PermitOf<'self'>;
162
+ type SharingPermit = PermitOf<'sharing'>;
163
+ type RecipientPermit = PermitOf<'recipient'>;
164
+ /**
165
+ * Optional additional metadata of a Permit
166
+ * Can be passed into the constructor, but not necessary
167
+ * Useful for deserialization
168
+ */
169
+ interface PermitMetadata {
170
+ /**
171
+ * EIP712 domain used to sign this permit.
172
+ * Should not be set manually, included in metadata as part of serialization flows.
173
+ */
174
+ _signedDomain?: EIP712Domain;
175
+ }
176
+ /**
177
+ * Utility types for permit creation
178
+ */
179
+ type CreateSelfPermitOptions = {
180
+ type?: 'self';
181
+ issuer: string;
182
+ name?: string;
183
+ expiration?: number;
184
+ validatorId?: number;
185
+ validatorContract?: string;
186
+ };
187
+ type CreateSharingPermitOptions = {
188
+ type?: 'sharing';
189
+ issuer: string;
190
+ recipient: string;
191
+ name?: string;
192
+ expiration?: number;
193
+ validatorId?: number;
194
+ validatorContract?: string;
195
+ };
196
+ type ImportSharedPermitOptions = {
197
+ type?: 'sharing';
198
+ issuer: string;
199
+ recipient: string;
200
+ issuerSignature: string;
201
+ name?: string;
202
+ expiration: number;
203
+ validatorId?: number;
204
+ validatorContract?: string;
205
+ };
206
+ type SerializedPermit = Omit<Permit, 'sealingPair'> & {
207
+ _signedDomain?: EIP712Domain;
208
+ sealingPair: {
209
+ privateKey: string;
210
+ publicKey: string;
211
+ };
212
+ };
213
+ /**
214
+ * A type representing the Permission struct that is passed to Permissioned.sol to grant encrypted data access.
215
+ */
216
+ type Permission = Expand<Omit<Permit, 'name' | 'type' | 'sealingPair' | 'hash'> & {
217
+ sealingKey: Hex;
218
+ }>;
219
+ /**
220
+ * A type representing the permit fields that are used to generate the hash
221
+ */
222
+ type PermitHashFields = Pick<Permit, 'type' | 'issuer' | 'expiration' | 'recipient' | 'validatorId' | 'validatorContract'>;
223
+ /**
224
+ * Validation result type
225
+ */
226
+ interface ValidationResult {
227
+ valid: boolean;
228
+ error: 'invalid-schema' | 'expired' | 'not-signed' | null;
229
+ }
230
+ /**
231
+ * Signature types for EIP712 signing
232
+ */
233
+ type PermitSignaturePrimaryType = 'PermissionedV2IssuerSelf' | 'PermissionedV2IssuerShared' | 'PermissionedV2Recipient';
234
+ type Expand<T> = T extends infer O ? {
235
+ [K in keyof O]: O[K];
236
+ } : never;
237
+
238
+ /**
239
+ * Main Permit utilities - functional approach for React compatibility
240
+ */
241
+ declare const PermitUtils: {
242
+ /**
243
+ * Create a self permit for personal use
244
+ */
245
+ createSelf: (options: CreateSelfPermitOptions) => SelfPermit;
246
+ /**
247
+ * Create a sharing permit to be shared with another user
248
+ */
249
+ createSharing: (options: CreateSharingPermitOptions) => SharingPermit;
250
+ /**
251
+ * Import a shared permit from various input formats
252
+ */
253
+ importShared: (options: ImportSharedPermitOptions | string) => RecipientPermit;
254
+ /**
255
+ * Sign a permit with the provided wallet client
256
+ */
257
+ sign: <T extends Permit>(permit: T, publicClient: PublicClient, walletClient: WalletClient) => Promise<T>;
258
+ /**
259
+ * Create and sign a self permit in one operation
260
+ */
261
+ createSelfAndSign: (options: CreateSelfPermitOptions, publicClient: PublicClient, walletClient: WalletClient) => Promise<SelfPermit>;
262
+ /**
263
+ * Create and sign a sharing permit in one operation
264
+ */
265
+ createSharingAndSign: (options: CreateSharingPermitOptions, publicClient: PublicClient, walletClient: WalletClient) => Promise<SharingPermit>;
266
+ /**
267
+ * Import and sign a shared permit in one operation from various input formats
268
+ */
269
+ importSharedAndSign: (options: ImportSharedPermitOptions | string, publicClient: PublicClient, walletClient: WalletClient) => Promise<RecipientPermit>;
270
+ /**
271
+ * Deserialize a permit from serialized data
272
+ */
273
+ deserialize: (data: SerializedPermit) => Permit;
274
+ /**
275
+ * Serialize a permit for storage
276
+ */
277
+ serialize: (permit: Permit) => SerializedPermit;
278
+ /**
279
+ * Validate a permit (schema-level validation)
280
+ */
281
+ validateSchema: (permit: Permit) => {
282
+ name: string;
283
+ type: "self" | "sharing" | "recipient";
284
+ issuer: `0x${string}`;
285
+ expiration: number;
286
+ recipient: `0x${string}`;
287
+ validatorId: number;
288
+ validatorContract: `0x${string}`;
289
+ issuerSignature: `0x${string}`;
290
+ recipientSignature: `0x${string}`;
291
+ sealingPair?: {
292
+ privateKey: string;
293
+ publicKey: string;
294
+ } | undefined;
295
+ };
296
+ /**
297
+ * Validate a permit (holistic validation).
298
+ *
299
+ * This validates:
300
+ * - Permit schema (shape + invariants)
301
+ * - Permit is signed
302
+ * - Permit is not expired
303
+ *
304
+ * For schema-only validation, use `validateSchema(permit)`.
305
+ */
306
+ validate: (permit: Permit) => {
307
+ name: string;
308
+ type: "self" | "sharing" | "recipient";
309
+ issuer: `0x${string}`;
310
+ expiration: number;
311
+ recipient: `0x${string}`;
312
+ validatorId: number;
313
+ validatorContract: `0x${string}`;
314
+ issuerSignature: `0x${string}`;
315
+ recipientSignature: `0x${string}`;
316
+ sealingPair?: {
317
+ privateKey: string;
318
+ publicKey: string;
319
+ } | undefined;
320
+ };
321
+ /**
322
+ * Get the permission object from a permit (for use in contracts)
323
+ */
324
+ getPermission: (permit: Permit, skipValidation?: boolean) => Permission;
325
+ /**
326
+ * Get a stable hash for the permit (used as key in storage)
327
+ */
328
+ getHash: (permit: PermitHashFields) => string;
329
+ /**
330
+ * Export permit data for sharing (removes sensitive fields)
331
+ */
332
+ export: (permit: Permit) => string;
333
+ /**
334
+ * Unseal encrypted data using the permit's sealing key
335
+ */
336
+ unseal: (permit: Permit, ciphertext: EthEncryptedData) => bigint;
337
+ /**
338
+ * Check if permit is expired
339
+ */
340
+ isExpired: (permit: Permit) => boolean;
341
+ /**
342
+ * Check if permit is signed
343
+ */
344
+ isSigned: (permit: Permit) => boolean;
345
+ /**
346
+ * Check if permit is signed and not expired
347
+ */
348
+ isSignedAndNotExpired: (permit: Permit) => ValidationResult;
349
+ /**
350
+ * Assert that permit is signed and not expired
351
+ */
352
+ assertSignedAndNotExpired: (permit: Permit) => void;
353
+ isValid: (permit: Permit) => ValidationResult;
354
+ /**
355
+ * Update permit name (returns new permit instance)
356
+ */
357
+ updateName: (permit: Permit, name: string) => Permit;
358
+ /**
359
+ * Fetch EIP712 domain from the blockchain
360
+ */
361
+ fetchEIP712Domain: (publicClient: PublicClient) => Promise<EIP712Domain>;
362
+ /**
363
+ * Check if permit's signed domain matches the provided domain
364
+ */
365
+ matchesDomain: (permit: Permit, domain: EIP712Domain) => boolean;
366
+ /**
367
+ * Check if permit's signed domain is valid for the current chain
368
+ */
369
+ checkSignedDomainValid: (permit: Permit, publicClient: PublicClient) => Promise<boolean>;
370
+ /**
371
+ * Check if permit passes the on-chain validation
372
+ */
373
+ checkValidityOnChain: (permit: Permit, publicClient: PublicClient) => Promise<boolean>;
374
+ };
375
+
376
+ export { type CreateSelfPermitOptions as C, type EIP712Types as E, GenerateSealingKey as G, type ImportSharedPermitOptions as I, type Permit as P, type RecipientPermit as R, type SerializedPermit as S, type ValidationResult as V, type PermitSignaturePrimaryType as a, type Permission as b, type EIP712Message as c, type CreateSharingPermitOptions as d, type PermitMetadata as e, type EIP712Domain as f, PermitUtils as g, SealingKey$1 as h, type EthEncryptedData as i, type EIP712Type as j, type PermitType as k, type PermitOf as l, type SelfPermit as m, type SharingPermit as n, type PermitHashFields as o, type Expand as p };