@net-protocol/storage 0.1.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/README.md +232 -0
- package/dist/index.d.mts +523 -0
- package/dist/index.d.ts +523 -0
- package/dist/index.js +2120 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2079 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +73 -0
package/README.md
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# @net-protocol/storage
|
|
2
|
+
|
|
3
|
+
Net Storage SDK for key-value storage on the Net protocol. Supports regular storage, chunked storage, and XML storage patterns.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @net-protocol/storage
|
|
9
|
+
# or
|
|
10
|
+
yarn add @net-protocol/storage
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Dependencies
|
|
14
|
+
|
|
15
|
+
- `@net-protocol/core` - Core Net protocol SDK
|
|
16
|
+
- `viem` - Ethereum library
|
|
17
|
+
- `pako` - Compression library (for chunked storage)
|
|
18
|
+
- `wagmi` (peer dependency) - Required for React hooks only
|
|
19
|
+
- `react` (peer dependency) - Required for React hooks only
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### React Hooks
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { useStorage, useXmlStorage, useStorageFromRouter } from "@net-protocol/storage";
|
|
27
|
+
|
|
28
|
+
// Basic storage read
|
|
29
|
+
function MyComponent() {
|
|
30
|
+
const { data, isLoading, error } = useStorage({
|
|
31
|
+
chainId: 8453,
|
|
32
|
+
key: "my-key",
|
|
33
|
+
operatorAddress: "0x...",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (isLoading) return <div>Loading...</div>;
|
|
37
|
+
if (error) return <div>Error: {error.message}</div>;
|
|
38
|
+
|
|
39
|
+
const [text, data] = data || [];
|
|
40
|
+
return <div>{text}</div>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Storage with output format options
|
|
44
|
+
function StorageWithFormat() {
|
|
45
|
+
// Get data as plain string (default is hex)
|
|
46
|
+
const { data: stringData } = useStorage({
|
|
47
|
+
chainId: 8453,
|
|
48
|
+
key: "my-key",
|
|
49
|
+
operatorAddress: "0x...",
|
|
50
|
+
outputFormat: "string", // Returns plain string instead of hex
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Use StorageRouter for automatic detection (latest version only)
|
|
54
|
+
const { data: routerData } = useStorage({
|
|
55
|
+
chainId: 8453,
|
|
56
|
+
key: "my-key",
|
|
57
|
+
operatorAddress: "0x...",
|
|
58
|
+
useRouter: true, // Automatically detects regular vs chunked storage
|
|
59
|
+
outputFormat: "string",
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Get historical version
|
|
63
|
+
const { data: historicalData } = useStorage({
|
|
64
|
+
chainId: 8453,
|
|
65
|
+
key: "my-key",
|
|
66
|
+
operatorAddress: "0x...",
|
|
67
|
+
index: 0, // 0-based index for historical versions
|
|
68
|
+
outputFormat: "string",
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// XML storage with recursive resolution
|
|
73
|
+
function XmlComponent() {
|
|
74
|
+
const { data, isLoading, isXml } = useXmlStorage({
|
|
75
|
+
chainId: 8453,
|
|
76
|
+
key: "my-xml-key",
|
|
77
|
+
operatorAddress: "0x...",
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return <div>{data}</div>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// XML storage with format options
|
|
84
|
+
function XmlWithFormats() {
|
|
85
|
+
// Return as tuple format with hex output
|
|
86
|
+
const { data: tupleData } = useXmlStorage({
|
|
87
|
+
chainId: 8453,
|
|
88
|
+
key: "my-xml-key",
|
|
89
|
+
operatorAddress: "0x...",
|
|
90
|
+
returnFormat: "tuple", // Returns [text, data] tuple
|
|
91
|
+
outputFormat: "hex", // Data is hex string (default)
|
|
92
|
+
useRouter: true, // Use StorageRouter for automatic detection
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Return as tuple format with string output
|
|
96
|
+
const { data: tupleStringData } = useXmlStorage({
|
|
97
|
+
chainId: 8453,
|
|
98
|
+
key: "my-xml-key",
|
|
99
|
+
operatorAddress: "0x...",
|
|
100
|
+
returnFormat: "tuple",
|
|
101
|
+
outputFormat: "string", // Data is plain string
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Return as object format (default)
|
|
105
|
+
const { data: objectData, filename, isXml } = useXmlStorage({
|
|
106
|
+
chainId: 8453,
|
|
107
|
+
key: "my-xml-key",
|
|
108
|
+
operatorAddress: "0x...",
|
|
109
|
+
returnFormat: "object", // Returns { data, filename, isLoading, error, isXml }
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Storage from router (handles chunked storage)
|
|
114
|
+
function StorageComponent() {
|
|
115
|
+
const { data, isLoading } = useStorageFromRouter({
|
|
116
|
+
chainId: 8453,
|
|
117
|
+
storageKey: "0x...",
|
|
118
|
+
operatorAddress: "0x...",
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return <div>{data?.[1]}</div>;
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### StorageClient (Non-React)
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { StorageClient } from "@net-protocol/storage";
|
|
129
|
+
|
|
130
|
+
// Create client
|
|
131
|
+
const client = new StorageClient({
|
|
132
|
+
chainId: 8453,
|
|
133
|
+
overrides: { rpcUrls: ["https://custom-rpc.com"] }, // Optional
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// Get storage value
|
|
137
|
+
const data = await client.get({
|
|
138
|
+
key: "my-key",
|
|
139
|
+
operator: "0x...",
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Get historical version
|
|
143
|
+
const historical = await client.getValueAtIndex({
|
|
144
|
+
key: "my-key",
|
|
145
|
+
operator: "0x...",
|
|
146
|
+
index: 0, // 0-based index
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// Get via StorageRouter (handles chunked storage)
|
|
150
|
+
const routerData = await client.getViaRouter({
|
|
151
|
+
key: "my-key",
|
|
152
|
+
operator: "0x...",
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// Read with XML resolution
|
|
156
|
+
const xmlData = await client.readStorageData({
|
|
157
|
+
key: "my-xml-key",
|
|
158
|
+
operator: "0x...",
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Utilities
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import {
|
|
166
|
+
getStorageKeyBytes,
|
|
167
|
+
chunkDataForStorage,
|
|
168
|
+
assembleChunks,
|
|
169
|
+
parseNetReferences,
|
|
170
|
+
processDataForStorage,
|
|
171
|
+
} from "@net-protocol/storage";
|
|
172
|
+
|
|
173
|
+
// Generate storage key bytes
|
|
174
|
+
const keyBytes = getStorageKeyBytes("my-key");
|
|
175
|
+
|
|
176
|
+
// Chunk data for storage
|
|
177
|
+
const chunks = chunkDataForStorage("large data string");
|
|
178
|
+
|
|
179
|
+
// Assemble chunks
|
|
180
|
+
const assembled = assembleChunks(chunks);
|
|
181
|
+
|
|
182
|
+
// Parse XML references
|
|
183
|
+
const references = parseNetReferences("<net k=\"hash\" v=\"0.0.1\" />");
|
|
184
|
+
|
|
185
|
+
// Process data for XML storage
|
|
186
|
+
const result = processDataForStorage(data, operatorAddress);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## API Reference
|
|
190
|
+
|
|
191
|
+
### React Hooks
|
|
192
|
+
|
|
193
|
+
- `useStorage` - Read storage value (latest or historical)
|
|
194
|
+
- `useStorageForOperator` - Get all storage keys for an operator
|
|
195
|
+
- `useStorageForOperatorAndKey` - Get storage value by operator and key
|
|
196
|
+
- `useBulkStorage` - Bulk read storage values
|
|
197
|
+
- `useStorageTotalWrites` - Get total number of versions
|
|
198
|
+
- `useXmlStorage` - Read XML storage with recursive resolution
|
|
199
|
+
- `useStorageFromRouter` - Read from StorageRouter (handles chunked storage)
|
|
200
|
+
|
|
201
|
+
### StorageClient Methods
|
|
202
|
+
|
|
203
|
+
- `get(params)` - Get storage value (latest)
|
|
204
|
+
- `getValueAtIndex(params)` - Get storage value at historical index
|
|
205
|
+
- `getTotalWrites(params)` - Get total number of versions
|
|
206
|
+
- `bulkGet(params)` - Bulk read storage values
|
|
207
|
+
- `getViaRouter(params)` - Get via StorageRouter (handles chunked storage)
|
|
208
|
+
- `getChunkedMetadata(params)` - Get chunked storage metadata
|
|
209
|
+
- `getChunked(params)` - Get chunked storage chunks
|
|
210
|
+
- `getForOperator(params)` - Get all keys for operator
|
|
211
|
+
- `getForOperatorAndKey(params)` - Get storage by operator and key
|
|
212
|
+
- `readStorageData(params)` - Read with XML resolution
|
|
213
|
+
- `readChunkedStorage(params)` - Read chunked storage with decompression
|
|
214
|
+
|
|
215
|
+
## Storage Types
|
|
216
|
+
|
|
217
|
+
### Regular Storage
|
|
218
|
+
|
|
219
|
+
Simple key-value storage using `Storage.sol`. Values are stored directly on-chain.
|
|
220
|
+
|
|
221
|
+
### Chunked Storage
|
|
222
|
+
|
|
223
|
+
Large data storage using `ChunkedStorage.sol`. Data is compressed (gzip) and split into 20KB chunks.
|
|
224
|
+
|
|
225
|
+
### XML Storage
|
|
226
|
+
|
|
227
|
+
Hierarchical storage using XML references. Supports recursive resolution and operator inheritance.
|
|
228
|
+
|
|
229
|
+
## License
|
|
230
|
+
|
|
231
|
+
MIT
|
|
232
|
+
|