@kynesyslabs/demosdk 2.8.10 → 2.8.13

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.
@@ -1,31 +1,40 @@
1
- import type { StorageProgramPayload, StorageProgramAccessControl } from "../types/blockchain/TransactionSubtypes/StorageProgramTransaction";
1
+ import type { StorageProgramPayload, StorageProgramACL, StorageACLMode, StorageEncoding, StorageLocation, StorageGroupPermissions, StorageProgramAccessControl } from "../types/blockchain/TransactionSubtypes/StorageProgramTransaction";
2
2
  /**
3
- * Storage Program class for creating and managing key-value storage on Demos Network
3
+ * StorageProgram - Unified storage solution for Demos Network
4
+ *
5
+ * Supports both JSON (structured) and Binary (raw) data storage
6
+ * with robust access control and size-based pricing.
4
7
  *
5
8
  * Features:
6
9
  * - Deterministic address derivation (stor-{sha256})
7
- * - Key-value storage with 128KB limit
8
- * - Access control (private, public, restricted, deployer-only)
9
- * - Nested data structures (max 64 levels)
10
+ * - Dual encoding: JSON key-value or raw binary
11
+ * - Robust ACL: owner, allowed, blacklisted, public, groups
12
+ * - Size limit: 1MB for both encodings
13
+ * - Pricing: 1 DEM per 10KB
14
+ * - Permanent storage, owner/ACL-deletable only
15
+ * - IPFS-ready (stubs for future hybrid storage)
10
16
  *
11
17
  * @example
12
18
  * ```typescript
13
19
  * import { StorageProgram } from '@kynesyslabs/demosdk'
14
20
  *
15
- * // Derive storage address
16
- * const address = StorageProgram.deriveStorageAddress(
17
- * 'myDeployerAddress',
18
- * 'myAppConfig',
19
- * 'randomSalt123'
21
+ * // Create JSON storage program
22
+ * const jsonPayload = StorageProgram.createStorageProgram(
23
+ * 'demos1abc...',
24
+ * 'myConfig',
25
+ * { apiKey: 'secret', settings: { theme: 'dark' } },
26
+ * 'json',
27
+ * { mode: 'public' }
20
28
  * )
21
29
  *
22
- * // Create storage program
23
- * const payload = StorageProgram.createStorageProgram(
24
- * 'myDeployerAddress',
25
- * 'myAppConfig',
26
- * { apiKey: 'secret', endpoint: 'https://api.example.com' },
27
- * 'public',
28
- * 'randomSalt123'
30
+ * // Create Binary storage program
31
+ * const binaryPayload = StorageProgram.createStorageProgram(
32
+ * 'demos1abc...',
33
+ * 'myImage',
34
+ * Buffer.from(imageData).toString('base64'),
35
+ * 'binary',
36
+ * { mode: 'restricted', allowed: ['demos1user1...'] },
37
+ * { metadata: { filename: 'avatar.png', mimeType: 'image/png' } }
29
38
  * )
30
39
  * ```
31
40
  */
