@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/core/error.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export enum
|
|
1
|
+
export enum CofheErrorCode {
|
|
2
2
|
InternalError = 'INTERNAL_ERROR',
|
|
3
3
|
UnknownEnvironment = 'UNKNOWN_ENVIRONMENT',
|
|
4
4
|
InitTfheFailed = 'INIT_TFHE_FAILED',
|
|
@@ -44,8 +44,8 @@ export enum CofhesdkErrorCode {
|
|
|
44
44
|
RehydrateKeysStoreFailed = 'REHYDRATE_KEYS_STORE_FAILED',
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
export type
|
|
48
|
-
code:
|
|
47
|
+
export type CofheErrorParams = {
|
|
48
|
+
code: CofheErrorCode;
|
|
49
49
|
message: string;
|
|
50
50
|
cause?: Error;
|
|
51
51
|
hint?: string;
|
|
@@ -53,28 +53,28 @@ export type CofhesdkErrorParams = {
|
|
|
53
53
|
};
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
|
-
*
|
|
56
|
+
* CofheError class
|
|
57
57
|
* This class is used to create errors that are specific to the CoFHE SDK
|
|
58
58
|
* It extends the Error class and adds a code, cause, hint, and context
|
|
59
59
|
* The code is used to identify the type of error
|
|
60
|
-
* The cause is used to indicate the inner error that caused the
|
|
60
|
+
* The cause is used to indicate the inner error that caused the CofheError
|
|
61
61
|
* The hint is used to provide a hint about how to fix the error
|
|
62
62
|
* The context is used to provide additional context about the state that caused the error
|
|
63
63
|
* The serialize method is used to serialize the error to a JSON string
|
|
64
64
|
* The toString method is used to provide a human-readable string representation of the error
|
|
65
65
|
*/
|
|
66
|
-
export class
|
|
67
|
-
public readonly code:
|
|
66
|
+
export class CofheError extends Error {
|
|
67
|
+
public readonly code: CofheErrorCode;
|
|
68
68
|
public readonly cause?: Error;
|
|
69
69
|
public readonly hint?: string;
|
|
70
70
|
public readonly context?: Record<string, unknown>;
|
|
71
71
|
|
|
72
|
-
constructor({ code, message, cause, hint, context }:
|
|
72
|
+
constructor({ code, message, cause, hint, context }: CofheErrorParams) {
|
|
73
73
|
// If there's a cause, append its message to provide full context
|
|
74
74
|
const fullMessage = cause ? `${message} | Caused by: ${cause.message}` : message;
|
|
75
75
|
|
|
76
76
|
super(fullMessage);
|
|
77
|
-
this.name = '
|
|
77
|
+
this.name = 'CofheError';
|
|
78
78
|
this.code = code;
|
|
79
79
|
this.cause = cause;
|
|
80
80
|
this.hint = hint;
|
|
@@ -82,22 +82,22 @@ export class CofhesdkError extends Error {
|
|
|
82
82
|
|
|
83
83
|
// Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
84
84
|
if (Error.captureStackTrace) {
|
|
85
|
-
Error.captureStackTrace(this,
|
|
85
|
+
Error.captureStackTrace(this, CofheError);
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
|
-
* Creates a
|
|
91
|
-
* If the error is a
|
|
92
|
-
* If a wrapperError is provided, it is used to create the new
|
|
90
|
+
* Creates a CofheError from an unknown error
|
|
91
|
+
* If the error is a CofheError, it is returned unchanged, else a new CofheError is created
|
|
92
|
+
* If a wrapperError is provided, it is used to create the new CofheError, else a default is used
|
|
93
93
|
*/
|
|
94
|
-
static fromError(error: unknown, wrapperError?:
|
|
95
|
-
if (
|
|
94
|
+
static fromError(error: unknown, wrapperError?: CofheErrorParams): CofheError {
|
|
95
|
+
if (isCofheError(error)) return error;
|
|
96
96
|
|
|
97
97
|
const cause = error instanceof Error ? error : new Error(`${error}`);
|
|
98
98
|
|
|
99
|
-
return new
|
|
100
|
-
code: wrapperError?.code ??
|
|
99
|
+
return new CofheError({
|
|
100
|
+
code: wrapperError?.code ?? CofheErrorCode.InternalError,
|
|
101
101
|
message: wrapperError?.message ?? 'An internal error occurred',
|
|
102
102
|
hint: wrapperError?.hint,
|
|
103
103
|
context: wrapperError?.context,
|
|
@@ -165,4 +165,4 @@ const bigintSafeJsonStringify = (value: unknown): string => {
|
|
|
165
165
|
});
|
|
166
166
|
};
|
|
167
167
|
|
|
168
|
-
export const
|
|
168
|
+
export const isCofheError = (error: unknown): error is CofheError => error instanceof CofheError;
|
package/core/fetchKeys.test.ts
CHANGED
|
@@ -5,11 +5,11 @@ import { sepolia, arbSepolia } from '@/chains';
|
|
|
5
5
|
|
|
6
6
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
7
7
|
import { fetchKeys } from './fetchKeys.js';
|
|
8
|
-
import { type
|
|
8
|
+
import { type CofheConfig, createCofheConfigBase } from './config.js';
|
|
9
9
|
import { createKeysStore, type KeysStorage } from './keyStore.js';
|
|
10
10
|
|
|
11
11
|
describe('fetchKeys', () => {
|
|
12
|
-
let config:
|
|
12
|
+
let config: CofheConfig;
|
|
13
13
|
let mockTfhePublicKeyDeserializer: any;
|
|
14
14
|
let mockCompactPkeCrsDeserializer: any;
|
|
15
15
|
let keysStorage: KeysStorage;
|
|
@@ -19,7 +19,7 @@ describe('fetchKeys', () => {
|
|
|
19
19
|
vi.clearAllMocks();
|
|
20
20
|
|
|
21
21
|
// Setup config with real chains
|
|
22
|
-
config =
|
|
22
|
+
config = createCofheConfigBase({
|
|
23
23
|
supportedChains: [sepolia, arbSepolia],
|
|
24
24
|
});
|
|
25
25
|
|
package/core/fetchKeys.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { hardhat } from '@/chains';
|
|
2
2
|
|
|
3
|
-
import { type
|
|
3
|
+
import { type CofheConfig, getCoFheUrlOrThrow } from './config.js';
|
|
4
4
|
import { type KeysStorage } from './keyStore.js';
|
|
5
5
|
|
|
6
6
|
const PUBLIC_KEY_LENGTH_MIN = 15_000;
|
|
@@ -118,7 +118,7 @@ const fetchCrs = async (
|
|
|
118
118
|
/**
|
|
119
119
|
* Retrieves the FHE public key and the CRS from the provider.
|
|
120
120
|
* If the key/crs already exists in the store it is returned, else it is fetched, stored, and returned
|
|
121
|
-
* @param {
|
|
121
|
+
* @param {CofheConfig} config - The configuration object for the CoFHE client
|
|
122
122
|
* @param {number} chainId - The chain to fetch the FHE key for, if no chainId provided, undefined is returned
|
|
123
123
|
* @param securityZone - The security zone for which to retrieve the key (default 0).
|
|
124
124
|
* @param tfhePublicKeyDeserializer - The serializer for the FHE public key (used for validation).
|
|
@@ -127,7 +127,7 @@ const fetchCrs = async (
|
|
|
127
127
|
* @returns {Promise<[[string, boolean], [string, boolean]]>} - A promise that resolves to [[fheKey, fheKeyFetchedFromCoFHE], [crs, crsFetchedFromCoFHE]]
|
|
128
128
|
*/
|
|
129
129
|
export const fetchKeys = async (
|
|
130
|
-
config:
|
|
130
|
+
config: CofheConfig,
|
|
131
131
|
chainId: number,
|
|
132
132
|
securityZone: number = 0,
|
|
133
133
|
tfhePublicKeyDeserializer: FheKeyDeserializer,
|
package/core/index.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
// Client (base implementations)
|
|
2
|
-
export {
|
|
2
|
+
export { createCofheClientBase, InitialConnectStore as CONNECT_STORE_DEFAULTS } from './client.js';
|
|
3
3
|
|
|
4
4
|
// Configuration (base implementations)
|
|
5
|
-
export {
|
|
6
|
-
export type {
|
|
5
|
+
export { createCofheConfigBase, getCofheConfigItem } from './config.js';
|
|
6
|
+
export type { CofheConfig, CofheInputConfig, CofheInternalConfig } from './config.js';
|
|
7
7
|
|
|
8
8
|
// Types
|
|
9
9
|
export type {
|
|
10
10
|
// Client types
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
CofheClient as CofheClient,
|
|
12
|
+
CofheClientParams as CofheClientParams,
|
|
13
|
+
CofheClientConnectionState as CofheClientConnectionState,
|
|
14
|
+
CofheClientPermits as CofheClientPermits,
|
|
15
15
|
} from './clientTypes.js';
|
|
16
16
|
|
|
17
17
|
export type {
|
|
@@ -59,8 +59,8 @@ export {
|
|
|
59
59
|
} from './types.js';
|
|
60
60
|
|
|
61
61
|
// Error handling
|
|
62
|
-
export {
|
|
63
|
-
export type {
|
|
62
|
+
export { CofheError, CofheErrorCode, isCofheError } from './error.js';
|
|
63
|
+
export type { CofheErrorParams } from './error.js';
|
|
64
64
|
|
|
65
65
|
// Key fetching
|
|
66
66
|
export { fetchKeys } from './fetchKeys.js';
|
|
@@ -72,7 +72,9 @@ export type { KeysStorage, KeysStore } from './keyStore.js';
|
|
|
72
72
|
|
|
73
73
|
// Builders (exported via client, but can be imported directly for typing)
|
|
74
74
|
export { EncryptInputsBuilder } from './encrypt/encryptInputsBuilder.js';
|
|
75
|
-
export {
|
|
75
|
+
export { DecryptForViewBuilder } from './decrypt/decryptForViewBuilder.js';
|
|
76
|
+
export { DecryptForTxBuilder } from './decrypt/decryptForTxBuilder.js';
|
|
77
|
+
export type { DecryptForTxResult } from './decrypt/decryptForTxBuilder.js';
|
|
76
78
|
|
|
77
79
|
// ZK utilities
|
|
78
80
|
export type {
|
|
@@ -89,7 +91,8 @@ export {
|
|
|
89
91
|
MOCKS_ZK_VERIFIER_ADDRESS,
|
|
90
92
|
MOCKS_ZK_VERIFIER_SIGNER_ADDRESS,
|
|
91
93
|
MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY,
|
|
92
|
-
|
|
94
|
+
MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
|
|
95
|
+
MOCKS_THRESHOLD_NETWORK_ADDRESS,
|
|
93
96
|
TEST_BED_ADDRESS,
|
|
94
97
|
} from './consts.js';
|
|
95
98
|
|
package/core/utils.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type PublicClient, type WalletClient } from 'viem';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { CofheError, CofheErrorCode } from './error.js';
|
|
3
|
+
import { FheTypes } from './types.js';
|
|
4
4
|
|
|
5
5
|
export const toHexString = (bytes: Uint8Array) =>
|
|
6
6
|
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
|
|
@@ -50,15 +50,15 @@ export async function getPublicClientChainID(publicClient: PublicClient) {
|
|
|
50
50
|
try {
|
|
51
51
|
chainId = publicClient.chain?.id ?? (await publicClient.getChainId());
|
|
52
52
|
} catch (e) {
|
|
53
|
-
throw new
|
|
54
|
-
code:
|
|
53
|
+
throw new CofheError({
|
|
54
|
+
code: CofheErrorCode.PublicWalletGetChainIdFailed,
|
|
55
55
|
message: 'getting chain ID from public client failed',
|
|
56
56
|
cause: e instanceof Error ? e : undefined,
|
|
57
57
|
});
|
|
58
58
|
}
|
|
59
59
|
if (chainId === null) {
|
|
60
|
-
throw new
|
|
61
|
-
code:
|
|
60
|
+
throw new CofheError({
|
|
61
|
+
code: CofheErrorCode.PublicWalletGetChainIdFailed,
|
|
62
62
|
message: 'chain ID from public client is null',
|
|
63
63
|
});
|
|
64
64
|
}
|
|
@@ -73,15 +73,15 @@ export async function getWalletClientAccount(walletClient: WalletClient) {
|
|
|
73
73
|
address = (await walletClient.getAddresses())?.[0];
|
|
74
74
|
}
|
|
75
75
|
} catch (e) {
|
|
76
|
-
throw new
|
|
77
|
-
code:
|
|
76
|
+
throw new CofheError({
|
|
77
|
+
code: CofheErrorCode.PublicWalletGetAddressesFailed,
|
|
78
78
|
message: 'getting address from wallet client failed',
|
|
79
79
|
cause: e instanceof Error ? e : undefined,
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
82
|
if (!address) {
|
|
83
|
-
throw new
|
|
84
|
-
code:
|
|
83
|
+
throw new CofheError({
|
|
84
|
+
code: CofheErrorCode.PublicWalletGetAddressesFailed,
|
|
85
85
|
message: 'address from wallet client is null',
|
|
86
86
|
});
|
|
87
87
|
}
|