@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
package/core/types.ts
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
type PermitUtils,
|
|
9
|
-
} from '@/permits';
|
|
10
|
-
|
|
11
|
-
import { type PublicClient, type WalletClient } from 'viem';
|
|
12
|
-
import { type CofhesdkConfig } from './config.js';
|
|
13
|
-
import { DecryptHandlesBuilder } from './decrypt/decryptHandleBuilder.js';
|
|
14
|
-
import { EncryptInputsBuilder } from './encrypt/encryptInputsBuilder.js';
|
|
15
|
-
import { type ZkBuilderAndCrsGenerator } from './encrypt/zkPackProveVerify.js';
|
|
16
|
-
import { type FheKeyDeserializer } from './fetchKeys.js';
|
|
17
|
-
import { permits } from './permits.js';
|
|
18
|
-
import { type Result } from './result.js';
|
|
19
|
-
|
|
20
|
-
// UTILS
|
|
1
|
+
export type TfheInitializer = () => Promise<boolean>;
|
|
2
|
+
|
|
3
|
+
export interface IStorage {
|
|
4
|
+
getItem: (name: string) => Promise<any>;
|
|
5
|
+
setItem: (name: string, value: any) => Promise<void>;
|
|
6
|
+
removeItem: (name: string) => Promise<void>;
|
|
7
|
+
}
|
|
21
8
|
|
|
22
9
|
export type Primitive = null | undefined | string | number | boolean | symbol | bigint;
|
|
23
10
|
export type LiteralToPrimitive<T> = T extends number
|
|
@@ -36,89 +23,11 @@ export type LiteralToPrimitive<T> = T extends number
|
|
|
36
23
|
? undefined
|
|
37
24
|
: never;
|
|
38
25
|
|
|
39
|
-
// CLIENT
|
|
40
|
-
|
|
41
|
-
export type CofhesdkClient = {
|
|
42
|
-
// --- state access ---
|
|
43
|
-
getSnapshot(): CofhesdkClientConnectionState;
|
|
44
|
-
subscribe(listener: Listener): () => void;
|
|
45
|
-
|
|
46
|
-
// --- initialization results ---
|
|
47
|
-
// (functions that may be run during initialization based on config)
|
|
48
|
-
readonly initializationResults: {
|
|
49
|
-
keyFetchResult: Promise<Result<boolean>>;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
// --- convenience flags (read-only) ---
|
|
53
|
-
readonly connected: boolean;
|
|
54
|
-
readonly connecting: boolean;
|
|
55
|
-
|
|
56
|
-
// --- config & platform-specific ---
|
|
57
|
-
readonly config: CofhesdkConfig;
|
|
58
|
-
|
|
59
|
-
connect(publicClient: PublicClient, walletClient: WalletClient): Promise<Result<boolean>>;
|
|
60
|
-
/**
|
|
61
|
-
* Types docstring
|
|
62
|
-
*/
|
|
63
|
-
encryptInputs<T extends EncryptableItem[]>(inputs: [...T]): EncryptInputsBuilder<[...T]>;
|
|
64
|
-
decryptHandle<U extends FheTypes>(ctHash: bigint, utype: U): DecryptHandlesBuilder<U>;
|
|
65
|
-
permits: CofhesdkClientPermits;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
export type CofhesdkClientConnectionState = {
|
|
69
|
-
connected: boolean;
|
|
70
|
-
connecting: boolean;
|
|
71
|
-
connectError: unknown | undefined;
|
|
72
|
-
chainId: number | undefined;
|
|
73
|
-
account: string | undefined;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
type Listener = (snapshot: CofhesdkClientConnectionState) => void;
|
|
77
|
-
|
|
78
|
-
export type CofhesdkClientPermits = {
|
|
79
|
-
getSnapshot: typeof permits.getSnapshot;
|
|
80
|
-
subscribe: typeof permits.subscribe;
|
|
81
|
-
|
|
82
|
-
// Creation methods (require connection, no params)
|
|
83
|
-
createSelf: (options: CreateSelfPermitOptions) => Promise<Result<Permit>>;
|
|
84
|
-
createSharing: (options: CreateSharingPermitOptions) => Promise<Result<Permit>>;
|
|
85
|
-
importShared: (options: ImportSharedPermitOptions | any | string) => Promise<Result<Permit>>;
|
|
86
|
-
|
|
87
|
-
// Retrieval methods (chainId/account optional)
|
|
88
|
-
getPermit: (hash: string, chainId?: number, account?: string) => Promise<Result<Permit | undefined>>;
|
|
89
|
-
getPermits: (chainId?: number, account?: string) => Promise<Result<Record<string, Permit>>>;
|
|
90
|
-
getActivePermit: (chainId?: number, account?: string) => Promise<Result<Permit | undefined>>;
|
|
91
|
-
getActivePermitHash: (chainId?: number, account?: string) => Promise<Result<string | undefined>>;
|
|
92
|
-
|
|
93
|
-
// Mutation methods (chainId/account optional)
|
|
94
|
-
selectActivePermit: (hash: string, chainId?: number, account?: string) => Promise<Result<void>>;
|
|
95
|
-
removePermit: (hash: string, chainId?: number, account?: string) => Promise<Result<void>>;
|
|
96
|
-
removeActivePermit: (chainId?: number, account?: string) => Promise<Result<void>>;
|
|
97
|
-
|
|
98
|
-
// Utils
|
|
99
|
-
getHash: typeof PermitUtils.getHash;
|
|
100
|
-
serialize: typeof PermitUtils.serialize;
|
|
101
|
-
deserialize: typeof PermitUtils.deserialize;
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
export type TfheInitializer = () => Promise<boolean>;
|
|
105
|
-
|
|
106
|
-
export type CofhesdkClientParams = {
|
|
107
|
-
config: CofhesdkConfig;
|
|
108
|
-
zkBuilderAndCrsGenerator: ZkBuilderAndCrsGenerator;
|
|
109
|
-
tfhePublicKeyDeserializer: FheKeyDeserializer;
|
|
110
|
-
compactPkeCrsDeserializer: FheKeyDeserializer;
|
|
111
|
-
initTfhe: TfheInitializer;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
export interface IStorage {
|
|
115
|
-
getItem: (name: string) => Promise<any>;
|
|
116
|
-
setItem: (name: string, value: any) => Promise<void>;
|
|
117
|
-
removeItem: (name: string) => Promise<void>;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
26
|
// FHE TYPES
|
|
121
27
|
|
|
28
|
+
export const FheTypeValues = ['bool', 'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'address'] as const;
|
|
29
|
+
export type FheTypeValue = (typeof FheTypeValues)[number];
|
|
30
|
+
|
|
122
31
|
export enum FheTypes {
|
|
123
32
|
Bool = 0,
|
|
124
33
|
Uint4 = 1,
|
|
@@ -152,6 +61,19 @@ export enum FheTypes {
|
|
|
152
61
|
Int256 = 29,
|
|
153
62
|
}
|
|
154
63
|
|
|
64
|
+
export const FheTypeValueUtype = {
|
|
65
|
+
bool: FheTypes.Bool,
|
|
66
|
+
uint8: FheTypes.Uint8,
|
|
67
|
+
uint16: FheTypes.Uint16,
|
|
68
|
+
uint32: FheTypes.Uint32,
|
|
69
|
+
uint64: FheTypes.Uint64,
|
|
70
|
+
uint128: FheTypes.Uint128,
|
|
71
|
+
address: FheTypes.Uint160,
|
|
72
|
+
} as const satisfies Record<FheTypeValue, FheTypes>;
|
|
73
|
+
export type FheTypeValueUtypeMap<T extends FheTypeValue> = T extends keyof typeof FheTypeValueUtype
|
|
74
|
+
? (typeof FheTypeValueUtype)[T]
|
|
75
|
+
: never;
|
|
76
|
+
|
|
155
77
|
/**
|
|
156
78
|
* List of All FHE uint types (excludes bool and address)
|
|
157
79
|
*/
|
|
@@ -189,12 +111,19 @@ export type EncryptedNumber = {
|
|
|
189
111
|
securityZone: number;
|
|
190
112
|
};
|
|
191
113
|
|
|
192
|
-
export type EncryptedItemInput = {
|
|
114
|
+
export type EncryptedItemInput<TSignature = string> = {
|
|
193
115
|
ctHash: bigint;
|
|
194
116
|
securityZone: number;
|
|
195
117
|
utype: FheTypes;
|
|
196
|
-
signature:
|
|
118
|
+
signature: TSignature;
|
|
197
119
|
};
|
|
120
|
+
|
|
121
|
+
export function assertCorrectEncryptedItemInput(
|
|
122
|
+
input: EncryptedItemInput
|
|
123
|
+
): asserts input is EncryptedItemInput<`0x${string}`> {
|
|
124
|
+
if (!input.signature.startsWith('0x')) throw new Error('Signature must be a hex string starting with 0x');
|
|
125
|
+
}
|
|
126
|
+
|
|
198
127
|
export type EncryptedBoolInput = EncryptedItemInput & {
|
|
199
128
|
utype: FheTypes.Bool;
|
|
200
129
|
};
|
|
@@ -213,66 +142,156 @@ export type EncryptedUint64Input = EncryptedItemInput & {
|
|
|
213
142
|
export type EncryptedUint128Input = EncryptedItemInput & {
|
|
214
143
|
utype: FheTypes.Uint128;
|
|
215
144
|
};
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
145
|
+
// [U256-DISABLED]
|
|
146
|
+
// export type EncryptedUint256Input = EncryptedItemInput & {
|
|
147
|
+
// utype: FheTypes.Uint256;
|
|
148
|
+
// };
|
|
219
149
|
export type EncryptedAddressInput = EncryptedItemInput & {
|
|
220
150
|
utype: FheTypes.Uint160;
|
|
221
151
|
};
|
|
222
152
|
|
|
223
|
-
export type
|
|
224
|
-
data:
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
export type EncryptableUint8 = {
|
|
228
|
-
data: string | bigint;
|
|
229
|
-
utype: FheTypes.Uint8;
|
|
230
|
-
};
|
|
231
|
-
export type EncryptableUint16 = {
|
|
232
|
-
data: string | bigint;
|
|
233
|
-
utype: FheTypes.Uint16;
|
|
234
|
-
};
|
|
235
|
-
export type EncryptableUint32 = {
|
|
236
|
-
data: string | bigint;
|
|
237
|
-
utype: FheTypes.Uint32;
|
|
153
|
+
export type EncryptableBase<U extends FheTypes, D> = {
|
|
154
|
+
data: D;
|
|
155
|
+
securityZone: number;
|
|
156
|
+
utype: U;
|
|
238
157
|
};
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
158
|
+
|
|
159
|
+
export type EncryptableBool = EncryptableBase<FheTypes.Bool, boolean>;
|
|
160
|
+
export type EncryptableUint8 = EncryptableBase<FheTypes.Uint8, string | bigint>;
|
|
161
|
+
export type EncryptableUint16 = EncryptableBase<FheTypes.Uint16, string | bigint>;
|
|
162
|
+
export type EncryptableUint32 = EncryptableBase<FheTypes.Uint32, string | bigint>;
|
|
163
|
+
export type EncryptableUint64 = EncryptableBase<FheTypes.Uint64, string | bigint>;
|
|
164
|
+
export type EncryptableUint128 = EncryptableBase<FheTypes.Uint128, string | bigint>;
|
|
165
|
+
// [U256-DISABLED]
|
|
166
|
+
// export type EncryptableUint256 = EncryptableBase<FheTypes.Uint256, string | bigint>;
|
|
167
|
+
export type EncryptableAddress = EncryptableBase<FheTypes.Uint160, string | bigint>;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Maps FheTypeValue to its corresponding Encryptable type
|
|
171
|
+
* If a new FheTypeValue is added, this type must be updated to include it
|
|
172
|
+
*/
|
|
173
|
+
type EncryptableTypeMap = {
|
|
174
|
+
bool: EncryptableBool;
|
|
175
|
+
address: EncryptableAddress;
|
|
176
|
+
uint8: EncryptableUint8;
|
|
177
|
+
uint16: EncryptableUint16;
|
|
178
|
+
uint32: EncryptableUint32;
|
|
179
|
+
uint64: EncryptableUint64;
|
|
180
|
+
uint128: EncryptableUint128;
|
|
181
|
+
// [U256-DISABLED]
|
|
182
|
+
// uint256: EncryptableUint256;
|
|
242
183
|
};
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Ensures all FheTypeValue keys are present as factory functions.
|
|
187
|
+
* TypeScript will error if EncryptableTypeMap is missing any FheTypeValue key.
|
|
188
|
+
*/
|
|
189
|
+
type EncryptableFactories = {
|
|
190
|
+
[K in FheTypeValue]: (data: EncryptableTypeMap[K]['data'], securityZone?: number) => EncryptableTypeMap[K];
|
|
246
191
|
};
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
192
|
+
|
|
193
|
+
type EncryptableFactory = EncryptableFactories & {
|
|
194
|
+
create: {
|
|
195
|
+
(type: 'bool', data: EncryptableBool['data'], securityZone?: number): EncryptableBool;
|
|
196
|
+
(type: 'address', data: EncryptableAddress['data'], securityZone?: number): EncryptableAddress;
|
|
197
|
+
(type: 'uint8', data: EncryptableUint8['data'], securityZone?: number): EncryptableUint8;
|
|
198
|
+
(type: 'uint16', data: EncryptableUint16['data'], securityZone?: number): EncryptableUint16;
|
|
199
|
+
(type: 'uint32', data: EncryptableUint32['data'], securityZone?: number): EncryptableUint32;
|
|
200
|
+
(type: 'uint64', data: EncryptableUint64['data'], securityZone?: number): EncryptableUint64;
|
|
201
|
+
(type: 'uint128', data: EncryptableUint128['data'], securityZone?: number): EncryptableUint128;
|
|
202
|
+
// [U256-DISABLED]
|
|
203
|
+
// (type: 'uint256', data: EncryptableUint256['data'], securityZone?: number): EncryptableUint256;
|
|
204
|
+
|
|
205
|
+
(type: FheTypeValue, data: EncryptableItem['data'], securityZone?: number): EncryptableItem;
|
|
206
|
+
};
|
|
255
207
|
};
|
|
256
208
|
|
|
257
|
-
|
|
258
|
-
bool: (data: EncryptableBool['data'], securityZone = 0) =>
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
({ data, securityZone, utype: FheTypes.Uint16 }) as EncryptableUint16,
|
|
266
|
-
uint32: (data: EncryptableUint32['data'], securityZone = 0) =>
|
|
267
|
-
({ data, securityZone, utype: FheTypes.Uint32 }) as EncryptableUint32,
|
|
268
|
-
uint64: (data: EncryptableUint64['data'], securityZone = 0) =>
|
|
269
|
-
({ data, securityZone, utype: FheTypes.Uint64 }) as EncryptableUint64,
|
|
270
|
-
uint128: (data: EncryptableUint128['data'], securityZone = 0) =>
|
|
271
|
-
({ data, securityZone, utype: FheTypes.Uint128 }) as EncryptableUint128,
|
|
209
|
+
const EncryptableFactoriesImpl = {
|
|
210
|
+
bool: (data: EncryptableBool['data'], securityZone = 0) => ({ data, securityZone, utype: FheTypes.Bool }),
|
|
211
|
+
address: (data: EncryptableAddress['data'], securityZone = 0) => ({ data, securityZone, utype: FheTypes.Uint160 }),
|
|
212
|
+
uint8: (data: EncryptableUint8['data'], securityZone = 0) => ({ data, securityZone, utype: FheTypes.Uint8 }),
|
|
213
|
+
uint16: (data: EncryptableUint16['data'], securityZone = 0) => ({ data, securityZone, utype: FheTypes.Uint16 }),
|
|
214
|
+
uint32: (data: EncryptableUint32['data'], securityZone = 0) => ({ data, securityZone, utype: FheTypes.Uint32 }),
|
|
215
|
+
uint64: (data: EncryptableUint64['data'], securityZone = 0) => ({ data, securityZone, utype: FheTypes.Uint64 }),
|
|
216
|
+
uint128: (data: EncryptableUint128['data'], securityZone = 0) => ({ data, securityZone, utype: FheTypes.Uint128 }),
|
|
272
217
|
// [U256-DISABLED]
|
|
273
218
|
// uint256: (data: EncryptableUint256['data'], securityZone = 0) =>
|
|
274
219
|
// ({ data, securityZone, utype: FheTypes.Uint256 }) as EncryptableUint256,
|
|
275
|
-
}
|
|
220
|
+
} satisfies EncryptableFactories;
|
|
221
|
+
|
|
222
|
+
/* eslint-disable no-redeclare */
|
|
223
|
+
function createEncryptableByLiteral(
|
|
224
|
+
type: 'bool',
|
|
225
|
+
data: EncryptableBool['data'],
|
|
226
|
+
securityZone?: number
|
|
227
|
+
): EncryptableBool;
|
|
228
|
+
function createEncryptableByLiteral(
|
|
229
|
+
type: 'address',
|
|
230
|
+
data: EncryptableAddress['data'],
|
|
231
|
+
securityZone?: number
|
|
232
|
+
): EncryptableAddress;
|
|
233
|
+
function createEncryptableByLiteral(
|
|
234
|
+
type: 'uint8',
|
|
235
|
+
data: EncryptableUint8['data'],
|
|
236
|
+
securityZone?: number
|
|
237
|
+
): EncryptableUint8;
|
|
238
|
+
function createEncryptableByLiteral(
|
|
239
|
+
type: 'uint16',
|
|
240
|
+
data: EncryptableUint16['data'],
|
|
241
|
+
securityZone?: number
|
|
242
|
+
): EncryptableUint16;
|
|
243
|
+
function createEncryptableByLiteral(
|
|
244
|
+
type: 'uint32',
|
|
245
|
+
data: EncryptableUint32['data'],
|
|
246
|
+
securityZone?: number
|
|
247
|
+
): EncryptableUint32;
|
|
248
|
+
function createEncryptableByLiteral(
|
|
249
|
+
type: 'uint64',
|
|
250
|
+
data: EncryptableUint64['data'],
|
|
251
|
+
securityZone?: number
|
|
252
|
+
): EncryptableUint64;
|
|
253
|
+
function createEncryptableByLiteral(
|
|
254
|
+
type: 'uint128',
|
|
255
|
+
data: EncryptableUint128['data'],
|
|
256
|
+
securityZone?: number
|
|
257
|
+
): EncryptableUint128;
|
|
258
|
+
function createEncryptableByLiteral(
|
|
259
|
+
type: FheTypeValue,
|
|
260
|
+
data: EncryptableItem['data'],
|
|
261
|
+
securityZone?: number
|
|
262
|
+
): EncryptableItem;
|
|
263
|
+
function createEncryptableByLiteral(
|
|
264
|
+
type: FheTypeValue,
|
|
265
|
+
data: EncryptableItem['data'],
|
|
266
|
+
securityZone = 0
|
|
267
|
+
): EncryptableItem {
|
|
268
|
+
switch (type) {
|
|
269
|
+
case 'bool': {
|
|
270
|
+
if (typeof data !== 'boolean') throw new Error('Bool encryptable data must be boolean');
|
|
271
|
+
return EncryptableFactoriesImpl.bool(data, securityZone);
|
|
272
|
+
}
|
|
273
|
+
case 'address':
|
|
274
|
+
case 'uint8':
|
|
275
|
+
case 'uint16':
|
|
276
|
+
case 'uint32':
|
|
277
|
+
case 'uint64':
|
|
278
|
+
case 'uint128': {
|
|
279
|
+
if (typeof data === 'boolean') throw new Error('Uint encryptable data must be string or bigint');
|
|
280
|
+
return EncryptableFactoriesImpl[type](data, securityZone);
|
|
281
|
+
}
|
|
282
|
+
default: {
|
|
283
|
+
// Exhaustiveness guard
|
|
284
|
+
const _exhaustive: never = type;
|
|
285
|
+
throw new Error(`Unsupported encryptable type: ${_exhaustive}`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/* eslint-enable no-redeclare */
|
|
290
|
+
|
|
291
|
+
export const Encryptable = {
|
|
292
|
+
...EncryptableFactoriesImpl,
|
|
293
|
+
create: createEncryptableByLiteral,
|
|
294
|
+
} satisfies EncryptableFactory;
|
|
276
295
|
|
|
277
296
|
export type EncryptableItem =
|
|
278
297
|
| EncryptableBool
|
|
@@ -285,6 +304,25 @@ export type EncryptableItem =
|
|
|
285
304
|
// | EncryptableUint256
|
|
286
305
|
| EncryptableAddress;
|
|
287
306
|
|
|
307
|
+
export type EncryptableItemByFheType<T extends FheTypes> = T extends FheTypes.Bool
|
|
308
|
+
? EncryptableBool
|
|
309
|
+
: T extends FheTypes.Uint8
|
|
310
|
+
? EncryptableUint8
|
|
311
|
+
: T extends FheTypes.Uint16
|
|
312
|
+
? EncryptableUint16
|
|
313
|
+
: T extends FheTypes.Uint32
|
|
314
|
+
? EncryptableUint32
|
|
315
|
+
: T extends FheTypes.Uint64
|
|
316
|
+
? EncryptableUint64
|
|
317
|
+
: T extends FheTypes.Uint128
|
|
318
|
+
? EncryptableUint128
|
|
319
|
+
: // [U256-DISABLED]
|
|
320
|
+
// : T extends FheTypes.Uint256
|
|
321
|
+
// ? EncryptableUint256
|
|
322
|
+
T extends FheTypes.Uint160
|
|
323
|
+
? EncryptableAddress
|
|
324
|
+
: never;
|
|
325
|
+
|
|
288
326
|
// COFHE Encrypt
|
|
289
327
|
export type EncryptableToEncryptedItemInputMap<E extends EncryptableItem> = E extends EncryptableBool
|
|
290
328
|
? EncryptedBoolInput
|
|
@@ -338,7 +376,15 @@ export enum EncryptStep {
|
|
|
338
376
|
Verify = 'verify',
|
|
339
377
|
}
|
|
340
378
|
|
|
341
|
-
export
|
|
379
|
+
export function isLastEncryptionStep(step: EncryptStep): boolean {
|
|
380
|
+
return step === EncryptStep.Verify;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
export type EncryptStepCallbackContext = Record<string, any> & {
|
|
384
|
+
isStart: boolean;
|
|
385
|
+
isEnd: boolean;
|
|
386
|
+
duration: number;
|
|
387
|
+
};
|
|
342
388
|
export type EncryptStepCallbackFunction = (state: EncryptStep, context?: EncryptStepCallbackContext) => void;
|
|
343
389
|
|
|
344
390
|
// DECRYPT
|
package/core/utils.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type PublicClient, type WalletClient } from 'viem';
|
|
2
2
|
import { CofhesdkError, CofhesdkErrorCode } from './error.js';
|
|
3
|
+
import { Encryptable, FheTypes } from './types.js';
|
|
3
4
|
|
|
4
5
|
export const toHexString = (bytes: Uint8Array) =>
|
|
5
6
|
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
|
|
@@ -65,7 +66,7 @@ export async function getPublicClientChainID(publicClient: PublicClient) {
|
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
export async function getWalletClientAccount(walletClient: WalletClient) {
|
|
68
|
-
let address: string | undefined;
|
|
69
|
+
let address: `0x${string}` | undefined;
|
|
69
70
|
try {
|
|
70
71
|
address = walletClient.account?.address;
|
|
71
72
|
if (!address) {
|
|
@@ -86,3 +87,44 @@ export async function getWalletClientAccount(walletClient: WalletClient) {
|
|
|
86
87
|
}
|
|
87
88
|
return address;
|
|
88
89
|
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Converts FheTypes enum to string representation for serialization
|
|
93
|
+
* Used when passing data to Web Workers or other serialization contexts
|
|
94
|
+
*/
|
|
95
|
+
export function fheTypeToString(utype: FheTypes): string {
|
|
96
|
+
switch (utype) {
|
|
97
|
+
case FheTypes.Bool:
|
|
98
|
+
return 'bool';
|
|
99
|
+
case FheTypes.Uint4:
|
|
100
|
+
return 'uint4';
|
|
101
|
+
case FheTypes.Uint8:
|
|
102
|
+
return 'uint8';
|
|
103
|
+
case FheTypes.Uint16:
|
|
104
|
+
return 'uint16';
|
|
105
|
+
case FheTypes.Uint32:
|
|
106
|
+
return 'uint32';
|
|
107
|
+
case FheTypes.Uint64:
|
|
108
|
+
return 'uint64';
|
|
109
|
+
case FheTypes.Uint128:
|
|
110
|
+
return 'uint128';
|
|
111
|
+
case FheTypes.Uint160:
|
|
112
|
+
return 'uint160';
|
|
113
|
+
case FheTypes.Uint256:
|
|
114
|
+
return 'uint256';
|
|
115
|
+
case FheTypes.Uint512:
|
|
116
|
+
return 'uint512';
|
|
117
|
+
case FheTypes.Uint1024:
|
|
118
|
+
return 'uint1024';
|
|
119
|
+
case FheTypes.Uint2048:
|
|
120
|
+
return 'uint2048';
|
|
121
|
+
case FheTypes.Uint2:
|
|
122
|
+
return 'uint2';
|
|
123
|
+
case FheTypes.Uint6:
|
|
124
|
+
return 'uint6';
|
|
125
|
+
case FheTypes.Uint10:
|
|
126
|
+
return 'uint10';
|
|
127
|
+
default:
|
|
128
|
+
throw new Error(`Unknown FheType: ${utype}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
package/dist/adapters.d.cts
CHANGED
|
@@ -78,12 +78,12 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
78
78
|
gas: bigint;
|
|
79
79
|
nonce: number;
|
|
80
80
|
value: bigint;
|
|
81
|
-
|
|
81
|
+
blobVersionedHashes?: undefined;
|
|
82
82
|
gasPrice: bigint;
|
|
83
|
+
maxFeePerBlobGas?: undefined;
|
|
83
84
|
maxFeePerGas?: undefined;
|
|
84
85
|
maxPriorityFeePerGas?: undefined;
|
|
85
86
|
accessList?: undefined;
|
|
86
|
-
blobVersionedHashes?: undefined;
|
|
87
87
|
authorizationList?: undefined;
|
|
88
88
|
hash: viem.Hash;
|
|
89
89
|
r: viem.Hex;
|
|
@@ -103,12 +103,12 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
103
103
|
gas: bigint;
|
|
104
104
|
nonce: number;
|
|
105
105
|
value: bigint;
|
|
106
|
-
|
|
106
|
+
blobVersionedHashes?: undefined;
|
|
107
107
|
gasPrice: bigint;
|
|
108
|
+
maxFeePerBlobGas?: undefined;
|
|
108
109
|
maxFeePerGas?: undefined;
|
|
109
110
|
maxPriorityFeePerGas?: undefined;
|
|
110
111
|
accessList: viem.AccessList;
|
|
111
|
-
blobVersionedHashes?: undefined;
|
|
112
112
|
authorizationList?: undefined;
|
|
113
113
|
hash: viem.Hash;
|
|
114
114
|
r: viem.Hex;
|
|
@@ -128,12 +128,12 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
128
128
|
gas: bigint;
|
|
129
129
|
nonce: number;
|
|
130
130
|
value: bigint;
|
|
131
|
-
|
|
131
|
+
blobVersionedHashes?: undefined;
|
|
132
132
|
gasPrice?: undefined;
|
|
133
|
+
maxFeePerBlobGas?: undefined;
|
|
133
134
|
maxFeePerGas: bigint;
|
|
134
135
|
maxPriorityFeePerGas: bigint;
|
|
135
136
|
accessList: viem.AccessList;
|
|
136
|
-
blobVersionedHashes?: undefined;
|
|
137
137
|
authorizationList?: undefined;
|
|
138
138
|
hash: viem.Hash;
|
|
139
139
|
r: viem.Hex;
|
|
@@ -153,12 +153,12 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
153
153
|
gas: bigint;
|
|
154
154
|
nonce: number;
|
|
155
155
|
value: bigint;
|
|
156
|
-
|
|
156
|
+
blobVersionedHashes: readonly viem.Hex[];
|
|
157
157
|
gasPrice?: undefined;
|
|
158
|
+
maxFeePerBlobGas: bigint;
|
|
158
159
|
maxFeePerGas: bigint;
|
|
159
160
|
maxPriorityFeePerGas: bigint;
|
|
160
161
|
accessList: viem.AccessList;
|
|
161
|
-
blobVersionedHashes: readonly viem.Hex[];
|
|
162
162
|
authorizationList?: undefined;
|
|
163
163
|
hash: viem.Hash;
|
|
164
164
|
r: viem.Hex;
|
|
@@ -178,12 +178,12 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
178
178
|
gas: bigint;
|
|
179
179
|
nonce: number;
|
|
180
180
|
value: bigint;
|
|
181
|
-
|
|
181
|
+
blobVersionedHashes?: undefined;
|
|
182
182
|
gasPrice?: undefined;
|
|
183
|
+
maxFeePerBlobGas?: undefined;
|
|
183
184
|
maxFeePerGas: bigint;
|
|
184
185
|
maxPriorityFeePerGas: bigint;
|
|
185
186
|
accessList: viem.AccessList;
|
|
186
|
-
blobVersionedHashes?: undefined;
|
|
187
187
|
authorizationList: viem.SignedAuthorizationList;
|
|
188
188
|
hash: viem.Hash;
|
|
189
189
|
r: viem.Hex;
|
|
@@ -228,12 +228,12 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
228
228
|
gas: bigint;
|
|
229
229
|
nonce: number;
|
|
230
230
|
value: bigint;
|
|
231
|
-
|
|
231
|
+
blobVersionedHashes?: undefined;
|
|
232
232
|
gasPrice: bigint;
|
|
233
|
+
maxFeePerBlobGas?: undefined;
|
|
233
234
|
maxFeePerGas?: undefined;
|
|
234
235
|
maxPriorityFeePerGas?: undefined;
|
|
235
236
|
accessList?: undefined;
|
|
236
|
-
blobVersionedHashes?: undefined;
|
|
237
237
|
authorizationList?: undefined;
|
|
238
238
|
hash: viem.Hash;
|
|
239
239
|
r: viem.Hex;
|
|
@@ -253,12 +253,12 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
253
253
|
gas: bigint;
|
|
254
254
|
nonce: number;
|
|
255
255
|
value: bigint;
|
|
256
|
-
|
|
256
|
+
blobVersionedHashes?: undefined;
|
|
257
257
|
gasPrice: bigint;
|
|
258
|
+
maxFeePerBlobGas?: undefined;
|
|
258
259
|
maxFeePerGas?: undefined;
|
|
259
260
|
maxPriorityFeePerGas?: undefined;
|
|
260
261
|
accessList: viem.AccessList;
|
|
261
|
-
blobVersionedHashes?: undefined;
|
|
262
262
|
authorizationList?: undefined;
|
|
263
263
|
hash: viem.Hash;
|
|
264
264
|
r: viem.Hex;
|
|
@@ -278,12 +278,12 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
278
278
|
gas: bigint;
|
|
279
279
|
nonce: number;
|
|
280
280
|
value: bigint;
|
|
281
|
-
|
|
281
|
+
blobVersionedHashes?: undefined;
|
|
282
282
|
gasPrice?: undefined;
|
|
283
|
+
maxFeePerBlobGas?: undefined;
|
|
283
284
|
maxFeePerGas: bigint;
|
|
284
285
|
maxPriorityFeePerGas: bigint;
|
|
285
286
|
accessList: viem.AccessList;
|
|
286
|
-
blobVersionedHashes?: undefined;
|
|
287
287
|
authorizationList?: undefined;
|
|
288
288
|
hash: viem.Hash;
|
|
289
289
|
r: viem.Hex;
|
|
@@ -303,12 +303,12 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
303
303
|
gas: bigint;
|
|
304
304
|
nonce: number;
|
|
305
305
|
value: bigint;
|
|
306
|
-
|
|
306
|
+
blobVersionedHashes: readonly viem.Hex[];
|
|
307
307
|
gasPrice?: undefined;
|
|
308
|
+
maxFeePerBlobGas: bigint;
|
|
308
309
|
maxFeePerGas: bigint;
|
|
309
310
|
maxPriorityFeePerGas: bigint;
|
|
310
311
|
accessList: viem.AccessList;
|
|
311
|
-
blobVersionedHashes: readonly viem.Hex[];
|
|
312
312
|
authorizationList?: undefined;
|
|
313
313
|
hash: viem.Hash;
|
|
314
314
|
r: viem.Hex;
|
|
@@ -328,12 +328,12 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
328
328
|
gas: bigint;
|
|
329
329
|
nonce: number;
|
|
330
330
|
value: bigint;
|
|
331
|
-
|
|
331
|
+
blobVersionedHashes?: undefined;
|
|
332
332
|
gasPrice?: undefined;
|
|
333
|
+
maxFeePerBlobGas?: undefined;
|
|
333
334
|
maxFeePerGas: bigint;
|
|
334
335
|
maxPriorityFeePerGas: bigint;
|
|
335
336
|
accessList: viem.AccessList;
|
|
336
|
-
blobVersionedHashes?: undefined;
|
|
337
337
|
authorizationList: viem.SignedAuthorizationList;
|
|
338
338
|
hash: viem.Hash;
|
|
339
339
|
r: viem.Hex;
|
|
@@ -6858,6 +6858,7 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
6858
6858
|
}, (request["parameters"] extends readonly viem.PrepareTransactionRequestParameterType[] ? request["parameters"][number] : "type" | "gas" | "nonce" | "blobVersionedHashes" | "fees" | "chainId") extends infer T_8 ? T_8 extends (request["parameters"] extends readonly viem.PrepareTransactionRequestParameterType[] ? request["parameters"][number] : "type" | "gas" | "nonce" | "blobVersionedHashes" | "fees" | "chainId") ? T_8 extends "fees" ? "gasPrice" | "maxFeePerGas" | "maxPriorityFeePerGas" : T_8 : never : never> & (unknown extends request["kzg"] ? {} : Pick<request, "kzg">))[K]; } : never>;
|
|
6859
6859
|
readContract: <const abi extends viem.Abi | readonly unknown[], functionName extends viem.ContractFunctionName<abi, "pure" | "view">, const args extends viem.ContractFunctionArgs<abi, "pure" | "view", functionName>>(args: viem.ReadContractParameters<abi, functionName, args>) => Promise<viem.ReadContractReturnType<abi, functionName, args>>;
|
|
6860
6860
|
sendRawTransaction: (args: viem.SendRawTransactionParameters) => Promise<viem.SendRawTransactionReturnType>;
|
|
6861
|
+
sendRawTransactionSync: (args: viem.SendRawTransactionSyncParameters) => Promise<viem.TransactionReceipt>;
|
|
6861
6862
|
simulate: <const calls extends readonly unknown[]>(args: viem.SimulateBlocksParameters<calls>) => Promise<viem.SimulateBlocksReturnType<calls>>;
|
|
6862
6863
|
simulateBlocks: <const calls extends readonly unknown[]>(args: viem.SimulateBlocksParameters<calls>) => Promise<viem.SimulateBlocksReturnType<calls>>;
|
|
6863
6864
|
simulateCalls: <const calls extends readonly unknown[]>(args: viem.SimulateCallsParameters<calls>) => Promise<viem.SimulateCallsReturnType<calls>>;
|
|
@@ -13425,8 +13426,24 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
13425
13426
|
} | undefined;
|
|
13426
13427
|
id: string;
|
|
13427
13428
|
}>;
|
|
13429
|
+
sendCallsSync: <const calls extends readonly unknown[], chainOverride extends viem.Chain | undefined = undefined>(parameters: viem.SendCallsSyncParameters<viem.Chain | undefined, viem.Account | undefined, chainOverride, calls>) => Promise<{
|
|
13430
|
+
id: string;
|
|
13431
|
+
atomic: boolean;
|
|
13432
|
+
capabilities?: {
|
|
13433
|
+
[key: string]: any;
|
|
13434
|
+
} | {
|
|
13435
|
+
[x: string]: any;
|
|
13436
|
+
} | undefined;
|
|
13437
|
+
chainId: number;
|
|
13438
|
+
receipts?: viem.WalletCallReceipt<bigint, "success" | "reverted">[] | undefined;
|
|
13439
|
+
version: string;
|
|
13440
|
+
statusCode: number;
|
|
13441
|
+
status: "pending" | "success" | "failure" | undefined;
|
|
13442
|
+
}>;
|
|
13428
13443
|
sendRawTransaction: (args: viem.SendRawTransactionParameters) => Promise<viem.SendRawTransactionReturnType>;
|
|
13444
|
+
sendRawTransactionSync: (args: viem.SendRawTransactionSyncParameters) => Promise<viem.TransactionReceipt>;
|
|
13429
13445
|
sendTransaction: <const request extends viem.SendTransactionRequest<viem.Chain | undefined, chainOverride>, chainOverride extends viem.Chain | undefined = undefined>(args: viem.SendTransactionParameters<viem.Chain | undefined, viem.Account | undefined, chainOverride, request>) => Promise<viem.SendTransactionReturnType>;
|
|
13446
|
+
sendTransactionSync: <const request extends viem.SendTransactionSyncRequest<viem.Chain | undefined, chainOverride>, chainOverride extends viem.Chain | undefined = undefined>(args: viem.SendTransactionSyncParameters<viem.Chain | undefined, viem.Account | undefined, chainOverride, request>) => Promise<viem.SendTransactionSyncReturnType>;
|
|
13430
13447
|
showCallsStatus: (parameters: viem.ShowCallsStatusParameters) => Promise<viem.ShowCallsStatusReturnType>;
|
|
13431
13448
|
signAuthorization: (parameters: viem.SignAuthorizationParameters<viem.Account | undefined>) => Promise<viem.SignAuthorizationReturnType>;
|
|
13432
13449
|
signMessage: (args: viem.SignMessageParameters<viem.Account | undefined>) => Promise<viem.SignMessageReturnType>;
|
|
@@ -14534,6 +14551,7 @@ declare function WagmiAdapter(walletClient: WalletClient, publicClient: PublicCl
|
|
|
14534
14551
|
waitForCallsStatus: (parameters: viem.WaitForCallsStatusParameters) => Promise<viem.WaitForCallsStatusReturnType>;
|
|
14535
14552
|
watchAsset: (args: viem.WatchAssetParameters) => Promise<viem.WatchAssetReturnType>;
|
|
14536
14553
|
writeContract: <const abi extends viem.Abi | readonly unknown[], functionName extends viem.ContractFunctionName<abi, "nonpayable" | "payable">, args_1 extends viem.ContractFunctionArgs<abi, "nonpayable" | "payable", functionName>, chainOverride extends viem.Chain | undefined = undefined>(args: viem.WriteContractParameters<abi, functionName, args_1, viem.Chain | undefined, viem.Account | undefined, chainOverride>) => Promise<viem.WriteContractReturnType>;
|
|
14554
|
+
writeContractSync: <const abi extends viem.Abi | readonly unknown[], functionName extends viem.ContractFunctionName<abi, "nonpayable" | "payable">, args_1 extends viem.ContractFunctionArgs<abi, "nonpayable" | "payable", functionName>, chainOverride extends viem.Chain | undefined = undefined>(args: viem.WriteContractSyncParameters<abi, functionName, args_1, viem.Chain | undefined, viem.Account | undefined, chainOverride>) => Promise<viem.WriteContractSyncReturnType>;
|
|
14537
14555
|
extend: <const client extends {
|
|
14538
14556
|
[x: string]: unknown;
|
|
14539
14557
|
account?: undefined;
|