@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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
# @cofhe/sdk Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 35024b6: Remove `sdk` from function names and exported types. Rename:
|
|
8
|
+
|
|
9
|
+
- `createCofhesdkConfig` -> `createCofheConfig`
|
|
10
|
+
- `createCofhesdkClient` -> `createCofheClient`
|
|
11
|
+
- `hre.cofhesdk.*` -> `hre.cofhe.*`
|
|
12
|
+
- `hre.cofhesdk.createCofheConfig()` → `hre.cofhe.createConfig()`
|
|
13
|
+
- `hre.cofhesdk.createCofheClient()` → `hre.cofhe.createClient()`
|
|
14
|
+
- `hre.cofhesdk.createBatteriesIncludedCofheClient()` → `hre.cofhe.createClientWithBatteries()`
|
|
15
|
+
|
|
16
|
+
- 29c2401: implement decrypt-with-proof flows and related tests:
|
|
17
|
+
|
|
18
|
+
- Implement production `decryptForTx` backed by Threshold Network `POST /decrypt`, with explicit permit vs global-allowance selection.
|
|
19
|
+
- Rename mocks “Query Decrypter” -> “Threshold Network” and update SDK constants/contracts/artifacts accordingly.
|
|
20
|
+
- Extend mock contracts + hardhat plugin to publish & verify decryption results on-chain, and add end-to-end integration tests.
|
|
21
|
+
|
|
22
|
+
- 650ea48: Align builder patterns in cofhe client api (`client.encryptInputs(..).encrypt()` and `client.decryptHandles(..).decrypt()`) to use the same terminator function `.execute()` instead of `.encrypt()`/`.decrypt()`.
|
|
23
|
+
|
|
24
|
+
Rename `setStepCallback` of encryptInputs builder to `onStep` to improve ergonomics.
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- 5467d77: Adds `config.mocks.encryptDelay: number | [number, number, number, number, number]` to allow configurable mock encryption delay. Defaults to 0 delay on hardhat to keep tests quick.
|
|
29
|
+
- 73b1502: Add `cofheClient.connection` getter which exposes inner connection state without using `getSnapshot()` reactive utility.
|
|
30
|
+
|
|
31
|
+
## 0.2.1
|
|
32
|
+
|
|
33
|
+
### Patch Changes
|
|
34
|
+
|
|
35
|
+
- 409bfdf: Add `hash` field to permits, calculated at permit creation time. Replaces `PermitUtils.getHash(permit)` with `permit.hash`.
|
|
36
|
+
- ac47e2f: Add `PermitUtils.checkValidityOnChain` to validate permits against the on-chain deployed ACL (source of truth).
|
|
37
|
+
- 8af1b70: Updated to Zod 4 for improved performance and type safety. Includes internal changes to validation schemas and error handling that should not affect external API usage.
|
|
38
|
+
|
|
3
39
|
## 0.2.0
|
|
4
40
|
|
|
5
41
|
### Minor Changes
|
package/chains/defineChain.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CofheChainSchema, type CofheChain } from './types.js';
|
|
2
|
+
import { z } from 'zod';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Defines and validates a CofheChain configuration
|
|
@@ -10,8 +11,7 @@ export function defineChain(chainConfig: CofheChain): CofheChain {
|
|
|
10
11
|
const result = CofheChainSchema.safeParse(chainConfig);
|
|
11
12
|
|
|
12
13
|
if (!result.success) {
|
|
13
|
-
|
|
14
|
-
throw new Error(`Invalid chain configuration: ${errorMessages.join(', ')}`);
|
|
14
|
+
throw new Error(`Invalid chain configuration: ${z.prettifyError(result.error)}`, { cause: result.error });
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
return result.data;
|
package/chains/types.ts
CHANGED
|
@@ -16,11 +16,11 @@ export const CofheChainSchema = z.object({
|
|
|
16
16
|
/** Network identifier */
|
|
17
17
|
network: z.string().min(1),
|
|
18
18
|
/** coFhe service URL */
|
|
19
|
-
coFheUrl: z.
|
|
19
|
+
coFheUrl: z.url(),
|
|
20
20
|
/** Verifier service URL */
|
|
21
|
-
verifierUrl: z.
|
|
21
|
+
verifierUrl: z.url(),
|
|
22
22
|
/** Threshold network service URL */
|
|
23
|
-
thresholdNetworkUrl: z.
|
|
23
|
+
thresholdNetworkUrl: z.url(),
|
|
24
24
|
/** Environment type */
|
|
25
25
|
environment: EnvironmentSchema,
|
|
26
26
|
});
|
package/core/baseBuilder.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { type PublicClient, type WalletClient } from 'viem';
|
|
2
|
-
import { type
|
|
3
|
-
import {
|
|
2
|
+
import { type CofheConfig } from './config.js';
|
|
3
|
+
import { CofheError, CofheErrorCode } from './error.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Base parameters that all builders need
|
|
7
7
|
*/
|
|
8
8
|
export type BaseBuilderParams = {
|
|
9
|
-
config:
|
|
9
|
+
config: CofheConfig | undefined;
|
|
10
10
|
publicClient: PublicClient | undefined;
|
|
11
11
|
walletClient: WalletClient | undefined;
|
|
12
12
|
|
|
@@ -21,7 +21,7 @@ export type BaseBuilderParams = {
|
|
|
21
21
|
* for working with clients, config, and chain IDs
|
|
22
22
|
*/
|
|
23
23
|
export abstract class BaseBuilder {
|
|
24
|
-
protected config:
|
|
24
|
+
protected config: CofheConfig;
|
|
25
25
|
|
|
26
26
|
protected publicClient: PublicClient | undefined;
|
|
27
27
|
protected walletClient: WalletClient | undefined;
|
|
@@ -32,8 +32,8 @@ export abstract class BaseBuilder {
|
|
|
32
32
|
constructor(params: BaseBuilderParams) {
|
|
33
33
|
// Check that config is provided
|
|
34
34
|
if (!params.config) {
|
|
35
|
-
throw new
|
|
36
|
-
code:
|
|
35
|
+
throw new CofheError({
|
|
36
|
+
code: CofheErrorCode.MissingConfig,
|
|
37
37
|
message: 'Builder config is undefined',
|
|
38
38
|
hint: 'Ensure client has been created with a config.',
|
|
39
39
|
context: {
|
|
@@ -55,12 +55,12 @@ export abstract class BaseBuilder {
|
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
57
|
* Asserts that this.chainId is populated
|
|
58
|
-
* @throws {
|
|
58
|
+
* @throws {CofheError} If chainId is not set
|
|
59
59
|
*/
|
|
60
60
|
protected assertChainId(): asserts this is this & { chainId: number } {
|
|
61
61
|
if (this.chainId) return;
|
|
62
|
-
throw new
|
|
63
|
-
code:
|
|
62
|
+
throw new CofheError({
|
|
63
|
+
code: CofheErrorCode.ChainIdUninitialized,
|
|
64
64
|
message: 'Chain ID is not set',
|
|
65
65
|
hint: 'Ensure client.connect() has been called and awaited, or use setChainId(...) to set the chainId explicitly.',
|
|
66
66
|
context: {
|
|
@@ -71,12 +71,12 @@ export abstract class BaseBuilder {
|
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
73
|
* Asserts that this.account is populated
|
|
74
|
-
* @throws {
|
|
74
|
+
* @throws {CofheError} If account is not set
|
|
75
75
|
*/
|
|
76
76
|
protected assertAccount(): asserts this is this & { account: string } {
|
|
77
77
|
if (this.account) return;
|
|
78
|
-
throw new
|
|
79
|
-
code:
|
|
78
|
+
throw new CofheError({
|
|
79
|
+
code: CofheErrorCode.AccountUninitialized,
|
|
80
80
|
message: 'Account is not set',
|
|
81
81
|
hint: 'Ensure client.connect() has been called and awaited, or use setAccount(...) to set the account explicitly.',
|
|
82
82
|
context: {
|
|
@@ -87,12 +87,12 @@ export abstract class BaseBuilder {
|
|
|
87
87
|
|
|
88
88
|
/**
|
|
89
89
|
* Asserts that this.publicClient is populated
|
|
90
|
-
* @throws {
|
|
90
|
+
* @throws {CofheError} If publicClient is not set
|
|
91
91
|
*/
|
|
92
92
|
protected assertPublicClient(): asserts this is this & { publicClient: PublicClient } {
|
|
93
93
|
if (this.publicClient) return;
|
|
94
|
-
throw new
|
|
95
|
-
code:
|
|
94
|
+
throw new CofheError({
|
|
95
|
+
code: CofheErrorCode.MissingPublicClient,
|
|
96
96
|
message: 'Public client not found',
|
|
97
97
|
hint: 'Ensure client.connect() has been called with a publicClient.',
|
|
98
98
|
context: {
|
|
@@ -103,12 +103,12 @@ export abstract class BaseBuilder {
|
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
105
|
* Asserts that this.walletClient is populated
|
|
106
|
-
* @throws {
|
|
106
|
+
* @throws {CofheError} If walletClient is not set
|
|
107
107
|
*/
|
|
108
108
|
protected assertWalletClient(): asserts this is this & { walletClient: WalletClient } {
|
|
109
109
|
if (this.walletClient) return;
|
|
110
|
-
throw new
|
|
111
|
-
code:
|
|
110
|
+
throw new CofheError({
|
|
111
|
+
code: CofheErrorCode.MissingWalletClient,
|
|
112
112
|
message: 'Wallet client not found',
|
|
113
113
|
hint: 'Ensure client.connect() has been called with a walletClient.',
|
|
114
114
|
context: {
|
package/core/client.test.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
-
import {
|
|
3
|
-
import { type
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { createCofheClientBase } from './client.js';
|
|
3
|
+
import { type CofheClient, type CofheClientConnectionState } from './clientTypes.js';
|
|
4
|
+
import { createCofheConfigBase, type CofheEnvironment } from './config.js';
|
|
5
|
+
import { CofheError, CofheErrorCode } from './error.js';
|
|
6
6
|
import { type PublicClient, type WalletClient } from 'viem';
|
|
7
7
|
import { EncryptInputsBuilder } from './encrypt/encryptInputsBuilder.js';
|
|
8
8
|
import { Encryptable } from './types.js';
|
|
@@ -31,9 +31,9 @@ const createMockWalletClient = (addresses = ['0x12345678901234567890123456789012
|
|
|
31
31
|
getAddresses: vi.fn().mockResolvedValue(addresses),
|
|
32
32
|
}) as any;
|
|
33
33
|
|
|
34
|
-
const createTestClient = ():
|
|
35
|
-
const config =
|
|
36
|
-
return
|
|
34
|
+
const createTestClient = (): CofheClient => {
|
|
35
|
+
const config = createCofheConfigBase({ supportedChains: [] });
|
|
36
|
+
return createCofheClientBase({
|
|
37
37
|
config,
|
|
38
38
|
zkBuilderAndCrsGenerator: {} as any,
|
|
39
39
|
tfhePublicKeyDeserializer: {} as any,
|
|
@@ -42,8 +42,8 @@ const createTestClient = (): CofhesdkClient => {
|
|
|
42
42
|
});
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
describe('
|
|
46
|
-
let client:
|
|
45
|
+
describe('createCofheClientBase', () => {
|
|
46
|
+
let client: CofheClient;
|
|
47
47
|
|
|
48
48
|
beforeEach(() => {
|
|
49
49
|
client = createTestClient();
|
|
@@ -59,11 +59,15 @@ describe('createCofhesdkClientBase', () => {
|
|
|
59
59
|
expect(snapshot.account).toBe(undefined);
|
|
60
60
|
expect(snapshot.publicClient).toBe(undefined);
|
|
61
61
|
expect(snapshot.walletClient).toBe(undefined);
|
|
62
|
+
|
|
63
|
+
const connection = client.connection;
|
|
64
|
+
expect(connection).toEqual(snapshot);
|
|
62
65
|
});
|
|
63
66
|
|
|
64
67
|
it('should expose convenience flags', () => {
|
|
65
68
|
expect(client.connected).toBe(false);
|
|
66
69
|
expect(client.connecting).toBe(false);
|
|
70
|
+
expect(client.connection.connected).toBe(false);
|
|
67
71
|
});
|
|
68
72
|
|
|
69
73
|
it('should expose config', () => {
|
|
@@ -74,10 +78,10 @@ describe('createCofhesdkClientBase', () => {
|
|
|
74
78
|
|
|
75
79
|
describe('environment', () => {
|
|
76
80
|
it('should create a client with the correct environment', async () => {
|
|
77
|
-
const environments:
|
|
81
|
+
const environments: CofheEnvironment[] = ['node', 'hardhat', 'web', 'react'];
|
|
78
82
|
for (const environment of environments) {
|
|
79
|
-
const config =
|
|
80
|
-
const client =
|
|
83
|
+
const config = createCofheConfigBase({ environment, supportedChains: [] });
|
|
84
|
+
const client = createCofheClientBase({
|
|
81
85
|
config,
|
|
82
86
|
zkBuilderAndCrsGenerator: {} as any,
|
|
83
87
|
tfhePublicKeyDeserializer: {} as any,
|
|
@@ -92,7 +96,7 @@ describe('createCofhesdkClientBase', () => {
|
|
|
92
96
|
|
|
93
97
|
describe('reactive state', () => {
|
|
94
98
|
it('should notify subscribers of state changes', async () => {
|
|
95
|
-
const states:
|
|
99
|
+
const states: CofheClientConnectionState[] = [];
|
|
96
100
|
client.subscribe((snapshot) => states.push(snapshot));
|
|
97
101
|
|
|
98
102
|
const publicClient = createMockPublicClient();
|
|
@@ -117,7 +121,7 @@ describe('createCofhesdkClientBase', () => {
|
|
|
117
121
|
});
|
|
118
122
|
|
|
119
123
|
it('should stop notifications after unsubscribe', async () => {
|
|
120
|
-
const states:
|
|
124
|
+
const states: CofheClientConnectionState[] = [];
|
|
121
125
|
const unsubscribe = client.subscribe((snapshot) => states.push(snapshot));
|
|
122
126
|
|
|
123
127
|
unsubscribe();
|
|
@@ -141,11 +145,11 @@ describe('createCofhesdkClientBase', () => {
|
|
|
141
145
|
expect(client.connected).toBe(true);
|
|
142
146
|
expect(client.connecting).toBe(false);
|
|
143
147
|
|
|
144
|
-
const
|
|
145
|
-
expect(
|
|
146
|
-
expect(
|
|
147
|
-
expect(
|
|
148
|
-
expect(
|
|
148
|
+
const connection = client.connection;
|
|
149
|
+
expect(connection.chainId).toBe(11155111);
|
|
150
|
+
expect(connection.account).toBe('0xabcd');
|
|
151
|
+
expect(connection.publicClient).toBe(publicClient);
|
|
152
|
+
expect(connection.walletClient).toBe(walletClient);
|
|
149
153
|
});
|
|
150
154
|
|
|
151
155
|
it('should set connecting state during connection', async () => {
|
|
@@ -176,6 +180,97 @@ describe('createCofhesdkClientBase', () => {
|
|
|
176
180
|
await promise1;
|
|
177
181
|
});
|
|
178
182
|
|
|
183
|
+
it('should ensure the latest connection attempt wins when connecting twice', async () => {
|
|
184
|
+
let resolveChainId1: (value: number) => void;
|
|
185
|
+
let resolveAddresses1: (value: string[]) => void;
|
|
186
|
+
let resolveChainId2: (value: number) => void;
|
|
187
|
+
let resolveAddresses2: (value: string[]) => void;
|
|
188
|
+
|
|
189
|
+
const chainIdPromise1 = new Promise<number>((resolve) => {
|
|
190
|
+
resolveChainId1 = resolve;
|
|
191
|
+
});
|
|
192
|
+
const addressesPromise1 = new Promise<string[]>((resolve) => {
|
|
193
|
+
resolveAddresses1 = resolve;
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const chainIdPromise2 = new Promise<number>((resolve) => {
|
|
197
|
+
resolveChainId2 = resolve;
|
|
198
|
+
});
|
|
199
|
+
const addressesPromise2 = new Promise<string[]>((resolve) => {
|
|
200
|
+
resolveAddresses2 = resolve;
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const publicClient1 = createMockPublicClient() as any;
|
|
204
|
+
publicClient1.getChainId = vi.fn().mockReturnValue(chainIdPromise1);
|
|
205
|
+
const walletClient1 = createMockWalletClient() as any;
|
|
206
|
+
walletClient1.getAddresses = vi.fn().mockReturnValue(addressesPromise1);
|
|
207
|
+
|
|
208
|
+
const publicClient2 = createMockPublicClient() as any;
|
|
209
|
+
publicClient2.getChainId = vi.fn().mockReturnValue(chainIdPromise2);
|
|
210
|
+
const walletClient2 = createMockWalletClient() as any;
|
|
211
|
+
walletClient2.getAddresses = vi.fn().mockReturnValue(addressesPromise2);
|
|
212
|
+
|
|
213
|
+
const promise1 = client.connect(publicClient1, walletClient1);
|
|
214
|
+
expect(client.connecting).toBe(true);
|
|
215
|
+
|
|
216
|
+
const promise2 = client.connect(publicClient2, walletClient2);
|
|
217
|
+
|
|
218
|
+
// Resolve the second connect first
|
|
219
|
+
resolveChainId2!(222);
|
|
220
|
+
resolveAddresses2!(['0x2222222222222222222222222222222222222222']);
|
|
221
|
+
await promise2;
|
|
222
|
+
|
|
223
|
+
expect(client.connected).toBe(true);
|
|
224
|
+
expect(client.connecting).toBe(false);
|
|
225
|
+
expect(client.connection.chainId).toBe(222);
|
|
226
|
+
expect(client.connection.account).toBe('0x2222222222222222222222222222222222222222');
|
|
227
|
+
expect(client.connection.publicClient).toBe(publicClient2);
|
|
228
|
+
expect(client.connection.walletClient).toBe(walletClient2);
|
|
229
|
+
|
|
230
|
+
// Now resolve the first connect; it must not overwrite the latest state.
|
|
231
|
+
resolveChainId1!(111);
|
|
232
|
+
resolveAddresses1!(['0x1111111111111111111111111111111111111111']);
|
|
233
|
+
await promise1;
|
|
234
|
+
|
|
235
|
+
expect(client.connection.chainId).toBe(222);
|
|
236
|
+
expect(client.connection.account).toBe('0x2222222222222222222222222222222222222222');
|
|
237
|
+
expect(client.connection.publicClient).toBe(publicClient2);
|
|
238
|
+
expect(client.connection.walletClient).toBe(walletClient2);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('should allow disconnect while connecting and never end up connected afterwards', async () => {
|
|
242
|
+
let resolveChainId: (value: number) => void;
|
|
243
|
+
let resolveAddresses: (value: string[]) => void;
|
|
244
|
+
|
|
245
|
+
const chainIdPromise = new Promise<number>((resolve) => {
|
|
246
|
+
resolveChainId = resolve;
|
|
247
|
+
});
|
|
248
|
+
const addressesPromise = new Promise<string[]>((resolve) => {
|
|
249
|
+
resolveAddresses = resolve;
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
const publicClient = createMockPublicClient() as any;
|
|
253
|
+
publicClient.getChainId = vi.fn().mockReturnValue(chainIdPromise);
|
|
254
|
+
|
|
255
|
+
const walletClient = createMockWalletClient() as any;
|
|
256
|
+
walletClient.getAddresses = vi.fn().mockReturnValue(addressesPromise);
|
|
257
|
+
|
|
258
|
+
const connectPromise = client.connect(publicClient, walletClient);
|
|
259
|
+
expect(client.connecting).toBe(true);
|
|
260
|
+
|
|
261
|
+
client.disconnect();
|
|
262
|
+
expect(client.connected).toBe(false);
|
|
263
|
+
expect(client.connecting).toBe(false);
|
|
264
|
+
|
|
265
|
+
resolveChainId!(11155111);
|
|
266
|
+
resolveAddresses!(['0x1234567890123456789012345678901234567890']);
|
|
267
|
+
|
|
268
|
+
await connectPromise;
|
|
269
|
+
|
|
270
|
+
expect(client.connected).toBe(false);
|
|
271
|
+
expect(client.connecting).toBe(false);
|
|
272
|
+
});
|
|
273
|
+
|
|
179
274
|
it('should handle publicClient.getChainId throwing an error', async () => {
|
|
180
275
|
const publicClient = createMockPublicClient();
|
|
181
276
|
const getChainIdError = new Error('Network error');
|
|
@@ -185,12 +280,12 @@ describe('createCofhesdkClientBase', () => {
|
|
|
185
280
|
try {
|
|
186
281
|
await client.connect(publicClient, walletClient);
|
|
187
282
|
} catch (error) {
|
|
188
|
-
expect(error).toBeInstanceOf(
|
|
189
|
-
expect((error as
|
|
190
|
-
expect((error as
|
|
283
|
+
expect(error).toBeInstanceOf(CofheError);
|
|
284
|
+
expect((error as CofheError).code).toBe(CofheErrorCode.PublicWalletGetChainIdFailed);
|
|
285
|
+
expect((error as CofheError).message).toBe(
|
|
191
286
|
'getting chain ID from public client failed | Caused by: Network error'
|
|
192
287
|
);
|
|
193
|
-
expect((error as
|
|
288
|
+
expect((error as CofheError).cause).toBe(getChainIdError);
|
|
194
289
|
}
|
|
195
290
|
});
|
|
196
291
|
|
|
@@ -204,9 +299,9 @@ describe('createCofhesdkClientBase', () => {
|
|
|
204
299
|
try {
|
|
205
300
|
await connectPromise;
|
|
206
301
|
} catch (error) {
|
|
207
|
-
expect(error).toBeInstanceOf(
|
|
208
|
-
expect((error as
|
|
209
|
-
expect((error as
|
|
302
|
+
expect(error).toBeInstanceOf(CofheError);
|
|
303
|
+
expect((error as CofheError).code).toBe(CofheErrorCode.PublicWalletGetChainIdFailed);
|
|
304
|
+
expect((error as CofheError).message).toBe('chain ID from public client is null');
|
|
210
305
|
}
|
|
211
306
|
});
|
|
212
307
|
|
|
@@ -221,12 +316,12 @@ describe('createCofhesdkClientBase', () => {
|
|
|
221
316
|
try {
|
|
222
317
|
await connectPromise;
|
|
223
318
|
} catch (error) {
|
|
224
|
-
expect(error).toBeInstanceOf(
|
|
225
|
-
expect((error as
|
|
226
|
-
expect((error as
|
|
319
|
+
expect(error).toBeInstanceOf(CofheError);
|
|
320
|
+
expect((error as CofheError).code).toBe(CofheErrorCode.PublicWalletGetAddressesFailed);
|
|
321
|
+
expect((error as CofheError).message).toBe(
|
|
227
322
|
'getting address from wallet client failed | Caused by: Network error'
|
|
228
323
|
);
|
|
229
|
-
expect((error as
|
|
324
|
+
expect((error as CofheError).cause).toBe(getAddressesError);
|
|
230
325
|
}
|
|
231
326
|
});
|
|
232
327
|
|
|
@@ -239,9 +334,9 @@ describe('createCofhesdkClientBase', () => {
|
|
|
239
334
|
try {
|
|
240
335
|
await connectPromise;
|
|
241
336
|
} catch (error) {
|
|
242
|
-
expect(error).toBeInstanceOf(
|
|
243
|
-
expect((error as
|
|
244
|
-
expect((error as
|
|
337
|
+
expect(error).toBeInstanceOf(CofheError);
|
|
338
|
+
expect((error as CofheError).code).toBe(CofheErrorCode.PublicWalletGetAddressesFailed);
|
|
339
|
+
expect((error as CofheError).message).toBe('address from wallet client is null');
|
|
245
340
|
}
|
|
246
341
|
});
|
|
247
342
|
|
|
@@ -256,23 +351,42 @@ describe('createCofhesdkClientBase', () => {
|
|
|
256
351
|
try {
|
|
257
352
|
await connectPromise;
|
|
258
353
|
} catch (error) {
|
|
259
|
-
expect(error).toBeInstanceOf(
|
|
260
|
-
expect((error as
|
|
261
|
-
expect((error as
|
|
354
|
+
expect(error).toBeInstanceOf(CofheError);
|
|
355
|
+
expect((error as CofheError).code).toBe(CofheErrorCode.PublicWalletGetChainIdFailed);
|
|
356
|
+
expect((error as CofheError).message).toBe(
|
|
262
357
|
'getting chain ID from public client failed | Caused by: Network error'
|
|
263
358
|
);
|
|
264
|
-
expect((error as
|
|
359
|
+
expect((error as CofheError).cause).toBe(getChainIdError);
|
|
265
360
|
}
|
|
266
361
|
});
|
|
362
|
+
|
|
363
|
+
it('should disconnect and clear connection state', async () => {
|
|
364
|
+
const publicClient = createMockPublicClient(11155111);
|
|
365
|
+
const walletClient = createMockWalletClient(['0xabcd']);
|
|
366
|
+
|
|
367
|
+
await client.connect(publicClient, walletClient);
|
|
368
|
+
expect(client.connected).toBe(true);
|
|
369
|
+
|
|
370
|
+
client.disconnect();
|
|
371
|
+
|
|
372
|
+
expect(client.connected).toBe(false);
|
|
373
|
+
expect(client.connecting).toBe(false);
|
|
374
|
+
|
|
375
|
+
expect(client.connection.chainId).toBe(undefined);
|
|
376
|
+
expect(client.connection.account).toBe(undefined);
|
|
377
|
+
expect(client.connection.publicClient).toBe(undefined);
|
|
378
|
+
expect(client.connection.walletClient).toBe(undefined);
|
|
379
|
+
expect(client.connection.connectError).toBe(undefined);
|
|
380
|
+
});
|
|
267
381
|
});
|
|
268
382
|
|
|
269
383
|
describe('encryptInputs', () => {
|
|
270
384
|
it('should throw if not connected', async () => {
|
|
271
385
|
try {
|
|
272
|
-
await client.encryptInputs([Encryptable.uint8(1n), Encryptable.uint8(2n), Encryptable.uint8(3n)]).
|
|
386
|
+
await client.encryptInputs([Encryptable.uint8(1n), Encryptable.uint8(2n), Encryptable.uint8(3n)]).execute();
|
|
273
387
|
} catch (error) {
|
|
274
|
-
expect(error).toBeInstanceOf(
|
|
275
|
-
expect((error as
|
|
388
|
+
expect(error).toBeInstanceOf(CofheError);
|
|
389
|
+
expect((error as CofheError).code).toBe(CofheErrorCode.NotConnected);
|
|
276
390
|
}
|
|
277
391
|
});
|
|
278
392
|
|
|
@@ -286,7 +400,7 @@ describe('createCofhesdkClientBase', () => {
|
|
|
286
400
|
|
|
287
401
|
expect(builder).toBeDefined();
|
|
288
402
|
expect(builder).toBeInstanceOf(EncryptInputsBuilder);
|
|
289
|
-
expect(builder).toHaveProperty('
|
|
403
|
+
expect(builder).toHaveProperty('execute');
|
|
290
404
|
expect(builder.getChainId()).toBe(123);
|
|
291
405
|
expect(builder.getAccount()).toBe('0xtest');
|
|
292
406
|
});
|