@cofhe/sdk 0.0.0-beta-20251027110729
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 +47 -0
- package/adapters/ethers5.test.ts +174 -0
- package/adapters/ethers5.ts +36 -0
- package/adapters/ethers6.test.ts +169 -0
- package/adapters/ethers6.ts +36 -0
- package/adapters/hardhat-node.ts +167 -0
- package/adapters/hardhat.hh2.test.ts +159 -0
- package/adapters/hardhat.ts +37 -0
- package/adapters/index.test.ts +25 -0
- package/adapters/index.ts +5 -0
- package/adapters/smartWallet.ts +91 -0
- package/adapters/test-utils.ts +53 -0
- package/adapters/types.ts +6 -0
- package/adapters/wagmi.test.ts +156 -0
- package/adapters/wagmi.ts +17 -0
- package/chains/chains/arbSepolia.ts +14 -0
- package/chains/chains/baseSepolia.ts +14 -0
- package/chains/chains/hardhat.ts +15 -0
- package/chains/chains/sepolia.ts +14 -0
- package/chains/chains.test.ts +49 -0
- package/chains/defineChain.ts +18 -0
- package/chains/index.ts +33 -0
- package/chains/types.ts +32 -0
- package/core/baseBuilder.ts +138 -0
- package/core/client.test.ts +298 -0
- package/core/client.ts +308 -0
- package/core/config.test.ts +224 -0
- package/core/config.ts +213 -0
- package/core/decrypt/MockQueryDecrypterAbi.ts +129 -0
- package/core/decrypt/cofheMocksSealOutput.ts +57 -0
- package/core/decrypt/decryptHandleBuilder.ts +281 -0
- package/core/decrypt/decryptUtils.ts +28 -0
- package/core/decrypt/tnSealOutput.ts +59 -0
- package/core/encrypt/MockZkVerifierAbi.ts +106 -0
- package/core/encrypt/cofheMocksZkVerifySign.ts +278 -0
- package/core/encrypt/encryptInputsBuilder.test.ts +735 -0
- package/core/encrypt/encryptInputsBuilder.ts +512 -0
- package/core/encrypt/encryptUtils.ts +64 -0
- package/core/encrypt/zkPackProveVerify.ts +273 -0
- package/core/error.ts +170 -0
- package/core/fetchKeys.test.ts +212 -0
- package/core/fetchKeys.ts +170 -0
- package/core/index.ts +77 -0
- package/core/keyStore.test.ts +226 -0
- package/core/keyStore.ts +127 -0
- package/core/permits.test.ts +242 -0
- package/core/permits.ts +136 -0
- package/core/result.test.ts +180 -0
- package/core/result.ts +67 -0
- package/core/test-utils.ts +45 -0
- package/core/types.ts +352 -0
- package/core/utils.ts +88 -0
- package/dist/adapters.cjs +88 -0
- package/dist/adapters.d.cts +14558 -0
- package/dist/adapters.d.ts +14558 -0
- package/dist/adapters.js +83 -0
- package/dist/chains.cjs +101 -0
- package/dist/chains.d.cts +99 -0
- package/dist/chains.d.ts +99 -0
- package/dist/chains.js +1 -0
- package/dist/chunk-GZCQQYVI.js +93 -0
- package/dist/chunk-KFGPTJ6X.js +2295 -0
- package/dist/chunk-LU7BMUUT.js +804 -0
- package/dist/core.cjs +3174 -0
- package/dist/core.d.cts +16 -0
- package/dist/core.d.ts +16 -0
- package/dist/core.js +3 -0
- package/dist/node.cjs +3090 -0
- package/dist/node.d.cts +22 -0
- package/dist/node.d.ts +22 -0
- package/dist/node.js +90 -0
- package/dist/permit-S9CnI6MF.d.cts +333 -0
- package/dist/permit-S9CnI6MF.d.ts +333 -0
- package/dist/permits.cjs +856 -0
- package/dist/permits.d.cts +1056 -0
- package/dist/permits.d.ts +1056 -0
- package/dist/permits.js +1 -0
- package/dist/types-KImPrEIe.d.cts +48 -0
- package/dist/types-KImPrEIe.d.ts +48 -0
- package/dist/types-PhwGgQvs.d.ts +953 -0
- package/dist/types-bB7wLj0q.d.cts +953 -0
- package/dist/web.cjs +3067 -0
- package/dist/web.d.cts +22 -0
- package/dist/web.d.ts +22 -0
- package/dist/web.js +64 -0
- package/node/client.test.ts +152 -0
- package/node/config.test.ts +68 -0
- package/node/encryptInputs.test.ts +175 -0
- package/node/index.ts +96 -0
- package/node/storage.ts +51 -0
- package/package.json +120 -0
- package/permits/index.ts +67 -0
- package/permits/localstorage.test.ts +118 -0
- package/permits/permit.test.ts +474 -0
- package/permits/permit.ts +396 -0
- package/permits/sealing.test.ts +84 -0
- package/permits/sealing.ts +131 -0
- package/permits/signature.ts +79 -0
- package/permits/store.test.ts +128 -0
- package/permits/store.ts +168 -0
- package/permits/test-utils.ts +20 -0
- package/permits/types.ts +174 -0
- package/permits/utils.ts +63 -0
- package/permits/validation.test.ts +288 -0
- package/permits/validation.ts +349 -0
- package/web/client.web.test.ts +152 -0
- package/web/config.web.test.ts +71 -0
- package/web/encryptInputs.web.test.ts +195 -0
- package/web/index.ts +97 -0
- package/web/storage.ts +20 -0
package/core/types.ts
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/* eslint-disable no-unused-vars */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type CreateSelfPermitOptions,
|
|
5
|
+
type Permit,
|
|
6
|
+
type CreateSharingPermitOptions,
|
|
7
|
+
type ImportSharedPermitOptions,
|
|
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
|
|
21
|
+
|
|
22
|
+
export type Primitive = null | undefined | string | number | boolean | symbol | bigint;
|
|
23
|
+
export type LiteralToPrimitive<T> = T extends number
|
|
24
|
+
? number
|
|
25
|
+
: T extends bigint
|
|
26
|
+
? bigint
|
|
27
|
+
: T extends string
|
|
28
|
+
? string
|
|
29
|
+
: T extends boolean
|
|
30
|
+
? boolean
|
|
31
|
+
: T extends symbol
|
|
32
|
+
? symbol
|
|
33
|
+
: T extends null
|
|
34
|
+
? null
|
|
35
|
+
: T extends undefined
|
|
36
|
+
? undefined
|
|
37
|
+
: never;
|
|
38
|
+
|
|
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
|
+
// FHE TYPES
|
|
121
|
+
|
|
122
|
+
export enum FheTypes {
|
|
123
|
+
Bool = 0,
|
|
124
|
+
Uint4 = 1,
|
|
125
|
+
Uint8 = 2,
|
|
126
|
+
Uint16 = 3,
|
|
127
|
+
Uint32 = 4,
|
|
128
|
+
Uint64 = 5,
|
|
129
|
+
Uint128 = 6,
|
|
130
|
+
Uint160 = 7,
|
|
131
|
+
Uint256 = 8,
|
|
132
|
+
Uint512 = 9,
|
|
133
|
+
Uint1024 = 10,
|
|
134
|
+
Uint2048 = 11,
|
|
135
|
+
Uint2 = 12,
|
|
136
|
+
Uint6 = 13,
|
|
137
|
+
Uint10 = 14,
|
|
138
|
+
Uint12 = 15,
|
|
139
|
+
Uint14 = 16,
|
|
140
|
+
Int2 = 17,
|
|
141
|
+
Int4 = 18,
|
|
142
|
+
Int6 = 19,
|
|
143
|
+
Int8 = 20,
|
|
144
|
+
Int10 = 21,
|
|
145
|
+
Int12 = 22,
|
|
146
|
+
Int14 = 23,
|
|
147
|
+
Int16 = 24,
|
|
148
|
+
Int32 = 25,
|
|
149
|
+
Int64 = 26,
|
|
150
|
+
Int128 = 27,
|
|
151
|
+
Int160 = 28,
|
|
152
|
+
Int256 = 29,
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* List of All FHE uint types (excludes bool and address)
|
|
157
|
+
*/
|
|
158
|
+
export const FheUintUTypes = [
|
|
159
|
+
FheTypes.Uint8,
|
|
160
|
+
FheTypes.Uint16,
|
|
161
|
+
FheTypes.Uint32,
|
|
162
|
+
FheTypes.Uint64,
|
|
163
|
+
FheTypes.Uint128,
|
|
164
|
+
// [U256-DISABLED]
|
|
165
|
+
// FheTypes.Uint256,
|
|
166
|
+
] as const;
|
|
167
|
+
export type FheUintUTypesType = (typeof FheUintUTypes)[number];
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* List of All FHE types (uints, bool, and address)
|
|
171
|
+
*/
|
|
172
|
+
export const FheAllUTypes = [
|
|
173
|
+
FheTypes.Bool,
|
|
174
|
+
FheTypes.Uint8,
|
|
175
|
+
FheTypes.Uint16,
|
|
176
|
+
FheTypes.Uint32,
|
|
177
|
+
FheTypes.Uint64,
|
|
178
|
+
FheTypes.Uint128,
|
|
179
|
+
// [U256-DISABLED]
|
|
180
|
+
// FheTypes.Uint256,
|
|
181
|
+
FheTypes.Uint160,
|
|
182
|
+
] as const;
|
|
183
|
+
type FheAllUTypesType = (typeof FheAllUTypes)[number];
|
|
184
|
+
|
|
185
|
+
// ENCRYPT
|
|
186
|
+
|
|
187
|
+
export type EncryptedNumber = {
|
|
188
|
+
data: Uint8Array;
|
|
189
|
+
securityZone: number;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export type EncryptedItemInput = {
|
|
193
|
+
ctHash: bigint;
|
|
194
|
+
securityZone: number;
|
|
195
|
+
utype: FheTypes;
|
|
196
|
+
signature: string;
|
|
197
|
+
};
|
|
198
|
+
export type EncryptedBoolInput = EncryptedItemInput & {
|
|
199
|
+
utype: FheTypes.Bool;
|
|
200
|
+
};
|
|
201
|
+
export type EncryptedUint8Input = EncryptedItemInput & {
|
|
202
|
+
utype: FheTypes.Uint8;
|
|
203
|
+
};
|
|
204
|
+
export type EncryptedUint16Input = EncryptedItemInput & {
|
|
205
|
+
utype: FheTypes.Uint16;
|
|
206
|
+
};
|
|
207
|
+
export type EncryptedUint32Input = EncryptedItemInput & {
|
|
208
|
+
utype: FheTypes.Uint32;
|
|
209
|
+
};
|
|
210
|
+
export type EncryptedUint64Input = EncryptedItemInput & {
|
|
211
|
+
utype: FheTypes.Uint64;
|
|
212
|
+
};
|
|
213
|
+
export type EncryptedUint128Input = EncryptedItemInput & {
|
|
214
|
+
utype: FheTypes.Uint128;
|
|
215
|
+
};
|
|
216
|
+
export type EncryptedUint256Input = EncryptedItemInput & {
|
|
217
|
+
utype: FheTypes.Uint256;
|
|
218
|
+
};
|
|
219
|
+
export type EncryptedAddressInput = EncryptedItemInput & {
|
|
220
|
+
utype: FheTypes.Uint160;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
export type EncryptableBool = {
|
|
224
|
+
data: boolean;
|
|
225
|
+
utype: FheTypes.Bool;
|
|
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;
|
|
238
|
+
};
|
|
239
|
+
export type EncryptableUint64 = {
|
|
240
|
+
data: string | bigint;
|
|
241
|
+
utype: FheTypes.Uint64;
|
|
242
|
+
};
|
|
243
|
+
export type EncryptableUint128 = {
|
|
244
|
+
data: string | bigint;
|
|
245
|
+
utype: FheTypes.Uint128;
|
|
246
|
+
};
|
|
247
|
+
// [U256-DISABLED]
|
|
248
|
+
// export type EncryptableUint256 = {
|
|
249
|
+
// data: string | bigint;
|
|
250
|
+
// utype: FheTypes.Uint256;
|
|
251
|
+
// };
|
|
252
|
+
export type EncryptableAddress = {
|
|
253
|
+
data: string | bigint;
|
|
254
|
+
utype: FheTypes.Uint160;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
export const Encryptable = {
|
|
258
|
+
bool: (data: EncryptableBool['data'], securityZone = 0) =>
|
|
259
|
+
({ data, securityZone, utype: FheTypes.Bool }) as EncryptableBool,
|
|
260
|
+
address: (data: EncryptableAddress['data'], securityZone = 0) =>
|
|
261
|
+
({ data, securityZone, utype: FheTypes.Uint160 }) as EncryptableAddress,
|
|
262
|
+
uint8: (data: EncryptableUint8['data'], securityZone = 0) =>
|
|
263
|
+
({ data, securityZone, utype: FheTypes.Uint8 }) as EncryptableUint8,
|
|
264
|
+
uint16: (data: EncryptableUint16['data'], securityZone = 0) =>
|
|
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,
|
|
272
|
+
// [U256-DISABLED]
|
|
273
|
+
// uint256: (data: EncryptableUint256['data'], securityZone = 0) =>
|
|
274
|
+
// ({ data, securityZone, utype: FheTypes.Uint256 }) as EncryptableUint256,
|
|
275
|
+
} as const;
|
|
276
|
+
|
|
277
|
+
export type EncryptableItem =
|
|
278
|
+
| EncryptableBool
|
|
279
|
+
| EncryptableUint8
|
|
280
|
+
| EncryptableUint16
|
|
281
|
+
| EncryptableUint32
|
|
282
|
+
| EncryptableUint64
|
|
283
|
+
| EncryptableUint128
|
|
284
|
+
// [U256-DISABLED]
|
|
285
|
+
// | EncryptableUint256
|
|
286
|
+
| EncryptableAddress;
|
|
287
|
+
|
|
288
|
+
// COFHE Encrypt
|
|
289
|
+
export type EncryptableToEncryptedItemInputMap<E extends EncryptableItem> = E extends EncryptableBool
|
|
290
|
+
? EncryptedBoolInput
|
|
291
|
+
: E extends EncryptableUint8
|
|
292
|
+
? EncryptedUint8Input
|
|
293
|
+
: E extends EncryptableUint16
|
|
294
|
+
? EncryptedUint16Input
|
|
295
|
+
: E extends EncryptableUint32
|
|
296
|
+
? EncryptedUint32Input
|
|
297
|
+
: E extends EncryptableUint64
|
|
298
|
+
? EncryptedUint64Input
|
|
299
|
+
: E extends EncryptableUint128
|
|
300
|
+
? EncryptedUint128Input
|
|
301
|
+
: // [U256-DISABLED]
|
|
302
|
+
// : E extends EncryptableUint256
|
|
303
|
+
// ? EncryptedUint256Input
|
|
304
|
+
E extends EncryptableAddress
|
|
305
|
+
? EncryptedAddressInput
|
|
306
|
+
: never;
|
|
307
|
+
|
|
308
|
+
export type EncryptedItemInputs<T> = T extends Primitive
|
|
309
|
+
? LiteralToPrimitive<T>
|
|
310
|
+
: T extends EncryptableItem
|
|
311
|
+
? EncryptableToEncryptedItemInputMap<T>
|
|
312
|
+
: {
|
|
313
|
+
[K in keyof T]: EncryptedItemInputs<T[K]>;
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
export function isEncryptableItem(value: unknown): value is EncryptableItem {
|
|
317
|
+
return (
|
|
318
|
+
// Is object and exists
|
|
319
|
+
typeof value === 'object' &&
|
|
320
|
+
value !== null &&
|
|
321
|
+
// Has securityZone
|
|
322
|
+
'securityZone' in value &&
|
|
323
|
+
typeof value.securityZone === 'number' &&
|
|
324
|
+
// Has utype
|
|
325
|
+
'utype' in value &&
|
|
326
|
+
FheAllUTypes.includes(value.utype as FheAllUTypesType) &&
|
|
327
|
+
// Has data
|
|
328
|
+
'data' in value &&
|
|
329
|
+
['string', 'number', 'bigint', 'boolean'].includes(typeof value.data)
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export enum EncryptStep {
|
|
334
|
+
InitTfhe = 'initTfhe',
|
|
335
|
+
FetchKeys = 'fetchKeys',
|
|
336
|
+
Pack = 'pack',
|
|
337
|
+
Prove = 'prove',
|
|
338
|
+
Verify = 'verify',
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export type EncryptStepCallbackContext = Record<string, any> & { isStart: boolean; isEnd: boolean; duration: number };
|
|
342
|
+
export type EncryptStepCallbackFunction = (state: EncryptStep, context?: EncryptStepCallbackContext) => void;
|
|
343
|
+
|
|
344
|
+
// DECRYPT
|
|
345
|
+
|
|
346
|
+
export type UnsealedItem<U extends FheTypes> = U extends FheTypes.Bool
|
|
347
|
+
? boolean
|
|
348
|
+
: U extends FheTypes.Uint160
|
|
349
|
+
? string
|
|
350
|
+
: U extends FheUintUTypesType
|
|
351
|
+
? bigint
|
|
352
|
+
: never;
|
package/core/utils.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { type PublicClient, type WalletClient } from 'viem';
|
|
2
|
+
import { CofhesdkError, CofhesdkErrorCode } from './error.js';
|
|
3
|
+
|
|
4
|
+
export const toHexString = (bytes: Uint8Array) =>
|
|
5
|
+
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
|
|
6
|
+
|
|
7
|
+
export const fromHexString = (hexString: string): Uint8Array => {
|
|
8
|
+
const cleanString = hexString.length % 2 === 1 ? `0${hexString}` : hexString;
|
|
9
|
+
const arr = cleanString.replace(/^0x/, '').match(/.{1,2}/g);
|
|
10
|
+
if (!arr) return new Uint8Array();
|
|
11
|
+
return new Uint8Array(arr.map((byte) => parseInt(byte, 16)));
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const toBigIntOrThrow = (value: bigint | string): bigint => {
|
|
15
|
+
if (typeof value === 'bigint') {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
return BigInt(value);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw new Error('Invalid input: Unable to convert to bigint');
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const validateBigIntInRange = (value: bigint, max: bigint, min: bigint = 0n): void => {
|
|
27
|
+
if (typeof value !== 'bigint') {
|
|
28
|
+
throw new Error('Value must be of type bigint');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (value > max || value < min) {
|
|
32
|
+
throw new Error(`Value out of range: ${max} - ${min}, try a different uint type`);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Helper function to convert hex string to bytes
|
|
37
|
+
export const hexToBytes = (hex: string): Uint8Array => {
|
|
38
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
39
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
40
|
+
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
41
|
+
}
|
|
42
|
+
return bytes;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
46
|
+
|
|
47
|
+
export async function getPublicClientChainID(publicClient: PublicClient) {
|
|
48
|
+
let chainId: number | null = null;
|
|
49
|
+
try {
|
|
50
|
+
chainId = publicClient.chain?.id ?? (await publicClient.getChainId());
|
|
51
|
+
} catch (e) {
|
|
52
|
+
throw new CofhesdkError({
|
|
53
|
+
code: CofhesdkErrorCode.PublicWalletGetChainIdFailed,
|
|
54
|
+
message: 'getting chain ID from public client failed',
|
|
55
|
+
cause: e instanceof Error ? e : undefined,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
if (chainId === null) {
|
|
59
|
+
throw new CofhesdkError({
|
|
60
|
+
code: CofhesdkErrorCode.PublicWalletGetChainIdFailed,
|
|
61
|
+
message: 'chain ID from public client is null',
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return chainId;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function getWalletClientAccount(walletClient: WalletClient) {
|
|
68
|
+
let address: string | undefined;
|
|
69
|
+
try {
|
|
70
|
+
address = walletClient.account?.address;
|
|
71
|
+
if (!address) {
|
|
72
|
+
address = (await walletClient.getAddresses())?.[0];
|
|
73
|
+
}
|
|
74
|
+
} catch (e) {
|
|
75
|
+
throw new CofhesdkError({
|
|
76
|
+
code: CofhesdkErrorCode.PublicWalletGetAddressesFailed,
|
|
77
|
+
message: 'getting address from wallet client failed',
|
|
78
|
+
cause: e instanceof Error ? e : undefined,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (!address) {
|
|
82
|
+
throw new CofhesdkError({
|
|
83
|
+
code: CofhesdkErrorCode.PublicWalletGetAddressesFailed,
|
|
84
|
+
message: 'address from wallet client is null',
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return address;
|
|
88
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var viem = require('viem');
|
|
4
|
+
var accounts = require('viem/accounts');
|
|
5
|
+
|
|
6
|
+
// adapters/ethers5.ts
|
|
7
|
+
async function Ethers5Adapter(provider, signer) {
|
|
8
|
+
const transport = provider && "send" in provider && typeof provider.send === "function" ? (
|
|
9
|
+
// @ts-ignore - ethers5 provider.send is not typed
|
|
10
|
+
viem.custom({ request: ({ method, params }) => provider.send(method, params ?? []) })
|
|
11
|
+
) : (() => {
|
|
12
|
+
throw new Error("Provider does not support EIP-1193 interface");
|
|
13
|
+
})();
|
|
14
|
+
const address = await signer.getAddress();
|
|
15
|
+
let account;
|
|
16
|
+
if ("privateKey" in signer && typeof signer.privateKey === "string") {
|
|
17
|
+
account = accounts.privateKeyToAccount(signer.privateKey);
|
|
18
|
+
} else if (provider && typeof provider.send === "function") {
|
|
19
|
+
account = address;
|
|
20
|
+
} else {
|
|
21
|
+
throw new Error("Signer does not expose a private key and no injected wallet is available.");
|
|
22
|
+
}
|
|
23
|
+
const publicClient = viem.createPublicClient({ transport });
|
|
24
|
+
const walletClient = viem.createWalletClient({ transport, account });
|
|
25
|
+
return { publicClient, walletClient };
|
|
26
|
+
}
|
|
27
|
+
async function Ethers6Adapter(provider, signer) {
|
|
28
|
+
const transport = provider && "send" in provider && typeof provider.send === "function" ? (
|
|
29
|
+
// @ts-ignore - ethers6 provider.send is not typed
|
|
30
|
+
viem.custom({ request: ({ method, params }) => provider.send(method, params ?? []) })
|
|
31
|
+
) : (() => {
|
|
32
|
+
throw new Error("Provider does not support EIP-1193 interface");
|
|
33
|
+
})();
|
|
34
|
+
const address = await signer.getAddress();
|
|
35
|
+
let account;
|
|
36
|
+
if ("privateKey" in signer && typeof signer.privateKey === "string") {
|
|
37
|
+
account = accounts.privateKeyToAccount(signer.privateKey);
|
|
38
|
+
} else if (provider && typeof provider.send === "function") {
|
|
39
|
+
account = address;
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error("Signer does not expose a private key and no injected wallet is available.");
|
|
42
|
+
}
|
|
43
|
+
const publicClient = viem.createPublicClient({ transport });
|
|
44
|
+
const walletClient = viem.createWalletClient({ transport, account });
|
|
45
|
+
return { publicClient, walletClient };
|
|
46
|
+
}
|
|
47
|
+
async function WagmiAdapter(walletClient, publicClient) {
|
|
48
|
+
if (!walletClient) {
|
|
49
|
+
throw new Error("WalletClient is required");
|
|
50
|
+
}
|
|
51
|
+
if (!publicClient) {
|
|
52
|
+
throw new Error("PublicClient is required");
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
publicClient,
|
|
56
|
+
walletClient
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
async function HardhatSignerAdapter(signer) {
|
|
60
|
+
const provider = signer.provider;
|
|
61
|
+
if (!provider) {
|
|
62
|
+
throw new Error("Signer must have a provider");
|
|
63
|
+
}
|
|
64
|
+
const transport = viem.custom({
|
|
65
|
+
request: async ({ method, params }) => {
|
|
66
|
+
if ("request" in provider && typeof provider.request === "function") {
|
|
67
|
+
return await provider.request({ method, params });
|
|
68
|
+
} else if ("send" in provider && typeof provider.send === "function") {
|
|
69
|
+
return await provider.send(
|
|
70
|
+
method,
|
|
71
|
+
params || []
|
|
72
|
+
);
|
|
73
|
+
} else {
|
|
74
|
+
throw new Error("Provider does not support EIP-1193 request method");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
const address = await signer.getAddress();
|
|
79
|
+
const account = address;
|
|
80
|
+
const publicClient = viem.createPublicClient({ transport });
|
|
81
|
+
const walletClient = viem.createWalletClient({ transport, account });
|
|
82
|
+
return { publicClient, walletClient };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
exports.Ethers5Adapter = Ethers5Adapter;
|
|
86
|
+
exports.Ethers6Adapter = Ethers6Adapter;
|
|
87
|
+
exports.HardhatSignerAdapter = HardhatSignerAdapter;
|
|
88
|
+
exports.WagmiAdapter = WagmiAdapter;
|