@cofhe/sdk 0.1.1 → 0.2.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 +14 -0
- package/adapters/ethers6.ts +28 -28
- package/adapters/hardhat.ts +0 -1
- package/adapters/index.test.ts +14 -19
- package/adapters/smartWallet.ts +81 -73
- package/adapters/test-utils.ts +45 -45
- package/adapters/types.ts +3 -3
- package/chains/chains/localcofhe.ts +14 -0
- package/chains/chains.test.ts +2 -1
- package/chains/index.ts +3 -1
- package/core/baseBuilder.ts +30 -49
- package/core/client.test.ts +94 -77
- package/core/client.ts +133 -149
- package/core/clientTypes.ts +108 -0
- package/core/config.test.ts +22 -11
- package/core/config.ts +16 -9
- package/core/decrypt/decryptHandleBuilder.ts +51 -45
- package/core/decrypt/{tnSealOutput.ts → tnSealOutputV1.ts} +1 -1
- package/core/decrypt/tnSealOutputV2.ts +298 -0
- package/core/encrypt/cofheMocksZkVerifySign.ts +16 -10
- package/core/encrypt/encryptInputsBuilder.test.ts +132 -116
- package/core/encrypt/encryptInputsBuilder.ts +159 -111
- package/core/encrypt/encryptUtils.ts +6 -3
- package/core/encrypt/zkPackProveVerify.ts +70 -8
- package/core/error.ts +0 -2
- package/core/fetchKeys.test.ts +1 -18
- package/core/fetchKeys.ts +0 -26
- package/core/index.ts +29 -17
- package/core/keyStore.ts +65 -38
- package/core/permits.test.ts +253 -1
- package/core/permits.ts +80 -16
- package/core/types.ts +198 -152
- package/core/utils.ts +43 -1
- package/dist/adapters.d.cts +38 -20
- package/dist/adapters.d.ts +38 -20
- package/dist/chains.cjs +14 -1
- package/dist/chains.d.cts +23 -1
- package/dist/chains.d.ts +23 -1
- package/dist/chains.js +1 -1
- package/dist/{chunk-LU7BMUUT.js → chunk-UGBVZNRT.js} +39 -25
- package/dist/{chunk-GZCQQYVI.js → chunk-WEAZ25JO.js} +14 -2
- package/dist/{chunk-KFGPTJ6X.js → chunk-WGCRJCBR.js} +1920 -1692
- package/dist/{types-bB7wLj0q.d.cts → clientTypes-5_1nwtUe.d.cts} +308 -347
- package/dist/{types-PhwGgQvs.d.ts → clientTypes-Es7fyi65.d.ts} +308 -347
- package/dist/core.cjs +2872 -2632
- package/dist/core.d.cts +101 -6
- package/dist/core.d.ts +101 -6
- package/dist/core.js +3 -3
- package/dist/node.cjs +2716 -2520
- package/dist/node.d.cts +3 -3
- package/dist/node.d.ts +3 -3
- package/dist/node.js +4 -3
- package/dist/{permit-S9CnI6MF.d.cts → permit-fUSe6KKq.d.cts} +31 -15
- package/dist/{permit-S9CnI6MF.d.ts → permit-fUSe6KKq.d.ts} +31 -15
- package/dist/permits.cjs +39 -24
- package/dist/permits.d.cts +137 -148
- package/dist/permits.d.ts +137 -148
- package/dist/permits.js +1 -1
- package/dist/web.cjs +2929 -2518
- package/dist/web.d.cts +21 -5
- package/dist/web.d.ts +21 -5
- package/dist/web.js +185 -9
- package/dist/zkProve.worker.cjs +93 -0
- package/dist/zkProve.worker.d.cts +2 -0
- package/dist/zkProve.worker.d.ts +2 -0
- package/dist/zkProve.worker.js +91 -0
- package/node/client.test.ts +20 -25
- package/node/encryptInputs.test.ts +18 -38
- package/node/index.ts +1 -0
- package/package.json +14 -14
- package/permits/index.ts +1 -0
- package/permits/localstorage.test.ts +0 -1
- package/permits/permit.test.ts +25 -22
- package/permits/permit.ts +30 -21
- package/permits/sealing.test.ts +3 -3
- package/permits/sealing.ts +2 -2
- package/permits/store.ts +5 -7
- package/permits/test-utils.ts +1 -1
- package/permits/types.ts +17 -0
- package/permits/utils.ts +0 -1
- package/permits/validation.ts +24 -4
- package/web/client.web.test.ts +20 -25
- package/web/config.web.test.ts +0 -2
- package/web/encryptInputs.web.test.ts +31 -54
- package/web/index.ts +65 -1
- package/web/storage.ts +19 -5
- package/web/worker.builder.web.test.ts +148 -0
- package/web/worker.config.web.test.ts +329 -0
- package/web/worker.output.web.test.ts +84 -0
- package/web/workerManager.test.ts +80 -0
- package/web/workerManager.ts +214 -0
- package/web/workerManager.web.test.ts +114 -0
- package/web/zkProve.worker.ts +133 -0
- package/core/result.test.ts +0 -180
- package/core/result.ts +0 -67
- package/core/test-utils.ts +0 -45
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// TODO: Extract client types to its own file, keep this one as primitives
|
|
2
|
+
import { type PublicClient, type WalletClient } from 'viem';
|
|
3
|
+
import { type CofhesdkConfig, type CofhesdkEnvironment } from './config.js';
|
|
4
|
+
import { type DecryptHandlesBuilder } from './decrypt/decryptHandleBuilder.js';
|
|
5
|
+
import { type EncryptInputsBuilder } from './encrypt/encryptInputsBuilder.js';
|
|
6
|
+
import { type ZkBuilderAndCrsGenerator, type ZkProveWorkerFunction } from './encrypt/zkPackProveVerify.js';
|
|
7
|
+
import { type FheKeyDeserializer } from './fetchKeys.js';
|
|
8
|
+
import { permits } from './permits.js';
|
|
9
|
+
import type { EncryptableItem, FheTypes, TfheInitializer } from './types.js';
|
|
10
|
+
import type { PermitUtils } from 'permits/permit.js';
|
|
11
|
+
import type {
|
|
12
|
+
CreateSelfPermitOptions,
|
|
13
|
+
Permit,
|
|
14
|
+
CreateSharingPermitOptions,
|
|
15
|
+
ImportSharedPermitOptions,
|
|
16
|
+
SharingPermit,
|
|
17
|
+
RecipientPermit,
|
|
18
|
+
SelfPermit,
|
|
19
|
+
} from 'permits/types.js';
|
|
20
|
+
|
|
21
|
+
// CLIENT
|
|
22
|
+
|
|
23
|
+
export type CofhesdkClient<TConfig extends CofhesdkConfig = CofhesdkConfig> = {
|
|
24
|
+
// --- state access ---
|
|
25
|
+
getSnapshot(): CofhesdkClientConnectionState;
|
|
26
|
+
subscribe(listener: Listener): () => void;
|
|
27
|
+
|
|
28
|
+
// --- convenience flags (read-only) ---
|
|
29
|
+
readonly connected: boolean;
|
|
30
|
+
readonly connecting: boolean;
|
|
31
|
+
|
|
32
|
+
// --- config & platform-specific ---
|
|
33
|
+
readonly config: TConfig;
|
|
34
|
+
|
|
35
|
+
connect(publicClient: PublicClient, walletClient: WalletClient): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Types docstring
|
|
38
|
+
*/
|
|
39
|
+
encryptInputs<T extends EncryptableItem[]>(inputs: [...T]): EncryptInputsBuilder<[...T]>;
|
|
40
|
+
decryptHandle<U extends FheTypes>(ctHash: bigint, utype: U): DecryptHandlesBuilder<U>;
|
|
41
|
+
permits: CofhesdkClientPermits;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type CofhesdkClientConnectionState = {
|
|
45
|
+
connected: boolean;
|
|
46
|
+
connecting: boolean;
|
|
47
|
+
connectError: unknown | undefined;
|
|
48
|
+
chainId: number | undefined;
|
|
49
|
+
account: `0x${string}` | undefined;
|
|
50
|
+
publicClient: PublicClient | undefined;
|
|
51
|
+
walletClient: WalletClient | undefined;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type Listener = (snapshot: CofhesdkClientConnectionState) => void;
|
|
55
|
+
|
|
56
|
+
export type CofhesdkClientPermitsClients = {
|
|
57
|
+
publicClient: PublicClient;
|
|
58
|
+
walletClient: WalletClient;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type CofhesdkClientPermits = {
|
|
62
|
+
getSnapshot: typeof permits.getSnapshot;
|
|
63
|
+
subscribe: typeof permits.subscribe;
|
|
64
|
+
|
|
65
|
+
// Creation methods (require connection, no params)
|
|
66
|
+
createSelf: (options: CreateSelfPermitOptions, clients?: CofhesdkClientPermitsClients) => Promise<SelfPermit>;
|
|
67
|
+
createSharing: (
|
|
68
|
+
options: CreateSharingPermitOptions,
|
|
69
|
+
clients?: CofhesdkClientPermitsClients
|
|
70
|
+
) => Promise<SharingPermit>;
|
|
71
|
+
importShared: (
|
|
72
|
+
options: ImportSharedPermitOptions | string,
|
|
73
|
+
clients?: CofhesdkClientPermitsClients
|
|
74
|
+
) => Promise<RecipientPermit>;
|
|
75
|
+
|
|
76
|
+
// Retrieval methods (chainId/account optional)
|
|
77
|
+
getPermit: (hash: string, chainId?: number, account?: string) => Promise<Permit | undefined>;
|
|
78
|
+
getPermits: (chainId?: number, account?: string) => Promise<Record<string, Permit>>;
|
|
79
|
+
getActivePermit: (chainId?: number, account?: string) => Promise<Permit | undefined>;
|
|
80
|
+
getActivePermitHash: (chainId?: number, account?: string) => Promise<string | undefined>;
|
|
81
|
+
|
|
82
|
+
// Get or create methods (get active or create new, chainId/account optional)
|
|
83
|
+
getOrCreateSelfPermit: (chainId?: number, account?: string, options?: CreateSelfPermitOptions) => Promise<Permit>;
|
|
84
|
+
getOrCreateSharingPermit: (
|
|
85
|
+
options: CreateSharingPermitOptions,
|
|
86
|
+
chainId?: number,
|
|
87
|
+
account?: string
|
|
88
|
+
) => Promise<Permit>;
|
|
89
|
+
|
|
90
|
+
// Mutation methods (chainId/account optional)
|
|
91
|
+
selectActivePermit: (hash: string, chainId?: number, account?: string) => void;
|
|
92
|
+
removePermit: (hash: string, chainId?: number, account?: string, force?: boolean) => void;
|
|
93
|
+
removeActivePermit: (chainId?: number, account?: string) => void;
|
|
94
|
+
|
|
95
|
+
// Utils
|
|
96
|
+
getHash: typeof PermitUtils.getHash;
|
|
97
|
+
serialize: typeof PermitUtils.serialize;
|
|
98
|
+
deserialize: typeof PermitUtils.deserialize;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export type CofhesdkClientParams<TConfig extends CofhesdkConfig> = {
|
|
102
|
+
config: TConfig;
|
|
103
|
+
zkBuilderAndCrsGenerator: ZkBuilderAndCrsGenerator;
|
|
104
|
+
tfhePublicKeyDeserializer: FheKeyDeserializer;
|
|
105
|
+
compactPkeCrsDeserializer: FheKeyDeserializer;
|
|
106
|
+
initTfhe: TfheInitializer;
|
|
107
|
+
zkProveWorkerFn?: ZkProveWorkerFunction;
|
|
108
|
+
};
|
package/core/config.test.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* eslint-disable no-unused-vars */
|
|
2
1
|
import { sepolia, hardhat } from '@/chains';
|
|
3
2
|
|
|
4
3
|
import { describe, it, expect, vi } from 'vitest';
|
|
@@ -52,6 +51,17 @@ describe('createCofhesdkConfigBase', () => {
|
|
|
52
51
|
expect(getNestedValue(result, path)).toEqual(expectedValue);
|
|
53
52
|
};
|
|
54
53
|
|
|
54
|
+
it('environment', () => {
|
|
55
|
+
expectInvalidConfigItem('environment', 'not-a-valid-environment');
|
|
56
|
+
expectInvalidConfigItem('environment', 123);
|
|
57
|
+
expectInvalidConfigItem('environment', {});
|
|
58
|
+
|
|
59
|
+
expectValidConfigItem('environment', 'node', 'node');
|
|
60
|
+
expectValidConfigItem('environment', 'hardhat', 'hardhat');
|
|
61
|
+
expectValidConfigItem('environment', 'web', 'web');
|
|
62
|
+
expectValidConfigItem('environment', 'react', 'react');
|
|
63
|
+
});
|
|
64
|
+
|
|
55
65
|
it('supportedChains', () => {
|
|
56
66
|
expectInvalidConfigItem('supportedChains', {});
|
|
57
67
|
expectInvalidConfigItem('supportedChains', 'not-an-array');
|
|
@@ -62,16 +72,6 @@ describe('createCofhesdkConfigBase', () => {
|
|
|
62
72
|
expectValidConfigItem('supportedChains', [sepolia, hardhat], [sepolia, hardhat]);
|
|
63
73
|
});
|
|
64
74
|
|
|
65
|
-
it('fheKeysPrefetching', () => {
|
|
66
|
-
expectInvalidConfigItem('fheKeysPrefetching', 'invalid-option');
|
|
67
|
-
expectInvalidConfigItem('fheKeysPrefetching', 5);
|
|
68
|
-
|
|
69
|
-
expectValidConfigItem('fheKeysPrefetching', 'CONNECTED_CHAIN', 'CONNECTED_CHAIN');
|
|
70
|
-
expectValidConfigItem('fheKeysPrefetching', 'SUPPORTED_CHAINS', 'SUPPORTED_CHAINS');
|
|
71
|
-
expectValidConfigItem('fheKeysPrefetching', 'OFF', 'OFF');
|
|
72
|
-
expectValidConfigItem('fheKeysPrefetching', undefined, 'OFF');
|
|
73
|
-
});
|
|
74
|
-
|
|
75
75
|
it('permitGeneration', () => {
|
|
76
76
|
expectInvalidConfigItem('permitGeneration', 'not-a-boolean');
|
|
77
77
|
expectInvalidConfigItem('permitGeneration', null);
|
|
@@ -144,6 +144,17 @@ describe('createCofhesdkConfigBase', () => {
|
|
|
144
144
|
expectValidConfigItem('mocks.sealOutputDelay', 1000, 1000);
|
|
145
145
|
});
|
|
146
146
|
|
|
147
|
+
it('useWorkers', () => {
|
|
148
|
+
expectInvalidConfigItem('useWorkers', 'not-a-boolean');
|
|
149
|
+
expectInvalidConfigItem('useWorkers', null);
|
|
150
|
+
expectInvalidConfigItem('useWorkers', 123);
|
|
151
|
+
expectInvalidConfigItem('useWorkers', {});
|
|
152
|
+
|
|
153
|
+
expectValidConfigItem('useWorkers', true, true);
|
|
154
|
+
expectValidConfigItem('useWorkers', false, false);
|
|
155
|
+
expectValidConfigItem('useWorkers', undefined, true); // defaults to true
|
|
156
|
+
});
|
|
157
|
+
|
|
147
158
|
it('should get config item', () => {
|
|
148
159
|
const config: CofhesdkInputConfig = {
|
|
149
160
|
supportedChains: [sepolia],
|
package/core/config.ts
CHANGED
|
@@ -5,18 +5,16 @@ import { type WalletClient } from 'viem';
|
|
|
5
5
|
import { CofhesdkError, CofhesdkErrorCode } from './error.js';
|
|
6
6
|
import { type IStorage } from './types.js';
|
|
7
7
|
|
|
8
|
+
export type CofhesdkEnvironment = 'node' | 'hardhat' | 'web' | 'react';
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
11
|
* Usable config type inferred from the schema
|
|
10
12
|
*/
|
|
11
13
|
export type CofhesdkConfig = {
|
|
14
|
+
/** Environment that the SDK is running in */
|
|
15
|
+
environment: 'node' | 'hardhat' | 'web' | 'react';
|
|
16
|
+
/** List of supported chains */
|
|
12
17
|
supportedChains: CofheChain[];
|
|
13
|
-
/**
|
|
14
|
-
* Strategy for fetching FHE keys
|
|
15
|
-
* - CONNECTED_CHAIN: Fetch keys for the connected chain (provided by the publicClient)
|
|
16
|
-
* - SUPPORTED_CHAINS: Fetch keys for all supported chains (provided by the supportedChains config)
|
|
17
|
-
* - OFF: Do not fetch keys (fetching occurs during encryptInputs)
|
|
18
|
-
* */
|
|
19
|
-
fheKeysPrefetching: 'CONNECTED_CHAIN' | 'SUPPORTED_CHAINS' | 'OFF';
|
|
20
18
|
/**
|
|
21
19
|
* How permits are generated
|
|
22
20
|
* - ON_CONNECT: Generate a permit when client.connect() is called
|
|
@@ -32,6 +30,12 @@ export type CofhesdkConfig = {
|
|
|
32
30
|
* (defaults to indexedDB on web, filesystem on node)
|
|
33
31
|
*/
|
|
34
32
|
fheKeyStorage: IStorage | null;
|
|
33
|
+
/**
|
|
34
|
+
* Whether to use Web Workers for ZK proof generation (web platform only)
|
|
35
|
+
* When enabled, heavy WASM computation is offloaded to prevent UI freezing
|
|
36
|
+
* Default: true
|
|
37
|
+
*/
|
|
38
|
+
useWorkers: boolean;
|
|
35
39
|
/** Mocks configs */
|
|
36
40
|
mocks: {
|
|
37
41
|
/**
|
|
@@ -52,10 +56,10 @@ export type CofhesdkInternalConfig = {
|
|
|
52
56
|
* Zod schema for configuration validation
|
|
53
57
|
*/
|
|
54
58
|
export const CofhesdkConfigSchema = z.object({
|
|
59
|
+
/** Environment that the SDK is running in */
|
|
60
|
+
environment: z.enum(['node', 'hardhat', 'web', 'react']).optional().default('node'),
|
|
55
61
|
/** List of supported chain configurations */
|
|
56
62
|
supportedChains: z.array(z.custom<CofheChain>()),
|
|
57
|
-
/** Strategy for fetching FHE keys */
|
|
58
|
-
fheKeysPrefetching: z.enum(['CONNECTED_CHAIN', 'SUPPORTED_CHAINS', 'OFF']).optional().default('OFF'),
|
|
59
63
|
/** How permits are generated */
|
|
60
64
|
permitGeneration: z.enum(['ON_CONNECT', 'ON_DECRYPT_HANDLES', 'MANUAL']).optional().default('ON_CONNECT'),
|
|
61
65
|
/** Default permit expiration in seconds, default is 30 days */
|
|
@@ -72,6 +76,8 @@ export const CofhesdkConfigSchema = z.object({
|
|
|
72
76
|
})
|
|
73
77
|
.or(z.null())
|
|
74
78
|
.default(null),
|
|
79
|
+
/** Whether to use Web Workers for ZK proof generation (web platform only) */
|
|
80
|
+
useWorkers: z.boolean().optional().default(true),
|
|
75
81
|
/** Mocks configs */
|
|
76
82
|
mocks: z
|
|
77
83
|
.object({
|
|
@@ -91,6 +97,7 @@ export const CofhesdkConfigSchema = z.object({
|
|
|
91
97
|
* Input config type inferred from the schema
|
|
92
98
|
*/
|
|
93
99
|
export type CofhesdkInputConfig = z.input<typeof CofhesdkConfigSchema>;
|
|
100
|
+
|
|
94
101
|
/**
|
|
95
102
|
* Creates and validates a cofhesdk configuration (base implementation)
|
|
96
103
|
* @param config - The configuration object to validate
|
|
@@ -3,13 +3,13 @@ import { type Permit, PermitUtils } from '@/permits';
|
|
|
3
3
|
|
|
4
4
|
import { FheTypes, type UnsealedItem } from '../types.js';
|
|
5
5
|
import { getThresholdNetworkUrlOrThrow } from '../config.js';
|
|
6
|
-
import { type Result, resultWrapper } from '../result.js';
|
|
7
6
|
import { CofhesdkError, CofhesdkErrorCode } from '../error.js';
|
|
8
7
|
import { permits } from '../permits.js';
|
|
9
8
|
import { isValidUtype, convertViaUtype } from './decryptUtils.js';
|
|
10
9
|
import { BaseBuilder, type BaseBuilderParams } from '../baseBuilder.js';
|
|
11
10
|
import { cofheMocksSealOutput } from './cofheMocksSealOutput.js';
|
|
12
|
-
import {
|
|
11
|
+
// import { tnSealOutputV1 } from './tnSealOutputV1.js';
|
|
12
|
+
import { tnSealOutputV2 } from './tnSealOutputV2.js';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* API
|
|
@@ -26,7 +26,7 @@ import { tnSealOutput } from './tnSealOutput.js';
|
|
|
26
26
|
* If permitHash not set, uses chainId and account to get active permit
|
|
27
27
|
* If permit is set, uses permit to decrypt regardless of chainId, account, or permitHash
|
|
28
28
|
*
|
|
29
|
-
* Returns
|
|
29
|
+
* Returns the unsealed item.
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
32
|
type DecryptHandlesBuilderParams<U extends FheTypes> = BaseBuilderParams & {
|
|
@@ -151,9 +151,9 @@ export class DecryptHandlesBuilder<U extends FheTypes> extends BaseBuilder {
|
|
|
151
151
|
return this.permit;
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
private getThresholdNetworkUrl(
|
|
155
|
-
|
|
156
|
-
return getThresholdNetworkUrlOrThrow(config, chainId);
|
|
154
|
+
private async getThresholdNetworkUrl(): Promise<string> {
|
|
155
|
+
this.assertChainId();
|
|
156
|
+
return getThresholdNetworkUrlOrThrow(this.config, this.chainId);
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
private validateUtypeOrThrow(): void {
|
|
@@ -170,20 +170,20 @@ export class DecryptHandlesBuilder<U extends FheTypes> extends BaseBuilder {
|
|
|
170
170
|
private async getResolvedPermit(): Promise<Permit> {
|
|
171
171
|
if (this.permit) return this.permit;
|
|
172
172
|
|
|
173
|
-
|
|
174
|
-
|
|
173
|
+
this.assertChainId();
|
|
174
|
+
this.assertAccount();
|
|
175
175
|
|
|
176
176
|
// Fetch with permit hash
|
|
177
177
|
if (this.permitHash) {
|
|
178
|
-
const permit = await permits.getPermit(chainId, account, this.permitHash);
|
|
178
|
+
const permit = await permits.getPermit(this.chainId, this.account, this.permitHash);
|
|
179
179
|
if (!permit) {
|
|
180
180
|
throw new CofhesdkError({
|
|
181
181
|
code: CofhesdkErrorCode.PermitNotFound,
|
|
182
|
-
message: `Permit with hash <${this.permitHash}> not found for account <${account}> and chainId <${chainId}>`,
|
|
182
|
+
message: `Permit with hash <${this.permitHash}> not found for account <${this.account}> and chainId <${this.chainId}>`,
|
|
183
183
|
hint: 'Ensure the permit exists and is valid.',
|
|
184
184
|
context: {
|
|
185
|
-
chainId,
|
|
186
|
-
account,
|
|
185
|
+
chainId: this.chainId,
|
|
186
|
+
account: this.account,
|
|
187
187
|
permitHash: this.permitHash,
|
|
188
188
|
},
|
|
189
189
|
});
|
|
@@ -192,15 +192,15 @@ export class DecryptHandlesBuilder<U extends FheTypes> extends BaseBuilder {
|
|
|
192
192
|
}
|
|
193
193
|
|
|
194
194
|
// Fetch with active permit
|
|
195
|
-
const permit = await permits.getActivePermit(chainId, account);
|
|
195
|
+
const permit = await permits.getActivePermit(this.chainId, this.account);
|
|
196
196
|
if (!permit) {
|
|
197
197
|
throw new CofhesdkError({
|
|
198
198
|
code: CofhesdkErrorCode.PermitNotFound,
|
|
199
|
-
message: `Active permit not found for chainId <${chainId}> and account <${account}>`,
|
|
199
|
+
message: `Active permit not found for chainId <${this.chainId}> and account <${this.account}>`,
|
|
200
200
|
hint: 'Ensure a permit exists for this account on this chain.',
|
|
201
201
|
context: {
|
|
202
|
-
chainId,
|
|
203
|
-
account,
|
|
202
|
+
chainId: this.chainId,
|
|
203
|
+
account: this.account,
|
|
204
204
|
},
|
|
205
205
|
});
|
|
206
206
|
}
|
|
@@ -211,18 +211,23 @@ export class DecryptHandlesBuilder<U extends FheTypes> extends BaseBuilder {
|
|
|
211
211
|
* On hardhat, interact with MockZkVerifier contract instead of CoFHE
|
|
212
212
|
*/
|
|
213
213
|
private async mocksSealOutput(permit: Permit): Promise<bigint> {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
214
|
+
this.assertPublicClient();
|
|
215
|
+
|
|
216
|
+
const mocksSealOutputDelay = this.config.mocks.sealOutputDelay;
|
|
217
|
+
return cofheMocksSealOutput(this.ctHash, this.utype, permit, this.publicClient, mocksSealOutputDelay);
|
|
217
218
|
}
|
|
218
219
|
|
|
219
220
|
/**
|
|
220
221
|
* In the production context, perform a true decryption with the CoFHE coprocessor.
|
|
221
222
|
*/
|
|
222
|
-
private async productionSealOutput(
|
|
223
|
-
|
|
223
|
+
private async productionSealOutput(permit: Permit): Promise<bigint> {
|
|
224
|
+
this.assertChainId();
|
|
225
|
+
this.assertPublicClient();
|
|
226
|
+
|
|
227
|
+
const thresholdNetworkUrl = await this.getThresholdNetworkUrl();
|
|
224
228
|
const permission = PermitUtils.getPermission(permit, true);
|
|
225
|
-
const sealed = await
|
|
229
|
+
// const sealed = await tnSealOutputV1(this.ctHash, this.chainId, permission, thresholdNetworkUrl);
|
|
230
|
+
const sealed = await tnSealOutputV2(this.ctHash, this.chainId, permission, thresholdNetworkUrl);
|
|
226
231
|
return PermitUtils.unseal(permit, sealed);
|
|
227
232
|
}
|
|
228
233
|
|
|
@@ -246,36 +251,37 @@ export class DecryptHandlesBuilder<U extends FheTypes> extends BaseBuilder {
|
|
|
246
251
|
*
|
|
247
252
|
* @returns The unsealed item.
|
|
248
253
|
*/
|
|
249
|
-
decrypt(): Promise<
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
await this.requireConnectedOrThrow();
|
|
254
|
+
async decrypt(): Promise<UnsealedItem<U>> {
|
|
255
|
+
// Ensure utype is valid
|
|
256
|
+
this.validateUtypeOrThrow();
|
|
253
257
|
|
|
254
|
-
|
|
255
|
-
|
|
258
|
+
// Resolve permit
|
|
259
|
+
const permit = await this.getResolvedPermit();
|
|
256
260
|
|
|
257
|
-
|
|
258
|
-
|
|
261
|
+
// Ensure permit validity
|
|
262
|
+
// TODO: This doesn't validate permit expiration
|
|
263
|
+
// TODO: This doesn't throw, returns a validation result instead
|
|
264
|
+
PermitUtils.validate(permit);
|
|
259
265
|
|
|
260
|
-
|
|
261
|
-
|
|
266
|
+
// TODO: Add this further validation step for the permit
|
|
267
|
+
// TODO: Ensure this throws if the permit is invalid
|
|
268
|
+
PermitUtils.isValid(permit);
|
|
262
269
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
270
|
+
// Extract chainId from signed permit
|
|
271
|
+
// Use this chainId to fetch the threshold network URL since this.chainId may be undefined
|
|
272
|
+
const chainId = permit._signedDomain!.chainId;
|
|
266
273
|
|
|
267
|
-
|
|
268
|
-
|
|
274
|
+
// Check permit validity on-chain
|
|
275
|
+
// TODO: PermitUtils.validateOnChain(permit, this.publicClient);
|
|
269
276
|
|
|
270
|
-
|
|
277
|
+
let unsealed: bigint;
|
|
271
278
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
279
|
+
if (chainId === hardhat.id) {
|
|
280
|
+
unsealed = await this.mocksSealOutput(permit);
|
|
281
|
+
} else {
|
|
282
|
+
unsealed = await this.productionSealOutput(permit);
|
|
283
|
+
}
|
|
277
284
|
|
|
278
|
-
|
|
279
|
-
});
|
|
285
|
+
return convertViaUtype(this.utype, unsealed);
|
|
280
286
|
}
|
|
281
287
|
}
|
|
@@ -2,7 +2,7 @@ import { type Permission, type EthEncryptedData } from '@/permits';
|
|
|
2
2
|
|
|
3
3
|
import { CofhesdkError, CofhesdkErrorCode } from '../error.js';
|
|
4
4
|
|
|
5
|
-
export async function
|
|
5
|
+
export async function tnSealOutputV1(
|
|
6
6
|
ctHash: bigint,
|
|
7
7
|
chainId: number,
|
|
8
8
|
permission: Permission,
|