@cofhe/sdk 0.6.0 → 0.7.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 (114) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/acps/acp.ts +411 -0
  3. package/acps/index.ts +70 -0
  4. package/{permits → acps}/onchain-utils.ts +49 -24
  5. package/acps/sealing.ts +90 -0
  6. package/acps/signature.ts +89 -0
  7. package/acps/store.ts +172 -0
  8. package/acps/test/acp.test.ts +615 -0
  9. package/acps/test/localstorage.test.ts +105 -0
  10. package/acps/test/sealing.test.ts +77 -0
  11. package/acps/test/store.test.ts +88 -0
  12. package/acps/test/validation.test.ts +361 -0
  13. package/acps/test-utils.ts +32 -0
  14. package/acps/types.ts +252 -0
  15. package/acps/validation.ts +392 -0
  16. package/adapters/test/ethers5.test.ts +4 -1
  17. package/adapters/test/ethers6.test.ts +4 -1
  18. package/adapters/test/wagmi.test.ts +5 -2
  19. package/chains/chains/stagingCofhe.ts +21 -0
  20. package/chains/index.ts +3 -1
  21. package/chains/test/chains.test.ts +2 -1
  22. package/core/acps.ts +625 -0
  23. package/core/client.ts +136 -39
  24. package/core/clientTypes.ts +52 -41
  25. package/core/config.ts +66 -5
  26. package/core/debug.ts +72 -0
  27. package/core/decrypt/MockThresholdNetworkAbi.ts +20 -11
  28. package/core/decrypt/apiError.ts +104 -0
  29. package/core/decrypt/cofheMocksDecryptForTx.ts +11 -11
  30. package/core/decrypt/cofheMocksDecryptForView.ts +7 -7
  31. package/core/decrypt/decryptForTxBuilder.ts +102 -102
  32. package/core/decrypt/decryptForViewBuilder.ts +90 -90
  33. package/core/decrypt/submitRetry.ts +38 -30
  34. package/core/decrypt/tnDecryptV1.ts +5 -5
  35. package/core/decrypt/tnDecryptV2.ts +45 -34
  36. package/core/decrypt/tnSealOutputV1.ts +4 -3
  37. package/core/decrypt/tnSealOutputV2.ts +44 -31
  38. package/core/encrypt/cofheMocksZkVerifySign.ts +59 -74
  39. package/core/encrypt/encryptInputsBuilder.ts +86 -52
  40. package/core/encrypt/zkPackProveVerify.ts +25 -18
  41. package/core/error.ts +34 -6
  42. package/core/index.ts +14 -14
  43. package/core/test/acpDefaults.test.ts +52 -0
  44. package/core/test/acps.test.ts +596 -0
  45. package/core/test/apiError.test.ts +130 -0
  46. package/core/test/client.test.ts +22 -19
  47. package/core/test/config.test.ts +25 -5
  48. package/core/test/decrypt.test.ts +40 -35
  49. package/core/test/decryptBuilders.test.ts +68 -68
  50. package/core/test/decryptErrorCodes.test.ts +217 -0
  51. package/core/test/encryptInputsBuilder.test.ts +72 -41
  52. package/core/test/pollCallbacks.test.ts +89 -18
  53. package/core/test/stagingRedirect.ts +18 -0
  54. package/core/test/submitRetry.test.ts +182 -0
  55. package/core/types.ts +9 -69
  56. package/dist/acp-Wi6isVQI.d.cts +407 -0
  57. package/dist/acp-Wi6isVQI.d.ts +407 -0
  58. package/dist/acps.cjs +1081 -0
  59. package/dist/acps.d.cts +480 -0
  60. package/dist/acps.d.ts +480 -0
  61. package/dist/acps.js +2 -0
  62. package/dist/chains.cjs +14 -1
  63. package/dist/chains.d.cts +30 -1
  64. package/dist/chains.d.ts +30 -1
  65. package/dist/chains.js +1 -1
  66. package/dist/{chunk-PE5V5CCV.js → chunk-43USWPEH.js} +1000 -604
  67. package/dist/{chunk-MTRAXQXC.js → chunk-N6IDQRRU.js} +14 -2
  68. package/dist/chunk-Q7CBWGQX.js +1029 -0
  69. package/dist/{clientTypes-CyUvRRzA.d.ts → clientTypes-BN3nbzYM.d.ts} +342 -165
  70. package/dist/{clientTypes-BDy1qIBu.d.cts → clientTypes-CYZjFznO.d.cts} +342 -165
  71. package/dist/core.cjs +1431 -1026
  72. package/dist/core.d.cts +55 -10
  73. package/dist/core.d.ts +55 -10
  74. package/dist/core.js +3 -3
  75. package/dist/node.cjs +1356 -976
  76. package/dist/node.d.cts +2 -2
  77. package/dist/node.d.ts +2 -2
  78. package/dist/node.js +3 -3
  79. package/dist/web.cjs +1356 -976
  80. package/dist/web.d.cts +2 -2
  81. package/dist/web.d.ts +2 -2
  82. package/dist/web.js +3 -3
  83. package/node/test/inherited.test.ts +75 -65
  84. package/node/test/tfheinit.test.ts +23 -8
  85. package/package.json +6 -6
  86. package/web/test/client.web.test.ts +5 -1
  87. package/web/test/inherited.web.test.ts +75 -65
  88. package/web/test/tfheinit.web.test.ts +14 -5
  89. package/web/test/worker.config.web.test.ts +38 -23
  90. package/web/test/worker.output.web.test.ts +25 -24
  91. package/core/encrypt/encryptUtils.ts +0 -67
  92. package/core/permits.ts +0 -206
  93. package/core/test/permits.test.ts +0 -534
  94. package/dist/chunk-VB62WYPL.js +0 -978
  95. package/dist/permit-DnVMDT5h.d.cts +0 -376
  96. package/dist/permit-DnVMDT5h.d.ts +0 -376
  97. package/dist/permits.cjs +0 -1026
  98. package/dist/permits.d.cts +0 -353
  99. package/dist/permits.d.ts +0 -353
  100. package/dist/permits.js +0 -2
  101. package/permits/index.ts +0 -68
  102. package/permits/permit.ts +0 -385
  103. package/permits/sealing.ts +0 -131
  104. package/permits/signature.ts +0 -79
  105. package/permits/store.ts +0 -157
  106. package/permits/test/localstorage.test.ts +0 -113
  107. package/permits/test/permit.test.ts +0 -557
  108. package/permits/test/sealing.test.ts +0 -84
  109. package/permits/test/store.test.ts +0 -88
  110. package/permits/test/validation.test.ts +0 -361
  111. package/permits/test-utils.ts +0 -28
  112. package/permits/types.ts +0 -204
  113. package/permits/validation.ts +0 -327
  114. /package/{permits → acps}/utils.ts +0 -0
