@cofhe/sdk 0.4.0 → 0.5.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 (95) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/adapters/{ethers5.test.ts → test/ethers5.test.ts} +2 -2
  3. package/adapters/{ethers6.test.ts → test/ethers6.test.ts} +2 -2
  4. package/adapters/{hardhat.hh2.test.ts → test/hardhat.hh2.test.ts} +2 -2
  5. package/adapters/{index.test.ts → test/index.test.ts} +1 -1
  6. package/adapters/{wagmi.test.ts → test/wagmi.test.ts} +1 -1
  7. package/chains/{chains.test.ts → test/chains.test.ts} +1 -1
  8. package/core/client.ts +11 -1
  9. package/core/clientTypes.ts +3 -1
  10. package/core/consts.ts +9 -0
  11. package/core/decrypt/cofheMocksDecryptForTx.ts +14 -3
  12. package/core/decrypt/decryptForTxBuilder.ts +16 -2
  13. package/core/decrypt/decryptForViewBuilder.ts +14 -7
  14. package/core/decrypt/polling.ts +14 -0
  15. package/core/decrypt/tnDecryptV2.ts +250 -110
  16. package/core/decrypt/tnSealOutputV2.ts +245 -104
  17. package/core/decrypt/verifyDecryptResult.ts +65 -0
  18. package/core/encrypt/cofheMocksZkVerifySign.ts +6 -6
  19. package/core/encrypt/zkPackProveVerify.ts +10 -19
  20. package/core/fetchKeys.ts +0 -2
  21. package/core/index.ts +9 -1
  22. package/core/keyStore.ts +5 -2
  23. package/core/permits.ts +5 -0
  24. package/core/{client.test.ts → test/client.test.ts} +7 -7
  25. package/core/{config.test.ts → test/config.test.ts} +1 -1
  26. package/core/test/decrypt.test.ts +252 -0
  27. package/core/test/decryptBuilders.test.ts +390 -0
  28. package/core/{encrypt → test}/encryptInputsBuilder.test.ts +61 -6
  29. package/core/{fetchKeys.test.ts → test/fetchKeys.test.ts} +3 -3
  30. package/core/{keyStore.test.ts → test/keyStore.test.ts} +5 -3
  31. package/core/{permits.test.ts → test/permits.test.ts} +42 -1
  32. package/core/test/pollCallbacks.test.ts +563 -0
  33. package/core/types.ts +13 -0
  34. package/dist/chains.d.cts +2 -2
  35. package/dist/chains.d.ts +2 -2
  36. package/dist/chunk-4FP4V35O.js +13 -0
  37. package/dist/{chunk-NWDKXBIP.js → chunk-MRCKUMOS.js} +62 -22
  38. package/dist/{chunk-MXND5SVN.js → chunk-S7OKGLFD.js} +485 -207
  39. package/dist/{clientTypes-kkrRdawm.d.ts → clientTypes-BSbwairE.d.cts} +23 -6
  40. package/dist/{clientTypes-ACVWbrXL.d.cts → clientTypes-DDmcgZ0a.d.ts} +23 -6
  41. package/dist/core.cjs +561 -244
  42. package/dist/core.d.cts +24 -6
  43. package/dist/core.d.ts +24 -6
  44. package/dist/core.js +3 -2
  45. package/dist/node.cjs +566 -246
  46. package/dist/node.d.cts +3 -3
  47. package/dist/node.d.ts +3 -3
  48. package/dist/node.js +14 -7
  49. package/dist/{permit-MZ502UBl.d.cts → permit-DnVMDT5h.d.cts} +34 -4
  50. package/dist/{permit-MZ502UBl.d.ts → permit-DnVMDT5h.d.ts} +34 -4
  51. package/dist/permits.cjs +66 -29
  52. package/dist/permits.d.cts +18 -13
  53. package/dist/permits.d.ts +18 -13
  54. package/dist/permits.js +2 -1
  55. package/dist/web.cjs +604 -256
  56. package/dist/web.d.cts +8 -4
  57. package/dist/web.d.ts +8 -4
  58. package/dist/web.js +49 -14
  59. package/dist/zkProve.worker.cjs +72 -64
  60. package/dist/zkProve.worker.js +71 -64
  61. package/node/index.ts +13 -4
  62. package/node/test/client.test.ts +25 -0
  63. package/node/test/config.test.ts +16 -0
  64. package/node/test/inherited.test.ts +244 -0
  65. package/node/test/tfheinit.test.ts +56 -0
  66. package/package.json +24 -22
  67. package/permits/permit.ts +31 -5
  68. package/permits/sealing.ts +1 -1
  69. package/permits/{localstorage.test.ts → test/localstorage.test.ts} +2 -2
  70. package/permits/{permit.test.ts → test/permit.test.ts} +35 -1
  71. package/permits/{sealing.test.ts → test/sealing.test.ts} +1 -1
  72. package/permits/{store.test.ts → test/store.test.ts} +2 -2
  73. package/permits/{validation.test.ts → test/validation.test.ts} +82 -6
  74. package/permits/types.ts +1 -1
  75. package/permits/validation.ts +42 -2
  76. package/web/const.ts +2 -0
  77. package/web/index.ts +40 -11
  78. package/web/storage.ts +18 -3
  79. package/web/{client.web.test.ts → test/client.web.test.ts} +13 -1
  80. package/web/test/config.web.test.ts +16 -0
  81. package/web/test/inherited.web.test.ts +245 -0
  82. package/web/test/tfheinit.web.test.ts +62 -0
  83. package/web/{worker.config.web.test.ts → test/worker.config.web.test.ts} +1 -1
  84. package/web/{worker.output.web.test.ts → test/worker.output.web.test.ts} +1 -1
  85. package/web/{workerManager.test.ts → test/workerManager.test.ts} +1 -1
  86. package/web/{workerManager.web.test.ts → test/workerManager.web.test.ts} +1 -1
  87. package/web/zkProve.worker.ts +94 -84
  88. package/node/client.test.ts +0 -147
  89. package/node/config.test.ts +0 -68
  90. package/node/encryptInputs.test.ts +0 -155
  91. package/web/config.web.test.ts +0 -69
  92. package/web/encryptInputs.web.test.ts +0 -172
  93. package/web/worker.builder.web.test.ts +0 -148
  94. /package/dist/{types-YiAC4gig.d.cts → types-C07FK-cL.d.cts} +0 -0
  95. /package/dist/{types-YiAC4gig.d.ts → types-C07FK-cL.d.ts} +0 -0