@@ -33,7 +42,7 @@ export declare class StorageProgram {
33
42
  /**
34
43
  * Derive a deterministic storage program address
35
44
  *
36
- * @param deployerAddress - Address of the program deployer
45
+ * @param deployerAddress - Address of the program deployer (will be owner)
37
46
  * @param programName - Name of the storage program
38
47
  * @param salt - Optional random salt for uniqueness (default: empty string)
39
48
  * @returns Storage address in format: stor-{first 40 chars of sha256}
@@ -52,43 +61,75 @@ export declare class StorageProgram {
52
61
  /**
53
62
  * Create a new Storage Program
54
63
  *
55
- * @param deployerAddress - Address creating the storage program (will be the deployer)
56
- * @param programName - Name of the storage program
57
- * @param initialData - Initial key-value data to store
58
- * @param accessControl - Access control mode (default: 'private')
59
- * @param salt - Optional random salt for address derivation
60
- * @param allowedAddresses - List of allowed addresses (for 'restricted' mode)
64
+ * @param deployerAddress - Address creating the storage program (becomes owner)
65
+ * @param programName - Unique name for the program
66
+ * @param data - Initial data (JSON object or base64 binary string)
67
+ * @param encoding - "json" or "binary" (default: "json")
68
+ * @param acl - Access control configuration (default: owner-only)
69
+ * @param options - Additional options (salt, metadata, storageLocation)
61
70
  * @returns StorageProgramPayload for transaction creation
62
71
  *
63
72
  * @example
64
73
  * ```typescript
74
+ * // JSON storage with public read access
75
+ * const payload = StorageProgram.createStorageProgram(
76
+ * 'demos1abc...',
77
+ * 'appConfig',
78
+ * { version: '1.0', features: ['auth', 'storage'] },
79
+ * 'json',
80
+ * { mode: 'public' }
81
+ * )
82
+ *
83
+ * // Binary storage with group access
65
84
  * const payload = StorageProgram.createStorageProgram(
66
85
  * 'demos1abc...',
67
- * 'userPreferences',
68
- * { theme: 'dark', language: 'en' },
69
- * 'private'
86
+ * 'teamDocument',
87
+ * base64EncodedPdf,
88
+ * 'binary',
89
+ * {
90
+ * mode: 'restricted',
91
+ * groups: {
92
+ * editors: { members: ['demos1a...', 'demos1b...'], permissions: ['read', 'write'] },
93
+ * viewers: { members: ['demos1c...'], permissions: ['read'] }
94
+ * }
95
+ * },
96
+ * { metadata: { filename: 'report.pdf', mimeType: 'application/pdf' } }
70
97
  * )
71
98
  * ```
72
99
  */
73
- static createStorageProgram(deployerAddress: string, programName: string, initialData: Record<string, any>, accessControl?: StorageProgramAccessControl, salt?: string, allowedAddresses?: string[]): StorageProgramPayload;
100
+ static createStorageProgram(deployerAddress: string, programName: string, data: Record<string, any> | string, encoding?: StorageEncoding, acl?: Partial<StorageProgramACL>, options?: {
101
+ salt?: string;
102
+ metadata?: Record<string, unknown>;
103
+ storageLocation?: StorageLocation;
104
+ }): StorageProgramPayload;
74
105
  /**
75
- * Write or update key-value data in a Storage Program
106
+ * Write/update data in a Storage Program
76
107
  *
77
108
  * @param storageAddress - The storage program address (stor-{hash})
78
- * @param data - Key-value data to write/update
109
+ * @param data - Data to write (JSON object or base64 binary string)
110
+ * @param encoding - "json" or "binary" (default: "json")
79
111
  * @returns StorageProgramPayload for transaction creation
80
112
  *
81
113
  * @example
82
114
  * ```typescript
115
+ * // Update JSON data
116
+ * const payload = StorageProgram.writeStorage(
117
+ * 'stor-7a8b9c...',
118
+ * { newKey: 'value', existingKey: 'updatedValue' },
119
+ * 'json'
120
+ * )
121
+ *
122
+ * // Update binary data
83
123
  * const payload = StorageProgram.writeStorage(
84
124
  * 'stor-7a8b9c...',
85
- * { newKey: 'value', existingKey: 'updatedValue' }
125
+ * newBase64Content,
126
+ * 'binary'
86
127
  * )
87
128
  * ```
88
129
  */
89
- static writeStorage(storageAddress: string, data: Record<string, any>): StorageProgramPayload;
130
+ static writeStorage(storageAddress: string, data: Record<string, any> | string, encoding?: StorageEncoding): StorageProgramPayload;
90
131
  /**
91
- * Read data from a Storage Program (query operation, not a transaction)
132
+ * Read data from a Storage Program (creates payload for RPC)
92
133
  *
93
134
  * Note: This creates a payload for validation purposes.
94
135
  * Actual reads should use RPC endpoints like GET /storage-program/:address
@@ -107,32 +148,45 @@ export declare class StorageProgram {
107
148
  */
108
149
  static readStorage(storageAddress: string): StorageProgramPayload;
109
150
  /**
110
- * Update access control settings for a Storage Program (deployer only)
151
+ * Update access control settings for a Storage Program (owner only)
111
152
  *
112
153
  * @param storageAddress - The storage program address
113
- * @param accessControl - New access control mode
114
- * @param allowedAddresses - Updated list of allowed addresses (for 'restricted' mode)
154
+ * @param acl - New access control configuration
115
155
  * @returns StorageProgramPayload for transaction creation
116
156
  *
117
157
  * @example
118
158
  * ```typescript
119
- * // Change from private to public
159
+ * // Change to public access
120
160
  * const payload = StorageProgram.updateAccessControl(
121
161
  * 'stor-7a8b9c...',
122
- * 'public'
162
+ * { mode: 'public' }
123
163
  * )
124
164
  *
125
- * // Set restricted access with allowlist
165
+ * // Add users to blacklist
126
166
  * const payload = StorageProgram.updateAccessControl(
127
167
  * 'stor-7a8b9c...',
128
- * 'restricted',
129
- * ['demos1user1...', 'demos1user2...']
168
+ * {
169
+ * mode: 'public',
170
+ * blacklisted: ['demos1bad...', 'demos1spam...']
171
+ * }
172
+ * )
173
+ *
174
+ * // Set up group-based access
175
+ * const payload = StorageProgram.updateAccessControl(
176
+ * 'stor-7a8b9c...',
177
+ * {
178
+ * mode: 'restricted',
179
+ * groups: {
180
+ * admins: { members: ['demos1admin...'], permissions: ['read', 'write', 'delete'] },
181
+ * users: { members: ['demos1user1...', 'demos1user2...'], permissions: ['read'] }
182
+ * }
183
+ * }
130
184
  * )
131
185
  * ```
132
186
  */
133
- static updateAccessControl(storageAddress: string, accessControl: StorageProgramAccessControl, allowedAddresses?: string[]): StorageProgramPayload;
187
+ static updateAccessControl(storageAddress: string, acl: Partial<StorageProgramACL>): StorageProgramPayload;
134
188
  /**
135
- * Delete an entire Storage Program (deployer only)
189
+ * Delete a Storage Program (owner/ACL-permissioned only)
136
190
  *
137
191
  * WARNING: This operation is irreversible and will delete all stored data.
138
192
  *
@@ -146,35 +200,66 @@ export declare class StorageProgram {
146
200
  */
147
201
  static deleteStorageProgram(storageAddress: string): StorageProgramPayload;
148
202
  /**
149
- * Validate storage size against 128KB limit
203
+ * Validate data size against 1MB limit
150
204
  *
151
- * @param data - The data object to validate
152
- * @returns true if size is within limit, false otherwise
205
+ * @param data - Data to validate (JSON object or base64 string)
206
+ * @param encoding - Encoding type ("json" or "binary")
207
+ * @returns true if size is within 1MB limit, false otherwise
153
208
  *
154
209
  * @example
155
210
  * ```typescript
156
- * const data = { key: 'value', nested: { data: 'here' } }
157
- * if (StorageProgram.validateSize(data)) {
211
+ * // Validate JSON data
212
+ * if (StorageProgram.validateSize({ key: 'value' }, 'json')) {
213
+ * // Safe to store
214
+ * }
215
+ *
216
+ * // Validate binary data
217
+ * if (StorageProgram.validateSize(base64String, 'binary')) {
158
218
  * // Safe to store
159
219
  * }
160
220
  * ```
161
221
  */
162
- static validateSize(data: Record<string, any>): boolean;
222
+ static validateSize(data: Record<string, any> | string, encoding?: StorageEncoding): boolean;
163
223
  /**
164
- * Get the size of data in bytes
224
+ * Get data size in bytes
165
225
  *
166
- * @param data - The data object to measure
226
+ * @param data - Data to measure (JSON object or base64 string)
227
+ * @param encoding - Encoding type ("json" or "binary")
167
228
  * @returns Size in bytes
168
229
  *
169
230
  * @example
170
231
  * ```typescript
171
- * const size = StorageProgram.getDataSize({ key: 'value' })
232
+ * const size = StorageProgram.getDataSize({ key: 'value' }, 'json')
172
233
  * console.log(`Data size: ${size} bytes`)
173
234
  * ```
174
235
  */
175
- static getDataSize(data: Record<string, any>): number;
236
+ static getDataSize(data: Record<string, any> | string, encoding?: StorageEncoding): number;
176
237
  /**
177
- * Validate nesting depth (max 64 levels)
238
+ * Calculate storage fee based on data size
239
+ *
240
+ * Pricing: 1 DEM per 10KB (minimum 1 DEM)
241
+ *
242
+ * @param data - Data to calculate fee for (JSON object or base64 string)
243
+ * @param encoding - Encoding type ("json" or "binary")
244
+ * @returns Fee in DEM (bigint)
245
+ *
246
+ * @example
247
+ * ```typescript
248
+ * const fee = StorageProgram.calculateStorageFee({ key: 'value' }, 'json')
249
+ * console.log(`Storage fee: ${fee} DEM`)
250
+ *
251
+ * // Examples:
252
+ * // 5KB -> 1 DEM
253
+ * // 15KB -> 2 DEM
254
+ * // 100KB -> 10 DEM
255
+ * // 1MB -> 103 DEM
256
+ * ```
257
+ */
258
+ static calculateStorageFee(data: Record<string, any> | string, encoding?: StorageEncoding): bigint;
259
+ /**
260
+ * Validate JSON nesting depth (max 64 levels)
261
+ *
262
+ * Only applicable for JSON encoding.
178
263
  *
179
264
  * @param data - The data object to validate
180
265
  * @param maxDepth - Maximum allowed nesting depth (default: 64)
@@ -189,4 +274,109 @@ export declare class StorageProgram {
189
274
  * ```
190
275
  */
191
276
  static validateNestingDepth(data: any, maxDepth?: number): boolean;
277
+ /**
278
+ * Check if an address has permission for an operation
279
+ *
280
+ * ACL Resolution Priority:
281
+ * 1. Owner -> FULL ACCESS (always)
282
+ * 2. Blacklisted -> DENIED (even if in allowed/groups)
283
+ * 3. Allowed -> permissions granted
284
+ * 4. Groups -> check group permissions
285
+ * 5. Mode fallback: owner/restricted -> DENIED, public -> READ only
286
+ *
287
+ * @param acl - Access control configuration
288
+ * @param ownerAddress - Owner address of the storage program
289
+ * @param requestingAddress - Address requesting permission
290
+ * @param permission - Permission type to check
291
+ * @returns true if permission is granted
292
+ *
293
+ * @example
294
+ * ```typescript
295
+ * const acl = { mode: 'public', blacklisted: ['demos1spam...'] }
296
+ * const canRead = StorageProgram.checkPermission(acl, owner, user, 'read')
297
+ * ```
298
+ */
299
+ static checkPermission(acl: StorageProgramACL, ownerAddress: string, requestingAddress: string, permission: "read" | "write" | "delete"): boolean;
300
+ /**
301
+ * Create a public ACL (anyone can read, owner writes)
302
+ *
303
+ * @returns StorageProgramACL configured for public read access
304
+ *
305
+ * @example
306
+ * ```typescript
307
+ * const acl = StorageProgram.publicACL()
308
+ * // { mode: 'public' }
309
+ * ```
310
+ */
311
+ static publicACL(): StorageProgramACL;
312
+ /**
313
+ * Create a private/owner-only ACL
314
+ *
315
+ * @returns StorageProgramACL configured for owner-only access
316
+ *
317
+ * @example
318
+ * ```typescript
319
+ * const acl = StorageProgram.privateACL()
320
+ * // { mode: 'owner' }
321
+ * ```
322
+ */
323
+ static privateACL(): StorageProgramACL;
324
+ /**
325
+ * Create a restricted ACL with allowed addresses
326
+ *
327
+ * @param allowed - List of addresses allowed to access
328
+ * @returns StorageProgramACL configured for restricted access
329
+ *
330
+ * @example
331
+ * ```typescript
332
+ * const acl = StorageProgram.restrictedACL(['demos1a...', 'demos1b...'])
333
+ * // { mode: 'restricted', allowed: ['demos1a...', 'demos1b...'] }
334
+ * ```
335
+ */
336
+ static restrictedACL(allowed: string[]): StorageProgramACL;
337
+ /**
338
+ * Create a group-based ACL
339
+ *
340
+ * @param groups - Named groups with members and permissions
341
+ * @returns StorageProgramACL configured for group-based access
342
+ *
343
+ * @example
344
+ * ```typescript
345
+ * const acl = StorageProgram.groupACL({
346
+ * admins: { members: ['demos1admin...'], permissions: ['read', 'write', 'delete'] },
347
+ * editors: { members: ['demos1ed1...', 'demos1ed2...'], permissions: ['read', 'write'] },
348
+ * viewers: { members: ['demos1view...'], permissions: ['read'] }
349
+ * })
350
+ * ```
351
+ */
352
+ static groupACL(groups: Record<string, StorageGroupPermissions>): StorageProgramACL;
353
+ /**
354
+ * Create an ACL with a blacklist
355
+ *
356
+ * @param mode - Base mode ('public' or 'restricted')
357
+ * @param blacklisted - Addresses to block
358
+ * @param allowed - Optional allowed addresses (for restricted mode)
359
+ * @returns StorageProgramACL with blacklist configured
360
+ *
361
+ * @example
362
+ * ```typescript
363
+ * // Public but block spam addresses
364
+ * const acl = StorageProgram.blacklistACL('public', ['demos1spam...'])
365
+ * ```
366
+ */
367
+ static blacklistACL(mode: StorageACLMode, blacklisted: string[], allowed?: string[]): StorageProgramACL;
368
+ /**
369
+ * @deprecated Use createStorageProgram with acl parameter instead.
370
+ * Kept for backward compatibility.
371
+ *
372
+ * Create a new Storage Program with legacy access control
373
+ */
374
+ static createStorageProgramLegacy(deployerAddress: string, programName: string, initialData: Record<string, any>, accessControl?: StorageProgramAccessControl, salt?: string, allowedAddresses?: string[]): StorageProgramPayload;
375
+ /**
376
+ * @deprecated Use updateAccessControl with acl parameter instead.
377
+ * Kept for backward compatibility.
378
+ *
379
+ * Update access control with legacy mode
380
+ */
381
+ static updateAccessControlLegacy(storageAddress: string, accessControl: StorageProgramAccessControl, allowedAddresses?: string[]): StorageProgramPayload;
192
382
  }