@opendatalabs/vana-sdk 0.1.0-alpha.e64ec83 → 0.1.0-alpha.e72c89b
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/README.md +253 -427
- package/package.json +55 -28
- package/dist/chains.browser.cjs +0 -93
- package/dist/chains.browser.cjs.map +0 -1
- package/dist/chains.browser.d.cts +0 -52
- package/dist/chains.browser.d.ts +0 -52
- package/dist/chains.browser.js +0 -63
- package/dist/chains.browser.js.map +0 -1
- package/dist/chains.cjs +0 -93
- package/dist/chains.cjs.map +0 -1
- package/dist/chains.d.cts +0 -2
- package/dist/chains.d.ts +0 -2
- package/dist/chains.js +0 -63
- package/dist/chains.js.map +0 -1
- package/dist/chains.node.cjs +0 -93
- package/dist/chains.node.cjs.map +0 -1
- package/dist/chains.node.d.cts +0 -2
- package/dist/chains.node.d.ts +0 -2
- package/dist/chains.node.js +0 -63
- package/dist/chains.node.js.map +0 -1
- package/dist/index.browser.d.ts +0 -31266
- package/dist/index.browser.js +0 -40698
- package/dist/index.browser.js.map +0 -1
- package/dist/index.d.cts +0 -31278
- package/dist/index.node.cjs +0 -41179
- package/dist/index.node.cjs.map +0 -1
- package/dist/index.node.d.cts +0 -31372
- package/dist/index.node.d.ts +0 -31372
- package/dist/index.node.js +0 -41041
- package/dist/index.node.js.map +0 -1
- package/dist/platform.browser.d.ts +0 -224
- package/dist/platform.browser.js +0 -467
- package/dist/platform.browser.js.map +0 -1
- package/dist/platform.cjs +0 -809
- package/dist/platform.cjs.map +0 -1
- package/dist/platform.d.cts +0 -1
- package/dist/platform.d.ts +0 -1
- package/dist/platform.js +0 -772
- package/dist/platform.js.map +0 -1
- package/dist/platform.node.cjs +0 -809
- package/dist/platform.node.cjs.map +0 -1
- package/dist/platform.node.d.cts +0 -264
- package/dist/platform.node.d.ts +0 -264
- package/dist/platform.node.js +0 -772
- package/dist/platform.node.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,33 +1,23 @@
|
|
|
1
1
|
# Vana SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **⚠️ ALPHA SOFTWARE - EXPERIMENTAL USE ONLY**
|
|
4
|
+
>
|
|
5
|
+
> This SDK is in early alpha development and is **NOT SUITABLE FOR PRODUCTION USE**.
|
|
6
|
+
> Features may change without notice, and data loss or unexpected behavior may occur.
|
|
7
|
+
> Use at your own risk and avoid using with mainnet assets or critical operations.
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
A TypeScript SDK for building data-driven applications on the Vana Network. Enable users to grant gasless permissions, manage encrypted data, and interact with privacy-preserving infrastructure.
|
|
10
|
+
|
|
11
|
+
[](https://www.npmjs.com/package/@opendatalabs/vana-sdk)
|
|
6
12
|
[](https://www.typescriptlang.org/)
|
|
7
13
|
[](https://opensource.org/licenses/ISC)
|
|
8
14
|
|
|
9
|
-
[Documentation](https://vana-com.github.io/vana-sdk) • [Examples](#examples) • [
|
|
10
|
-
|
|
11
|
-
## Why Vana SDK?
|
|
12
|
-
|
|
13
|
-
- **🔐 Gasless Permissions**: EIP-712 based permission system with zero gas fees for users
|
|
14
|
-
- **📁 Data Management**: Query, upload, and manage encrypted user data files
|
|
15
|
-
- **🔄 Flexible Relaying**: Callback-based relay system supporting any gasless transaction infrastructure
|
|
16
|
-
- **📊 Schema Validation**: Built-in JSON Schema and SQLite schema validation with AJV
|
|
17
|
-
- **🔧 Type-Safe**: Full TypeScript support with comprehensive type definitions
|
|
18
|
-
- **🎯 Production Ready**: Battle-tested with comprehensive error handling and retry mechanisms
|
|
15
|
+
[API Documentation](https://vana-com.github.io/vana-sdk) • [Examples](#examples) • [Configuration](#configuration)
|
|
19
16
|
|
|
20
17
|
## Installation
|
|
21
18
|
|
|
22
19
|
```bash
|
|
23
|
-
|
|
24
|
-
npm install vana-sdk
|
|
25
|
-
|
|
26
|
-
# yarn
|
|
27
|
-
yarn add vana-sdk
|
|
28
|
-
|
|
29
|
-
# pnpm
|
|
30
|
-
pnpm add vana-sdk
|
|
20
|
+
npm install @opendatalabs/vana-sdk
|
|
31
21
|
```
|
|
32
22
|
|
|
33
23
|
**Peer Dependencies:**
|
|
@@ -38,13 +28,22 @@ npm install viem@^2.31.7
|
|
|
38
28
|
|
|
39
29
|
## Quick Start
|
|
40
30
|
|
|
41
|
-
|
|
31
|
+
The Vana SDK provides optimized builds for different environments:
|
|
32
|
+
|
|
33
|
+
| Build | Use Case | Crypto Implementation | Configuration |
|
|
34
|
+
| -------------- | --------------------------------- | ---------------------------------- | ----------------- |
|
|
35
|
+
| **`/browser`** | Browser apps (React, Vue) | Pure JavaScript (@noble/secp256k1) | **Zero config** ✓ |
|
|
36
|
+
| **`/node`** | Server-side (Node.js, API routes) | Native bindings (secp256k1) | Zero config ✓ |
|
|
37
|
+
|
|
38
|
+
### Browser Applications
|
|
39
|
+
|
|
40
|
+
The browser build uses pure JavaScript cryptography and requires **no special configuration**:
|
|
42
41
|
|
|
43
42
|
```typescript
|
|
44
|
-
|
|
43
|
+
// Browser build - works out of the box with any bundler
|
|
44
|
+
import { Vana, mokshaTestnet } from "@opendatalabs/vana-sdk/browser";
|
|
45
45
|
import { createWalletClient, http } from "viem";
|
|
46
46
|
import { privateKeyToAccount } from "viem/accounts";
|
|
47
|
-
import { mokshaTestnet } from "vana-sdk";
|
|
48
47
|
|
|
49
48
|
// Create wallet client
|
|
50
49
|
const account = privateKeyToAccount("0x...");
|
|
@@ -54,104 +53,140 @@ const walletClient = createWalletClient({
|
|
|
54
53
|
transport: http("https://rpc.moksha.vana.org"),
|
|
55
54
|
});
|
|
56
55
|
|
|
57
|
-
// Initialize
|
|
58
|
-
const vana =
|
|
56
|
+
// Initialize SDK
|
|
57
|
+
const vana = Vana({
|
|
59
58
|
walletClient,
|
|
60
|
-
// Optional: configure gasless relayer
|
|
61
59
|
relayerUrl: "https://relayer.moksha.vana.org",
|
|
62
60
|
});
|
|
63
61
|
```
|
|
64
62
|
|
|
65
|
-
###
|
|
63
|
+
### Server-side Applications (Node.js)
|
|
64
|
+
|
|
65
|
+
The Node.js build uses native secp256k1 bindings for optimal performance:
|
|
66
66
|
|
|
67
67
|
```typescript
|
|
68
|
-
//
|
|
68
|
+
// For server-side applications (Next.js API routes, Express)
|
|
69
|
+
import { Vana, mokshaTestnet } from "@opendatalabs/vana-sdk/node";
|
|
70
|
+
import { createWalletClient, http } from "viem";
|
|
71
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
72
|
+
|
|
73
|
+
// Create wallet client
|
|
74
|
+
const account = privateKeyToAccount("0x...");
|
|
75
|
+
const walletClient = createWalletClient({
|
|
76
|
+
account,
|
|
77
|
+
chain: mokshaTestnet,
|
|
78
|
+
transport: http("https://rpc.moksha.vana.org"),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Initialize SDK
|
|
82
|
+
const vana = Vana({
|
|
83
|
+
walletClient,
|
|
84
|
+
relayerUrl: "https://relayer.moksha.vana.org",
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Grant gasless permission
|
|
69
88
|
const txHash = await vana.permissions.grant({
|
|
70
|
-
|
|
89
|
+
grantee: "0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36",
|
|
71
90
|
operation: "llm_inference",
|
|
72
91
|
parameters: {
|
|
73
|
-
prompt: "Analyze my
|
|
92
|
+
prompt: "Analyze my data for insights",
|
|
74
93
|
maxTokens: 1000,
|
|
75
|
-
files: [12, 15, 28], // Specific file IDs
|
|
76
|
-
model: "gpt-4",
|
|
77
94
|
},
|
|
78
|
-
expiresAt: Math.floor(Date.now() / 1000) +
|
|
95
|
+
expiresAt: Math.floor(Date.now() / 1000) + 86400, // 24 hours
|
|
79
96
|
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Core Features
|
|
80
100
|
|
|
81
|
-
|
|
101
|
+
### Gasless Permissions
|
|
102
|
+
|
|
103
|
+
Users can grant data access permissions without paying gas fees through EIP-712 signatures and relay infrastructure.
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
// Grant permission with custom parameters
|
|
107
|
+
await vana.permissions.grant({
|
|
108
|
+
grantee: applicationAddress,
|
|
109
|
+
operation: "data_analysis",
|
|
110
|
+
parameters: {
|
|
111
|
+
analysisType: "sentiment",
|
|
112
|
+
files: [12, 15, 28],
|
|
113
|
+
model: "gpt-4",
|
|
114
|
+
},
|
|
115
|
+
});
|
|
82
116
|
```
|
|
83
117
|
|
|
84
|
-
###
|
|
118
|
+
### Encrypted Data Management
|
|
119
|
+
|
|
120
|
+
Upload, query, and manage encrypted user data files with built-in schema validation.
|
|
85
121
|
|
|
86
122
|
```typescript
|
|
87
|
-
//
|
|
123
|
+
// Query user files
|
|
88
124
|
const files = await vana.data.getUserFiles({
|
|
89
|
-
|
|
125
|
+
owner: "0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36",
|
|
90
126
|
});
|
|
91
127
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
128
|
+
// Upload encrypted file with decryption permissions
|
|
129
|
+
const result = await vana.data.upload({
|
|
130
|
+
content: "Sensitive user data",
|
|
131
|
+
filename: "user-data.json",
|
|
132
|
+
schemaId: 123,
|
|
133
|
+
permissions: [
|
|
134
|
+
{
|
|
135
|
+
account: "0xServerAddress...", // Who can decrypt
|
|
136
|
+
publicKey: "0x04ServerKey...", // Their public key
|
|
137
|
+
},
|
|
138
|
+
],
|
|
95
139
|
});
|
|
96
140
|
```
|
|
97
141
|
|
|
98
|
-
###
|
|
142
|
+
### Flexible Storage
|
|
143
|
+
|
|
144
|
+
Abstract storage layer supporting IPFS, Google Drive, and custom providers.
|
|
99
145
|
|
|
100
146
|
```typescript
|
|
101
|
-
//
|
|
102
|
-
|
|
147
|
+
// For browser applications
|
|
148
|
+
import { StorageManager, PinataStorage } from "@opendatalabs/vana-sdk/browser";
|
|
149
|
+
// OR for server-side applications
|
|
150
|
+
// import { StorageManager, PinataStorage } from "@opendatalabs/vana-sdk/node";
|
|
103
151
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
152
|
+
const storageManager = new StorageManager();
|
|
153
|
+
storageManager.register(
|
|
154
|
+
"ipfs",
|
|
155
|
+
new PinataStorage({
|
|
156
|
+
apiKey: process.env.PINATA_API_KEY,
|
|
157
|
+
secretKey: process.env.PINATA_SECRET_KEY,
|
|
108
158
|
}),
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const encryptedData = await encryptUserData(userData, encryptionKey);
|
|
112
|
-
|
|
113
|
-
// Upload to IPFS and register on-chain
|
|
114
|
-
const result = await vana.data.uploadEncryptedFile({
|
|
115
|
-
data: encryptedData,
|
|
116
|
-
schemaId: 123, // JSON schema for browsing data
|
|
117
|
-
filename: "browsing-data.json",
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
console.log("File uploaded:", result.fileId, result.url);
|
|
159
|
+
);
|
|
121
160
|
```
|
|
122
161
|
|
|
123
162
|
## Architecture
|
|
124
163
|
|
|
125
|
-
The
|
|
126
|
-
|
|
127
|
-
### Core Controllers
|
|
164
|
+
The SDK provides four main controllers:
|
|
128
165
|
|
|
129
|
-
| Controller
|
|
130
|
-
|
|
|
131
|
-
|
|
|
132
|
-
|
|
|
133
|
-
|
|
|
134
|
-
|
|
|
166
|
+
| Controller | Purpose | Key Methods |
|
|
167
|
+
| ------------- | ------------------------------ | ----------------------------------------------------------------- |
|
|
168
|
+
| `permissions` | Gasless permission management | `grant()`, `revoke()`, `getUserPermissions()` |
|
|
169
|
+
| `data` | File management and validation | `getUserFiles()`, `uploadEncryptedFile()`, `validateDataSchema()` |
|
|
170
|
+
| `server` | Trusted server operations | `trustServer()`, `processWithTrustedServer()` |
|
|
171
|
+
| `protocol` | Contract interaction | `getContract()`, `getAvailableContracts()` |
|
|
135
172
|
|
|
136
|
-
|
|
173
|
+
## Configuration
|
|
137
174
|
|
|
138
175
|
```typescript
|
|
139
|
-
const vana =
|
|
140
|
-
// Required: Wallet client for signing
|
|
176
|
+
const vana = Vana({
|
|
141
177
|
walletClient,
|
|
142
178
|
|
|
143
|
-
//
|
|
144
|
-
relayerUrl: "https://
|
|
179
|
+
// Gasless transaction relay
|
|
180
|
+
relayerUrl: "https://relayer.moksha.vana.org",
|
|
145
181
|
|
|
146
|
-
//
|
|
182
|
+
// Custom relay callbacks
|
|
147
183
|
relayerCallbacks: {
|
|
148
184
|
submitPermissionGrant: async (typedData, signature) => {
|
|
149
|
-
// Custom relay implementation
|
|
150
185
|
return await customRelayer.submit(typedData, signature);
|
|
151
186
|
},
|
|
152
187
|
},
|
|
153
188
|
|
|
154
|
-
//
|
|
189
|
+
// Storage configuration
|
|
155
190
|
storageManager: new StorageManager({
|
|
156
191
|
defaultProvider: "ipfs",
|
|
157
192
|
providers: {
|
|
@@ -159,63 +194,92 @@ const vana = new Vana({
|
|
|
159
194
|
},
|
|
160
195
|
}),
|
|
161
196
|
|
|
162
|
-
//
|
|
197
|
+
// Subgraph for efficient queries
|
|
163
198
|
subgraphUrl: "https://api.thegraph.com/subgraphs/name/vana/moksha",
|
|
164
199
|
});
|
|
165
200
|
```
|
|
166
201
|
|
|
167
|
-
##
|
|
168
|
-
|
|
169
|
-
### Gasless Permissions
|
|
202
|
+
## Error Handling
|
|
170
203
|
|
|
171
|
-
The
|
|
204
|
+
The SDK provides specific error types for different failure scenarios:
|
|
172
205
|
|
|
173
206
|
```typescript
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
maxDataPoints: 1000,
|
|
183
|
-
},
|
|
184
|
-
});
|
|
207
|
+
import {
|
|
208
|
+
RelayerError,
|
|
209
|
+
UserRejectedRequestError,
|
|
210
|
+
SchemaValidationError,
|
|
211
|
+
NetworkError,
|
|
212
|
+
} from "@opendatalabs/vana-sdk/browser";
|
|
213
|
+
// OR for server-side applications
|
|
214
|
+
// } from "@opendatalabs/vana-sdk/node";
|
|
185
215
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
//
|
|
191
|
-
|
|
216
|
+
try {
|
|
217
|
+
await vana.permissions.grant(params);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (error instanceof UserRejectedRequestError) {
|
|
220
|
+
// User cancelled transaction
|
|
221
|
+
} else if (error instanceof RelayerError) {
|
|
222
|
+
// Relayer service error
|
|
223
|
+
} else if (error instanceof SchemaValidationError) {
|
|
224
|
+
// Schema validation failed
|
|
225
|
+
}
|
|
226
|
+
}
|
|
192
227
|
```
|
|
193
228
|
|
|
194
|
-
|
|
229
|
+
## Supported Networks
|
|
230
|
+
|
|
231
|
+
| Network | Chain ID | RPC URL |
|
|
232
|
+
| ------------------ | -------- | ----------------------------- |
|
|
233
|
+
| **Vana Mainnet** | `1480` | `https://rpc.vana.org` |
|
|
234
|
+
| **Moksha Testnet** | `14800` | `https://rpc.moksha.vana.org` |
|
|
195
235
|
|
|
196
|
-
|
|
236
|
+
## Examples
|
|
237
|
+
|
|
238
|
+
### Complete Data Sharing Flow
|
|
197
239
|
|
|
198
240
|
```typescript
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
// Optional: override subgraph URL
|
|
203
|
-
subgraphUrl: "https://custom-subgraph.com/graphql",
|
|
204
|
-
});
|
|
241
|
+
import { Vana } from "@opendatalabs/vana-sdk/browser";
|
|
242
|
+
// OR for server-side applications
|
|
243
|
+
// } from "@opendatalabs/vana-sdk/node";
|
|
205
244
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
245
|
+
async function shareDataWithServer() {
|
|
246
|
+
const vana = Vana({ walletClient });
|
|
247
|
+
|
|
248
|
+
// Step 1: Upload encrypted file with decryption permissions
|
|
249
|
+
const uploadResult = await vana.data.upload({
|
|
250
|
+
content: { data: "sensitive medical records" },
|
|
251
|
+
filename: "health-data.json",
|
|
252
|
+
schemaId: 123,
|
|
253
|
+
permissions: [
|
|
254
|
+
{
|
|
255
|
+
// Grant decryption access to the AI server
|
|
256
|
+
account: "0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36",
|
|
257
|
+
publicKey: "0x04abc...", // Server's public key for encryption
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// Step 2: Grant operation permissions for what the server can do
|
|
263
|
+
const permissionResult = await vana.permissions.grant({
|
|
264
|
+
grantee: "0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36",
|
|
265
|
+
fileIds: [BigInt(uploadResult.fileId)],
|
|
266
|
+
operation: "medical_analysis",
|
|
267
|
+
parameters: {
|
|
268
|
+
model: "medical-ai-v2",
|
|
269
|
+
analysisType: "comprehensive",
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
return { uploadResult, permissionResult };
|
|
274
|
+
}
|
|
209
275
|
```
|
|
210
276
|
|
|
211
277
|
### Schema Validation
|
|
212
278
|
|
|
213
|
-
Built-in support for validating data schemas and user data:
|
|
214
|
-
|
|
215
279
|
```typescript
|
|
216
|
-
//
|
|
280
|
+
// Define data schema
|
|
217
281
|
const schema = {
|
|
218
|
-
name: "
|
|
282
|
+
name: "Social Media Export",
|
|
219
283
|
version: "1.0.0",
|
|
220
284
|
dialect: "json",
|
|
221
285
|
schema: {
|
|
@@ -224,345 +288,130 @@ const schema = {
|
|
|
224
288
|
posts: { type: "array" },
|
|
225
289
|
profile: { type: "object" },
|
|
226
290
|
},
|
|
291
|
+
required: ["profile"],
|
|
227
292
|
},
|
|
228
293
|
};
|
|
229
294
|
|
|
295
|
+
// Validate schema
|
|
230
296
|
vana.data.validateDataSchema(schema);
|
|
231
297
|
|
|
232
|
-
// Validate user data
|
|
233
|
-
const userData = {
|
|
298
|
+
// Validate user data
|
|
299
|
+
const userData = {
|
|
300
|
+
profile: { username: "alice" },
|
|
301
|
+
posts: [],
|
|
302
|
+
};
|
|
234
303
|
vana.data.validateDataAgainstSchema(userData, schema);
|
|
235
304
|
```
|
|
236
305
|
|
|
237
|
-
### Flexible Relay System
|
|
238
|
-
|
|
239
|
-
Configure gasless transactions using callbacks instead of fixed HTTP APIs:
|
|
240
|
-
|
|
241
|
-
```typescript
|
|
242
|
-
const vana = new Vana({
|
|
243
|
-
walletClient,
|
|
244
|
-
relayerCallbacks: {
|
|
245
|
-
// Custom permission grant relaying
|
|
246
|
-
submitPermissionGrant: async (typedData, signature) => {
|
|
247
|
-
const response = await fetch("/api/relay/grant", {
|
|
248
|
-
method: "POST",
|
|
249
|
-
body: JSON.stringify({ typedData, signature }),
|
|
250
|
-
});
|
|
251
|
-
const result = await response.json();
|
|
252
|
-
return result.transactionHash;
|
|
253
|
-
},
|
|
254
|
-
|
|
255
|
-
// Custom revocation relaying
|
|
256
|
-
submitPermissionRevoke: async (typedData, signature) => {
|
|
257
|
-
return await myCustomRelayer.revoke(typedData, signature);
|
|
258
|
-
},
|
|
259
|
-
},
|
|
260
|
-
});
|
|
261
|
-
```
|
|
262
|
-
|
|
263
306
|
## API Reference
|
|
264
307
|
|
|
265
|
-
### Permissions
|
|
308
|
+
### Permissions
|
|
266
309
|
|
|
267
310
|
```typescript
|
|
268
|
-
// Grant permission
|
|
311
|
+
// Grant operation permission
|
|
269
312
|
await vana.permissions.grant({
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
313
|
+
grantee: Address,
|
|
314
|
+
fileIds: bigint[],
|
|
315
|
+
operation: string,
|
|
316
|
+
parameters: object,
|
|
317
|
+
expiresAt?: number
|
|
318
|
+
}): Promise<PermissionGrantResult>
|
|
319
|
+
|
|
320
|
+
// Revoke permission
|
|
277
321
|
await vana.permissions.revoke({
|
|
278
|
-
grantId: string
|
|
279
|
-
nonce?: bigint // Optional nonce override
|
|
322
|
+
grantId: string
|
|
280
323
|
}): Promise<Hash>
|
|
281
324
|
|
|
282
|
-
//
|
|
325
|
+
// Get user permissions
|
|
283
326
|
await vana.permissions.getUserPermissions({
|
|
284
|
-
|
|
285
|
-
subgraphUrl?: string // Optional subgraph override
|
|
327
|
+
owner: Address
|
|
286
328
|
}): Promise<GrantedPermission[]>
|
|
287
|
-
|
|
288
|
-
// Trust a server
|
|
289
|
-
await vana.permissions.trustServer({
|
|
290
|
-
serverAddress: Address, // Server's address
|
|
291
|
-
serverUrl: string // Server's URL
|
|
292
|
-
}): Promise<Hash>
|
|
293
329
|
```
|
|
294
330
|
|
|
295
|
-
### Data
|
|
331
|
+
### Data
|
|
296
332
|
|
|
297
333
|
```typescript
|
|
298
334
|
// Get user files
|
|
299
335
|
await vana.data.getUserFiles({
|
|
300
|
-
|
|
301
|
-
subgraphUrl?: string // Optional subgraph override
|
|
336
|
+
owner: Address
|
|
302
337
|
}): Promise<UserFile[]>
|
|
303
338
|
|
|
304
|
-
// Upload
|
|
305
|
-
await vana.data.
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
//
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
):
|
|
339
|
+
// Upload data with automatic encryption
|
|
340
|
+
await vana.data.upload({
|
|
341
|
+
content: string | Blob | Buffer,
|
|
342
|
+
filename?: string,
|
|
343
|
+
schemaId?: number,
|
|
344
|
+
permissions?: Array<{
|
|
345
|
+
account: Address, // Who can decrypt
|
|
346
|
+
publicKey: string // Their public key
|
|
347
|
+
}>,
|
|
348
|
+
encrypt?: boolean // Default: true
|
|
349
|
+
}): Promise<UploadResult>
|
|
350
|
+
|
|
351
|
+
// Validate schema
|
|
352
|
+
vana.data.validateDataSchema(schema: unknown): void
|
|
315
353
|
|
|
316
354
|
// Validate data against schema
|
|
317
|
-
vana.data.validateDataAgainstSchema(
|
|
318
|
-
data: unknown, // Data to validate
|
|
319
|
-
schema: DataSchema // Data schema
|
|
320
|
-
): void
|
|
321
|
-
|
|
322
|
-
// Fetch and validate remote schema
|
|
323
|
-
await vana.data.fetchAndValidateSchema(
|
|
324
|
-
url: string // Schema URL
|
|
325
|
-
): Promise<DataSchema>
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
### Server Controller
|
|
329
|
-
|
|
330
|
-
```typescript
|
|
331
|
-
// Process data with trusted server
|
|
332
|
-
await vana.server.processWithTrustedServer({
|
|
333
|
-
serverUrl: string, // Trusted server URL
|
|
334
|
-
operation: string, // Operation to perform
|
|
335
|
-
parameters: object // Operation parameters
|
|
336
|
-
}): Promise<any>
|
|
337
|
-
|
|
338
|
-
// Check server trust status
|
|
339
|
-
await vana.server.isServerTrusted({
|
|
340
|
-
user: Address, // User address
|
|
341
|
-
serverAddress: Address // Server address
|
|
342
|
-
}): Promise<boolean>
|
|
343
|
-
```
|
|
344
|
-
|
|
345
|
-
### Protocol Controller
|
|
346
|
-
|
|
347
|
-
```typescript
|
|
348
|
-
// Get contract information
|
|
349
|
-
vana.protocol.getContract(
|
|
350
|
-
contractName: VanaContract // Contract name
|
|
351
|
-
): ContractInfo
|
|
352
|
-
|
|
353
|
-
// List available contracts
|
|
354
|
-
vana.protocol.getAvailableContracts(): VanaContract[]
|
|
355
|
-
|
|
356
|
-
// Get contract addresses for chain
|
|
357
|
-
vana.protocol.getChainContracts(
|
|
358
|
-
chainId: number // Chain ID
|
|
359
|
-
): Record<VanaContract, Address>
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
## Storage Integration
|
|
363
|
-
|
|
364
|
-
The SDK includes a powerful storage abstraction supporting multiple providers:
|
|
365
|
-
|
|
366
|
-
### IPFS Storage
|
|
367
|
-
|
|
368
|
-
```typescript
|
|
369
|
-
import { StorageManager, PinataStorage } from "vana-sdk";
|
|
370
|
-
|
|
371
|
-
const storageManager = new StorageManager();
|
|
372
|
-
|
|
373
|
-
// Configure Pinata IPFS
|
|
374
|
-
const pinataStorage = new PinataStorage({
|
|
375
|
-
apiKey: process.env.PINATA_API_KEY,
|
|
376
|
-
secretKey: process.env.PINATA_SECRET_KEY,
|
|
377
|
-
gatewayUrl: "https://gateway.pinata.cloud/ipfs",
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
storageManager.register("ipfs", pinataStorage, true); // Default provider
|
|
381
|
-
|
|
382
|
-
// Upload file
|
|
383
|
-
const result = await storageManager.upload(encryptedBlob, "encrypted-data.bin");
|
|
384
|
-
```
|
|
385
|
-
|
|
386
|
-
### Google Drive Storage
|
|
387
|
-
|
|
388
|
-
```typescript
|
|
389
|
-
import { StorageManager, GoogleDriveStorage } from "vana-sdk";
|
|
390
|
-
|
|
391
|
-
const googleDriveStorage = new GoogleDriveStorage({
|
|
392
|
-
accessToken: process.env.GOOGLE_DRIVE_ACCESS_TOKEN,
|
|
393
|
-
refreshToken: process.env.GOOGLE_DRIVE_REFRESH_TOKEN,
|
|
394
|
-
clientId: process.env.GOOGLE_DRIVE_CLIENT_ID,
|
|
395
|
-
clientSecret: process.env.GOOGLE_DRIVE_CLIENT_SECRET,
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
storageManager.register("google-drive", googleDriveStorage);
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
### Custom Storage Provider
|
|
402
|
-
|
|
403
|
-
```typescript
|
|
404
|
-
class CustomStorage implements StorageProvider {
|
|
405
|
-
async upload(file: Blob, filename?: string): Promise<StorageUploadResult> {
|
|
406
|
-
// Custom upload logic
|
|
407
|
-
return { url: "custom://uploaded-file", size: file.size };
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
async download(url: string): Promise<Blob> {
|
|
411
|
-
// Custom download logic
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
// ... other required methods
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
storageManager.register("custom", new CustomStorage());
|
|
418
|
-
```
|
|
419
|
-
|
|
420
|
-
## Error Handling
|
|
421
|
-
|
|
422
|
-
The SDK provides comprehensive error handling with specific error types:
|
|
423
|
-
|
|
424
|
-
```typescript
|
|
425
|
-
import {
|
|
426
|
-
RelayerError,
|
|
427
|
-
UserRejectedRequestError,
|
|
428
|
-
SchemaValidationError,
|
|
429
|
-
InvalidConfigurationError,
|
|
430
|
-
NetworkError,
|
|
431
|
-
} from "vana-sdk";
|
|
432
|
-
|
|
433
|
-
try {
|
|
434
|
-
await vana.permissions.grant(params);
|
|
435
|
-
} catch (error) {
|
|
436
|
-
if (error instanceof UserRejectedRequestError) {
|
|
437
|
-
// User rejected the signature request
|
|
438
|
-
console.log("User cancelled transaction");
|
|
439
|
-
} else if (error instanceof RelayerError) {
|
|
440
|
-
// Relayer service error
|
|
441
|
-
console.log(`Relayer error (${error.statusCode}): ${error.message}`);
|
|
442
|
-
} else if (error instanceof SchemaValidationError) {
|
|
443
|
-
// Schema validation failed
|
|
444
|
-
console.log(`Schema error: ${error.message}`);
|
|
445
|
-
} else if (error instanceof NetworkError) {
|
|
446
|
-
// Network connectivity issue
|
|
447
|
-
console.log(`Network error: ${error.message}`);
|
|
448
|
-
} else {
|
|
449
|
-
// Unexpected error
|
|
450
|
-
console.error("Unexpected error:", error);
|
|
451
|
-
}
|
|
452
|
-
}
|
|
355
|
+
vana.data.validateDataAgainstSchema(data: unknown, schema: DataSchema): void
|
|
453
356
|
```
|
|
454
357
|
|
|
455
|
-
##
|
|
358
|
+
## Documentation
|
|
456
359
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
360
|
+
- [API Documentation](https://vana-com.github.io/vana-sdk) - Complete TypeDoc API reference
|
|
361
|
+
- [Getting Started](https://vana-com.github.io/vana-sdk/getting-started) - Step-by-step setup guide
|
|
362
|
+
- [Architecture](https://vana-com.github.io/vana-sdk/architecture) - SDK design and patterns
|
|
363
|
+
- [Configuration](https://vana-com.github.io/vana-sdk/configuration) - All configuration options
|
|
364
|
+
- [Security](https://vana-com.github.io/vana-sdk/security) - Best practices and security
|
|
461
365
|
|
|
462
|
-
|
|
366
|
+
## Support
|
|
463
367
|
|
|
464
|
-
**
|
|
368
|
+
- **Documentation**: [vana-com.github.io/vana-sdk](https://vana-com.github.io/vana-sdk)
|
|
369
|
+
- **Issues**: [GitHub Issues](https://github.com/vana-com/vana-sdk/issues)
|
|
370
|
+
- **Discord**: [Join our community](https://discord.gg/vanabuilders)
|
|
465
371
|
|
|
466
|
-
|
|
467
|
-
Network Name: Vana Moksha Testnet
|
|
468
|
-
RPC URL: https://rpc.moksha.vana.org
|
|
469
|
-
Chain ID: 14800
|
|
470
|
-
Currency Symbol: VANA
|
|
471
|
-
Block Explorer: https://moksha.vanascan.io
|
|
472
|
-
```
|
|
372
|
+
## Generated Code
|
|
473
373
|
|
|
474
|
-
|
|
374
|
+
The SDK includes automatically generated code from various sources to provide type-safe interfaces. All generated files are located in `src/generated/` and should **never be edited manually**.
|
|
475
375
|
|
|
476
|
-
###
|
|
376
|
+
### Code Generation Scripts
|
|
477
377
|
|
|
478
|
-
|
|
479
|
-
|
|
378
|
+
| Script | Purpose | Generated Files |
|
|
379
|
+
| ---------------------------- | -------------------------------------- | --------------------------- |
|
|
380
|
+
| `npm run fetch-abis` | Smart contract ABIs from blockchain | `src/generated/abi/*.ts` |
|
|
381
|
+
| `npm run fetch-server-types` | Personal server API types from OpenAPI | `src/generated/server/*.ts` |
|
|
382
|
+
| `npm run codegen:subgraph` | GraphQL types from subgraph schema | `src/generated/subgraph.ts` |
|
|
480
383
|
|
|
481
|
-
|
|
482
|
-
// 1. Initialize SDK
|
|
483
|
-
const vana = new Vana({ walletClient });
|
|
384
|
+
### Network-Specific Generation
|
|
484
385
|
|
|
485
|
-
|
|
486
|
-
const encryptionKey = await generateEncryptionKey(walletClient);
|
|
487
|
-
const userData = new Blob([JSON.stringify({ data: "sensitive info" })]);
|
|
488
|
-
const encryptedData = await encryptUserData(userData, encryptionKey);
|
|
386
|
+
Some generation scripts support different networks:
|
|
489
387
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
filename: "user-data.json",
|
|
495
|
-
});
|
|
496
|
-
|
|
497
|
-
// 4. Grant permission to access the file
|
|
498
|
-
const permissionTx = await vana.permissions.grant({
|
|
499
|
-
to: "0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36",
|
|
500
|
-
operation: "ai_training",
|
|
501
|
-
parameters: {
|
|
502
|
-
files: [uploadResult.fileId],
|
|
503
|
-
model: "llm-v1",
|
|
504
|
-
maxTokens: 500,
|
|
505
|
-
},
|
|
506
|
-
});
|
|
388
|
+
```bash
|
|
389
|
+
# Generate subgraph types for different networks
|
|
390
|
+
npm run codegen:subgraph:moksha # Moksha testnet (default)
|
|
391
|
+
npm run codegen:subgraph:mainnet # Vana mainnet
|
|
507
392
|
|
|
508
|
-
|
|
509
|
-
|
|
393
|
+
# Generate ABIs for different networks
|
|
394
|
+
npm run fetch-abis moksha # Moksha testnet (default)
|
|
395
|
+
npm run fetch-abis mainnet # Vana mainnet
|
|
510
396
|
```
|
|
511
397
|
|
|
512
|
-
###
|
|
398
|
+
### Development Workflow
|
|
513
399
|
|
|
514
|
-
|
|
515
|
-
// Define a data schema
|
|
516
|
-
const instagramSchema = {
|
|
517
|
-
name: "Instagram Export",
|
|
518
|
-
version: "1.0.0",
|
|
519
|
-
description: "User's Instagram profile and posts data",
|
|
520
|
-
dialect: "json",
|
|
521
|
-
schema: {
|
|
522
|
-
type: "object",
|
|
523
|
-
properties: {
|
|
524
|
-
profile: {
|
|
525
|
-
type: "object",
|
|
526
|
-
properties: {
|
|
527
|
-
username: { type: "string" },
|
|
528
|
-
followers: { type: "number" },
|
|
529
|
-
verified: { type: "boolean" },
|
|
530
|
-
},
|
|
531
|
-
required: ["username"],
|
|
532
|
-
},
|
|
533
|
-
posts: {
|
|
534
|
-
type: "array",
|
|
535
|
-
items: {
|
|
536
|
-
type: "object",
|
|
537
|
-
properties: {
|
|
538
|
-
id: { type: "string" },
|
|
539
|
-
likes: { type: "number" },
|
|
540
|
-
caption: { type: "string" },
|
|
541
|
-
},
|
|
542
|
-
},
|
|
543
|
-
},
|
|
544
|
-
},
|
|
545
|
-
required: ["profile"],
|
|
546
|
-
},
|
|
547
|
-
};
|
|
548
|
-
|
|
549
|
-
// Validate the schema
|
|
550
|
-
vana.data.validateDataSchema(instagramSchema);
|
|
400
|
+
When working with the SDK:
|
|
551
401
|
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
};
|
|
402
|
+
1. **Never edit generated files** - They are overwritten on regeneration
|
|
403
|
+
2. **Regenerate after schema changes** - Run generation scripts when external schemas change
|
|
404
|
+
3. **Generated files are committed** - They're included in version control for consistency
|
|
405
|
+
4. **ESLint ignores generated code** - Style rules don't apply to generated files
|
|
557
406
|
|
|
558
|
-
|
|
407
|
+
```bash
|
|
408
|
+
# Regenerate all code after schema updates
|
|
409
|
+
npm run fetch-abis
|
|
410
|
+
npm run fetch-server-types
|
|
411
|
+
npm run codegen:subgraph
|
|
559
412
|
```
|
|
560
413
|
|
|
561
|
-
##
|
|
562
|
-
|
|
563
|
-
We welcome contributions to the Vana SDK! Please see our [Contributing Guide](../../CONTRIBUTING.md) for details.
|
|
564
|
-
|
|
565
|
-
### Development Setup
|
|
414
|
+
## Development
|
|
566
415
|
|
|
567
416
|
```bash
|
|
568
417
|
git clone https://github.com/vana-com/vana-sdk.git
|
|
@@ -572,29 +421,6 @@ npm run build
|
|
|
572
421
|
npm test
|
|
573
422
|
```
|
|
574
423
|
|
|
575
|
-
### Running Examples
|
|
576
|
-
|
|
577
|
-
```bash
|
|
578
|
-
cd examples/vana-sdk-demo
|
|
579
|
-
npm install
|
|
580
|
-
npm run dev
|
|
581
|
-
```
|
|
582
|
-
|
|
583
|
-
## Documentation
|
|
584
|
-
|
|
585
|
-
- [📚 API Documentation](https://vana-com.github.io/vana-sdk) - Complete TypeDoc API reference
|
|
586
|
-
- [🚀 Getting Started Guide](https://docs.vana.org/vana-sdk) - Step-by-step setup
|
|
587
|
-
- [🏗️ Architecture Guide](https://docs.vana.org/vana-sdk/architecture) - SDK design and patterns
|
|
588
|
-
- [🔧 Configuration Guide](https://docs.vana.org/vana-sdk/configuration) - All configuration options
|
|
589
|
-
- [🔒 Security Guide](https://docs.vana.org/vana-sdk/security) - Best practices and security
|
|
590
|
-
|
|
591
|
-
## Support
|
|
592
|
-
|
|
593
|
-
- **📖 Documentation**: [API Reference](https://vana-com.github.io/vana-sdk)
|
|
594
|
-
- **💬 Discord**: [Join our community](https://discord.gg/vanabuilders)
|
|
595
|
-
- **🐛 Issues**: [GitHub Issues](https://github.com/vana-com/vana-sdk/issues)
|
|
596
|
-
- **📧 Email**: [support@vana.org](mailto:support@vana.org)
|
|
597
|
-
|
|
598
424
|
## License
|
|
599
425
|
|
|
600
426
|
[ISC License](LICENSE) © Vana Foundation
|