package/dist/web.d.cts CHANGED
@@ -1,10 +1,12 @@
1
- import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-ACVWbrXL.cjs';
1
+ import { I as IStorage, C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-BSbwairE.cjs';
2
2
  import 'viem';
3
- import './types-YiAC4gig.cjs';
3
+ import './types-C07FK-cL.cjs';
4
4
  import 'zod';
5
- import './permit-MZ502UBl.cjs';
5
+ import './permit-DnVMDT5h.cjs';
6
6
  import 'zustand/vanilla';
7
7
 
8
+ declare function createSsrStorage(): IStorage;
9
+
8
10
  /**
9
11
  * Terminate the worker
10
12
  */
@@ -14,6 +16,8 @@ declare function terminateWorker(): void;
14
16
  */
15
17
  declare function areWorkersAvailable(): boolean;
16
18
 
19
+ declare const hasDOM: boolean;
20
+
17
21
  /**
18
22
  * Creates a CoFHE configuration for web with IndexedDB storage as default
19
23
  * @param config - The CoFHE input configuration (fheKeyStorage will default to IndexedDB if not provided)
@@ -35,4 +39,4 @@ declare function createCofheClient<TConfig extends CofheConfig>(config: TConfig)
35
39
  */
36
40
  declare function createCofheClientWithCustomWorker(config: CofheConfig, customZkProveWorkerFn: (fheKeyHex: string, crsHex: string, items: EncryptableItem[], metadata: Uint8Array) => Promise<Uint8Array>): CofheClient;
37
41
 
38
- export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, terminateWorker };
42
+ export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, createSsrStorage, hasDOM, terminateWorker };
package/dist/web.d.ts CHANGED
@@ -1,10 +1,12 @@
1
- import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-kkrRdawm.js';
1
+ import { I as IStorage, C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-DDmcgZ0a.js';
2
2
  import 'viem';
3
- import './types-YiAC4gig.js';
3
+ import './types-C07FK-cL.js';
4
4
  import 'zod';
5
- import './permit-MZ502UBl.js';
5
+ import './permit-DnVMDT5h.js';
6
6
  import 'zustand/vanilla';
7
7
 
8
+ declare function createSsrStorage(): IStorage;
9
+
8
10
  /**
9
11
  * Terminate the worker
10
12
  */
@@ -14,6 +16,8 @@ declare function terminateWorker(): void;
14
16
  */
15
17
  declare function areWorkersAvailable(): boolean;
16
18
 
19
+ declare const hasDOM: boolean;
20
+
17
21
  /**
18
22
  * Creates a CoFHE configuration for web with IndexedDB storage as default
19
23
  * @param config - The CoFHE input configuration (fheKeyStorage will default to IndexedDB if not provided)
@@ -35,4 +39,4 @@ declare function createCofheClient<TConfig extends CofheConfig>(config: TConfig)
35
39
  */
36
40
  declare function createCofheClientWithCustomWorker(config: CofheConfig, customZkProveWorkerFn: (fheKeyHex: string, crsHex: string, items: EncryptableItem[], metadata: Uint8Array) => Promise<Uint8Array>): CofheClient;
37
41
 
38
- export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, terminateWorker };
42
+ export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, createSsrStorage, hasDOM, terminateWorker };
package/dist/web.js CHANGED
@@ -1,15 +1,21 @@
1
- import { createCofheConfigBase, createCofheClientBase, fheTypeToString } from './chunk-MXND5SVN.js';
1
+ import { createCofheConfigBase, createCofheClientBase, fheTypeToString } from './chunk-S7OKGLFD.js';
2
2
  import './chunk-TBLR7NNE.js';
