@cofhe/sdk 0.2.0 → 0.3.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.
- package/CHANGELOG.md +36 -0
- package/chains/defineChain.ts +2 -2
- package/chains/types.ts +3 -3
- package/core/baseBuilder.ts +18 -18
- package/core/client.test.ts +155 -41
- package/core/client.ts +72 -32
- package/core/clientTypes.ts +28 -18
- package/core/config.test.ts +40 -33
- package/core/config.ts +56 -51
- package/core/consts.ts +22 -0
- package/core/decrypt/{MockQueryDecrypterAbi.ts → MockThresholdNetworkAbi.ts} +71 -21
- package/core/decrypt/cofheMocksDecryptForTx.ts +142 -0
- package/core/decrypt/{cofheMocksSealOutput.ts → cofheMocksDecryptForView.ts} +12 -14
- package/core/decrypt/decryptForTxBuilder.ts +340 -0
- package/core/decrypt/{decryptHandleBuilder.ts → decryptForViewBuilder.ts} +75 -42
- package/core/decrypt/tnDecrypt.ts +232 -0
- package/core/decrypt/tnSealOutputV1.ts +5 -5
- package/core/decrypt/tnSealOutputV2.ts +27 -27
- package/core/encrypt/cofheMocksZkVerifySign.ts +19 -26
- package/core/encrypt/encryptInputsBuilder.test.ts +57 -61
- package/core/encrypt/encryptInputsBuilder.ts +65 -42
- package/core/encrypt/zkPackProveVerify.ts +11 -11
- package/core/error.ts +18 -18
- package/core/fetchKeys.test.ts +3 -3
- package/core/fetchKeys.ts +3 -3
- package/core/index.ts +22 -11
- package/core/permits.test.ts +5 -6
- package/core/permits.ts +5 -4
- package/core/utils.ts +10 -10
- package/dist/chains.cjs +4 -7
- package/dist/chains.d.cts +12 -12
- package/dist/chains.d.ts +12 -12
- package/dist/chains.js +1 -1
- package/dist/{chunk-WGCRJCBR.js → chunk-2TPSCOW3.js} +820 -224
- package/dist/{chunk-UGBVZNRT.js → chunk-NWDKXBIP.js} +309 -189
- package/dist/{chunk-WEAZ25JO.js → chunk-TBLR7NNE.js} +4 -7
- package/dist/{clientTypes-5_1nwtUe.d.cts → clientTypes-6aTZPQ_4.d.ts} +233 -173
- package/dist/{clientTypes-Es7fyi65.d.ts → clientTypes-Bhq7pCSA.d.cts} +233 -173
- package/dist/core.cjs +1138 -418
- package/dist/core.d.cts +37 -24
- package/dist/core.d.ts +37 -24
- package/dist/core.js +3 -3
- package/dist/node.cjs +1082 -370
- package/dist/node.d.cts +12 -12
- package/dist/node.d.ts +12 -12
- package/dist/node.js +8 -8
- package/dist/{permit-fUSe6KKq.d.cts → permit-MZ502UBl.d.cts} +30 -33
- package/dist/{permit-fUSe6KKq.d.ts → permit-MZ502UBl.d.ts} +30 -33
- package/dist/permits.cjs +305 -187
- package/dist/permits.d.cts +111 -812
- package/dist/permits.d.ts +111 -812
- package/dist/permits.js +1 -1
- package/dist/types-YiAC4gig.d.cts +33 -0
- package/dist/types-YiAC4gig.d.ts +33 -0
- package/dist/web.cjs +1085 -373
- package/dist/web.d.cts +13 -13
- package/dist/web.d.ts +13 -13
- package/dist/web.js +10 -10
- package/node/client.test.ts +34 -34
- package/node/config.test.ts +11 -11
- package/node/encryptInputs.test.ts +29 -29
- package/node/index.ts +15 -15
- package/package.json +3 -3
- package/permits/localstorage.test.ts +9 -13
- package/permits/onchain-utils.ts +221 -0
- package/permits/permit.test.ts +51 -5
- package/permits/permit.ts +28 -74
- package/permits/store.test.ts +10 -50
- package/permits/store.ts +4 -14
- package/permits/test-utils.ts +10 -2
- package/permits/types.ts +22 -9
- package/permits/utils.ts +0 -4
- package/permits/validation.test.ts +29 -32
- package/permits/validation.ts +112 -194
- package/web/client.web.test.ts +34 -34
- package/web/config.web.test.ts +11 -11
- package/web/encryptInputs.web.test.ts +29 -29
- package/web/index.ts +19 -19
- package/web/worker.builder.web.test.ts +28 -28
- package/web/worker.config.web.test.ts +47 -47
- package/web/worker.output.web.test.ts +10 -10
- package/dist/types-KImPrEIe.d.cts +0 -48
- package/dist/types-KImPrEIe.d.ts +0 -48
package/dist/web.d.cts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { C as
|
|
1
|
+
import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-Bhq7pCSA.cjs';
|
|
2
2
|
import 'viem';
|
|
3
|
-
import './types-
|
|
3
|
+
import './types-YiAC4gig.cjs';
|
|
4
4
|
import 'zod';
|
|
5
|
-
import './permit-
|
|
5
|
+
import './permit-MZ502UBl.cjs';
|
|
6
6
|
import 'zustand/vanilla';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -15,24 +15,24 @@ declare function terminateWorker(): void;
|
|
|
15
15
|
declare function areWorkersAvailable(): boolean;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* Creates a CoFHE
|
|
19
|
-
* @param config - The CoFHE
|
|
20
|
-
* @returns The CoFHE
|
|
18
|
+
* Creates a CoFHE configuration for web with IndexedDB storage as default
|
|
19
|
+
* @param config - The CoFHE input configuration (fheKeyStorage will default to IndexedDB if not provided)
|
|
20
|
+
* @returns The CoFHE configuration with web defaults applied
|
|
21
21
|
*/
|
|
22
|
-
declare function
|
|
22
|
+
declare function createCofheConfig(config: CofheInputConfig): CofheConfig;
|
|
23
23
|
/**
|
|
24
|
-
* Creates a CoFHE
|
|
24
|
+
* Creates a CoFHE client instance for web with TFHE automatically configured
|
|
25
25
|
* TFHE will be initialized automatically on first encryption - no manual setup required
|
|
26
26
|
* Workers are automatically enabled if available (can be disabled via config.useWorkers)
|
|
27
|
-
* @param config - The CoFHE
|
|
28
|
-
* @returns The CoFHE
|
|
27
|
+
* @param config - The CoFHE configuration (use createCofheConfig to create with web defaults)
|
|
28
|
+
* @returns The CoFHE client instance
|
|
29
29
|
*/
|
|
30
|
-
declare function
|
|
30
|
+
declare function createCofheClient<TConfig extends CofheConfig>(config: TConfig): CofheClient<TConfig>;
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* Test helper: Create a client with custom worker function (for testing fallback behavior)
|
|
34
34
|
* @internal - Only for testing purposes
|
|
35
35
|
*/
|
|
36
|
-
declare function
|
|
36
|
+
declare function createCofheClientWithCustomWorker(config: CofheConfig, customZkProveWorkerFn: (fheKeyHex: string, crsHex: string, items: EncryptableItem[], metadata: Uint8Array) => Promise<Uint8Array>): CofheClient;
|
|
37
37
|
|
|
38
|
-
export { areWorkersAvailable,
|
|
38
|
+
export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, terminateWorker };
|
package/dist/web.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { C as
|
|
1
|
+
import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-6aTZPQ_4.js';
|
|
2
2
|
import 'viem';
|
|
3
|
-
import './types-
|
|
3
|
+
import './types-YiAC4gig.js';
|
|
4
4
|
import 'zod';
|
|
5
|
-
import './permit-
|
|
5
|
+
import './permit-MZ502UBl.js';
|
|
6
6
|
import 'zustand/vanilla';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -15,24 +15,24 @@ declare function terminateWorker(): void;
|
|
|
15
15
|
declare function areWorkersAvailable(): boolean;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* Creates a CoFHE
|
|
19
|
-
* @param config - The CoFHE
|
|
20
|
-
* @returns The CoFHE
|
|
18
|
+
* Creates a CoFHE configuration for web with IndexedDB storage as default
|
|
19
|
+
* @param config - The CoFHE input configuration (fheKeyStorage will default to IndexedDB if not provided)
|
|
20
|
+
* @returns The CoFHE configuration with web defaults applied
|
|
21
21
|
*/
|
|
22
|
-
declare function
|
|
22
|
+
declare function createCofheConfig(config: CofheInputConfig): CofheConfig;
|
|
23
23
|
/**
|
|
24
|
-
* Creates a CoFHE
|
|
24
|
+
* Creates a CoFHE client instance for web with TFHE automatically configured
|
|
25
25
|
* TFHE will be initialized automatically on first encryption - no manual setup required
|
|
26
26
|
* Workers are automatically enabled if available (can be disabled via config.useWorkers)
|
|
27
|
-
* @param config - The CoFHE
|
|
28
|
-
* @returns The CoFHE
|
|
27
|
+
* @param config - The CoFHE configuration (use createCofheConfig to create with web defaults)
|
|
28
|
+
* @returns The CoFHE client instance
|
|
29
29
|
*/
|
|
30
|
-
declare function
|
|
30
|
+
declare function createCofheClient<TConfig extends CofheConfig>(config: TConfig): CofheClient<TConfig>;
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* Test helper: Create a client with custom worker function (for testing fallback behavior)
|
|
34
34
|
* @internal - Only for testing purposes
|
|
35
35
|
*/
|
|
36
|
-
declare function
|
|
36
|
+
declare function createCofheClientWithCustomWorker(config: CofheConfig, customZkProveWorkerFn: (fheKeyHex: string, crsHex: string, items: EncryptableItem[], metadata: Uint8Array) => Promise<Uint8Array>): CofheClient;
|
|
37
37
|
|
|
38
|
-
export { areWorkersAvailable,
|
|
38
|
+
export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, terminateWorker };
|
package/dist/web.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import './chunk-
|
|
3
|
-
import './chunk-
|
|
1
|
+
import { createCofheConfigBase, createCofheClientBase, fheTypeToString } from './chunk-2TPSCOW3.js';
|
|
2
|
+
import './chunk-TBLR7NNE.js';
|
|
3
|
+
import './chunk-NWDKXBIP.js';
|
|
4
4
|
import { constructClient } from 'iframe-shared-storage';
|
|
5
5
|
import init, { init_panic_hook, TfheCompactPublicKey, CompactPkeCrs, ProvenCompactCiphertextList } from 'tfhe';
|
|
6
6
|
|
|
@@ -207,15 +207,15 @@ async function zkProveWithWorker(fheKeyHex, crsHex, items, metadata) {
|
|
|
207
207
|
const workerManager2 = getWorkerManager();
|
|
208
208
|
return await workerManager2.submitProof(fheKeyHex, crsHex, serializedItems, metadata);
|
|
209
209
|
}
|
|
210
|
-
function
|
|
211
|
-
return
|
|
210
|
+
function createCofheConfig(config) {
|
|
211
|
+
return createCofheConfigBase({
|
|
212
212
|
environment: "web",
|
|
213
213
|
...config,
|
|
214
214
|
fheKeyStorage: config.fheKeyStorage === null ? null : config.fheKeyStorage ?? createWebStorage()
|
|
215
215
|
});
|
|
216
216
|
}
|
|
217
|
-
function
|
|
218
|
-
return
|
|
217
|
+
function createCofheClient(config) {
|
|
218
|
+
return createCofheClientBase({
|
|
219
219
|
config,
|
|
220
220
|
zkBuilderAndCrsGenerator,
|
|
221
221
|
tfhePublicKeyDeserializer,
|
|
@@ -226,8 +226,8 @@ function createCofhesdkClient(config) {
|
|
|
226
226
|
zkProveWorkerFn: areWorkersAvailable() ? zkProveWithWorker : void 0
|
|
227
227
|
});
|
|
228
228
|
}
|
|
229
|
-
function
|
|
230
|
-
return
|
|
229
|
+
function createCofheClientWithCustomWorker(config, customZkProveWorkerFn) {
|
|
230
|
+
return createCofheClientBase({
|
|
231
231
|
config,
|
|
232
232
|
zkBuilderAndCrsGenerator,
|
|
233
233
|
tfhePublicKeyDeserializer,
|
|
@@ -237,4 +237,4 @@ function createCofhesdkClientWithCustomWorker(config, customZkProveWorkerFn) {
|
|
|
237
237
|
});
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
-
export { areWorkersAvailable,
|
|
240
|
+
export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, terminateWorker };
|
package/node/client.test.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { arbSepolia as
|
|
1
|
+
import { type CofheClient, CofheError, CofheErrorCode } from '@/core';
|
|
2
|
+
import { arbSepolia as cofheArbSepolia } from '@/chains';
|
|
3
3
|
|
|
4
4
|
import { describe, it, expect, beforeAll, beforeEach, vi } from 'vitest';
|
|
5
5
|
import type { PublicClient, WalletClient } from 'viem';
|
|
6
6
|
import { createPublicClient, createWalletClient, http } from 'viem';
|
|
7
7
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
8
8
|
import { arbitrumSepolia as viemArbitrumSepolia } from 'viem/chains';
|
|
9
|
-
import {
|
|
9
|
+
import { createCofheClient, createCofheConfig } from './index.js';
|
|
10
10
|
|
|
11
11
|
// Real test setup - no mocks
|
|
12
12
|
const TEST_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
|
|
13
13
|
const TEST_ACCOUNT = privateKeyToAccount(TEST_PRIVATE_KEY).address;
|
|
14
14
|
|
|
15
15
|
describe('@cofhe/node - Client Integration Tests', () => {
|
|
16
|
-
let
|
|
16
|
+
let cofheClient: CofheClient;
|
|
17
17
|
let publicClient: PublicClient;
|
|
18
18
|
let walletClient: WalletClient;
|
|
19
19
|
|
|
@@ -33,62 +33,62 @@ describe('@cofhe/node - Client Integration Tests', () => {
|
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
beforeEach(() => {
|
|
36
|
-
const config =
|
|
37
|
-
supportedChains: [
|
|
36
|
+
const config = createCofheConfig({
|
|
37
|
+
supportedChains: [cofheArbSepolia],
|
|
38
38
|
});
|
|
39
|
-
|
|
39
|
+
cofheClient = createCofheClient(config);
|
|
40
40
|
});
|
|
41
41
|
|
|
42
42
|
describe('Real Client Initialization', () => {
|
|
43
43
|
it('should create a client with real node-tfhe', () => {
|
|
44
|
-
expect(
|
|
45
|
-
expect(
|
|
46
|
-
expect(
|
|
44
|
+
expect(cofheClient).toBeDefined();
|
|
45
|
+
expect(cofheClient.config).toBeDefined();
|
|
46
|
+
expect(cofheClient.connected).toBe(false);
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
it('should automatically use filesystem storage as default', () => {
|
|
50
|
-
expect(
|
|
51
|
-
expect(
|
|
50
|
+
expect(cofheClient.config.fheKeyStorage).toBeDefined();
|
|
51
|
+
expect(cofheClient.config.fheKeyStorage).not.toBeNull();
|
|
52
52
|
});
|
|
53
53
|
|
|
54
54
|
it('should have all expected methods', () => {
|
|
55
|
-
expect(typeof
|
|
56
|
-
expect(typeof
|
|
57
|
-
expect(typeof
|
|
58
|
-
expect(typeof
|
|
59
|
-
expect(typeof
|
|
55
|
+
expect(typeof cofheClient.connect).toBe('function');
|
|
56
|
+
expect(typeof cofheClient.encryptInputs).toBe('function');
|
|
57
|
+
expect(typeof cofheClient.decryptForView).toBe('function');
|
|
58
|
+
expect(typeof cofheClient.getSnapshot).toBe('function');
|
|
59
|
+
expect(typeof cofheClient.subscribe).toBe('function');
|
|
60
60
|
});
|
|
61
61
|
});
|
|
62
62
|
|
|
63
63
|
describe('Environment', () => {
|
|
64
64
|
it('should have the correct environment', () => {
|
|
65
|
-
expect(
|
|
65
|
+
expect(cofheClient.config.environment).toBe('node');
|
|
66
66
|
});
|
|
67
67
|
});
|
|
68
68
|
|
|
69
69
|
describe('Real Connection', () => {
|
|
70
70
|
it('should connect to real chain', async () => {
|
|
71
|
-
await
|
|
71
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
72
72
|
|
|
73
|
-
expect(
|
|
73
|
+
expect(cofheClient.connected).toBe(true);
|
|
74
74
|
|
|
75
|
-
const snapshot =
|
|
75
|
+
const snapshot = cofheClient.getSnapshot();
|
|
76
76
|
expect(snapshot.connected).toBe(true);
|
|
77
|
-
expect(snapshot.chainId).toBe(
|
|
77
|
+
expect(snapshot.chainId).toBe(cofheArbSepolia.id);
|
|
78
78
|
expect(snapshot.account).toBe(TEST_ACCOUNT);
|
|
79
79
|
}, 30000);
|
|
80
80
|
|
|
81
81
|
it('should handle real network errors', async () => {
|
|
82
82
|
try {
|
|
83
|
-
await
|
|
83
|
+
await cofheClient.connect(
|
|
84
84
|
{
|
|
85
85
|
getChainId: vi.fn().mockRejectedValue(new Error('Network error')),
|
|
86
86
|
} as unknown as PublicClient,
|
|
87
87
|
walletClient
|
|
88
88
|
);
|
|
89
89
|
} catch (error) {
|
|
90
|
-
expect(error).toBeInstanceOf(
|
|
91
|
-
expect((error as
|
|
90
|
+
expect(error).toBeInstanceOf(CofheError);
|
|
91
|
+
expect((error as CofheError).code).toBe(CofheErrorCode.PublicWalletGetChainIdFailed);
|
|
92
92
|
}
|
|
93
93
|
}, 30000);
|
|
94
94
|
});
|
|
@@ -96,11 +96,11 @@ describe('@cofhe/node - Client Integration Tests', () => {
|
|
|
96
96
|
describe('State Management', () => {
|
|
97
97
|
it('should track connection state changes', async () => {
|
|
98
98
|
const states: any[] = [];
|
|
99
|
-
const unsubscribe =
|
|
99
|
+
const unsubscribe = cofheClient.subscribe((snapshot) => {
|
|
100
100
|
states.push({ ...snapshot });
|
|
101
101
|
});
|
|
102
102
|
|
|
103
|
-
await
|
|
103
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
104
104
|
|
|
105
105
|
unsubscribe();
|
|
106
106
|
|
|
@@ -116,32 +116,32 @@ describe('@cofhe/node - Client Integration Tests', () => {
|
|
|
116
116
|
const lastState = states[states.length - 1];
|
|
117
117
|
expect(lastState.connected).toBe(true);
|
|
118
118
|
expect(lastState.connecting).toBe(false);
|
|
119
|
-
expect(lastState.chainId).toBe(
|
|
119
|
+
expect(lastState.chainId).toBe(cofheArbSepolia.id);
|
|
120
120
|
}, 30000);
|
|
121
121
|
});
|
|
122
122
|
|
|
123
123
|
describe('Builder Creation', () => {
|
|
124
124
|
it('should create encrypt builder after connection', async () => {
|
|
125
|
-
await
|
|
125
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
126
126
|
|
|
127
|
-
const builder =
|
|
127
|
+
const builder = cofheClient.encryptInputs([{ data: 100n, utype: 2, securityZone: 0 }]);
|
|
128
128
|
|
|
129
129
|
expect(builder).toBeDefined();
|
|
130
130
|
expect(typeof builder.setChainId).toBe('function');
|
|
131
131
|
expect(typeof builder.setAccount).toBe('function');
|
|
132
132
|
expect(typeof builder.setSecurityZone).toBe('function');
|
|
133
|
-
expect(typeof builder.
|
|
133
|
+
expect(typeof builder.execute).toBe('function');
|
|
134
134
|
}, 30000);
|
|
135
135
|
|
|
136
136
|
it('should create decrypt builder after connection', async () => {
|
|
137
|
-
await
|
|
137
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
138
138
|
|
|
139
|
-
const builder =
|
|
139
|
+
const builder = cofheClient.decryptForView(123n, 2);
|
|
140
140
|
|
|
141
141
|
expect(builder).toBeDefined();
|
|
142
142
|
expect(typeof builder.setChainId).toBe('function');
|
|
143
143
|
expect(typeof builder.setAccount).toBe('function');
|
|
144
|
-
expect(typeof builder.
|
|
144
|
+
expect(typeof builder.execute).toBe('function');
|
|
145
145
|
}, 30000);
|
|
146
146
|
});
|
|
147
147
|
});
|
package/node/config.test.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { arbSepolia } from '@/chains';
|
|
2
2
|
|
|
3
3
|
import { describe, it, expect } from 'vitest';
|
|
4
|
-
import {
|
|
4
|
+
import { createCofheConfig, createCofheClient } from './index.js';
|
|
5
5
|
|
|
6
6
|
describe('@cofhe/node - Config', () => {
|
|
7
|
-
describe('
|
|
7
|
+
describe('createCofheConfig', () => {
|
|
8
8
|
it('should automatically inject filesystem storage as default', () => {
|
|
9
|
-
const config =
|
|
9
|
+
const config = createCofheConfig({
|
|
10
10
|
supportedChains: [arbSepolia],
|
|
11
11
|
});
|
|
12
12
|
|
|
@@ -21,7 +21,7 @@ describe('@cofhe/node - Config', () => {
|
|
|
21
21
|
setItem: () => Promise.resolve(),
|
|
22
22
|
removeItem: () => Promise.resolve(),
|
|
23
23
|
};
|
|
24
|
-
const config =
|
|
24
|
+
const config = createCofheConfig({
|
|
25
25
|
supportedChains: [arbSepolia],
|
|
26
26
|
fheKeyStorage: customStorage,
|
|
27
27
|
});
|
|
@@ -30,7 +30,7 @@ describe('@cofhe/node - Config', () => {
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
it('should allow null storage', () => {
|
|
33
|
-
const config =
|
|
33
|
+
const config = createCofheConfig({
|
|
34
34
|
supportedChains: [arbSepolia],
|
|
35
35
|
fheKeyStorage: null,
|
|
36
36
|
});
|
|
@@ -39,26 +39,26 @@ describe('@cofhe/node - Config', () => {
|
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
it('should preserve all other config options', () => {
|
|
42
|
-
const config =
|
|
42
|
+
const config = createCofheConfig({
|
|
43
43
|
supportedChains: [arbSepolia],
|
|
44
44
|
mocks: {
|
|
45
|
-
|
|
45
|
+
decryptDelay: 0,
|
|
46
46
|
},
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
expect(config.supportedChains).toEqual([arbSepolia]);
|
|
50
|
-
expect(config.mocks.
|
|
50
|
+
expect(config.mocks.decryptDelay).toBe(0);
|
|
51
51
|
expect(config.fheKeyStorage).toBeDefined();
|
|
52
52
|
});
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
-
describe('
|
|
55
|
+
describe('createCofheClient with config', () => {
|
|
56
56
|
it('should create client with validated config', () => {
|
|
57
|
-
const config =
|
|
57
|
+
const config = createCofheConfig({
|
|
58
58
|
supportedChains: [arbSepolia],
|
|
59
59
|
});
|
|
60
60
|
|
|
61
|
-
const client =
|
|
61
|
+
const client = createCofheClient(config);
|
|
62
62
|
|
|
63
63
|
expect(client).toBeDefined();
|
|
64
64
|
expect(client.config).toBe(config);
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { Encryptable, FheTypes, type
|
|
2
|
-
import { arbSepolia as
|
|
1
|
+
import { Encryptable, FheTypes, type CofheClient, CofheErrorCode, CofheError } from '@/core';
|
|
2
|
+
import { arbSepolia as cofheArbSepolia } from '@/chains';
|
|
3
3
|
|
|
4
4
|
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
|
|
5
5
|
import type { PublicClient, WalletClient } from 'viem';
|
|
6
6
|
import { createPublicClient, createWalletClient, http } from 'viem';
|
|
7
7
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
8
8
|
import { arbitrumSepolia as viemArbitrumSepolia } from 'viem/chains';
|
|
9
|
-
import {
|
|
9
|
+
import { createCofheClient, createCofheConfig } from './index.js';
|
|
10
10
|
|
|
11
11
|
// Real test setup - using actual node-tfhe
|
|
12
12
|
const TEST_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
|
|
13
13
|
|
|
14
14
|
describe('@cofhe/node - Encrypt Inputs', () => {
|
|
15
|
-
let
|
|
15
|
+
let cofheClient: CofheClient;
|
|
16
16
|
let publicClient: PublicClient;
|
|
17
17
|
let walletClient: WalletClient;
|
|
18
18
|
|
|
@@ -32,39 +32,39 @@ describe('@cofhe/node - Encrypt Inputs', () => {
|
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
beforeEach(() => {
|
|
35
|
-
const config =
|
|
36
|
-
supportedChains: [
|
|
35
|
+
const config = createCofheConfig({
|
|
36
|
+
supportedChains: [cofheArbSepolia],
|
|
37
37
|
});
|
|
38
|
-
|
|
38
|
+
cofheClient = createCofheClient(config);
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
describe('Real TFHE Initialization', () => {
|
|
42
42
|
it('should initialize node-tfhe on first encryption', async () => {
|
|
43
|
-
await
|
|
43
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
44
44
|
|
|
45
45
|
// This will trigger real TFHE initialization
|
|
46
|
-
const encrypted = await
|
|
46
|
+
const encrypted = await cofheClient.encryptInputs([Encryptable.uint128(100n)]).execute();
|
|
47
47
|
|
|
48
48
|
// If we get here, TFHE was initialized successfully
|
|
49
49
|
expect(encrypted).toBeDefined();
|
|
50
50
|
}, 60000); // Longer timeout for real operations
|
|
51
51
|
|
|
52
52
|
it('should handle multiple encryptions without re-initializing', async () => {
|
|
53
|
-
await
|
|
53
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
54
54
|
|
|
55
55
|
// First encryption
|
|
56
|
-
await
|
|
56
|
+
await cofheClient.encryptInputs([Encryptable.uint128(100n)]).execute();
|
|
57
57
|
|
|
58
58
|
// Second encryption should reuse initialization
|
|
59
|
-
await
|
|
59
|
+
await cofheClient.encryptInputs([Encryptable.uint64(50n)]).execute();
|
|
60
60
|
}, 120000);
|
|
61
61
|
});
|
|
62
62
|
|
|
63
63
|
describe('Real Encryption', () => {
|
|
64
64
|
it('should encrypt a bool with real TFHE', async () => {
|
|
65
|
-
await
|
|
65
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
66
66
|
|
|
67
|
-
const encrypted = await
|
|
67
|
+
const encrypted = await cofheClient.encryptInputs([Encryptable.bool(true)]).execute();
|
|
68
68
|
|
|
69
69
|
expect(encrypted.length).toBe(1);
|
|
70
70
|
expect(encrypted[0].utype).toBe(FheTypes.Bool);
|
|
@@ -76,7 +76,7 @@ describe('@cofhe/node - Encrypt Inputs', () => {
|
|
|
76
76
|
}, 60000);
|
|
77
77
|
|
|
78
78
|
it('should encrypt all supported types together', async () => {
|
|
79
|
-
await
|
|
79
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
80
80
|
|
|
81
81
|
const inputs = [
|
|
82
82
|
Encryptable.bool(false),
|
|
@@ -88,7 +88,7 @@ describe('@cofhe/node - Encrypt Inputs', () => {
|
|
|
88
88
|
Encryptable.address('0x742d35Cc6634C0532925a3b844D16faC4c175E99'),
|
|
89
89
|
];
|
|
90
90
|
|
|
91
|
-
const encrypted = await
|
|
91
|
+
const encrypted = await cofheClient.encryptInputs(inputs).execute();
|
|
92
92
|
|
|
93
93
|
expect(encrypted.length).toBe(7);
|
|
94
94
|
// Verify each type
|
|
@@ -104,15 +104,15 @@ describe('@cofhe/node - Encrypt Inputs', () => {
|
|
|
104
104
|
|
|
105
105
|
describe('Real Builder Pattern', () => {
|
|
106
106
|
it('should support chaining builder methods with real encryption', async () => {
|
|
107
|
-
await
|
|
107
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
108
108
|
|
|
109
|
-
const snapshot =
|
|
110
|
-
const encrypted = await
|
|
109
|
+
const snapshot = cofheClient.getSnapshot();
|
|
110
|
+
const encrypted = await cofheClient
|
|
111
111
|
.encryptInputs([Encryptable.uint128(100n)])
|
|
112
112
|
.setChainId(snapshot.chainId!)
|
|
113
113
|
.setAccount(snapshot.account!)
|
|
114
114
|
.setSecurityZone(0)
|
|
115
|
-
.
|
|
115
|
+
.execute();
|
|
116
116
|
|
|
117
117
|
expect(encrypted.length).toBe(1);
|
|
118
118
|
expect(encrypted[0].utype).toBe(FheTypes.Uint128);
|
|
@@ -123,32 +123,32 @@ describe('@cofhe/node - Encrypt Inputs', () => {
|
|
|
123
123
|
it('should fail gracefully when not connected', async () => {
|
|
124
124
|
// Don't connect the client
|
|
125
125
|
try {
|
|
126
|
-
await
|
|
126
|
+
await cofheClient.encryptInputs([Encryptable.uint128(100n)]).execute();
|
|
127
127
|
} catch (error) {
|
|
128
|
-
expect(error).toBeInstanceOf(
|
|
129
|
-
expect((error as
|
|
128
|
+
expect(error).toBeInstanceOf(CofheError);
|
|
129
|
+
expect((error as CofheError).code).toBe(CofheErrorCode.NotConnected);
|
|
130
130
|
}
|
|
131
131
|
}, 30000);
|
|
132
132
|
|
|
133
133
|
it('should handle invalid CoFHE URL', async () => {
|
|
134
|
-
const badConfig =
|
|
134
|
+
const badConfig = createCofheConfig({
|
|
135
135
|
supportedChains: [
|
|
136
136
|
{
|
|
137
|
-
...
|
|
137
|
+
...cofheArbSepolia,
|
|
138
138
|
coFheUrl: 'http://invalid-cofhe-url.local',
|
|
139
139
|
verifierUrl: 'http://invalid-verifier-url.local',
|
|
140
140
|
},
|
|
141
141
|
],
|
|
142
142
|
});
|
|
143
143
|
|
|
144
|
-
const badClient =
|
|
144
|
+
const badClient = createCofheClient(badConfig);
|
|
145
145
|
await badClient.connect(publicClient, walletClient);
|
|
146
146
|
|
|
147
147
|
try {
|
|
148
|
-
await badClient.encryptInputs([Encryptable.uint128(100n)]).
|
|
148
|
+
await badClient.encryptInputs([Encryptable.uint128(100n)]).execute();
|
|
149
149
|
} catch (error) {
|
|
150
|
-
expect(error).toBeInstanceOf(
|
|
151
|
-
expect((error as
|
|
150
|
+
expect(error).toBeInstanceOf(CofheError);
|
|
151
|
+
expect((error as CofheError).code).toBe(CofheErrorCode.ZkVerifyFailed);
|
|
152
152
|
}
|
|
153
153
|
}, 60000);
|
|
154
154
|
});
|
package/node/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// Node.js specific functionality only
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
type
|
|
4
|
+
createCofheClientBase,
|
|
5
|
+
createCofheConfigBase,
|
|
6
|
+
type CofheClient,
|
|
7
|
+
type CofheConfig,
|
|
8
|
+
type CofheInputConfig,
|
|
9
9
|
type ZkBuilderAndCrsGenerator,
|
|
10
10
|
type FheKeyDeserializer,
|
|
11
11
|
} from '@/core';
|
|
@@ -68,12 +68,12 @@ const zkBuilderAndCrsGenerator: ZkBuilderAndCrsGenerator = (fhe: string, crs: st
|
|
|
68
68
|
};
|
|
69
69
|
|
|
70
70
|
/**
|
|
71
|
-
* Creates a CoFHE
|
|
72
|
-
* @param config - The CoFHE
|
|
73
|
-
* @returns The CoFHE
|
|
71
|
+
* Creates a CoFHE configuration for Node.js with filesystem storage as default
|
|
72
|
+
* @param config - The CoFHE input configuration (fheKeyStorage will default to filesystem if not provided)
|
|
73
|
+
* @returns The CoFHE configuration with Node.js defaults applied
|
|
74
74
|
*/
|
|
75
|
-
export function
|
|
76
|
-
return
|
|
75
|
+
export function createCofheConfig(config: CofheInputConfig): CofheConfig {
|
|
76
|
+
return createCofheConfigBase({
|
|
77
77
|
environment: 'node',
|
|
78
78
|
...config,
|
|
79
79
|
fheKeyStorage: config.fheKeyStorage === null ? null : config.fheKeyStorage ?? createNodeStorage(),
|
|
@@ -81,13 +81,13 @@ export function createCofhesdkConfig(config: CofhesdkInputConfig): CofhesdkConfi
|
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
|
-
* Creates a CoFHE
|
|
84
|
+
* Creates a CoFHE client instance for Node.js with node-tfhe automatically configured
|
|
85
85
|
* TFHE will be initialized automatically on first encryption - no manual setup required
|
|
86
|
-
* @param config - The CoFHE
|
|
87
|
-
* @returns The CoFHE
|
|
86
|
+
* @param config - The CoFHE configuration (use createCofheConfig to create with Node.js defaults)
|
|
87
|
+
* @returns The CoFHE client instance
|
|
88
88
|
*/
|
|
89
|
-
export function
|
|
90
|
-
return
|
|
89
|
+
export function createCofheClient(config: CofheConfig): CofheClient {
|
|
90
|
+
return createCofheClientBase({
|
|
91
91
|
config,
|
|
92
92
|
zkBuilderAndCrsGenerator,
|
|
93
93
|
tfhePublicKeyDeserializer,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cofhe/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "SDK for Fhenix COFHE coprocessor interaction",
|
|
6
6
|
"main": "./dist/core.cjs",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"tfhe": "0.11.1",
|
|
64
64
|
"tweetnacl": "^1.0.3",
|
|
65
65
|
"viem": "^2.38.6",
|
|
66
|
-
"zod": "^
|
|
66
|
+
"zod": "^4.0.0",
|
|
67
67
|
"zustand": "^5.0.1"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
@@ -111,7 +111,7 @@
|
|
|
111
111
|
"build": "tsup",
|
|
112
112
|
"dev": "tsup --watch",
|
|
113
113
|
"lint": "eslint \"**/*.ts*\"",
|
|
114
|
-
"
|
|
114
|
+
"check:types": "tsc --noEmit",
|
|
115
115
|
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
|
|
116
116
|
"test": "vitest run",
|
|
117
117
|
"test:watch": "vitest",
|