@cofhe/sdk 0.0.0-beta-20251027110729

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