package/permits/permit.ts DELETED
@@ -1,385 +0,0 @@
1
- import { keccak256, toHex, zeroAddress, parseAbi, type PublicClient, type WalletClient } from 'viem';
2
- import {
3
- type Permit,
4
- type SelfPermit,
5
- type SharingPermit,
6
- type RecipientPermit,
7
- type CreateSelfPermitOptions,
8
- type CreateSharingPermitOptions,
9
- type ImportSharedPermitOptions,
10
- type SerializedPermit,
11
- type EIP712Domain,
12
- type Permission,
13
- type EthEncryptedData,
14
- type PermitHashFields,
15
- } from './types.js';
16
- import {
17
- validateSelfPermitOptions,
18
- validateSharingPermitOptions,
19
- validateImportPermitOptions,
20
- validateSelfPermit,
21
- validateSharingPermit,
22
- validateImportPermit,
23
- ValidationUtils,
24
- } from './validation.js';
25
- import { SignatureUtils } from './signature.js';
26
- import { GenerateSealingKey, SealingKey } from './sealing.js';
27
- import { checkPermitValidityOnChain, getAclEIP712Domain } from './onchain-utils.js';
28
-
29
- /**
30
- * Main Permit utilities - functional approach for React compatibility
31
- */
32
- export const PermitUtils = {
33
- /**
34
- * Create a self permit for personal use
35
- */
36
- createSelf: (options: CreateSelfPermitOptions): SelfPermit => {
37
- const validation = validateSelfPermitOptions(options);
38
-
39
- // Always generate a new sealing key - users cannot provide their own
40
- const sealingPair = GenerateSealingKey();
41
-
42
- const permit = {
43
- hash: PermitUtils.getHash(validation),
44
- ...validation,
45
- sealingPair,
46
- _signedDomain: undefined,
47
- } satisfies SelfPermit;
48
-
49
- return permit;
50
- },
51
-
52
- /**
53
- * Create a sharing permit to be shared with another user
54
- */
55
- createSharing: (options: CreateSharingPermitOptions): SharingPermit => {
56
- const validation = validateSharingPermitOptions(options);
57
-
58
- // Always generate a new sealing key - users cannot provide their own
59
- const sealingPair = GenerateSealingKey();
60
-
61
- const permit = {
62
- hash: PermitUtils.getHash(validation),
63
- ...validation,
64
- sealingPair,
65
- _signedDomain: undefined,
66
- } satisfies SharingPermit;
67
-
68
- return permit;
69
- },
70
-
71
- /**
72
- * Import a shared permit from various input formats
73
- */
74
- importShared: (options: ImportSharedPermitOptions | string): RecipientPermit => {
75
- let parsedOptions: ImportSharedPermitOptions;
76
-
77
- // Handle different input types
78
- if (typeof options === 'string') {
79
- // Parse JSON string
80
- try {
81
- parsedOptions = JSON.parse(options);
82
- } catch (error) {
83
- throw new Error(`Failed to parse JSON string: ${error}`);
84
- }
85
- } else if (typeof options === 'object' && options !== null) {
86
- // Handle both ImportSharedPermitOptions and any object
87
- parsedOptions = options;
88
- } else {
89
- throw new Error('Invalid input type, expected ImportSharedPermitOptions, object, or string');
90
- }
91
-
92
- // Validate type if provided
93
- if (parsedOptions.type != null && parsedOptions.type !== 'sharing') {
94
- throw new Error(`Invalid permit type <${parsedOptions.type}>, must be "sharing"`);
95
- }
96
-
97
- const validation = validateImportPermitOptions({ ...parsedOptions, type: 'recipient' });
98
-
99
- // Always generate a new sealing key - users cannot provide their own
100
- const sealingPair = GenerateSealingKey();
101
-
102
- const permit = {
103
- hash: PermitUtils.getHash(validation),
104
- ...validation,
105
- sealingPair,
106
- _signedDomain: undefined,
107
- } satisfies RecipientPermit;
108
-
109
- return permit;
110
- },
111
-
112
- /**
113
- * Sign a permit with the provided wallet client
114
- */
115
- sign: async <T extends Permit>(permit: T, publicClient: PublicClient, walletClient: WalletClient): Promise<T> => {
116
- if (walletClient == null || walletClient.account == null) {
117
- throw new Error(
118
- 'Missing walletClient, you must pass in a `walletClient` for the connected user to create a permit signature'
119
- );
120
- }
121
-
122
- const primaryType = SignatureUtils.getPrimaryType(permit.type);
123
- const domain = await getAclEIP712Domain(publicClient);
124
- const { types, message } = SignatureUtils.getSignatureParams(PermitUtils.getPermission(permit, true), primaryType);
125
-
126
- const signature = await walletClient.signTypedData({
127
- domain,
128
- types,
129
- primaryType,
130
- message,
131
- account: walletClient.account,
132
- });
133
-
134
- let updatedPermit: Permit;
135
- if (permit.type === 'self' || permit.type === 'sharing') {
136
- updatedPermit = {
137
- ...permit,
138
- issuerSignature: signature,
139
- _signedDomain: domain,
140
- };
141
- } else {
142
- updatedPermit = {
143
- ...permit,
144
- recipientSignature: signature,
145
- _signedDomain: domain,
146
- };
147
- }
148
-
149
- return updatedPermit as T;
150
- },
151
-
152
- /**
153
- * Create and sign a self permit in one operation
154
- */
155
- createSelfAndSign: async (
156
- options: CreateSelfPermitOptions,
157
- publicClient: PublicClient,
158
- walletClient: WalletClient
159
- ): Promise<SelfPermit> => {
160
- const permit = PermitUtils.createSelf(options);
161
- return PermitUtils.sign(permit, publicClient, walletClient);
162
- },
163
-
164
- /**
165
- * Create and sign a sharing permit in one operation
166
- */
167
- createSharingAndSign: async (
168
- options: CreateSharingPermitOptions,
169
- publicClient: PublicClient,
170
- walletClient: WalletClient
171
- ): Promise<SharingPermit> => {
172
- const permit = PermitUtils.createSharing(options);
173
- return PermitUtils.sign(permit, publicClient, walletClient);
174
- },
175
-
176
- /**
177
- * Import and sign a shared permit in one operation from various input formats
178
- */
179
- importSharedAndSign: async (
180
- options: ImportSharedPermitOptions | string,
181
- publicClient: PublicClient,
182
- walletClient: WalletClient
183
- ): Promise<RecipientPermit> => {
184
- const permit = PermitUtils.importShared(options);
185
- return PermitUtils.sign(permit, publicClient, walletClient);
186
- },
187
-
188
- /**
189
- * Deserialize a permit from serialized data
190
- */
191
- deserialize: (data: SerializedPermit): Permit => {
192
- return {
193
- ...data,
194
- sealingPair: SealingKey.deserialize(data.sealingPair.privateKey, data.sealingPair.publicKey),
195
- };
196
- },
197
-
198
- /**
199
- * Serialize a permit for storage
200
- */
201
- serialize: (permit: Permit): SerializedPermit => {
202
- return {
203
- hash: permit.hash,
204
- name: permit.name,
205
- type: permit.type,
206
- issuer: permit.issuer,
207
- expiration: permit.expiration,
208
- recipient: permit.recipient,
209
- validatorId: permit.validatorId,
210
- validatorContract: permit.validatorContract,
211
- issuerSignature: permit.issuerSignature,
212
- recipientSignature: permit.recipientSignature,
213
- _signedDomain: permit._signedDomain,
214
- sealingPair: permit.sealingPair.serialize(),
215
- };
216
- },
217
-
218
- /**
219
- * Validate a permit (schema-level validation)
220
- */
221
- validateSchema: (permit: Permit) => {
222
- if (permit.type === 'self') {
223
- return validateSelfPermit(permit);
224
- } else if (permit.type === 'sharing') {
225
- return validateSharingPermit(permit);
226
- } else if (permit.type === 'recipient') {
227
- return validateImportPermit(permit);
228
- } else {
229
- throw new Error('Invalid permit type');
230
- }
231
- },
232
-
233
- /**
234
- * Validate a permit (holistic validation).
235
- *
236
- * This validates:
237
- * - Permit schema (shape + invariants)
238
- * - Permit is signed
239
- * - Permit is not expired
240
- *
241
- * For schema-only validation, use `validateSchema(permit)`.
242
- */
243
- validate: (permit: Permit) => {
244
- const validated = PermitUtils.validateSchema(permit);
245
- ValidationUtils.assertSignedAndNotExpired(validated as Permit);
246
- return validated;
247
- },
248
-
249
- /**
250
- * Get the permission object from a permit (for use in contracts)
251
- */
252
- getPermission: (permit: Permit, skipValidation = false): Permission => {
253
- if (!skipValidation) {
254
- PermitUtils.validateSchema(permit);
255
- }
256
-
257
- return {
258
- issuer: permit.issuer,
259
- expiration: permit.expiration,
260
- recipient: permit.recipient,
261
- validatorId: permit.validatorId,
262
- validatorContract: permit.validatorContract,
263
- sealingKey: `0x${permit.sealingPair.publicKey}`,
264
- issuerSignature: permit.issuerSignature,
265
- recipientSignature: permit.recipientSignature,
266
- };
267
- },
268
-
269
- /**
270
- * Get a stable hash for the permit (used as key in storage)
271
- */
272
- getHash: (permit: PermitHashFields): string => {
273
- const data = JSON.stringify({
274
- type: permit.type,
275
- issuer: permit.issuer,
276
- expiration: permit.expiration,
277
- recipient: permit.recipient,
278
- validatorId: permit.validatorId,
279
- validatorContract: permit.validatorContract,
280
- });
281
- return keccak256(toHex(data));
282
- },
283
-
284
- /**
285
- * Export permit data for sharing (removes sensitive fields)
286
- */
287
- export: (permit: Permit): string => {
288
- const cleanedPermit: Record<string, unknown> = {
289
- name: permit.name,
290
- type: permit.type,
291
- issuer: permit.issuer,
292
- expiration: permit.expiration,
293
- };
294
-
295
- if (permit.recipient !== zeroAddress) cleanedPermit.recipient = permit.recipient;
296
- if (permit.validatorId !== 0) cleanedPermit.validatorId = permit.validatorId;
297
- if (permit.validatorContract !== zeroAddress) cleanedPermit.validatorContract = permit.validatorContract;
298
- if (permit.type === 'sharing' && permit.issuerSignature !== '0x')
299
- cleanedPermit.issuerSignature = permit.issuerSignature;
300
-
301
- return JSON.stringify(cleanedPermit, undefined, 2);
302
- },
303
-
304
- /**
305
- * Unseal encrypted data using the permit's sealing key
306
- */
307
- unseal: (permit: Permit, ciphertext: EthEncryptedData): bigint => {
308
- return permit.sealingPair.unseal(ciphertext);
309
- },
310
-
311
- /**
312
- * Check if permit is expired
313
- */
314
- isExpired: (permit: Permit): boolean => {
315
- return ValidationUtils.isExpired(permit);
316
- },
317
-
318
- /**
319
- * Check if permit is signed
320
- */
321
- isSigned: (permit: Permit): boolean => {
322
- return ValidationUtils.isSigned(permit);
323
- },
324
-
325
- /**
326
- * Check if permit is signed and not expired
327
- */
328
- isSignedAndNotExpired: (permit: Permit) => {
329
- return ValidationUtils.isSignedAndNotExpired(permit);
330
- },
331
-
332
- /**
333
- * Assert that permit is signed and not expired
334
- */
335
- assertSignedAndNotExpired: (permit: Permit): void => {
336
- return ValidationUtils.assertSignedAndNotExpired(permit);
337
- },
338
-
339
- isValid: (permit: Permit) => {
340
- return ValidationUtils.isValid(permit);
341
- },
342
-
343
- /**
344
- * Update permit name (returns new permit instance)
345
- */
346
- updateName: (permit: Permit, name: string): Permit => {
347
- return { ...permit, name };
348
- },
349
-
350
- /**
351
- * Fetch EIP712 domain from the blockchain
352
- */
353
- fetchEIP712Domain: async (publicClient: PublicClient): Promise<EIP712Domain> => {
354
- return getAclEIP712Domain(publicClient);
355
- },
356
-
357
- /**
358
- * Check if permit's signed domain matches the provided domain
359
- */
360
- matchesDomain: (permit: Permit, domain: EIP712Domain): boolean => {
361
- return (
362
- permit._signedDomain?.name === domain.name &&
363
- permit._signedDomain?.version === domain.version &&
364
- permit._signedDomain?.verifyingContract === domain.verifyingContract &&
365
- permit._signedDomain?.chainId === domain.chainId
366
- );
367
- },
368
-
369
- /**
370
- * Check if permit's signed domain is valid for the current chain
371
- */
372
- checkSignedDomainValid: async (permit: Permit, publicClient: PublicClient): Promise<boolean> => {
373
- if (permit._signedDomain == null) return false;
374
- const domain = await getAclEIP712Domain(publicClient);
375
- return PermitUtils.matchesDomain(permit, domain);
376
- },
377
-
378
- /**
379
- * Check if permit passes the on-chain validation
380
- */
381
- checkValidityOnChain: async (permit: Permit, publicClient: PublicClient): Promise<boolean> => {
382
- const permission = PermitUtils.getPermission(permit);
383
- return checkPermitValidityOnChain(permission, publicClient);
384
- },
385
- };
@@ -1,131 +0,0 @@
1
- import nacl from 'tweetnacl';
2
- import { fromHexString, toBeArray, toBigInt, toHexString, isBigIntOrNumber, isString } from './utils.js';
3
-
4
- const PRIVATE_KEY_LENGTH = 64;
5
- const PUBLIC_KEY_LENGTH = 64;
6
-
7
- export type EthEncryptedData = {
8
- data: Uint8Array;
9
- public_key: Uint8Array;
10
- nonce: Uint8Array;
11
- };
12
-
13
- /**
14
- * A class representing a SealingKey which provides cryptographic sealing (encryption)
15
- * and unsealing (decryption) capabilities.
16
- */
17
- export class SealingKey {
18
- /**
19
- * The private key used for decryption.
20
- */
21
- privateKey: string;
22
- /**
23
- * The public key used for encryption.
24
- */
25
- publicKey: string;
26
-
27
- /**
28
- * Constructs a SealingKey instance with the given private and public keys.
29
- *
30
- * @param {string} privateKey - The private key used for decryption.
31
- * @param {string} publicKey - The public key used for encryption.
32
- * @throws Will throw an error if the provided keys lengths do not match
33
- * the required lengths for private and public keys.
34
- */
35
- constructor(privateKey: string, publicKey: string) {
36
- if (privateKey.length !== PRIVATE_KEY_LENGTH) {
37
- throw new Error(`Private key must be of length ${PRIVATE_KEY_LENGTH}`);
38
- }
39
-
40
- if (publicKey.length !== PUBLIC_KEY_LENGTH) {
41
- throw new Error(`Public key must be of length ${PUBLIC_KEY_LENGTH}`);
42
- }
43
-
44
- this.privateKey = privateKey;
45
- this.publicKey = publicKey;
46
- }
47
-
48
- unseal = (parsedData: EthEncryptedData): bigint => {
49
- // Ensure all parameters are Uint8Array
50
- const nonce = parsedData.nonce instanceof Uint8Array ? parsedData.nonce : new Uint8Array(parsedData.nonce);
51
-
52
- const ephemPublicKey =
53
- parsedData.public_key instanceof Uint8Array ? parsedData.public_key : new Uint8Array(parsedData.public_key);
54
-
55
- const dataToDecrypt = parsedData.data instanceof Uint8Array ? parsedData.data : new Uint8Array(parsedData.data);
56
-
57
- // Make sure the private key is also a Uint8Array
58
- const privateKeyBytes = fromHexString(this.privateKey);
59
-
60
- // Debug information
61
- // console.log("nonce length:", nonce.length);
62
- // console.log("ephemPublicKey length:", ephemPublicKey.length);
63
- // console.log("privateKeyBytes length:", privateKeyBytes.length);
64
- // console.log("dataToDecrypt length:", dataToDecrypt.length);
65
-
66
- // call the nacl box function to decrypt the data
67
- const decryptedMessage = nacl.box.open(dataToDecrypt, nonce, ephemPublicKey, privateKeyBytes);
68
-
69
- if (!decryptedMessage) {
70
- throw new Error('Failed to decrypt message');
71
- }
72
-
73
- return toBigInt(decryptedMessage);
74
- };
75
-
76
- /**
77
- * Serializes the SealingKey to a JSON object.
78
- */
79
- serialize = () => {
80
- return {
81
- privateKey: this.privateKey,
82
- publicKey: this.publicKey,
83
- };
84
- };
85
-
86
- /**
87
- * Deserializes the SealingKey from a JSON object.
88
- */
89
- static deserialize = (privateKey: string, publicKey: string): SealingKey => {
90
- return new SealingKey(privateKey, publicKey);
91
- };
92
-
93
- /**
94
- * Seals (encrypts) the provided message for a receiver with the specified public key.
95
- *
96
- * @param {bigint | number} value - The message to be encrypted.
97
- * @param {string} publicKey - The public key of the intended recipient.
98
- * @returns string - The encrypted message in hexadecimal format.
99
- * @static
100
- * @throws Will throw if the provided publicKey or value do not meet defined preconditions.
101
- */
102
- static seal = (value: bigint | number, publicKey: string): EthEncryptedData => {
103
- isString(publicKey);
104
- isBigIntOrNumber(value);
105
-
106
- // generate ephemeral keypair
107
- const ephemeralKeyPair = nacl.box.keyPair();
108
-
109
- const nonce = nacl.randomBytes(nacl.box.nonceLength);
110
-
111
- const encryptedMessage = nacl.box(toBeArray(value), nonce, fromHexString(publicKey), ephemeralKeyPair.secretKey);
112
-
113
- return {
114
- data: encryptedMessage,
115
- public_key: ephemeralKeyPair.publicKey,
116
- nonce: nonce,
117
- };
118
- };
119
- }
120
-
121
- /**
122
- * Asynchronously generates a new SealingKey.
123
- * This function uses the 'nacl' library to create a new public/private key pair for sealing purposes.
124
- * A sealing key is used to encrypt data such that it can only be unsealed (decrypted) by the owner of the corresponding private key.
125
- * @returns {SealingKey} - A new SealingKey object containing the hexadecimal strings of the public and private keys.
126
- */
127
- export const GenerateSealingKey = (): SealingKey => {
128
- const sodiumKeypair = nacl.box.keyPair();
129
-
130
- return new SealingKey(toHexString(sodiumKeypair.secretKey), toHexString(sodiumKeypair.publicKey));
131
- };
@@ -1,79 +0,0 @@
1
- import { type EIP712Message, type EIP712Types, type Permission, type PermitSignaturePrimaryType } from './types.js';
2
-
3
- const PermitSignatureAllFields = [
4
- { name: 'issuer', type: 'address' },
5
- { name: 'expiration', type: 'uint64' },
6
- { name: 'recipient', type: 'address' },
7
- { name: 'validatorId', type: 'uint256' },
8
- { name: 'validatorContract', type: 'address' },
9
- { name: 'sealingKey', type: 'bytes32' },
10
- { name: 'issuerSignature', type: 'bytes' },
11
- ] as const;
12
-
13
- type PermitSignatureFieldOption = (typeof PermitSignatureAllFields)[number]['name'];
14
-
15
- export const SignatureTypes = {
16
- PermissionedV2IssuerSelf: [
17
- 'issuer',
18
- 'expiration',
19
- 'recipient',
20
- 'validatorId',
21
- 'validatorContract',
22
- 'sealingKey',
23
- ] satisfies PermitSignatureFieldOption[],
24
- PermissionedV2IssuerShared: [
25
- 'issuer',
26
- 'expiration',
27
- 'recipient',
28
- 'validatorId',
29
- 'validatorContract',
30
- ] satisfies PermitSignatureFieldOption[],
31
- PermissionedV2Recipient: ['sealingKey', 'issuerSignature'] satisfies PermitSignatureFieldOption[],
32
- } as const;
33
-
34
- /**
35
- * Get signature types and message for EIP712 signing
36
- */
37
- export const getSignatureTypesAndMessage = <T extends PermitSignatureFieldOption>(
38
- primaryType: PermitSignaturePrimaryType,
39
- fields: T[] | readonly T[],
40
- values: Pick<Permission, T> & Partial<Permission>
41
- ): { types: EIP712Types; primaryType: string; message: EIP712Message } => {
42
- const types = {
43
- [primaryType]: PermitSignatureAllFields.filter((fieldType) => fields.includes(fieldType.name as T)),
44
- };
45
-
46
- const message: Record<T, string | string[] | number | number[]> = {} as Record<
47
- T,
48
- string | string[] | number | number[]
49
- >;
50
- fields.forEach((field) => {
51
- if (field in values) {
52
- message[field] = values[field];
53
- }
54
- });
55
-
56
- return { types, primaryType, message: message as EIP712Message };
57
- };
58
-
59
- /**
60
- * Signature utilities for permit operations
61
- */
62
- export const SignatureUtils = {
63
- /**
64
- * Get signature parameters for a permit
65
- */
66
- getSignatureParams: (permit: Permission, primaryType: PermitSignaturePrimaryType) => {
67
- return getSignatureTypesAndMessage(primaryType, SignatureTypes[primaryType], permit);
68
- },
69
-
70
- /**
71
- * Determine the required signature type based on permit type
72
- */
73
- getPrimaryType: (permitType: 'self' | 'sharing' | 'recipient'): PermitSignaturePrimaryType => {
74
- if (permitType === 'self') return 'PermissionedV2IssuerSelf';
75
- if (permitType === 'sharing') return 'PermissionedV2IssuerShared';
76
- if (permitType === 'recipient') return 'PermissionedV2Recipient';
77
- throw new Error(`Unknown permit type: ${permitType}`);
78
- },
79
- };