@cofhe/sdk 0.2.1 → 0.3.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.
- package/CHANGELOG.md +34 -0
- package/core/baseBuilder.ts +18 -18
- package/core/client.test.ts +58 -55
- package/core/client.ts +50 -30
- package/core/clientTypes.ts +21 -17
- package/core/config.test.ts +32 -33
- package/core/config.ts +47 -48
- package/core/consts.ts +6 -2
- package/core/decrypt/{MockQueryDecrypterAbi.ts → MockThresholdNetworkAbi.ts} +71 -21
- package/core/decrypt/cofheMocksDecryptForTx.ts +142 -0
- package/core/decrypt/{cofheMocksSealOutput.ts → cofheMocksDecryptForView.ts} +12 -12
- 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 +15 -15
- 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 +14 -11
- package/core/utils.ts +10 -10
- package/dist/{chunk-I5WFEYXX.js → chunk-2TPSCOW3.js} +791 -209
- package/dist/{chunk-R3B5TMVX.js → chunk-NWDKXBIP.js} +3 -2
- package/dist/{clientTypes-RqkgkV2i.d.ts → clientTypes-6aTZPQ_4.d.ts} +204 -85
- package/dist/{clientTypes-e4filDzK.d.cts → clientTypes-Bhq7pCSA.d.cts} +204 -85
- package/dist/core.cjs +799 -214
- package/dist/core.d.cts +25 -23
- package/dist/core.d.ts +25 -23
- package/dist/core.js +2 -2
- package/dist/node.cjs +748 -165
- package/dist/node.d.cts +10 -10
- package/dist/node.d.ts +10 -10
- package/dist/node.js +7 -7
- package/dist/permits.js +1 -1
- package/dist/web.cjs +751 -168
- package/dist/web.d.cts +11 -11
- package/dist/web.d.ts +11 -11
- package/dist/web.js +9 -9
- 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/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/web/client.web.test.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { arbSepolia as
|
|
1
|
+
import { CofheErrorCode, CofheError, type CofheClient } 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 { arbitrumSepolia as viemArbitrumSepolia } from 'viem/chains';
|
|
6
6
|
import type { PublicClient, WalletClient } from 'viem';
|
|
7
7
|
import { createPublicClient, createWalletClient, http } from 'viem';
|
|
8
8
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
9
|
-
import {
|
|
9
|
+
import { createCofheClient, createCofheConfig } from './index.js';
|
|
10
10
|
|
|
11
11
|
// Real test setup - runs in browser
|
|
12
12
|
const TEST_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
|
|
13
13
|
const TEST_ACCOUNT = privateKeyToAccount(TEST_PRIVATE_KEY).address;
|
|
14
14
|
|
|
15
15
|
describe('@cofhe/web - Client', () => {
|
|
16
|
-
let
|
|
16
|
+
let cofheClient: CofheClient;
|
|
17
17
|
let publicClient: PublicClient;
|
|
18
18
|
let walletClient: WalletClient;
|
|
19
19
|
|
|
@@ -33,62 +33,62 @@ describe('@cofhe/web - Client', () => {
|
|
|
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('Browser Client Initialization', () => {
|
|
43
43
|
it('should create a client with real tfhe for browser', () => {
|
|
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 IndexedDB 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('web');
|
|
66
66
|
});
|
|
67
67
|
});
|
|
68
68
|
|
|
69
69
|
describe('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 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/web - Client', () => {
|
|
|
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/web - Client', () => {
|
|
|
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/web/config.web.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/web - Config', () => {
|
|
7
|
-
describe('
|
|
7
|
+
describe('createCofheConfig', () => {
|
|
8
8
|
it('should automatically inject IndexedDB storage as default', () => {
|
|
9
|
-
const config =
|
|
9
|
+
const config = createCofheConfig({
|
|
10
10
|
supportedChains: [arbSepolia],
|
|
11
11
|
});
|
|
12
12
|
|
|
@@ -22,7 +22,7 @@ describe('@cofhe/web - Config', () => {
|
|
|
22
22
|
removeItem: () => Promise.resolve(),
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
const config =
|
|
25
|
+
const config = createCofheConfig({
|
|
26
26
|
supportedChains: [arbSepolia],
|
|
27
27
|
fheKeyStorage: customStorage,
|
|
28
28
|
});
|
|
@@ -31,7 +31,7 @@ describe('@cofhe/web - Config', () => {
|
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
it('should allow null storage', () => {
|
|
34
|
-
const config =
|
|
34
|
+
const config = createCofheConfig({
|
|
35
35
|
supportedChains: [arbSepolia],
|
|
36
36
|
fheKeyStorage: null,
|
|
37
37
|
});
|
|
@@ -40,26 +40,26 @@ describe('@cofhe/web - Config', () => {
|
|
|
40
40
|
});
|
|
41
41
|
|
|
42
42
|
it('should preserve all other config options', () => {
|
|
43
|
-
const config =
|
|
43
|
+
const config = createCofheConfig({
|
|
44
44
|
supportedChains: [arbSepolia],
|
|
45
45
|
mocks: {
|
|
46
|
-
|
|
46
|
+
decryptDelay: 500,
|
|
47
47
|
},
|
|
48
48
|
});
|
|
49
49
|
|
|
50
50
|
expect(config.supportedChains).toEqual([arbSepolia]);
|
|
51
|
-
expect(config.mocks.
|
|
51
|
+
expect(config.mocks.decryptDelay).toBe(500);
|
|
52
52
|
expect(config.fheKeyStorage).toBeDefined();
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
-
describe('
|
|
56
|
+
describe('createCofheClient with config', () => {
|
|
57
57
|
it('should create client with validated config', () => {
|
|
58
|
-
const config =
|
|
58
|
+
const config = createCofheConfig({
|
|
59
59
|
supportedChains: [arbSepolia],
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
-
const client =
|
|
62
|
+
const client = createCofheClient(config);
|
|
63
63
|
|
|
64
64
|
expect(client).toBeDefined();
|
|
65
65
|
expect(client.config).toBe(config);
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { arbSepolia as
|
|
2
|
-
import { Encryptable, FheTypes, type
|
|
1
|
+
import { arbSepolia as cofheArbSepolia } from '@/chains';
|
|
2
|
+
import { Encryptable, FheTypes, type CofheClient, CofheErrorCode, CofheError } from '@/core';
|
|
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 - runs in browser with real tfhe
|
|
12
12
|
const TEST_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
|
|
13
13
|
|
|
14
14
|
describe('@cofhe/web - Encrypt Inputs Browser Tests', () => {
|
|
15
|
-
let
|
|
15
|
+
let cofheClient: CofheClient;
|
|
16
16
|
let publicClient: PublicClient;
|
|
17
17
|
let walletClient: WalletClient;
|
|
18
18
|
|
|
@@ -32,39 +32,39 @@ describe('@cofhe/web - Encrypt Inputs Browser Tests', () => {
|
|
|
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('Browser TFHE Initialization', () => {
|
|
42
42
|
it('should initialize tfhe on first encryption', async () => {
|
|
43
|
-
await
|
|
43
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
44
44
|
|
|
45
45
|
// This will trigger real TFHE initialization in browser
|
|
46
|
-
const result = await
|
|
46
|
+
const result = await cofheClient.encryptInputs([Encryptable.uint128(100n)]).execute();
|
|
47
47
|
|
|
48
48
|
// If we get here, TFHE was initialized successfully
|
|
49
49
|
expect(result).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
|
-
expect(
|
|
56
|
+
expect(cofheClient.encryptInputs([Encryptable.uint128(100n)]).execute()).resolves.not.toThrow();
|
|
57
57
|
|
|
58
58
|
// Second encryption should reuse initialization
|
|
59
|
-
expect(
|
|
59
|
+
expect(cofheClient.encryptInputs([Encryptable.uint64(50n)]).execute()).resolves.not.toThrow();
|
|
60
60
|
}, 60000);
|
|
61
61
|
});
|
|
62
62
|
|
|
63
63
|
describe('Browser Encryption', () => {
|
|
64
64
|
it('should encrypt a bool with real TFHE in browser', async () => {
|
|
65
|
-
await
|
|
65
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
66
66
|
|
|
67
|
-
const result = await
|
|
67
|
+
const result = await cofheClient.encryptInputs([Encryptable.bool(true)]).execute();
|
|
68
68
|
|
|
69
69
|
expect(result).toBeDefined();
|
|
70
70
|
expect(result.length).toBe(1);
|
|
@@ -77,7 +77,7 @@ describe('@cofhe/web - Encrypt Inputs Browser Tests', () => {
|
|
|
77
77
|
}, 60000);
|
|
78
78
|
|
|
79
79
|
it('should encrypt all supported types together', async () => {
|
|
80
|
-
await
|
|
80
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
81
81
|
|
|
82
82
|
const inputs = [
|
|
83
83
|
Encryptable.bool(false),
|
|
@@ -89,7 +89,7 @@ describe('@cofhe/web - Encrypt Inputs Browser Tests', () => {
|
|
|
89
89
|
Encryptable.address('0x742d35Cc6634C0532925a3b844D16faC4c175E99'),
|
|
90
90
|
];
|
|
91
91
|
|
|
92
|
-
const result = await
|
|
92
|
+
const result = await cofheClient.encryptInputs(inputs).execute();
|
|
93
93
|
expect(result).toBeDefined();
|
|
94
94
|
|
|
95
95
|
expect(result.length).toBe(7);
|
|
@@ -106,15 +106,15 @@ describe('@cofhe/web - Encrypt Inputs Browser Tests', () => {
|
|
|
106
106
|
|
|
107
107
|
describe('Browser Builder Pattern', () => {
|
|
108
108
|
it('should support chaining builder methods with real encryption', async () => {
|
|
109
|
-
await
|
|
109
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
110
110
|
|
|
111
|
-
const snapshot =
|
|
112
|
-
const encrypted = await
|
|
111
|
+
const snapshot = cofheClient.getSnapshot();
|
|
112
|
+
const encrypted = await cofheClient
|
|
113
113
|
.encryptInputs([Encryptable.uint128(100n)])
|
|
114
114
|
.setChainId(snapshot.chainId!)
|
|
115
115
|
.setAccount(snapshot.account!)
|
|
116
116
|
.setSecurityZone(0)
|
|
117
|
-
.
|
|
117
|
+
.execute();
|
|
118
118
|
|
|
119
119
|
expect(encrypted.length).toBe(1);
|
|
120
120
|
expect(encrypted[0].utype).toBe(FheTypes.Uint128);
|
|
@@ -125,41 +125,41 @@ describe('@cofhe/web - Encrypt Inputs Browser Tests', () => {
|
|
|
125
125
|
it('should fail gracefully when not connected', async () => {
|
|
126
126
|
// Don't connect the client
|
|
127
127
|
try {
|
|
128
|
-
const promise =
|
|
128
|
+
const promise = cofheClient.encryptInputs([Encryptable.uint128(100n)]).execute();
|
|
129
129
|
} catch (error) {
|
|
130
|
-
expect(error).toBeInstanceOf(
|
|
131
|
-
expect((error as
|
|
130
|
+
expect(error).toBeInstanceOf(CofheError);
|
|
131
|
+
expect((error as CofheError).code).toBe(CofheErrorCode.NotConnected);
|
|
132
132
|
}
|
|
133
133
|
}, 30000);
|
|
134
134
|
|
|
135
135
|
it('should handle invalid CoFHE URL', async () => {
|
|
136
|
-
const badConfig =
|
|
136
|
+
const badConfig = createCofheConfig({
|
|
137
137
|
supportedChains: [
|
|
138
138
|
{
|
|
139
|
-
...
|
|
139
|
+
...cofheArbSepolia,
|
|
140
140
|
coFheUrl: 'http://invalid-cofhe-url.local',
|
|
141
141
|
verifierUrl: 'http://invalid-verifier-url.local',
|
|
142
142
|
},
|
|
143
143
|
],
|
|
144
144
|
});
|
|
145
145
|
|
|
146
|
-
const badClient =
|
|
146
|
+
const badClient = createCofheClient(badConfig);
|
|
147
147
|
await badClient.connect(publicClient, walletClient);
|
|
148
148
|
|
|
149
|
-
const promise = badClient.encryptInputs([Encryptable.uint128(100n)]).
|
|
149
|
+
const promise = badClient.encryptInputs([Encryptable.uint128(100n)]).execute();
|
|
150
150
|
expect(promise).rejects.toThrow();
|
|
151
151
|
}, 60000);
|
|
152
152
|
});
|
|
153
153
|
|
|
154
154
|
describe('Browser Performance', () => {
|
|
155
155
|
it('should handle consecutive encryptions efficiently', async () => {
|
|
156
|
-
await
|
|
156
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
157
157
|
|
|
158
158
|
const start = Date.now();
|
|
159
159
|
|
|
160
160
|
// Perform 5 encryptions
|
|
161
161
|
for (let i = 0; i < 5; i++) {
|
|
162
|
-
await
|
|
162
|
+
await cofheClient.encryptInputs([Encryptable.uint128(BigInt(i))]).execute();
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
const duration = Date.now() - start;
|
package/web/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// Web 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
|
type EncryptableItem,
|
|
@@ -95,12 +95,12 @@ async function zkProveWithWorker(
|
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
/**
|
|
98
|
-
* Creates a CoFHE
|
|
99
|
-
* @param config - The CoFHE
|
|
100
|
-
* @returns The CoFHE
|
|
98
|
+
* Creates a CoFHE configuration for web with IndexedDB storage as default
|
|
99
|
+
* @param config - The CoFHE input configuration (fheKeyStorage will default to IndexedDB if not provided)
|
|
100
|
+
* @returns The CoFHE configuration with web defaults applied
|
|
101
101
|
*/
|
|
102
|
-
export function
|
|
103
|
-
return
|
|
102
|
+
export function createCofheConfig(config: CofheInputConfig): CofheConfig {
|
|
103
|
+
return createCofheConfigBase({
|
|
104
104
|
environment: 'web',
|
|
105
105
|
...config,
|
|
106
106
|
fheKeyStorage: config.fheKeyStorage === null ? null : config.fheKeyStorage ?? createWebStorage(),
|
|
@@ -108,14 +108,14 @@ export function createCofhesdkConfig(config: CofhesdkInputConfig): CofhesdkConfi
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
|
-
* Creates a CoFHE
|
|
111
|
+
* Creates a CoFHE client instance for web with TFHE automatically configured
|
|
112
112
|
* TFHE will be initialized automatically on first encryption - no manual setup required
|
|
113
113
|
* Workers are automatically enabled if available (can be disabled via config.useWorkers)
|
|
114
|
-
* @param config - The CoFHE
|
|
115
|
-
* @returns The CoFHE
|
|
114
|
+
* @param config - The CoFHE configuration (use createCofheConfig to create with web defaults)
|
|
115
|
+
* @returns The CoFHE client instance
|
|
116
116
|
*/
|
|
117
|
-
export function
|
|
118
|
-
return
|
|
117
|
+
export function createCofheClient<TConfig extends CofheConfig>(config: TConfig): CofheClient<TConfig> {
|
|
118
|
+
return createCofheClientBase({
|
|
119
119
|
config,
|
|
120
120
|
zkBuilderAndCrsGenerator,
|
|
121
121
|
tfhePublicKeyDeserializer,
|
|
@@ -141,16 +141,16 @@ export { areWorkersAvailable };
|
|
|
141
141
|
* Test helper: Create a client with custom worker function (for testing fallback behavior)
|
|
142
142
|
* @internal - Only for testing purposes
|
|
143
143
|
*/
|
|
144
|
-
export function
|
|
145
|
-
config:
|
|
144
|
+
export function createCofheClientWithCustomWorker(
|
|
145
|
+
config: CofheConfig,
|
|
146
146
|
customZkProveWorkerFn: (
|
|
147
147
|
fheKeyHex: string,
|
|
148
148
|
crsHex: string,
|
|
149
149
|
items: EncryptableItem[],
|
|
150
150
|
metadata: Uint8Array
|
|
151
151
|
) => Promise<Uint8Array>
|
|
152
|
-
):
|
|
153
|
-
return
|
|
152
|
+
): CofheClient {
|
|
153
|
+
return createCofheClientBase({
|
|
154
154
|
config,
|
|
155
155
|
zkBuilderAndCrsGenerator,
|
|
156
156
|
tfhePublicKeyDeserializer,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
|
|
2
|
-
import {
|
|
3
|
-
import { Encryptable, type
|
|
4
|
-
import { arbSepolia as
|
|
2
|
+
import { createCofheClient, createCofheConfig } from './index.js';
|
|
3
|
+
import { Encryptable, type CofheClient } from '@/core';
|
|
4
|
+
import { arbSepolia as cofheArbSepolia } from '@/chains';
|
|
5
5
|
import { arbitrumSepolia as viemArbitrumSepolia } from 'viem/chains';
|
|
6
6
|
import { createPublicClient, createWalletClient, http, type PublicClient, type WalletClient } from 'viem';
|
|
7
7
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
@@ -9,7 +9,7 @@ import { privateKeyToAccount } from 'viem/accounts';
|
|
|
9
9
|
const TEST_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
|
|
10
10
|
|
|
11
11
|
describe('@cofhe/sdk/web - EncryptInputsBuilder Worker Methods', () => {
|
|
12
|
-
let
|
|
12
|
+
let cofheClient: CofheClient;
|
|
13
13
|
let publicClient: PublicClient;
|
|
14
14
|
let walletClient: WalletClient;
|
|
15
15
|
|
|
@@ -28,23 +28,23 @@ describe('@cofhe/sdk/web - EncryptInputsBuilder Worker Methods', () => {
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
beforeEach(async () => {
|
|
31
|
-
const config =
|
|
32
|
-
supportedChains: [
|
|
31
|
+
const config = createCofheConfig({
|
|
32
|
+
supportedChains: [cofheArbSepolia],
|
|
33
33
|
});
|
|
34
|
-
|
|
35
|
-
await
|
|
34
|
+
cofheClient = createCofheClient(config);
|
|
35
|
+
await cofheClient.connect(publicClient, walletClient);
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
describe('setUseWorker method', () => {
|
|
39
39
|
it('should have setUseWorker method on EncryptInputsBuilder', () => {
|
|
40
|
-
const builder =
|
|
40
|
+
const builder = cofheClient.encryptInputs([Encryptable.uint128(100n)]);
|
|
41
41
|
|
|
42
42
|
expect(builder).toHaveProperty('setUseWorker');
|
|
43
43
|
expect(typeof builder.setUseWorker).toBe('function');
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
it('should return builder for method chaining', () => {
|
|
47
|
-
const builder =
|
|
47
|
+
const builder = cofheClient.encryptInputs([Encryptable.uint128(100n)]);
|
|
48
48
|
const returnedBuilder = builder.setUseWorker(false);
|
|
49
49
|
|
|
50
50
|
// Should return the same builder instance (or at least same type)
|
|
@@ -53,37 +53,37 @@ describe('@cofhe/sdk/web - EncryptInputsBuilder Worker Methods', () => {
|
|
|
53
53
|
|
|
54
54
|
it('should allow chaining with other builder methods', () => {
|
|
55
55
|
// Should be able to chain setUseWorker with setStepCallback
|
|
56
|
-
const builder =
|
|
56
|
+
const builder = cofheClient
|
|
57
57
|
.encryptInputs([Encryptable.uint128(100n)])
|
|
58
58
|
.setUseWorker(false)
|
|
59
|
-
.
|
|
59
|
+
.onStep(() => {});
|
|
60
60
|
|
|
61
61
|
expect(builder).toBeDefined();
|
|
62
|
-
expect(builder).toHaveProperty('
|
|
62
|
+
expect(builder).toHaveProperty('execute');
|
|
63
63
|
});
|
|
64
64
|
|
|
65
65
|
it('should accept true parameter', () => {
|
|
66
66
|
expect(() => {
|
|
67
|
-
|
|
67
|
+
cofheClient.encryptInputs([Encryptable.uint128(100n)]).setUseWorker(true);
|
|
68
68
|
}).not.toThrow();
|
|
69
69
|
});
|
|
70
70
|
|
|
71
71
|
it('should accept false parameter', () => {
|
|
72
72
|
expect(() => {
|
|
73
|
-
|
|
73
|
+
cofheClient.encryptInputs([Encryptable.uint128(100n)]).setUseWorker(false);
|
|
74
74
|
}).not.toThrow();
|
|
75
75
|
});
|
|
76
76
|
|
|
77
77
|
it('should have getUseWorker method', () => {
|
|
78
|
-
const builder =
|
|
78
|
+
const builder = cofheClient.encryptInputs([Encryptable.uint128(100n)]);
|
|
79
79
|
|
|
80
80
|
expect(builder).toHaveProperty('getUseWorker');
|
|
81
81
|
expect(typeof builder.getUseWorker).toBe('function');
|
|
82
82
|
});
|
|
83
83
|
|
|
84
84
|
it('should return current useWorker value', () => {
|
|
85
|
-
const builderWithWorkers =
|
|
86
|
-
const builderWithoutWorkers =
|
|
85
|
+
const builderWithWorkers = cofheClient.encryptInputs([Encryptable.uint128(100n)]).setUseWorker(true);
|
|
86
|
+
const builderWithoutWorkers = cofheClient.encryptInputs([Encryptable.uint128(100n)]).setUseWorker(false);
|
|
87
87
|
|
|
88
88
|
// Should reflect config values
|
|
89
89
|
expect(builderWithWorkers.getUseWorker()).toBe(true);
|
|
@@ -91,7 +91,7 @@ describe('@cofhe/sdk/web - EncryptInputsBuilder Worker Methods', () => {
|
|
|
91
91
|
});
|
|
92
92
|
|
|
93
93
|
it('should reflect changes from setUseWorker', () => {
|
|
94
|
-
const builder =
|
|
94
|
+
const builder = cofheClient.encryptInputs([Encryptable.uint128(100n)]).setUseWorker(true);
|
|
95
95
|
expect(builder.getUseWorker()).toBe(true);
|
|
96
96
|
|
|
97
97
|
builder.setUseWorker(false);
|
|
@@ -104,23 +104,23 @@ describe('@cofhe/sdk/web - EncryptInputsBuilder Worker Methods', () => {
|
|
|
104
104
|
|
|
105
105
|
describe('Worker function availability', () => {
|
|
106
106
|
it('should initialize client without errors', () => {
|
|
107
|
-
const config =
|
|
108
|
-
supportedChains: [
|
|
107
|
+
const config = createCofheConfig({
|
|
108
|
+
supportedChains: [cofheArbSepolia],
|
|
109
109
|
useWorkers: true,
|
|
110
110
|
});
|
|
111
111
|
|
|
112
112
|
expect(() => {
|
|
113
|
-
|
|
113
|
+
createCofheClient(config);
|
|
114
114
|
}).not.toThrow();
|
|
115
115
|
});
|
|
116
116
|
|
|
117
117
|
it('should handle worker function when workers enabled', async () => {
|
|
118
|
-
const config =
|
|
119
|
-
supportedChains: [
|
|
118
|
+
const config = createCofheConfig({
|
|
119
|
+
supportedChains: [cofheArbSepolia],
|
|
120
120
|
useWorkers: true,
|
|
121
121
|
});
|
|
122
122
|
|
|
123
|
-
const client =
|
|
123
|
+
const client = createCofheClient(config);
|
|
124
124
|
await client.connect(publicClient, walletClient);
|
|
125
125
|
const builder = client.encryptInputs([Encryptable.uint128(100n)]);
|
|
126
126
|
|
|
@@ -131,12 +131,12 @@ describe('@cofhe/sdk/web - EncryptInputsBuilder Worker Methods', () => {
|
|
|
131
131
|
});
|
|
132
132
|
|
|
133
133
|
it('should handle when workers disabled', async () => {
|
|
134
|
-
const config =
|
|
135
|
-
supportedChains: [
|
|
134
|
+
const config = createCofheConfig({
|
|
135
|
+
supportedChains: [cofheArbSepolia],
|
|
136
136
|
useWorkers: false,
|
|
137
137
|
});
|
|
138
138
|
|
|
139
|
-
const client =
|
|
139
|
+
const client = createCofheClient(config);
|
|
140
140
|
await client.connect(publicClient, walletClient);
|
|
141
141
|
const builder = client.encryptInputs([Encryptable.uint128(100n)]);
|
|
142
142
|
|