@interfold/sdk 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/LICENSE.md +134 -0
- package/README.md +478 -0
- package/dist/contracts/index.cjs +320 -0
- package/dist/contracts/index.cjs.map +1 -0
- package/dist/contracts/index.d.ts +34 -0
- package/dist/contracts/index.js +296 -0
- package/dist/contracts/index.js.map +1 -0
- package/dist/crypto/index.cjs +1932 -0
- package/dist/crypto/index.cjs.map +1 -0
- package/dist/crypto/index.d.ts +59 -0
- package/dist/crypto/index.js +1892 -0
- package/dist/crypto/index.js.map +1 -0
- package/dist/events/index.cjs +270 -0
- package/dist/events/index.cjs.map +1 -0
- package/dist/events/index.d.ts +195 -0
- package/dist/events/index.js +241 -0
- package/dist/events/index.js.map +1 -0
- package/dist/index.cjs +2750 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +136 -0
- package/dist/index.js +2694 -0
- package/dist/index.js.map +1 -0
- package/dist/types-C9WZ1Khd.d.ts +64 -0
- package/package.json +73 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy
|
|
4
|
+
and distribute verbatim copies of this license document, but changing it is not allowed.
|
|
5
|
+
|
|
6
|
+
This version of the GNU Lesser General Public License incorporates the terms and conditions of
|
|
7
|
+
version 3 of the GNU General Public License, supplemented by the additional permissions listed
|
|
8
|
+
below.
|
|
9
|
+
|
|
10
|
+
0. Additional Definitions.
|
|
11
|
+
|
|
12
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the
|
|
13
|
+
"GNU GPL" refers to version 3 of the GNU General Public License.
|
|
14
|
+
|
|
15
|
+
"The Library" refers to a covered work governed by this License, other than an Application or a
|
|
16
|
+
Combined Work as defined below.
|
|
17
|
+
|
|
18
|
+
An "Application" is any work that makes use of an interface provided by the Library, but which is
|
|
19
|
+
not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed
|
|
20
|
+
a mode of using an interface provided by the Library.
|
|
21
|
+
|
|
22
|
+
A "Combined Work" is a work produced by combining or linking an Application with the Library. The
|
|
23
|
+
particular version of the Library with which the Combined Work was made is also called the "Linked
|
|
24
|
+
Version".
|
|
25
|
+
|
|
26
|
+
The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the
|
|
27
|
+
Combined Work, excluding any source code for portions of the Combined Work that, considered in
|
|
28
|
+
isolation, are based on the Application, and not on the Linked Version.
|
|
29
|
+
|
|
30
|
+
The "Corresponding Application Code" for a Combined Work means the object code and/or source code
|
|
31
|
+
for the Application, including any data and utility programs needed for reproducing the Combined
|
|
32
|
+
Work from the Application, but excluding the System Libraries of the Combined Work.
|
|
33
|
+
|
|
34
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
35
|
+
|
|
36
|
+
You may convey a covered work under sections 3 and 4 of this License without being bound by section
|
|
37
|
+
3 of the GNU GPL.
|
|
38
|
+
|
|
39
|
+
2. Conveying Modified Versions.
|
|
40
|
+
|
|
41
|
+
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or
|
|
42
|
+
data to be supplied by an Application that uses the facility (other than as an argument passed when
|
|
43
|
+
the facility is invoked), then you may convey a copy of the modified version:
|
|
44
|
+
|
|
45
|
+
a) under this License, provided that you make a good faith effort to ensure that, in the event an
|
|
46
|
+
Application does not supply the function or data, the facility still operates, and performs whatever
|
|
47
|
+
part of its purpose remains meaningful, or
|
|
48
|
+
|
|
49
|
+
b) under the GNU GPL, with none of the additional permissions of this License applicable to that
|
|
50
|
+
copy.
|
|
51
|
+
|
|
52
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
53
|
+
|
|
54
|
+
The object code form of an Application may incorporate material from a header file that is part of
|
|
55
|
+
the Library. You may convey such object code under terms of your choice, provided that, if the
|
|
56
|
+
incorporated material is not limited to numerical parameters, data structure layouts and accessors,
|
|
57
|
+
or small macros, inline functions and templates (ten or fewer lines in length), you do both of the
|
|
58
|
+
following:
|
|
59
|
+
|
|
60
|
+
a) Give prominent notice with each copy of the object code that the Library is used in it and that
|
|
61
|
+
the Library and its use are covered by this License.
|
|
62
|
+
|
|
63
|
+
b) Accompany the object code with a copy of the GNU GPL and this license document.
|
|
64
|
+
|
|
65
|
+
4. Combined Works.
|
|
66
|
+
|
|
67
|
+
You may convey a Combined Work under terms of your choice that, taken together, effectively do not
|
|
68
|
+
restrict modification of the portions of the Library contained in the Combined Work and reverse
|
|
69
|
+
engineering for debugging such modifications, if you also do each of the following:
|
|
70
|
+
|
|
71
|
+
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that
|
|
72
|
+
the Library and its use are covered by this License.
|
|
73
|
+
|
|
74
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
|
75
|
+
|
|
76
|
+
c) For a Combined Work that displays copyright notices during execution, include the copyright
|
|
77
|
+
notice for the Library among these notices, as well as a reference directing the user to the copies
|
|
78
|
+
of the GNU GPL and this license document.
|
|
79
|
+
|
|
80
|
+
d) Do one of the following:
|
|
81
|
+
|
|
82
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
83
|
+
License, and the Corresponding Application Code in a form
|
|
84
|
+
suitable for, and under terms that permit, the user to
|
|
85
|
+
recombine or relink the Application with a modified version of
|
|
86
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
87
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
88
|
+
Corresponding Source.
|
|
89
|
+
|
|
90
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
91
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
92
|
+
a copy of the Library already present on the user's computer
|
|
93
|
+
system, and (b) will operate properly with a modified version
|
|
94
|
+
of the Library that is interface-compatible with the Linked
|
|
95
|
+
Version.
|
|
96
|
+
|
|
97
|
+
e) Provide Installation Information, but only if you would otherwise be required to provide such
|
|
98
|
+
information under section 6 of the GNU GPL, and only to the extent that such information is
|
|
99
|
+
necessary to install and execute a modified version of the Combined Work produced by recombining or
|
|
100
|
+
relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the
|
|
101
|
+
Installation Information must accompany the Minimal Corresponding Source and Corresponding
|
|
102
|
+
Application Code. If you use option 4d1, you must provide the Installation Information in the manner
|
|
103
|
+
specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
|
104
|
+
|
|
105
|
+
5. Combined Libraries.
|
|
106
|
+
|
|
107
|
+
You may place library facilities that are a work based on the Library side by side in a single
|
|
108
|
+
library together with other library facilities that are not Applications and are not covered by this
|
|
109
|
+
License, and convey such a combined library under terms of your choice, if you do both of the
|
|
110
|
+
following:
|
|
111
|
+
|
|
112
|
+
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with
|
|
113
|
+
any other library facilities, conveyed under the terms of this License.
|
|
114
|
+
|
|
115
|
+
b) Give prominent notice with the combined library that part of it is a work based on the Library,
|
|
116
|
+
and explaining where to find the accompanying uncombined form of the same work.
|
|
117
|
+
|
|
118
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
119
|
+
|
|
120
|
+
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General
|
|
121
|
+
Public License from time to time. Such new versions will be similar in spirit to the present
|
|
122
|
+
version, but may differ in detail to address new problems or concerns.
|
|
123
|
+
|
|
124
|
+
Each version is given a distinguishing version number. If the Library as you received it specifies
|
|
125
|
+
that a certain numbered version of the GNU Lesser General Public License "or any later version"
|
|
126
|
+
applies to it, you have the option of following the terms and conditions either of that published
|
|
127
|
+
version or of any later version published by the Free Software Foundation. If the Library as you
|
|
128
|
+
received it does not specify a version number of the GNU Lesser General Public License, you may
|
|
129
|
+
choose any version of the GNU Lesser General Public License ever published by the Free Software
|
|
130
|
+
Foundation.
|
|
131
|
+
|
|
132
|
+
If the Library as you received it specifies that a proxy can decide whether future versions of the
|
|
133
|
+
GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any
|
|
134
|
+
version is permanent authorization for you to choose that version for the Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
# Interfold TypeScript SDK
|
|
2
|
+
|
|
3
|
+
A powerful, type-safe TypeScript SDK for interacting with Interfold smart contracts. This SDK
|
|
4
|
+
provides real-time event listening, contract interaction methods, and comprehensive error handling.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- **Event-driven architecture**: Listen to smart contract events in real-time
|
|
9
|
+
- **Type-safe**: Built with TypeScript and uses generated types from contracts
|
|
10
|
+
- **Easy contract interactions**: Simple methods for reading from and writing to contracts
|
|
11
|
+
- **React integration**: Includes React hooks for easy frontend integration (via `@interfold/react`)
|
|
12
|
+
- **Modular architecture**: Tree-shakeable sub-modules for contracts, events, and encryption
|
|
13
|
+
- **Encryption helpers**: Standalone FHE encryption functions with optional ZK proof generation
|
|
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 listening
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add @interfold/sdk
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { InterfoldSDK, InterfoldEventType, RegistryEventType } from '@interfold/sdk'
|
|
28
|
+
import { createPublicClient, createWalletClient, http, custom } from 'viem'
|
|
29
|
+
import { sepolia } from 'viem/chains'
|
|
30
|
+
|
|
31
|
+
// Initialize clients
|
|
32
|
+
const publicClient = createPublicClient({
|
|
33
|
+
chain: sepolia,
|
|
34
|
+
transport: http('YOUR_RPC_URL'),
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const walletClient = createWalletClient({
|
|
38
|
+
chain: sepolia,
|
|
39
|
+
transport: custom(window.ethereum),
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
// Create SDK instance
|
|
43
|
+
const sdk = new InterfoldSDK({
|
|
44
|
+
publicClient,
|
|
45
|
+
walletClient,
|
|
46
|
+
contracts: {
|
|
47
|
+
interfold: '0x...', // Your Interfold contract address
|
|
48
|
+
ciphernodeRegistry: '0x...', // Your CiphernodeRegistry contract address
|
|
49
|
+
feeToken: '0x...', // Your ERC-20 fee token address
|
|
50
|
+
},
|
|
51
|
+
chain: sepolia,
|
|
52
|
+
// Use 'SECURE_THRESHOLD_8192' for production; 'INSECURE_THRESHOLD_512' for local dev only
|
|
53
|
+
thresholdBfvParamsPresetName: 'SECURE_THRESHOLD_8192',
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
// Listen to events with the unified event system
|
|
57
|
+
sdk.onInterfoldEvent(InterfoldEventType.E3_REQUESTED, (event) => {
|
|
58
|
+
console.log('E3 Requested:', event.data)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
sdk.onInterfoldEvent(RegistryEventType.COMMITTEE_REQUESTED, (event) => {
|
|
62
|
+
console.log('Committee Requested:', event.data)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// Interact with contracts
|
|
66
|
+
const hash = await sdk.requestE3({
|
|
67
|
+
threshold: [1, 3],
|
|
68
|
+
inputWindow: [BigInt(0), BigInt(100)],
|
|
69
|
+
e3Program: '0x...',
|
|
70
|
+
e3ProgramParams: '0x...',
|
|
71
|
+
computeProviderParams: '0x...',
|
|
72
|
+
customParams: '0x...',
|
|
73
|
+
})
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Factory Method
|
|
77
|
+
|
|
78
|
+
For a simpler setup (especially on the server), use the static `InterfoldSDK.create()` factory:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { InterfoldSDK } from '@interfold/sdk'
|
|
82
|
+
import { sepolia } from 'viem/chains'
|
|
83
|
+
|
|
84
|
+
const sdk = InterfoldSDK.create({
|
|
85
|
+
rpcUrl: 'wss://sepolia.example.com',
|
|
86
|
+
contracts: {
|
|
87
|
+
interfold: '0x...',
|
|
88
|
+
ciphernodeRegistry: '0x...',
|
|
89
|
+
feeToken: '0x...',
|
|
90
|
+
},
|
|
91
|
+
chain: sepolia,
|
|
92
|
+
privateKey: '0x...', // optional — omit for read-only
|
|
93
|
+
// Use 'SECURE_THRESHOLD_8192' for production; 'INSECURE_THRESHOLD_512' for local dev only
|
|
94
|
+
thresholdBfvParamsPresetName: 'SECURE_THRESHOLD_8192',
|
|
95
|
+
})
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The factory auto-detects HTTP vs WebSocket transports and creates the appropriate viem clients.
|
|
99
|
+
|
|
100
|
+
## Usage within a browser
|
|
101
|
+
|
|
102
|
+
Usage within a typescript project should work out of the box, however in order to use wasm related
|
|
103
|
+
functionality of the SDK within the browser vite you must do the following:
|
|
104
|
+
|
|
105
|
+
- Use `vite`
|
|
106
|
+
- Use the `vite-plugin-top-level-await` plugin
|
|
107
|
+
- Use the `vite-plugin-wasm` plugin
|
|
108
|
+
- Exclude the `@interfold/wasm` package from bundling optimization.
|
|
109
|
+
|
|
110
|
+
This will enable `vite` to correctly bundle and serve the wasm bundle we use effectively.
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
import { defineConfig } from 'vite'
|
|
114
|
+
import wasm from 'vite-plugin-wasm'
|
|
115
|
+
import topLevelAwait from 'vite-plugin-top-level-await'
|
|
116
|
+
|
|
117
|
+
export default defineConfig({
|
|
118
|
+
// other config ...
|
|
119
|
+
optimizeDeps: {
|
|
120
|
+
exclude: ['@interfold/wasm'],
|
|
121
|
+
},
|
|
122
|
+
plugins: [wasm(), topLevelAwait()],
|
|
123
|
+
})
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Event System
|
|
127
|
+
|
|
128
|
+
The SDK uses a unified event system with TypeScript enums for type safety:
|
|
129
|
+
|
|
130
|
+
### Interfold Events
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
enum InterfoldEventType {
|
|
134
|
+
// E3 Lifecycle
|
|
135
|
+
E3_REQUESTED = 'E3Requested',
|
|
136
|
+
CIPHERTEXT_OUTPUT_PUBLISHED = 'CiphertextOutputPublished',
|
|
137
|
+
PLAINTEXT_OUTPUT_PUBLISHED = 'PlaintextOutputPublished',
|
|
138
|
+
|
|
139
|
+
// E3 Program Management
|
|
140
|
+
E3_PROGRAM_ENABLED = 'E3ProgramEnabled',
|
|
141
|
+
E3_PROGRAM_DISABLED = 'E3ProgramDisabled',
|
|
142
|
+
|
|
143
|
+
// Encryption Scheme Management
|
|
144
|
+
ENCRYPTION_SCHEME_ENABLED = 'EncryptionSchemeEnabled',
|
|
145
|
+
ENCRYPTION_SCHEME_DISABLED = 'EncryptionSchemeDisabled',
|
|
146
|
+
|
|
147
|
+
// Configuration
|
|
148
|
+
CIPHERNODE_REGISTRY_SET = 'CiphernodeRegistrySet',
|
|
149
|
+
MAX_DURATION_SET = 'MaxDurationSet',
|
|
150
|
+
ALLOWED_E3_PROGRAMS_PARAMS_SET = 'AllowedE3ProgramsParamsSet',
|
|
151
|
+
OWNERSHIP_TRANSFERRED = 'OwnershipTransferred',
|
|
152
|
+
INITIALIZED = 'Initialized',
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Registry Events
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
enum RegistryEventType {
|
|
160
|
+
COMMITTEE_REQUESTED = 'CommitteeRequested',
|
|
161
|
+
COMMITTEE_PUBLISHED = 'CommitteePublished',
|
|
162
|
+
COMMITTEE_FINALIZED = 'CommitteeFinalized',
|
|
163
|
+
INTERFOLD_SET = 'InterfoldSet',
|
|
164
|
+
OWNERSHIP_TRANSFERRED = 'OwnershipTransferred',
|
|
165
|
+
INITIALIZED = 'Initialized',
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Event Data Structure
|
|
170
|
+
|
|
171
|
+
Each event follows a consistent structure:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
interface InterfoldEvent<T extends AllEventTypes> {
|
|
175
|
+
type: T
|
|
176
|
+
data: EventData[T] // Typed based on event type
|
|
177
|
+
log: Log // Raw viem log
|
|
178
|
+
timestamp: Date
|
|
179
|
+
blockNumber: bigint
|
|
180
|
+
transactionHash: string
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## React Integration
|
|
185
|
+
|
|
186
|
+
The SDK includes a React hook via the `@interfold/react` package:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
pnpm add @interfold/react
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
import { useInterfoldSDK } from '@interfold/react'
|
|
194
|
+
|
|
195
|
+
function MyComponent() {
|
|
196
|
+
const {
|
|
197
|
+
sdk,
|
|
198
|
+
isInitialized,
|
|
199
|
+
error,
|
|
200
|
+
requestE3,
|
|
201
|
+
onInterfoldEvent,
|
|
202
|
+
off,
|
|
203
|
+
InterfoldEventType,
|
|
204
|
+
RegistryEventType,
|
|
205
|
+
} = useInterfoldSDK({
|
|
206
|
+
contracts: {
|
|
207
|
+
interfold: '0x...',
|
|
208
|
+
ciphernodeRegistry: '0x...',
|
|
209
|
+
feeToken: '0x...',
|
|
210
|
+
},
|
|
211
|
+
autoConnect: true,
|
|
212
|
+
// Use 'SECURE_THRESHOLD_8192' for production; 'INSECURE_THRESHOLD_512' for local dev only
|
|
213
|
+
thresholdBfvParamsPresetName: 'SECURE_THRESHOLD_8192',
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
useEffect(() => {
|
|
217
|
+
if (isInitialized) {
|
|
218
|
+
const handler = (event) => {
|
|
219
|
+
console.log('New E3 request:', event)
|
|
220
|
+
}
|
|
221
|
+
onInterfoldEvent(InterfoldEventType.E3_REQUESTED, handler)
|
|
222
|
+
return () => off(InterfoldEventType.E3_REQUESTED, handler)
|
|
223
|
+
}
|
|
224
|
+
}, [isInitialized])
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<div>
|
|
228
|
+
{error && <p>Error: {error}</p>}
|
|
229
|
+
{!isInitialized && <p>Initializing...</p>}
|
|
230
|
+
{/* Your UI */}
|
|
231
|
+
</div>
|
|
232
|
+
)
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
The hook uses wagmi's `usePublicClient` and `useWalletClient` under the hood, so your app must be
|
|
237
|
+
wrapped in a wagmi provider.
|
|
238
|
+
|
|
239
|
+
## Encryption Functions
|
|
240
|
+
|
|
241
|
+
The SDK provides standalone encryption functions for FHE (Fully Homomorphic Encryption) operations.
|
|
242
|
+
These can be used via the SDK instance or imported directly for tree-shaking:
|
|
243
|
+
|
|
244
|
+
### Via the SDK instance
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
// Generate a public key
|
|
248
|
+
const publicKey = await sdk.generatePublicKey()
|
|
249
|
+
|
|
250
|
+
// Encrypt a single number
|
|
251
|
+
const encrypted = await sdk.encryptNumber(42n, publicKey)
|
|
252
|
+
|
|
253
|
+
// Encrypt a vector
|
|
254
|
+
const encryptedVec = await sdk.encryptVector(BigUint64Array.from([1n, 2n, 3n]), publicKey)
|
|
255
|
+
|
|
256
|
+
// Encrypt with ZK proof generation
|
|
257
|
+
const { encryptedData, proof } = await sdk.encryptNumberAndGenProof(42n, publicKey)
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### Standalone imports
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
import {
|
|
264
|
+
generatePublicKey,
|
|
265
|
+
encryptNumber,
|
|
266
|
+
encryptVector,
|
|
267
|
+
encryptNumberAndGenProof,
|
|
268
|
+
encryptVectorAndGenProof,
|
|
269
|
+
encryptNumberAndGenInputs,
|
|
270
|
+
encryptVectorAndGenInputs,
|
|
271
|
+
computePublicKeyCommitment,
|
|
272
|
+
getThresholdBfvParamsSet,
|
|
273
|
+
} from '@interfold/sdk'
|
|
274
|
+
|
|
275
|
+
// Use 'SECURE_THRESHOLD_8192' for production; 'INSECURE_THRESHOLD_512' for local dev only
|
|
276
|
+
const presetName = 'SECURE_THRESHOLD_8192'
|
|
277
|
+
|
|
278
|
+
const publicKey = await generatePublicKey(presetName)
|
|
279
|
+
const encrypted = await encryptNumber(42n, publicKey, presetName)
|
|
280
|
+
const { encryptedData, proof } = await encryptNumberAndGenProof(42n, publicKey, presetName)
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Modular Imports
|
|
284
|
+
|
|
285
|
+
The SDK is organized into three sub-modules that can be imported independently for tree-shaking:
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
// Encryption functions and types
|
|
289
|
+
import { generatePublicKey, encryptNumber } from '@interfold/sdk/crypto'
|
|
290
|
+
|
|
291
|
+
// Contract client and types
|
|
292
|
+
import { ContractClient } from '@interfold/sdk/contracts'
|
|
293
|
+
import type { ContractAddresses, E3 } from '@interfold/sdk/contracts'
|
|
294
|
+
|
|
295
|
+
// Event listener and types
|
|
296
|
+
import { EventListener, InterfoldEventType, RegistryEventType } from '@interfold/sdk/events'
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
All sub-module exports are also re-exported from the main `@interfold/sdk` entry point for
|
|
300
|
+
convenience.
|
|
301
|
+
|
|
302
|
+
## API Reference
|
|
303
|
+
|
|
304
|
+
### Core Methods
|
|
305
|
+
|
|
306
|
+
#### Contract Interactions
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
// Approve fee token spending
|
|
310
|
+
await sdk.approveFeeToken(amount: bigint);
|
|
311
|
+
|
|
312
|
+
// Request a new E3 computation
|
|
313
|
+
await sdk.requestE3({
|
|
314
|
+
threshold: [number, number],
|
|
315
|
+
inputWindow: [bigint, bigint],
|
|
316
|
+
e3Program: `0x${string}`,
|
|
317
|
+
e3ProgramParams: `0x${string}`,
|
|
318
|
+
computeProviderParams: `0x${string}`,
|
|
319
|
+
customParams?: `0x${string}`,
|
|
320
|
+
gasLimit?: bigint
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// Publish ciphertext output
|
|
324
|
+
await sdk.publishCiphertextOutput(e3Id: bigint, ciphertextOutput: `0x${string}`, proof: `0x${string}`, gasLimit?: bigint);
|
|
325
|
+
|
|
326
|
+
// Read operations
|
|
327
|
+
const e3Data = await sdk.getE3(e3Id: bigint);
|
|
328
|
+
const publicKey = await sdk.getE3PublicKey(e3Id: bigint);
|
|
329
|
+
const quote = await sdk.getE3Quote(params: E3RequestParams);
|
|
330
|
+
const stage = await sdk.getE3Stage(e3Id: bigint);
|
|
331
|
+
const reason = await sdk.getFailureReason(e3Id: bigint);
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
#### Event Handling
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
sdk.onInterfoldEvent(eventType: AllEventTypes, callback: EventCallback);
|
|
338
|
+
|
|
339
|
+
sdk.off(eventType: AllEventTypes, callback: EventCallback);
|
|
340
|
+
|
|
341
|
+
sdk.once(eventType: AllEventTypes, callback: EventCallback);
|
|
342
|
+
|
|
343
|
+
const logs = await sdk.getHistoricalEvents(
|
|
344
|
+
eventType: AllEventTypes,
|
|
345
|
+
fromBlock?: bigint,
|
|
346
|
+
toBlock?: bigint
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
// Event polling (if websockets unavailable)
|
|
350
|
+
await sdk.startEventPolling();
|
|
351
|
+
sdk.stopEventPolling();
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
#### Encryption
|
|
355
|
+
|
|
356
|
+
```typescript
|
|
357
|
+
// Get BFV parameter set
|
|
358
|
+
const params = await sdk.getThresholdBfvParamsSet();
|
|
359
|
+
|
|
360
|
+
// Key generation
|
|
361
|
+
const publicKey = await sdk.generatePublicKey();
|
|
362
|
+
const commitment = await sdk.computePublicKeyCommitment(publicKey);
|
|
363
|
+
|
|
364
|
+
// Encrypt data
|
|
365
|
+
const encrypted = await sdk.encryptNumber(data: bigint, publicKey: Uint8Array);
|
|
366
|
+
const encryptedVec = await sdk.encryptVector(data: BigUint64Array, publicKey: Uint8Array);
|
|
367
|
+
|
|
368
|
+
// Encrypt with proof inputs (for ZK verification)
|
|
369
|
+
const { encryptedData, circuitInputs } = await sdk.encryptNumberAndGenInputs(data, publicKey);
|
|
370
|
+
const { encryptedData, circuitInputs } = await sdk.encryptVectorAndGenInputs(data, publicKey);
|
|
371
|
+
|
|
372
|
+
// Encrypt with full ZK proof generation
|
|
373
|
+
const { encryptedData, proof } = await sdk.encryptNumberAndGenProof(data, publicKey);
|
|
374
|
+
const { encryptedData, proof } = await sdk.encryptVectorAndGenProof(data, publicKey);
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
#### Utilities
|
|
378
|
+
|
|
379
|
+
```typescript
|
|
380
|
+
// Gas estimation
|
|
381
|
+
const gas = await sdk.estimateGas(functionName, args, contractAddress, abi, value?);
|
|
382
|
+
|
|
383
|
+
// Transaction waiting
|
|
384
|
+
const receipt = await sdk.waitForTransaction(hash);
|
|
385
|
+
|
|
386
|
+
// Cleanup
|
|
387
|
+
sdk.cleanup();
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
## Configuration
|
|
391
|
+
|
|
392
|
+
```typescript
|
|
393
|
+
interface SDKConfig {
|
|
394
|
+
publicClient: PublicClient
|
|
395
|
+
walletClient?: WalletClient
|
|
396
|
+
contracts: {
|
|
397
|
+
interfold: `0x${string}`
|
|
398
|
+
ciphernodeRegistry: `0x${string}`
|
|
399
|
+
feeToken: `0x${string}`
|
|
400
|
+
}
|
|
401
|
+
chain?: Chain
|
|
402
|
+
thresholdBfvParamsPresetName: ThresholdBfvParamsPresetName
|
|
403
|
+
}
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
`thresholdBfvParamsPresetName` selects the BFV parameter set used for encryption. It must match the
|
|
407
|
+
on-chain `paramSet` index registered in the Interfold contract:
|
|
408
|
+
|
|
409
|
+
| Preset name | On-chain `paramSet` index | Use case |
|
|
410
|
+
| -------------------------- | ------------------------- | ----------------------------------------------------------------------------------------------------------- |
|
|
411
|
+
| `'INSECURE_THRESHOLD_512'` | `0` | Local development and testing only — small polynomial degree (N=512), fast but not cryptographically secure |
|
|
412
|
+
| `'SECURE_THRESHOLD_8192'` | `1` | Production — full security parameters (N=8192, L=4 CRT moduli) |
|
|
413
|
+
|
|
414
|
+
Always use `'SECURE_THRESHOLD_8192'` in production. The insecure preset exists solely to speed up
|
|
415
|
+
local dev cycles.
|
|
416
|
+
|
|
417
|
+
## Error Handling
|
|
418
|
+
|
|
419
|
+
The SDK includes comprehensive error handling:
|
|
420
|
+
|
|
421
|
+
```typescript
|
|
422
|
+
import { SDKError } from '@interfold/sdk'
|
|
423
|
+
|
|
424
|
+
try {
|
|
425
|
+
await sdk.requestE3(params)
|
|
426
|
+
} catch (error) {
|
|
427
|
+
if (error instanceof SDKError) {
|
|
428
|
+
console.error(`SDK Error (${error.code}): ${error.message}`)
|
|
429
|
+
} else {
|
|
430
|
+
console.error('Unexpected error:', error)
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
## Development
|
|
436
|
+
|
|
437
|
+
### Building the SDK
|
|
438
|
+
|
|
439
|
+
```bash
|
|
440
|
+
cd packages/interfold-sdk
|
|
441
|
+
pnpm build
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
### Running the Demo
|
|
445
|
+
|
|
446
|
+
```bash
|
|
447
|
+
cd examples/basic/client
|
|
448
|
+
pnpm install
|
|
449
|
+
pnpm dev
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
The demo showcases all SDK features including real-time event listening and contract interactions.
|
|
453
|
+
|
|
454
|
+
### Testing
|
|
455
|
+
|
|
456
|
+
```bash
|
|
457
|
+
cd packages/interfold-sdk
|
|
458
|
+
pnpm test
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
## Architecture
|
|
462
|
+
|
|
463
|
+
The SDK is organized into a modular architecture with three domain-specific sub-modules:
|
|
464
|
+
|
|
465
|
+
- **InterfoldSDK** (`interfold-sdk.ts`): Main orchestrator class that delegates to sub-modules
|
|
466
|
+
- **Contracts** (`contracts/`): `ContractClient` for contract read/write operations, type
|
|
467
|
+
definitions for contract addresses and E3 data
|
|
468
|
+
- **Events** (`events/`): `EventListener` for real-time and historical event subscriptions, typed
|
|
469
|
+
event enums and data interfaces
|
|
470
|
+
- **Encryption** (`encryption/`): Standalone FHE encryption functions, BFV parameter management, ZK
|
|
471
|
+
proof generation
|
|
472
|
+
- **Utils** (`utils.ts`): Helper functions, error classes, encoding utilities
|
|
473
|
+
|
|
474
|
+
Each sub-module has its own `index.ts` entry point and can be imported independently.
|
|
475
|
+
|
|
476
|
+
## License
|
|
477
|
+
|
|
478
|
+
This project is licensed under the MIT License.
|