3
- import './chunk-NWDKXBIP.js';
3
+ import './chunk-MRCKUMOS.js';
4
+ import { TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT } from './chunk-4FP4V35O.js';
4
5
  import { constructClient } from 'iframe-shared-storage';
5
- import init, { init_panic_hook, TfheCompactPublicKey, CompactPkeCrs, ProvenCompactCiphertextList } from 'tfhe';
6
6
 
7
- var createWebStorage = () => {
7
+ // web/const.ts
8
+ var hasDOM = typeof globalThis?.document !== "undefined" && typeof globalThis?.window !== "undefined";
9
+
10
+ // web/storage.ts
11
+ var createWebStorage = (opts = { enableLog: false }) => {
12
+ if (!hasDOM)
13
+ throw new Error("createWebStorage can only be used in a browser environment");
8
14
  const client = constructClient({
9
15
  iframe: {
10
16
  src: "https://iframe-shared-storage.vercel.app/hub.html",
11
17
  messagingOptions: {
12
- enableLog: "both"
18
+ enableLog: opts.enableLog ? "both" : void 0
13
19
  },
14
20
  iframeReadyTimeoutMs: 3e4,
15
21
  // if the iframe is not initied during this interval AND a reuqest is made, such request will throw an error
@@ -32,6 +38,16 @@ var createWebStorage = () => {
32
38
  }
33
39
  };
34
40
  };
41
+ function createSsrStorage() {
42
+ console.warn("using no-op server-side SSR storage");
43
+ return {
44
+ getItem: async () => null,
45
+ setItem: async () => {
46
+ },
47
+ removeItem: async () => {
48
+ }
49
+ };
50
+ }
35
51
 
36
52
  // web/workerManager.ts
37
53
  var ZkProveWorkerManager = class {
@@ -171,15 +187,25 @@ function terminateWorker() {
171
187
  function areWorkersAvailable() {
172
188
  return typeof Worker !== "undefined";
173
189
  }
190
+
191
+ // web/index.ts
192
+ var tfheModule = null;
174
193
  var tfheInitialized = false;
175
194
  async function initTfhe() {
176
195
  if (tfheInitialized)
177
196
  return false;
178
- await init();
179
- await init_panic_hook();
197
+ tfheModule = await import('tfhe');
198
+ await tfheModule.default();
199
+ await tfheModule.init_panic_hook();
180
200
  tfheInitialized = true;
181
201
  return true;
182
202
  }
203
+ function requireTfhe() {
204
+ if (!tfheModule) {
205
+ throw new Error("TFHE not initialized \u2014 call initTfhe() (or any client method that triggers it) first");
206
+ }
207
+ return tfheModule;
208
+ }
183
209
  var fromHexString = (hexString) => {
184
210
  const cleanString = hexString.length % 2 === 1 ? `0${hexString}` : hexString;
185
211
  const arr = cleanString.replace(/^0x/, "").match(/.{1,2}/g);
@@ -187,16 +213,25 @@ var fromHexString = (hexString) => {
187
213
  return new Uint8Array();
188
214
  return new Uint8Array(arr.map((byte) => parseInt(byte, 16)));
189
215
  };
216
+ var _deserializeTfhePublicKey = (buff) => {
217
+ return requireTfhe().TfheCompactPublicKey.safe_deserialize(
218
+ fromHexString(buff),
219
+ TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT
220
+ );
221
+ };
222
+ var _deserializeCompactPkeCrs = (buff) => {
223
+ return requireTfhe().CompactPkeCrs.safe_deserialize(fromHexString(buff), TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
224
+ };
190
225
  var tfhePublicKeyDeserializer = (buff) => {
191
- TfheCompactPublicKey.deserialize(fromHexString(buff));
226
+ _deserializeTfhePublicKey(buff);
192
227
  };
193
228
  var compactPkeCrsDeserializer = (buff) => {
194
- CompactPkeCrs.deserialize(fromHexString(buff));
229
+ _deserializeCompactPkeCrs(buff);
195
230
  };
196
231
  var zkBuilderAndCrsGenerator = (fhe, crs) => {
197
- const fhePublicKey = TfheCompactPublicKey.deserialize(fromHexString(fhe));
198
- const zkBuilder = ProvenCompactCiphertextList.builder(fhePublicKey);
199
- const zkCrs = CompactPkeCrs.deserialize(fromHexString(crs));
232
+ const fhePublicKey = _deserializeTfhePublicKey(fhe);
233
+ const zkBuilder = requireTfhe().ProvenCompactCiphertextList.builder(fhePublicKey);
234
+ const zkCrs = _deserializeCompactPkeCrs(crs);
200
235
  return { zkBuilder, zkCrs };
201
236
  };
202
237
  async function zkProveWithWorker(fheKeyHex, crsHex, items, metadata) {
@@ -211,7 +246,7 @@ function createCofheConfig(config) {
211
246
  return createCofheConfigBase({
212
247
  environment: "web",
213
248
  ...config,
214
- fheKeyStorage: config.fheKeyStorage === null ? null : config.fheKeyStorage ?? createWebStorage()
249
+ fheKeyStorage: config.fheKeyStorage === null ? null : config.fheKeyStorage ?? (hasDOM ? createWebStorage() : createSsrStorage())
215
250
  });
216
251
  }
217
252
  function createCofheClient(config) {
@@ -237,4 +272,4 @@ function createCofheClientWithCustomWorker(config, customZkProveWorkerFn) {
237
272
  });
238
273
  }
239
274
 
240
- export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, terminateWorker };
275
+ export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, createSsrStorage, hasDOM, terminateWorker };
@@ -1,5 +1,8 @@
1
1
  'use strict';
2
2
 
3
+ // core/consts.ts
4
+ var TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT = BigInt(1 << 30);
5
+
3
6
  // web/zkProve.worker.ts
4
7
  var tfheModule = null;
5
8
  var initialized = false;
@@ -24,70 +27,75 @@ function fromHexString(hexString) {
24
27
  return new Uint8Array();
25
28
  return new Uint8Array(arr.map((byte) => parseInt(byte, 16)));
26
29
  }
27
- self.onmessage = async (event) => {
28
- const { id, type, fheKeyHex, crsHex, items, metadata } = event.data;
29
- if (type !== "zkProve") {
30
- self.postMessage({
31
- id,
32
- type: "error",
33
- error: "Invalid message type"
34
- });
35
- return;
36
- }
37
- try {
38
- await initTfhe();
39
- if (!tfheModule) {
40
- throw new Error("TFHE module not initialized");
30
+ if (typeof self !== "undefined") {
31
+ self.onmessage = async (event) => {
32
+ const { id, type, fheKeyHex, crsHex, items, metadata } = event.data;
33
+ if (type !== "zkProve") {
34
+ self.postMessage({
35
+ id,
36
+ type: "error",
37
+ error: "Invalid message type"
38
+ });
39
+ return;
41
40
  }
42
- const fheKeyBytes = fromHexString(fheKeyHex);
43
- const crsBytes = fromHexString(crsHex);
44
- const fheKey = tfheModule.TfheCompactPublicKey.deserialize(fheKeyBytes);
45
- const crs = tfheModule.CompactPkeCrs.deserialize(crsBytes);
46
- const builder = tfheModule.ProvenCompactCiphertextList.builder(fheKey);
47
- for (const item of items) {
48
- switch (item.utype) {
49
- case "bool":
50
- builder.push_boolean(Boolean(item.data));
51
- break;
52
- case "uint8":
53
- builder.push_u8(Number(item.data));
54
- break;
55
- case "uint16":
56
- builder.push_u16(Number(item.data));
57
- break;
58
- case "uint32":
59
- builder.push_u32(Number(item.data));
60
- break;
61
- case "uint64":
62
- builder.push_u64(BigInt(item.data));
63
- break;
64
- case "uint128":
65
- builder.push_u128(BigInt(item.data));
66
- break;
67
- case "uint160":
68
- builder.push_u160(BigInt(item.data));
69
- break;
70
- default:
71
- throw new Error(`Unsupported type: ${item.utype}`);
41
+ try {
42
+ await initTfhe();
43
+ if (!tfheModule) {
44
+ throw new Error("TFHE module not initialized");
72
45
  }
46
+ const fheKeyBytes = fromHexString(fheKeyHex);
47
+ const crsBytes = fromHexString(crsHex);
48
+ const fheKey = tfheModule.TfheCompactPublicKey.safe_deserialize(
49
+ fheKeyBytes,
50
+ TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT
51
+ );
52
+ const crs = tfheModule.CompactPkeCrs.safe_deserialize(crsBytes, TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
53
+ const builder = tfheModule.ProvenCompactCiphertextList.builder(fheKey);
54
+ for (const item of items) {
55
+ switch (item.utype) {
56
+ case "bool":
57
+ builder.push_boolean(Boolean(item.data));
58
+ break;
59
+ case "uint8":
60
+ builder.push_u8(Number(item.data));
61
+ break;
62
+ case "uint16":
63
+ builder.push_u16(Number(item.data));
64
+ break;
65
+ case "uint32":
66
+ builder.push_u32(Number(item.data));
67
+ break;
68
+ case "uint64":
69
+ builder.push_u64(BigInt(item.data));
70
+ break;
71
+ case "uint128":
72
+ builder.push_u128(BigInt(item.data));
73
+ break;
74
+ case "uint160":
75
+ builder.push_u160(BigInt(item.data));
76
+ break;
77
+ default:
78
+ throw new Error(`Unsupported type: ${item.utype}`);
79
+ }
80
+ }
81
+ const metadataBytes = new Uint8Array(metadata);
82
+ const compactList = builder.build_with_proof_packed(crs, metadataBytes, 1);
83
+ const result = compactList.safe_serialize(TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
84
+ self.postMessage({
85
+ id,
86
+ type: "success",
87
+ result: Array.from(result)
88
+ });
89
+ } catch (error) {
90
+ self.postMessage({
91
+ id,
92
+ type: "error",
93
+ error: error instanceof Error ? error.message : String(error)
94
+ });
73
95
  }
74
- const metadataBytes = new Uint8Array(metadata);
75
- const compactList = builder.build_with_proof_packed(crs, metadataBytes, 1);
76
- const result = compactList.serialize();
77
- self.postMessage({
78
- id,
79
- type: "success",
80
- result: Array.from(result)
81
- });
82
- } catch (error) {
83
- self.postMessage({
84
- id,
85
- type: "error",
86
- error: error instanceof Error ? error.message : String(error)
87
- });
88
- }
89
- };
90
- self.postMessage({
91
- id: "init",
92
- type: "ready"
93
- });
96
+ };
97
+ self.postMessage({
98
+ id: "init",
99
+ type: "ready"
100
+ });
101
+ }
@@ -1,3 +1,5 @@
1
+ import { TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT } from './chunk-4FP4V35O.js';
2
+
1
3
  // web/zkProve.worker.ts
2
4
  var tfheModule = null;
3
5
  var initialized = false;
@@ -22,70 +24,75 @@ function fromHexString(hexString) {
22
24
  return new Uint8Array();
23
25
  return new Uint8Array(arr.map((byte) => parseInt(byte, 16)));
24
26
  }
25
- self.onmessage = async (event) => {
26
- const { id, type, fheKeyHex, crsHex, items, metadata } = event.data;
27
- if (type !== "zkProve") {
28
- self.postMessage({
29
- id,
30
- type: "error",
31
- error: "Invalid message type"
32
- });
33
- return;
34
- }
35
- try {
36
- await initTfhe();
37
- if (!tfheModule) {
38
- throw new Error("TFHE module not initialized");
27
+ if (typeof self !== "undefined") {
28
+ self.onmessage = async (event) => {
29
+ const { id, type, fheKeyHex, crsHex, items, metadata } = event.data;
30
+ if (type !== "zkProve") {
31
+ self.postMessage({
32
+ id,
33
+ type: "error",
34
+ error: "Invalid message type"
35
+ });
36
+ return;
39
37
  }
40
- const fheKeyBytes = fromHexString(fheKeyHex);
41
- const crsBytes = fromHexString(crsHex);
42
- const fheKey = tfheModule.TfheCompactPublicKey.deserialize(fheKeyBytes);
43
- const crs = tfheModule.CompactPkeCrs.deserialize(crsBytes);
44
- const builder = tfheModule.ProvenCompactCiphertextList.builder(fheKey);
45
- for (const item of items) {
46
- switch (item.utype) {
47
- case "bool":
48
- builder.push_boolean(Boolean(item.data));
49
- break;
50
- case "uint8":
51
- builder.push_u8(Number(item.data));
52
- break;
53
- case "uint16":
54
- builder.push_u16(Number(item.data));
55
- break;
56
- case "uint32":
57
- builder.push_u32(Number(item.data));
58
- break;
59
- case "uint64":
60
- builder.push_u64(BigInt(item.data));
61
- break;
62
- case "uint128":
63
- builder.push_u128(BigInt(item.data));
64
- break;
65
- case "uint160":
66
- builder.push_u160(BigInt(item.data));
67
- break;
68
- default:
69
- throw new Error(`Unsupported type: ${item.utype}`);
38
+ try {
39
+ await initTfhe();
40
+ if (!tfheModule) {
41
+ throw new Error("TFHE module not initialized");
42
+ }
43
+ const fheKeyBytes = fromHexString(fheKeyHex);
44
+ const crsBytes = fromHexString(crsHex);
45
+ const fheKey = tfheModule.TfheCompactPublicKey.safe_deserialize(
46
+ fheKeyBytes,
47
+ TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT
48
+ );
49
+ const crs = tfheModule.CompactPkeCrs.safe_deserialize(crsBytes, TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
50
+ const builder = tfheModule.ProvenCompactCiphertextList.builder(fheKey);
51
+ for (const item of items) {
52
+ switch (item.utype) {
53
+ case "bool":
54
+ builder.push_boolean(Boolean(item.data));
55
+ break;
56
+ case "uint8":
57
+ builder.push_u8(Number(item.data));
58
+ break;
59
+ case "uint16":
60
+ builder.push_u16(Number(item.data));
61
+ break;
62
+ case "uint32":
63
+ builder.push_u32(Number(item.data));
64
+ break;
65
+ case "uint64":
66
+ builder.push_u64(BigInt(item.data));
67
+ break;
68
+ case "uint128":
69
+ builder.push_u128(BigInt(item.data));
70
+ break;
71
+ case "uint160":
72
+ builder.push_u160(BigInt(item.data));
73
+ break;
74
+ default:
75
+ throw new Error(`Unsupported type: ${item.utype}`);
76
+ }
70
77
  }
78
+ const metadataBytes = new Uint8Array(metadata);
79
+ const compactList = builder.build_with_proof_packed(crs, metadataBytes, 1);
80
+ const result = compactList.safe_serialize(TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
81
+ self.postMessage({
82
+ id,
83
+ type: "success",
84
+ result: Array.from(result)
85
+ });
86
+ } catch (error) {
87
+ self.postMessage({
88
+ id,
89
+ type: "error",
90
+ error: error instanceof Error ? error.message : String(error)
91
+ });
71
92
  }
72
- const metadataBytes = new Uint8Array(metadata);
73
- const compactList = builder.build_with_proof_packed(crs, metadataBytes, 1);
74
- const result = compactList.serialize();
75
- self.postMessage({
76
- id,
77
- type: "success",
78
- result: Array.from(result)
79
- });
80
- } catch (error) {
81
- self.postMessage({
82
- id,
83
- type: "error",
84
- error: error instanceof Error ? error.message : String(error)
85
- });
86
- }
87
- };
88
- self.postMessage({
89
- id: "init",
90
- type: "ready"
91
- });
93
+ };
94
+ self.postMessage({
95
+ id: "init",
96
+ type: "ready"
97
+ });
98
+ }
package/node/index.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  type CofheInputConfig,
9
9
  type ZkBuilderAndCrsGenerator,
10
10
  type FheKeyDeserializer,
11
+ TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT,
11
12
  } from '@/core';
12
13
 
13
14
  // Import node-specific storage (internal use only)
@@ -39,12 +40,20 @@ const fromHexString = (hexString: string): Uint8Array => {
39
40
  return new Uint8Array(arr.map((byte) => parseInt(byte, 16)));
40
41
  };
41
42
 
43
+ const _deserializeTfhePublicKey = (buff: string): TfheCompactPublicKey => {
44
+ return TfheCompactPublicKey.safe_deserialize(fromHexString(buff), TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
45
+ };
46
+
47
+ const _deserializeCompactPkeCrs = (buff: string): CompactPkeCrs => {
48
+ return CompactPkeCrs.safe_deserialize(fromHexString(buff), TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
49
+ };
50
+
42
51
  /**
43
52
  * Serializer for TFHE public keys
44
53
  * Validates that the buffer can be deserialized into a TfheCompactPublicKey
45
54
  */
46
55
  const tfhePublicKeyDeserializer: FheKeyDeserializer = (buff: string): void => {
47
- TfheCompactPublicKey.deserialize(fromHexString(buff));
56
+ _deserializeTfhePublicKey(buff);
48
57
  };
49
58
 
50
59
  /**
@@ -52,7 +61,7 @@ const tfhePublicKeyDeserializer: FheKeyDeserializer = (buff: string): void => {
52
61
  * Validates that the buffer can be deserialized into ZkCompactPkePublicParams
53
62
  */
54
63
  const compactPkeCrsDeserializer: FheKeyDeserializer = (buff: string): void => {
55
- CompactPkeCrs.deserialize(fromHexString(buff));
64
+ _deserializeCompactPkeCrs(buff);
56
65
  };
57
66
 
58
67
  /**
@@ -60,9 +69,9 @@ const compactPkeCrsDeserializer: FheKeyDeserializer = (buff: string): void => {
60
69
  * This is used internally by the SDK to create encrypted inputs
61
70
  */
62
71
  const zkBuilderAndCrsGenerator: ZkBuilderAndCrsGenerator = (fhe: string, crs: string) => {
63
- const fhePublicKey = TfheCompactPublicKey.deserialize(fromHexString(fhe));
72
+ const fhePublicKey = _deserializeTfhePublicKey(fhe);
64
73
  const zkBuilder = ProvenCompactCiphertextList.builder(fhePublicKey);
65
- const zkCrs = CompactPkeCrs.deserialize(fromHexString(crs));
74
+ const zkCrs = _deserializeCompactPkeCrs(crs);
66
75
 
67
76
  return { zkBuilder, zkCrs };
68
77
  };
@@ -0,0 +1,25 @@
1
+ import { type CofheClient } from '@/core';
2
+ import { arbSepolia as cofheArbSepolia } from '@/chains';
3
+
4
+ import { describe, it, expect, beforeEach } from 'vitest';
5
+ import { createCofheClient, createCofheConfig } from '../index.js';
6
+
7
+ describe('@cofhe/node - Client', () => {
8
+ let cofheClient: CofheClient;
9
+
10
+ beforeEach(() => {
11
+ const config = createCofheConfig({
12
+ supportedChains: [cofheArbSepolia],
13
+ });
14
+ cofheClient = createCofheClient(config);
15
+ });
16
+
17
+ it('should automatically use filesystem storage as default', () => {
18
+ expect(cofheClient.config.fheKeyStorage).toBeDefined();
19
+ expect(cofheClient.config.fheKeyStorage).not.toBeNull();
20
+ });
21
+
22
+ it('should have the correct environment', () => {
23
+ expect(cofheClient.config.environment).toBe('node');
24
+ });
25
+ });
@@ -0,0 +1,16 @@
1
+ import { arbSepolia } from '@/chains';
2
+
3
+ import { describe, it, expect } from 'vitest';
4
+ import { createCofheConfig } from '../index.js';
5
+
6
+ describe('@cofhe/node - Config', () => {
7
+ it('should automatically inject filesystem storage as default', () => {
8
+ const config = createCofheConfig({
9
+ supportedChains: [arbSepolia],
10
+ });
11
+
12
+ expect(config.fheKeyStorage).toBeDefined();
13
+ expect(config.fheKeyStorage).not.toBeNull();
14
+ expect(config.supportedChains).toEqual([arbSepolia]);
15
+ });
16
+ });