@enclave-e3/sdk 0.0.7-test

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/LICENSE.md ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/README.md ADDED
@@ -0,0 +1,340 @@
1
+ # Enclave TypeScript SDK
2
+
3
+ A powerful, type-safe TypeScript SDK for interacting with Enclave smart
4
+ contracts. This SDK provides real-time event listening, contract interaction
5
+ methods, and comprehensive error handling.
6
+
7
+ ## Features
8
+
9
+ - **Event-driven architecture**: Listen to smart contract events in real-time
10
+ - **Type-safe**: Built with TypeScript and uses generated types from contracts
11
+ - **Easy contract interactions**: Simple methods for reading from and writing to
12
+ contracts
13
+ - **React integration**: Includes React hooks for easy frontend integration
14
+ - **Error handling**: Comprehensive error handling with custom error types
15
+ - **Gas estimation**: Built-in gas estimation for transactions
16
+ - **Event polling**: Support for both WebSocket and polling-based event
17
+ listening
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ pnpm add @enclave-e3/sdk
23
+ ```
24
+
25
+ ## Quick Start
26
+
27
+ ```typescript
28
+ import {
29
+ EnclaveSDK,
30
+ EnclaveEventType,
31
+ RegistryEventType,
32
+ } from "@enclave-e3/sdk";
33
+ import { createPublicClient, createWalletClient, http, custom } from "viem";
34
+
35
+ // Initialize clients
36
+ const publicClient = createPublicClient({
37
+ transport: http("YOUR_RPC_URL"),
38
+ });
39
+
40
+ const walletClient = createWalletClient({
41
+ transport: custom(window.ethereum),
42
+ });
43
+
44
+ // Create SDK instance
45
+ const sdk = new EnclaveSDK({
46
+ publicClient,
47
+ walletClient,
48
+ contracts: {
49
+ enclave: "0x...", // Your Enclave contract address
50
+ ciphernodeRegistry: "0x...", // Your CiphernodeRegistry contract address
51
+ },
52
+ chainId: 1, // Optional
53
+ });
54
+
55
+ // Initialize the SDK
56
+ await sdk.initialize();
57
+
58
+ // Listen to events with the unified event system
59
+ sdk.onEnclaveEvent(EnclaveEventType.E3_REQUESTED, (event) => {
60
+ console.log("E3 Requested:", event.data);
61
+ });
62
+
63
+ sdk.onEnclaveEvent(RegistryEventType.CIPHERNODE_ADDED, (event) => {
64
+ console.log("Ciphernode Added:", event.data);
65
+ });
66
+
67
+ // Interact with contracts
68
+ const hash = await sdk.requestE3({
69
+ filter: "0x...",
70
+ threshold: [1, 3],
71
+ startWindow: [BigInt(0), BigInt(100)],
72
+ duration: BigInt(3600),
73
+ e3Program: "0x...",
74
+ e3ProgramParams: "0x...",
75
+ computeProviderParams: "0x...",
76
+ });
77
+ ```
78
+
79
+ ## Usage within a browser
80
+
81
+ Usage within a typescript project should work out of the box, however in order to use wasm related functionality of the SDK within the browser vite you must do the following:
82
+
83
+ - Use `vite`
84
+ - Use the `vite-plugin-top-level-await` plugin
85
+ - Use the `vite-plugin-wasm` plugin
86
+ - Exclude the `@enclave-e3/wasm` package from bundling optimization.
87
+
88
+ This will enable `vite` to correctly bundle and serve the wasm bundle we use effectively.
89
+
90
+ ```
91
+ import { defineConfig } from 'vite'
92
+ import wasm from 'vite-plugin-wasm'
93
+ import topLevelAwait from 'vite-plugin-top-level-await'
94
+
95
+ export default defineConfig({
96
+ // other config ...
97
+ optimizeDeps: {
98
+ exclude: ['@enclave-e3/wasm'],
99
+ },
100
+ plugins: [wasm(), topLevelAwait()],
101
+ })
102
+ ```
103
+
104
+ ## Event System
105
+
106
+ The SDK uses a unified event system with TypeScript enums for type safety:
107
+
108
+ ### Enclave Events
109
+
110
+ ```typescript
111
+ enum EnclaveEventType {
112
+ // E3 Lifecycle
113
+ E3_REQUESTED = "E3Requested",
114
+ E3_ACTIVATED = "E3Activated",
115
+ INPUT_PUBLISHED = "InputPublished",
116
+ CIPHERTEXT_OUTPUT_PUBLISHED = "CiphertextOutputPublished",
117
+ PLAINTEXT_OUTPUT_PUBLISHED = "PlaintextOutputPublished",
118
+
119
+ // E3 Program Management
120
+ E3_PROGRAM_ENABLED = "E3ProgramEnabled",
121
+ E3_PROGRAM_DISABLED = "E3ProgramDisabled",
122
+
123
+ // Configuration
124
+ CIPHERNODE_REGISTRY_SET = "CiphernodeRegistrySet",
125
+ MAX_DURATION_SET = "MaxDurationSet",
126
+ // ... more events
127
+ }
128
+ ```
129
+
130
+ ### Registry Events
131
+
132
+ ```typescript
133
+ enum RegistryEventType {
134
+ CIPHERNODE_ADDED = "CiphernodeAdded",
135
+ CIPHERNODE_REMOVED = "CiphernodeRemoved",
136
+ COMMITTEE_REQUESTED = "CommitteeRequested",
137
+ COMMITTEE_PUBLISHED = "CommitteePublished",
138
+ ENCLAVE_SET = "EnclaveSet",
139
+ // ... more events
140
+ }
141
+ ```
142
+
143
+ ### Event Data Structure
144
+
145
+ Each event follows a consistent structure:
146
+
147
+ ```typescript
148
+ interface EnclaveEvent<T extends AllEventTypes> {
149
+ type: T;
150
+ data: EventData[T]; // Typed based on event type
151
+ log: Log; // Raw viem log
152
+ timestamp: Date;
153
+ blockNumber: bigint;
154
+ transactionHash: string;
155
+ }
156
+ ```
157
+
158
+ ## React Integration
159
+
160
+ The SDK includes a React hook for easy integration:
161
+
162
+ ```typescript
163
+ import { useEnclaveSDK } from '@enclave-e3/contracts/sdk';
164
+
165
+ function MyComponent() {
166
+ const {
167
+ sdk,
168
+ isInitialized,
169
+ isConnecting,
170
+ error,
171
+ connectWallet,
172
+ requestE3,
173
+ onEnclaveEvent,
174
+ EnclaveEventType
175
+ } = useEnclaveSDK({
176
+ contracts: {
177
+ enclave: '0x...',
178
+ ciphernodeRegistry: '0x...'
179
+ },
180
+ rpcUrl: 'YOUR_RPC_URL',
181
+ autoConnect: true
182
+ });
183
+
184
+ useEffect(() => {
185
+ if (isInitialized) {
186
+ onEnclaveEvent(EnclaveEventType.E3_REQUESTED, (event) => {
187
+ console.log('New E3 request:', event);
188
+ });
189
+ }
190
+ }, [isInitialized]);
191
+
192
+ return (
193
+ <div>
194
+ {!isInitialized && (
195
+ <button onClick={connectWallet} disabled={isConnecting}>
196
+ {isConnecting ? 'Connecting...' : 'Connect Wallet'}
197
+ </button>
198
+ )}
199
+ {/* Your UI */}
200
+ </div>
201
+ );
202
+ }
203
+ ```
204
+
205
+ ## API Reference
206
+
207
+ ### Core Methods
208
+
209
+ #### Contract Interactions
210
+
211
+ ```typescript
212
+ // Request a new E3 computation
213
+ await sdk.requestE3({
214
+ filter: `0x${string}`,
215
+ threshold: [number, number],
216
+ startWindow: [bigint, bigint],
217
+ duration: bigint,
218
+ e3Program: `0x${string}`,
219
+ e3ProgramParams: `0x${string}`,
220
+ computeProviderParams: `0x${string}`,
221
+ value?: bigint,
222
+ gasLimit?: bigint
223
+ });
224
+
225
+ // Activate an E3 computation
226
+ await sdk.activateE3(e3Id: bigint, publicKey: `0x${string}`, gasLimit?: bigint);
227
+
228
+ // Publish input data
229
+ await sdk.publishInput(e3Id: bigint, data: `0x${string}`, gasLimit?: bigint);
230
+
231
+ // Read operations
232
+ const e3Data = await sdk.getE3(e3Id: bigint);
233
+ ```
234
+
235
+ #### Event Handling
236
+
237
+ ```typescript
238
+ sdk.onEnclaveEvent(eventType: AllEventTypes, callback: EventCallback);
239
+
240
+ sdk.off(eventType: AllEventTypes, callback: EventCallback);
241
+
242
+ const logs = await sdk.getHistoricalEvents(
243
+ eventType: AllEventTypes,
244
+ fromBlock?: bigint,
245
+ toBlock?: bigint
246
+ );
247
+
248
+ // Event polling (if websockets unavailable)
249
+ await sdk.startEventPolling();
250
+ sdk.stopEventPolling();
251
+ ```
252
+
253
+ #### Utilities
254
+
255
+ ```typescript
256
+ // Gas estimation
257
+ const gas = await sdk.estimateGas(functionName, args, contractAddress, abi, value?);
258
+
259
+ // Transaction waiting
260
+ const receipt = await sdk.waitForTransaction(hash);
261
+
262
+ // Configuration updates
263
+ sdk.updateConfig(newConfig: Partial<SDKConfig>);
264
+
265
+ // Cleanup
266
+ sdk.cleanup();
267
+ ```
268
+
269
+ ## Configuration
270
+
271
+ ```typescript
272
+ interface SDKConfig {
273
+ publicClient: PublicClient;
274
+ walletClient?: WalletClient;
275
+ contracts: {
276
+ enclave: `0x${string}`;
277
+ ciphernodeRegistry: `0x${string}`;
278
+ };
279
+ chainId?: number;
280
+ }
281
+ ```
282
+
283
+ ## Error Handling
284
+
285
+ The SDK includes comprehensive error handling:
286
+
287
+ ```typescript
288
+ import { SDKError } from "@enclave-e3/sdk";
289
+
290
+ try {
291
+ await sdk.requestE3(params);
292
+ } catch (error) {
293
+ if (error instanceof SDKError) {
294
+ console.error(`SDK Error (${error.code}): ${error.message}`);
295
+ } else {
296
+ console.error("Unexpected error:", error);
297
+ }
298
+ }
299
+ ```
300
+
301
+ ## Development
302
+
303
+ ### Building the SDK
304
+
305
+ ```bash
306
+ cd packages/enclave-contracts
307
+ pnpm compile
308
+ ```
309
+
310
+ ### Running the Demo
311
+
312
+ ```bash
313
+ cd examples/basic/client
314
+ pnpm install
315
+ pnpm dev
316
+ ```
317
+
318
+ The demo showcases all SDK features including real-time event listening and
319
+ contract interactions.
320
+
321
+ ### Testing
322
+
323
+ ```bash
324
+ cd packages/enclave-contracts
325
+ pnpm test
326
+ ```
327
+
328
+ ## Architecture
329
+
330
+ The SDK consists of several key components:
331
+
332
+ - **EnclaveSDK**: Main orchestrator class
333
+ - **ContractClient**: Handles contract read/write operations
334
+ - **EventListener**: Manages real-time event listening
335
+ - **Types**: TypeScript definitions with full type safety
336
+ - **Utils**: Helper functions and error classes
337
+
338
+ ## License
339
+
340
+ This project is licensed under the MIT License.