peerpigeon 0.0.8 → 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.
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from './EventEmitter.js';
2
+ import DebugLogger from './DebugLogger.js';
2
3
 
3
4
  // Dynamic import for unsea to handle both Node.js and browser environments
4
5
  let unsea = null;
@@ -55,10 +56,12 @@ async function initializeUnsea() {
55
56
  * - Optional immutability with CRDT support for collaborative editing
56
57
  * - Mutable layer for data originators
57
58
  * - Integration with WebDHT for distributed storage
59
+ * - Space separation for preventing data overwrites (Private, Public, Frozen)
58
60
  */
59
61
  export class DistributedStorageManager extends EventEmitter {
60
62
  constructor(mesh) {
61
63
  super();
64
+ this.debug = DebugLogger.create('DistributedStorageManager');
62
65
  this.mesh = mesh;
63
66
  this.webDHT = mesh.webDHT;
64
67
  this.cryptoManager = mesh.cryptoManager;
@@ -74,7 +77,15 @@ export class DistributedStorageManager extends EventEmitter {
74
77
  defaultTTL: null, // No expiration by default
75
78
  maxValueSize: 1024 * 1024, // 1MB default max size
76
79
  enableCRDT: true,
77
- conflictResolution: 'last-write-wins' // or 'crdt-merge'
80
+ conflictResolution: 'last-write-wins', // or 'crdt-merge'
81
+ spaceEnforcement: true // Enable space-based access control
82
+ };
83
+
84
+ // Storage spaces for data separation
85
+ this.spaces = {
86
+ PRIVATE: 'private', // Only owner can read/write, encrypted
87
+ PUBLIC: 'public', // Anyone can read, only owner can write
88
+ FROZEN: 'frozen' // Immutable once set, anyone can read
78
89
  };
79
90
 
80
91
  // Local storage for metadata and permissions
@@ -82,13 +93,15 @@ export class DistributedStorageManager extends EventEmitter {
82
93
  this.accessControl = new Map(); // keyId -> access control info
83
94
  this.crdtStates = new Map(); // keyId -> CRDT state for collaborative data
84
95
 
85
- // Track data ownership
96
+ // Track data ownership by space
86
97
  this.ownedKeys = new Set(); // Keys owned by this peer
98
+ this.spaceOwnership = new Map(); // space:key -> owner mapping
99
+ this.keyToSpaceMapping = new Map(); // baseKey -> {space, fullKey} mapping for transparent access
87
100
 
88
101
  // Track enabled state
89
102
  this.enabled = true; // Enable by default
90
103
 
91
- console.log(`DistributedStorageManager initialized for peer ${this.mesh.peerId?.substring(0, 8)}...`);
104
+ this.debug.log(`DistributedStorageManager initialized for peer ${this.mesh.peerId?.substring(0, 8)}...`);
92
105
  }
93
106
 
94
107
  /**
@@ -107,9 +120,9 @@ export class DistributedStorageManager extends EventEmitter {
107
120
  this.storageKeypair = await this.unsea.generateRandomPair();
108
121
  }
109
122
 
110
- console.log('📦 Storage encryption initialized with unsea');
123
+ this.debug.log('📦 Storage encryption initialized with unsea');
111
124
  } catch (error) {
112
- console.warn('📦 Failed to initialize storage encryption:', error);
125
+ this.debug.warn('📦 Failed to initialize storage encryption:', error);
113
126
  // Disable encryption if unsea fails to load
114
127
  this.config.encryptionEnabled = false;
115
128
  }
@@ -128,23 +141,262 @@ export class DistributedStorageManager extends EventEmitter {
128
141
  const timeout = 5000;
129
142
  const start = Date.now();
130
143
 
131
- while (!this.unsea || !this.storageKeypair) {
132
- if (Date.now() - start > timeout) {
133
- console.warn('📦 Crypto initialization timeout - proceeding without encryption');
134
- this.config.encryptionEnabled = false;
135
- break;
136
- }
144
+ while ((!this.unsea || !this.storageKeypair) && (Date.now() - start) < timeout) {
137
145
  await new Promise(resolve => setTimeout(resolve, 100));
138
146
  }
139
147
  }
140
148
 
149
+ /**
150
+ * Resolve a key to its actual storage location across spaces
151
+ * @param {string} key - The storage key (can be plain or space-prefixed)
152
+ * @returns {Object} - {space, baseKey, fullKey, keyId}
153
+ */
154
+ async resolveKey(key) {
155
+ // If key already has space prefix, use it as-is
156
+ const keyInfo = this.parseStorageKey(key);
157
+
158
+ // If it's not a bare key (has space prefix), use it directly
159
+ if (key.includes(':')) {
160
+ const keyId = await this.webDHT.generateKeyId(keyInfo.fullKey);
161
+ return {
162
+ space: keyInfo.space,
163
+ baseKey: keyInfo.baseKey,
164
+ fullKey: keyInfo.fullKey,
165
+ keyId
166
+ };
167
+ }
168
+
169
+ // For bare keys, check if we have a mapping
170
+ const mapping = this.keyToSpaceMapping.get(keyInfo.baseKey);
171
+ if (mapping) {
172
+ const keyId = await this.webDHT.generateKeyId(mapping.fullKey);
173
+ return {
174
+ space: mapping.space,
175
+ baseKey: keyInfo.baseKey,
176
+ fullKey: mapping.fullKey,
177
+ keyId
178
+ };
179
+ }
180
+
181
+ // If no mapping exists, try to find it by checking each space
182
+ for (const space of Object.values(this.spaces)) {
183
+ const testFullKey = this.createSpacedKey(space, keyInfo.baseKey);
184
+ const testKeyId = await this.webDHT.generateKeyId(testFullKey);
185
+
186
+ // Check if we have metadata for this key in this space
187
+ if (this.storageMetadata.has(testKeyId) || this.spaceOwnership.has(testFullKey)) {
188
+ // Update the mapping for future use
189
+ this.keyToSpaceMapping.set(keyInfo.baseKey, { space, fullKey: testFullKey });
190
+ return {
191
+ space,
192
+ baseKey: keyInfo.baseKey,
193
+ fullKey: testFullKey,
194
+ keyId: testKeyId
195
+ };
196
+ }
197
+ }
198
+
199
+ // If still not found, default to the parsed key (likely private space)
200
+ const keyId = await this.webDHT.generateKeyId(keyInfo.fullKey);
201
+ return {
202
+ space: keyInfo.space,
203
+ baseKey: keyInfo.baseKey,
204
+ fullKey: keyInfo.fullKey,
205
+ keyId
206
+ };
207
+ }
208
+
209
+ /**
210
+ * Parse a storage key to extract space and base key
211
+ * @param {string} key - The storage key (can be plain or space-prefixed)
212
+ * @returns {Object} - {space, baseKey, fullKey}
213
+ */
214
+ parseStorageKey(key) {
215
+ // Check if key has space prefix (e.g., "private:user:123" or "public:config")
216
+ const spacePrefixes = Object.values(this.spaces);
217
+ for (const space of spacePrefixes) {
218
+ if (key.startsWith(`${space}:`)) {
219
+ return {
220
+ space,
221
+ baseKey: key.substring(space.length + 1),
222
+ fullKey: key
223
+ };
224
+ }
225
+ }
226
+
227
+ // Default to private space if no prefix specified
228
+ return {
229
+ space: this.spaces.PRIVATE,
230
+ baseKey: key,
231
+ fullKey: `${this.spaces.PRIVATE}:${key}`
232
+ };
233
+ }
234
+
235
+ /**
236
+ * Create a space-prefixed key
237
+ * @param {string} space - The storage space
238
+ * @param {string} baseKey - The base key
239
+ * @returns {string} - The full key with space prefix
240
+ */
241
+ createSpacedKey(space, baseKey) {
242
+ if (!Object.values(this.spaces).includes(space)) {
243
+ throw new Error(`Invalid storage space: ${space}. Must be one of: ${Object.values(this.spaces).join(', ')}`);
244
+ }
245
+ return `${space}:${baseKey}`;
246
+ }
247
+
248
+ /**
249
+ * Check if a peer can access a key in a specific space
250
+ * @param {string} space - The storage space
251
+ * @param {string} key - The storage key
252
+ * @param {string} peerId - The peer ID requesting access
253
+ * @param {string} operation - The operation (read, write)
254
+ * @returns {boolean} - Whether access is allowed
255
+ */
256
+ canAccessSpace(space, key, peerId, operation = 'read') {
257
+ if (!this.config.spaceEnforcement) {
258
+ return true; // Space enforcement disabled
259
+ }
260
+
261
+ const spaceKey = `${space}:${key}`;
262
+ const owner = this.spaceOwnership.get(spaceKey);
263
+
264
+ switch (space) {
265
+ case this.spaces.PRIVATE:
266
+ // Private: Only owner can read/write, or if no owner exists (initial write)
267
+ return !owner || owner === peerId;
268
+
269
+ case this.spaces.PUBLIC:
270
+ if (operation === 'read') {
271
+ return true; // Anyone can read public data
272
+ } else {
273
+ return !owner || owner === peerId; // Only owner can write, or initial write
274
+ }
275
+
276
+ case this.spaces.FROZEN:
277
+ if (operation === 'read') {
278
+ return true; // Anyone can read frozen data
279
+ } else {
280
+ // For frozen data, only the owner can write (both initial and updates)
281
+ return !owner || owner === peerId; // Only owner can write, or initial write
282
+ }
283
+
284
+ default:
285
+ return false;
286
+ }
287
+ }
288
+
289
+ /**
290
+ * Determine appropriate space and access control from options
291
+ * @param {Object} options - Storage options
292
+ * @returns {Object} - {space, isPublic, isImmutable}
293
+ */
294
+ determineSpaceFromOptions(options = {}) {
295
+ // Allow explicit space specification
296
+ if (options.space && Object.values(this.spaces).includes(options.space)) {
297
+ const space = options.space;
298
+ return {
299
+ space,
300
+ isPublic: space !== this.spaces.PRIVATE,
301
+ isImmutable: space === this.spaces.FROZEN
302
+ };
303
+ }
304
+
305
+ // Legacy compatibility - determine space from isPublic/isImmutable
306
+ if (options.isImmutable) {
307
+ return {
308
+ space: this.spaces.FROZEN,
309
+ isPublic: true,
310
+ isImmutable: true
311
+ };
312
+ } else if (options.isPublic) {
313
+ return {
314
+ space: this.spaces.PUBLIC,
315
+ isPublic: true,
316
+ isImmutable: false
317
+ };
318
+ } else {
319
+ return {
320
+ space: this.spaces.PRIVATE,
321
+ isPublic: false,
322
+ isImmutable: false
323
+ };
324
+ }
325
+ }
326
+
327
+ /**
328
+ * Find if a base key exists in a different space
329
+ * @param {string} baseKey - The base key to search for
330
+ * @param {string} excludeSpace - The space to exclude from search
331
+ * @returns {string|null} - The space where the key exists, or null if not found
332
+ */
333
+ findKeyInDifferentSpace(baseKey, excludeSpace) {
334
+ for (const space of Object.values(this.spaces)) {
335
+ if (space !== excludeSpace) {
336
+ const spacedKey = this.createSpacedKey(space, baseKey);
337
+ if (this.spaceOwnership.has(spacedKey)) {
338
+ return space;
339
+ }
340
+ }
341
+ }
342
+ return null;
343
+ }
344
+
345
+ /**
346
+ * Check if the current peer has read access to a key with space awareness
347
+ * @param {string} keyId - The key ID
348
+ * @param {Object} metadata - The metadata object
349
+ * @param {string} space - The storage space
350
+ * @returns {boolean} - Whether access is allowed
351
+ */
352
+ hasReadAccessWithSpace(keyId, metadata, space) {
353
+ const accessControl = this.accessControl.get(keyId);
354
+
355
+ if (!accessControl && metadata) {
356
+ // Create access control from metadata
357
+ this.accessControl.set(keyId, {
358
+ isPublic: metadata.isPublic,
359
+ owner: metadata.owner,
360
+ allowedPeers: new Set(metadata.allowedPeers || []),
361
+ isImmutable: metadata.isImmutable,
362
+ space: metadata.space || space
363
+ });
364
+ return this.hasReadAccessWithSpace(keyId, metadata, space);
365
+ }
366
+
367
+ if (!accessControl) {
368
+ return false;
369
+ }
370
+
371
+ // Owner always has access
372
+ if (accessControl.owner === this.mesh.peerId) {
373
+ return true;
374
+ }
375
+
376
+ // Space-based access control
377
+ switch (space) {
378
+ case this.spaces.PRIVATE:
379
+ // Private: only owner and specifically allowed peers
380
+ return accessControl.allowedPeers.has(this.mesh.peerId);
381
+
382
+ case this.spaces.PUBLIC:
383
+ case this.spaces.FROZEN:
384
+ // Public and frozen: anyone can read
385
+ return true;
386
+
387
+ default:
388
+ return false;
389
+ }
390
+ }
391
+
141
392
  /**
142
393
  * Store data with encryption and access control
143
394
  * @param {string} key - The storage key
144
395
  * @param {any} value - The value to store
145
396
  * @param {Object} options - Storage options
146
- * @param {boolean} options.isPublic - Whether data is publicly readable (default: false - private and encrypted)
147
- * @param {boolean} options.isImmutable - Whether data is immutable for other peers (default: false)
397
+ * @param {string} options.space - Storage space ('private', 'public', 'frozen')
398
+ * @param {boolean} options.isPublic - Whether data is publicly readable (legacy, use space instead)
399
+ * @param {boolean} options.isImmutable - Whether data is immutable (legacy, use frozen space instead)
148
400
  * @param {boolean} options.enableCRDT - Whether to enable CRDT for collaborative editing (default: false)
149
401
  * @param {number} options.ttl - Time to live in milliseconds
150
402
  * @param {Array<string>} options.allowedPeers - Specific peers allowed to read private data
@@ -160,9 +412,31 @@ export class DistributedStorageManager extends EventEmitter {
160
412
  await this.waitForCrypto();
161
413
  }
162
414
 
163
- const keyId = await this.webDHT.generateKeyId(key);
415
+ // Parse key and determine storage space
416
+ const keyInfo = this.parseStorageKey(key);
417
+ const spaceConfig = this.determineSpaceFromOptions(options);
418
+
419
+ // Use explicit space from options if provided, otherwise use parsed space
420
+ const space = options.space || spaceConfig.space;
421
+ const fullKey = keyInfo.space === space ? keyInfo.fullKey : this.createSpacedKey(space, keyInfo.baseKey);
422
+ const baseKey = keyInfo.baseKey;
423
+
424
+ // Check space access permissions
425
+ if (!this.canAccessSpace(space, baseKey, this.mesh.peerId, 'write')) {
426
+ throw new Error(`Write access denied for space "${space}" and key "${baseKey}"`);
427
+ }
428
+
429
+ // Check if key already exists in a different space
430
+ if (this.config.spaceEnforcement) {
431
+ const existingSpaceKey = this.findKeyInDifferentSpace(baseKey, space);
432
+ if (existingSpaceKey) {
433
+ throw new Error(`Key "${baseKey}" already exists in space "${existingSpaceKey}" - cannot store in "${space}" space`);
434
+ }
435
+ }
436
+
437
+ const keyId = await this.webDHT.generateKeyId(fullKey);
164
438
  const timestamp = Date.now();
165
- console.log(`📦 Storing key: ${key}, keyId: ${keyId.substring(0, 8)}...`);
439
+ this.debug.log(`📦 Storing key: ${fullKey}, keyId: ${keyId.substring(0, 8)}...`);
166
440
 
167
441
  // Validate value size
168
442
  const serializedValue = JSON.stringify(value);
@@ -170,13 +444,15 @@ export class DistributedStorageManager extends EventEmitter {
170
444
  throw new Error(`Value size exceeds maximum allowed size of ${this.config.maxValueSize} bytes`);
171
445
  }
172
446
 
173
- // Create storage metadata - default to private for security
447
+ // Create storage metadata with space information
174
448
  const metadata = {
175
- key,
449
+ key: fullKey,
450
+ baseKey,
451
+ space,
176
452
  keyId,
177
453
  owner: this.mesh.peerId,
178
- isPublic: options.isPublic !== undefined ? options.isPublic : false, // Default to private
179
- isImmutable: options.isImmutable || false,
454
+ isPublic: spaceConfig.isPublic,
455
+ isImmutable: spaceConfig.isImmutable,
180
456
  enableCRDT: options.enableCRDT || false,
181
457
  allowedPeers: options.allowedPeers || [],
182
458
  createdAt: timestamp,
@@ -189,12 +465,19 @@ export class DistributedStorageManager extends EventEmitter {
189
465
  this.storageMetadata.set(keyId, metadata);
190
466
  this.ownedKeys.add(keyId);
191
467
 
468
+ // Track space ownership
469
+ this.spaceOwnership.set(fullKey, this.mesh.peerId);
470
+
471
+ // Track base key to space mapping for transparent access
472
+ this.keyToSpaceMapping.set(baseKey, { space, fullKey });
473
+
192
474
  // Set up access control
193
475
  this.accessControl.set(keyId, {
194
476
  isPublic: metadata.isPublic,
195
477
  owner: metadata.owner,
196
478
  allowedPeers: new Set(metadata.allowedPeers),
197
- isImmutable: metadata.isImmutable
479
+ isImmutable: metadata.isImmutable,
480
+ space
198
481
  });
199
482
 
200
483
  // Initialize CRDT state if enabled
@@ -213,8 +496,8 @@ export class DistributedStorageManager extends EventEmitter {
213
496
  encrypted: false
214
497
  };
215
498
 
216
- // Only encrypt private data (isPublic: false) to ensure owner-only access
217
- if (!metadata.isPublic && this.config.encryptionEnabled && this.unsea && this.storageKeypair) {
499
+ // Only encrypt private space data
500
+ if (space === this.spaces.PRIVATE && this.config.encryptionEnabled && this.unsea && this.storageKeypair) {
218
501
  try {
219
502
  // For private data, encrypt with the owner's keypair so only the owner can decrypt
220
503
  const encryptedValue = await this.unsea.encryptMessageWithMeta(serializedValue, this.storageKeypair);
@@ -224,32 +507,35 @@ export class DistributedStorageManager extends EventEmitter {
224
507
  encrypted: true,
225
508
  encryptedBy: this.mesh.peerId // Track who encrypted it
226
509
  };
227
- console.log(`📦 Encrypted private storage data for key: ${key} (owner-only access)`);
510
+ this.debug.log(`📦 Encrypted private space storage data for key: ${fullKey} (owner-only access)`);
228
511
  } catch (error) {
229
- console.warn(`Failed to encrypt private storage data for key ${key}:`, error);
512
+ this.debug.warn(`Failed to encrypt private space storage data for key ${fullKey}:`, error);
230
513
  // Fall back to unencrypted storage if encryption fails
231
514
  }
232
515
  } else {
233
- console.log(`📦 Storing ${metadata.isPublic ? 'public' : 'unencrypted'} data for key: ${key}`);
516
+ this.debug.log(`📦 Storing ${space} space data for key: ${fullKey}`);
234
517
  }
235
518
 
236
- // Store in WebDHT
519
+ // Store in WebDHT with space prefix
237
520
  try {
238
- console.log(`📦 Storing payload for key ${key}:`, {
521
+ this.debug.log(`📦 Storing payload for key ${fullKey}:`, {
239
522
  hasValue: !!storagePayload.value,
240
523
  hasMetadata: !!storagePayload.metadata,
241
- encrypted: storagePayload.encrypted
524
+ encrypted: storagePayload.encrypted,
525
+ space
242
526
  });
243
527
 
244
- await this.webDHT.put(`storage:${key}`, storagePayload, {
528
+ await this.webDHT.put(`storage:${fullKey}`, storagePayload, {
245
529
  ttl: metadata.ttl
246
530
  });
247
531
 
248
- console.log(`📦 Stored ${metadata.isPublic ? 'public' : 'private'} data for key: ${key}`);
532
+ this.debug.log(`📦 Stored ${space} space data for key: ${fullKey}`);
249
533
 
250
534
  // Emit storage event
251
535
  this.emit('dataStored', {
252
- key,
536
+ key: fullKey,
537
+ baseKey,
538
+ space,
253
539
  keyId,
254
540
  isPublic: metadata.isPublic,
255
541
  isImmutable: metadata.isImmutable,
@@ -258,12 +544,14 @@ export class DistributedStorageManager extends EventEmitter {
258
544
 
259
545
  return true;
260
546
  } catch (error) {
261
- console.error(`Failed to store data for key ${key}:`, error);
547
+ this.debug.error(`Failed to store data for key ${fullKey}:`, error);
262
548
  // Clean up local metadata on failure
263
549
  this.storageMetadata.delete(keyId);
264
550
  this.ownedKeys.delete(keyId);
265
551
  this.accessControl.delete(keyId);
266
552
  this.crdtStates.delete(keyId);
553
+ this.spaceOwnership.delete(fullKey);
554
+ this.keyToSpaceMapping.delete(baseKey);
267
555
  throw error;
268
556
  }
269
557
  }
@@ -285,30 +573,39 @@ export class DistributedStorageManager extends EventEmitter {
285
573
  await this.waitForCrypto();
286
574
  }
287
575
 
288
- const keyId = await this.webDHT.generateKeyId(key);
289
- console.log(`📦 Retrieving key: ${key}, keyId: ${keyId.substring(0, 8)}...`);
576
+ // Resolve the key to its actual storage location
577
+ const resolved = await this.resolveKey(key);
578
+ const { space, baseKey, fullKey, keyId } = resolved;
579
+
580
+ // Check space access permissions
581
+ if (!this.canAccessSpace(space, baseKey, this.mesh.peerId, 'read')) {
582
+ this.debug.warn(`📦 Access denied for space "${space}" and key "${baseKey}"`);
583
+ return null;
584
+ }
585
+
586
+ this.debug.log(`📦 Retrieving key: ${fullKey}, keyId: ${keyId.substring(0, 8)}...`);
290
587
 
291
588
  try {
292
589
  // Get data from WebDHT
293
- console.log(`📦 Attempting to retrieve data for key: ${key}`);
294
- const storagePayload = await this.webDHT.get(`storage:${key}`, {
590
+ this.debug.log(`📦 Attempting to retrieve data for key: ${fullKey}`);
591
+ const storagePayload = await this.webDHT.get(`storage:${fullKey}`, {
295
592
  forceRefresh: options.forceRefresh
296
593
  });
297
594
 
298
- console.log(`📦 Retrieved payload for key ${key}:`, {
595
+ this.debug.log(`📦 Retrieved payload for key ${fullKey}:`, {
299
596
  payloadExists: !!storagePayload,
300
597
  payloadType: typeof storagePayload,
301
- payloadValue: storagePayload,
598
+ space,
302
599
  keys: storagePayload ? Object.keys(storagePayload) : 'none'
303
600
  });
304
601
 
305
602
  if (!storagePayload || typeof storagePayload !== 'object') {
306
- console.log(`📦 No data found for key: ${key}`);
603
+ this.debug.log(`📦 No data found for key: ${fullKey}`);
307
604
  return null;
308
605
  }
309
606
 
310
607
  // Debug: Log the raw payload structure
311
- console.log('📦 Raw payload structure:', JSON.stringify(storagePayload, null, 2));
608
+ this.debug.log('📦 Raw payload structure:', JSON.stringify(storagePayload, null, 2));
312
609
 
313
610
  // Safely destructure the payload with detailed logging
314
611
  const value = storagePayload.value;
@@ -316,23 +613,30 @@ export class DistributedStorageManager extends EventEmitter {
316
613
  const encrypted = storagePayload.encrypted || false;
317
614
  const encryptedBy = storagePayload.encryptedBy;
318
615
 
319
- console.log('📦 Payload components:', {
616
+ this.debug.log('📦 Payload components:', {
320
617
  hasValue: value !== undefined,
321
618
  valueType: typeof value,
322
619
  hasMetadata: metadata !== undefined,
323
620
  metadataType: typeof metadata,
324
621
  encrypted,
622
+ space: metadata?.space || 'unknown',
325
623
  encryptedBy: encryptedBy?.substring(0, 8) + '...' || 'unknown'
326
624
  });
327
625
 
328
626
  if (!metadata) {
329
- console.warn(`📦 Invalid storage payload for key ${key}: missing metadata`);
627
+ this.debug.warn(`📦 Invalid storage payload for key ${fullKey}: missing metadata`);
330
628
  return null;
331
629
  }
332
630
 
333
- // Check access permissions
334
- if (!this.hasReadAccess(keyId, metadata)) {
335
- console.warn(`Access denied for key: ${key}`);
631
+ // Verify space consistency
632
+ if (metadata.space && metadata.space !== space) {
633
+ this.debug.warn(`📦 Space mismatch for key ${fullKey}: expected ${space}, got ${metadata.space}`);
634
+ return null;
635
+ }
636
+
637
+ // Check access permissions using space-aware logic
638
+ if (!this.hasReadAccessWithSpace(keyId, metadata, space)) {
639
+ this.debug.warn(`📦 Access denied for key: ${fullKey} in space: ${space}`);
336
640
  return null;
337
641
  }
338
642
 
@@ -341,10 +645,10 @@ export class DistributedStorageManager extends EventEmitter {
341
645
  if (encrypted && this.unsea && this.storageKeypair) {
342
646
  // Only allow decryption if this peer is the owner (encrypted the data)
343
647
  if (encryptedBy && encryptedBy !== this.mesh.peerId) {
344
- console.warn(`📦 Cannot decrypt data for key ${key}: encrypted by different peer (${encryptedBy.substring(0, 8)}...), current peer: ${this.mesh.peerId.substring(0, 8)}...`);
648
+ this.debug.warn(`📦 Cannot decrypt data for key ${key}: encrypted by different peer (${encryptedBy.substring(0, 8)}...), current peer: ${this.mesh.peerId.substring(0, 8)}...`);
345
649
  // For private data encrypted by another peer, deny access
346
650
  if (!metadata.isPublic) {
347
- console.warn(`Access denied for key: ${key} - private data encrypted by different peer`);
651
+ this.debug.warn(`Access denied for key: ${key} - private data encrypted by different peer`);
348
652
  return null;
349
653
  }
350
654
  }
@@ -352,15 +656,15 @@ export class DistributedStorageManager extends EventEmitter {
352
656
  try {
353
657
  const decryptedValue = await this.unsea.decryptMessageWithMeta(value, this.storageKeypair.epriv);
354
658
  finalValue = JSON.parse(decryptedValue);
355
- console.log(`🔓 Decrypted storage data for key: ${key}`);
659
+ this.debug.log(`🔓 Decrypted storage data for key: ${key}`);
356
660
  } catch (error) {
357
- console.error(`Failed to decrypt data for key ${key}:`, error);
661
+ this.debug.error(`Failed to decrypt data for key ${key}:`, error);
358
662
  // If this is private data and decryption fails, deny access
359
663
  if (!metadata.isPublic) {
360
664
  return null;
361
665
  }
362
666
  // For public data, if decryption fails, try to use the raw value
363
- console.warn(`📦 Using raw value for public key ${key} due to decryption failure`);
667
+ this.debug.warn(`📦 Using raw value for public key ${key} due to decryption failure`);
364
668
  finalValue = value;
365
669
  }
366
670
  }
@@ -372,15 +676,28 @@ export class DistributedStorageManager extends EventEmitter {
372
676
  isPublic: metadata.isPublic,
373
677
  owner: metadata.owner,
374
678
  allowedPeers: new Set(metadata.allowedPeers),
375
- isImmutable: metadata.isImmutable
679
+ isImmutable: metadata.isImmutable,
680
+ space: metadata.space || space
376
681
  });
682
+
683
+ // Track space ownership from retrieved metadata
684
+ if (metadata.owner && !this.spaceOwnership.has(fullKey)) {
685
+ this.spaceOwnership.set(fullKey, metadata.owner);
686
+ }
687
+
688
+ // Update key mapping for future transparent access
689
+ if (!key.includes(':')) {
690
+ this.keyToSpaceMapping.set(baseKey, { space, fullKey });
691
+ }
377
692
  }
378
693
 
379
- console.log(`📦 Retrieved ${metadata.isPublic ? 'public' : 'private'} data for key: ${key}`);
694
+ this.debug.log(`📦 Retrieved ${space} space data for key: ${fullKey}`);
380
695
 
381
696
  // Emit retrieval event
382
697
  this.emit('dataRetrieved', {
383
- key,
698
+ key: fullKey,
699
+ baseKey,
700
+ space,
384
701
  keyId,
385
702
  isPublic: metadata.isPublic,
386
703
  owner: metadata.owner
@@ -388,7 +705,7 @@ export class DistributedStorageManager extends EventEmitter {
388
705
 
389
706
  return finalValue;
390
707
  } catch (error) {
391
- console.error(`Failed to retrieve data for key ${key}:`, error);
708
+ this.debug.error(`Failed to retrieve data for key ${key}:`, error);
392
709
  return null;
393
710
  }
394
711
  }
@@ -406,21 +723,33 @@ export class DistributedStorageManager extends EventEmitter {
406
723
  throw new Error('WebDHT not available - ensure it is enabled in mesh configuration');
407
724
  }
408
725
 
409
- const keyId = await this.webDHT.generateKeyId(key);
726
+ // Resolve the key to its actual storage location
727
+ const resolved = await this.resolveKey(key);
728
+ const { fullKey, keyId } = resolved;
410
729
  const timestamp = Date.now();
411
730
 
412
731
  // Check if we have access to update
413
- const accessControl = this.accessControl.get(keyId);
414
- const metadata = this.storageMetadata.get(keyId);
732
+ let accessControl = this.accessControl.get(keyId);
733
+ let metadata = this.storageMetadata.get(keyId);
415
734
 
416
735
  if (!accessControl || !metadata) {
417
736
  // Try to retrieve metadata first
418
- const existingData = await this.retrieve(key);
737
+ const existingData = await this.retrieve(fullKey);
419
738
  if (!existingData) {
420
739
  throw new Error(`Key ${key} does not exist or is not accessible`);
421
740
  }
422
- // Retry after retrieving metadata
423
- return this.update(key, newValue, options);
741
+
742
+ // Check if metadata is now available after retrieve
743
+ const updatedAccessControl = this.accessControl.get(keyId);
744
+ const updatedMetadata = this.storageMetadata.get(keyId);
745
+
746
+ if (!updatedAccessControl || !updatedMetadata) {
747
+ throw new Error(`Key ${key} metadata could not be loaded - unable to update`);
748
+ }
749
+
750
+ // Update local references and continue with update logic
751
+ accessControl = updatedAccessControl;
752
+ metadata = updatedMetadata;
424
753
  }
425
754
 
426
755
  const isOwner = accessControl.owner === this.mesh.peerId;
@@ -457,26 +786,26 @@ export class DistributedStorageManager extends EventEmitter {
457
786
  encrypted: true,
458
787
  encryptedBy: this.mesh.peerId // Track who encrypted it
459
788
  };
460
- console.log(`📦 Encrypted updated storage data for key: ${key}`);
789
+ this.debug.log(`📦 Encrypted updated storage data for key: ${key}`);
461
790
  } catch (error) {
462
- console.warn(`Failed to encrypt updated storage data for key ${key}:`, error);
791
+ this.debug.warn(`Failed to encrypt updated storage data for key ${key}:`, error);
463
792
  }
464
793
  }
465
794
 
466
795
  try {
467
796
  // Use WebDHT update to notify all replicas and subscribers
468
- await this.webDHT.update(`storage:${key}`, storagePayload, {
797
+ await this.webDHT.update(`storage:${fullKey}`, storagePayload, {
469
798
  ttl: metadata.ttl
470
799
  });
471
800
 
472
801
  // Update local metadata
473
802
  this.storageMetadata.set(keyId, metadata);
474
803
 
475
- console.log(`📦 Updated ${metadata.isPublic ? 'public' : 'private'} data for key: ${key}`);
804
+ this.debug.log(`📦 Updated ${metadata.isPublic ? 'public' : 'private'} data for key: ${fullKey}`);
476
805
 
477
806
  // Emit update event
478
807
  this.emit('dataUpdated', {
479
- key,
808
+ key: fullKey,
480
809
  keyId,
481
810
  isPublic: metadata.isPublic,
482
811
  version: metadata.version,
@@ -485,7 +814,7 @@ export class DistributedStorageManager extends EventEmitter {
485
814
 
486
815
  return true;
487
816
  } catch (error) {
488
- console.error(`Failed to update data for key ${key}:`, error);
817
+ this.debug.error(`Failed to update data for key ${fullKey}:`, error);
489
818
  throw error;
490
819
  }
491
820
  }
@@ -564,7 +893,10 @@ export class DistributedStorageManager extends EventEmitter {
564
893
  throw new Error('WebDHT not available - ensure it is enabled in mesh configuration');
565
894
  }
566
895
 
567
- const keyId = await this.webDHT.generateKeyId(key);
896
+ // Resolve the key to its actual storage location
897
+ const resolved = await this.resolveKey(key);
898
+ const { baseKey, fullKey, keyId } = resolved;
899
+
568
900
  const accessControl = this.accessControl.get(keyId);
569
901
 
570
902
  if (!accessControl || accessControl.owner !== this.mesh.peerId) {
@@ -579,7 +911,7 @@ export class DistributedStorageManager extends EventEmitter {
579
911
  deletedBy: this.mesh.peerId
580
912
  };
581
913
 
582
- await this.webDHT.update(`storage:${key}`, tombstone);
914
+ await this.webDHT.update(`storage:${fullKey}`, tombstone);
583
915
 
584
916
  // Clean up local state
585
917
  this.storageMetadata.delete(keyId);
@@ -587,14 +919,18 @@ export class DistributedStorageManager extends EventEmitter {
587
919
  this.accessControl.delete(keyId);
588
920
  this.crdtStates.delete(keyId);
589
921
 
590
- console.log(`📦 Deleted data for key: ${key}`);
922
+ // Clean up space ownership tracking and key mapping
923
+ this.spaceOwnership.delete(fullKey);
924
+ this.keyToSpaceMapping.delete(baseKey);
925
+
926
+ this.debug.log(`📦 Deleted data for key: ${fullKey}`);
591
927
 
592
928
  // Emit deletion event
593
- this.emit('dataDeleted', { key, keyId });
929
+ this.emit('dataDeleted', { key: fullKey, keyId });
594
930
 
595
931
  return true;
596
932
  } catch (error) {
597
- console.error(`Failed to delete data for key ${key}:`, error);
933
+ this.debug.error(`Failed to delete data for key ${fullKey}:`, error);
598
934
  throw error;
599
935
  }
600
936
  }
@@ -612,7 +948,7 @@ export class DistributedStorageManager extends EventEmitter {
612
948
  // Subscribe to the WebDHT key
613
949
  const currentValue = await this.webDHT.subscribe(`storage:${key}`);
614
950
 
615
- console.log(`📦 Subscribed to storage key: ${key}`);
951
+ this.debug.log(`📦 Subscribed to storage key: ${key}`);
616
952
 
617
953
  return currentValue;
618
954
  }
@@ -627,7 +963,7 @@ export class DistributedStorageManager extends EventEmitter {
627
963
  }
628
964
 
629
965
  await this.webDHT.unsubscribe(`storage:${key}`);
630
- console.log(`📦 Unsubscribed from storage key: ${key}`);
966
+ this.debug.log(`📦 Unsubscribed from storage key: ${key}`);
631
967
  }
632
968
 
633
969
  /**
@@ -673,7 +1009,10 @@ export class DistributedStorageManager extends EventEmitter {
673
1009
  * @returns {Promise<boolean>} Success status
674
1010
  */
675
1011
  async grantAccess(key, peerId) {
676
- const keyId = await this.webDHT.generateKeyId(key);
1012
+ // Resolve the key to its actual storage location
1013
+ const resolved = await this.resolveKey(key);
1014
+ const { fullKey, keyId } = resolved;
1015
+
677
1016
  const accessControl = this.accessControl.get(keyId);
678
1017
  const metadata = this.storageMetadata.get(keyId);
679
1018
 
@@ -692,16 +1031,16 @@ export class DistributedStorageManager extends EventEmitter {
692
1031
 
693
1032
  // Update the stored metadata
694
1033
  try {
695
- const currentPayload = await this.webDHT.get(`storage:${key}`);
1034
+ const currentPayload = await this.webDHT.get(`storage:${fullKey}`);
696
1035
  if (currentPayload) {
697
1036
  currentPayload.metadata = metadata;
698
- await this.webDHT.update(`storage:${key}`, currentPayload);
1037
+ await this.webDHT.update(`storage:${fullKey}`, currentPayload);
699
1038
  }
700
1039
 
701
- console.log(`📦 Granted access to peer ${peerId.substring(0, 8)}... for key: ${key}`);
1040
+ this.debug.log(`📦 Granted access to peer ${peerId.substring(0, 8)}... for key: ${fullKey}`);
702
1041
 
703
1042
  // Emit access granted event
704
- this.emit('accessGranted', { key, keyId, peerId });
1043
+ this.emit('accessGranted', { key: fullKey, keyId, peerId });
705
1044
 
706
1045
  return true;
707
1046
  } catch (error) {
@@ -719,7 +1058,10 @@ export class DistributedStorageManager extends EventEmitter {
719
1058
  * @returns {Promise<boolean>} Success status
720
1059
  */
721
1060
  async revokeAccess(key, peerId) {
722
- const keyId = await this.webDHT.generateKeyId(key);
1061
+ // Resolve the key to its actual storage location
1062
+ const resolved = await this.resolveKey(key);
1063
+ const { fullKey, keyId } = resolved;
1064
+
723
1065
  const accessControl = this.accessControl.get(keyId);
724
1066
  const metadata = this.storageMetadata.get(keyId);
725
1067
 
@@ -734,16 +1076,16 @@ export class DistributedStorageManager extends EventEmitter {
734
1076
 
735
1077
  // Update the stored metadata
736
1078
  try {
737
- const currentPayload = await this.webDHT.get(`storage:${key}`);
1079
+ const currentPayload = await this.webDHT.get(`storage:${fullKey}`);
738
1080
  if (currentPayload) {
739
1081
  currentPayload.metadata = metadata;
740
- await this.webDHT.update(`storage:${key}`, currentPayload);
1082
+ await this.webDHT.update(`storage:${fullKey}`, currentPayload);
741
1083
  }
742
1084
 
743
- console.log(`📦 Revoked access from peer ${peerId.substring(0, 8)}... for key: ${key}`);
1085
+ this.debug.log(`📦 Revoked access from peer ${peerId.substring(0, 8)}... for key: ${fullKey}`);
744
1086
 
745
1087
  // Emit access revoked event
746
- this.emit('accessRevoked', { key, keyId, peerId });
1088
+ this.emit('accessRevoked', { key: fullKey, keyId, peerId });
747
1089
 
748
1090
  return true;
749
1091
  } catch (error) {
@@ -827,7 +1169,7 @@ export class DistributedStorageManager extends EventEmitter {
827
1169
  this.storageMetadata.delete(keyId);
828
1170
  this.accessControl.delete(keyId);
829
1171
  this.ownedKeys.delete(keyId);
830
- console.log(`📦 Cleaned up expired metadata for key: ${metadata.key}`);
1172
+ this.debug.log(`📦 Cleaned up expired metadata for key: ${metadata.key}`);
831
1173
  }
832
1174
  }
833
1175
 
@@ -835,7 +1177,7 @@ export class DistributedStorageManager extends EventEmitter {
835
1177
  for (const [keyId, crdtState] of this.crdtStates.entries()) {
836
1178
  if (crdtState.operations.length > 100) {
837
1179
  crdtState.operations = crdtState.operations.slice(-100);
838
- console.log(`📦 Cleaned up old CRDT operations for key: ${keyId.substring(0, 8)}...`);
1180
+ this.debug.log(`📦 Cleaned up old CRDT operations for key: ${keyId.substring(0, 8)}...`);
839
1181
  }
840
1182
  }
841
1183
  }
@@ -872,12 +1214,12 @@ export class DistributedStorageManager extends EventEmitter {
872
1214
  });
873
1215
  }
874
1216
  } catch (error) {
875
- console.warn(`Failed to backup key ${metadata.key}:`, error);
1217
+ this.debug.warn(`Failed to backup key ${metadata.key}:`, error);
876
1218
  }
877
1219
  }
878
1220
  }
879
1221
 
880
- console.log(`📦 Created backup with ${backupData.keys.length} keys`);
1222
+ this.debug.log(`📦 Created backup with ${backupData.keys.length} keys`);
881
1223
  return backupData;
882
1224
  }
883
1225
 
@@ -920,11 +1262,11 @@ export class DistributedStorageManager extends EventEmitter {
920
1262
  key: keyData.key,
921
1263
  error: error.message
922
1264
  });
923
- console.warn(`Failed to restore key ${keyData.key}:`, error);
1265
+ this.debug.warn(`Failed to restore key ${keyData.key}:`, error);
924
1266
  }
925
1267
  }
926
1268
 
927
- console.log(`📦 Restore complete: ${results.restored} restored, ${results.skipped} skipped, ${results.failed} failed`);
1269
+ this.debug.log(`📦 Restore complete: ${results.restored} restored, ${results.skipped} skipped, ${results.failed} failed`);
928
1270
  return results;
929
1271
  }
930
1272
 
@@ -1006,7 +1348,7 @@ export class DistributedStorageManager extends EventEmitter {
1006
1348
  });
1007
1349
 
1008
1350
  await Promise.allSettled(storePromises);
1009
- console.log(`📦 Bulk store complete: ${results.stored} stored, ${results.failed} failed`);
1351
+ this.debug.log(`📦 Bulk store complete: ${results.stored} stored, ${results.failed} failed`);
1010
1352
  return results;
1011
1353
  }
1012
1354
 
@@ -1024,7 +1366,7 @@ export class DistributedStorageManager extends EventEmitter {
1024
1366
  const value = await this.retrieve(key, options);
1025
1367
  results[key] = value;
1026
1368
  } catch (error) {
1027
- console.warn(`Failed to retrieve key ${key}:`, error);
1369
+ this.debug.warn(`Failed to retrieve key ${key}:`, error);
1028
1370
  results[key] = null;
1029
1371
  }
1030
1372
  });
@@ -1108,7 +1450,7 @@ export class DistributedStorageManager extends EventEmitter {
1108
1450
  await this.subscribe(key);
1109
1451
  subscriptions.add(key);
1110
1452
  } catch (error) {
1111
- console.warn(`Failed to subscribe to key ${key}:`, error);
1453
+ this.debug.warn(`Failed to subscribe to key ${key}:`, error);
1112
1454
  }
1113
1455
  }
1114
1456
 
@@ -1129,7 +1471,7 @@ export class DistributedStorageManager extends EventEmitter {
1129
1471
  try {
1130
1472
  await this.unsubscribe(key);
1131
1473
  } catch (error) {
1132
- console.warn(`Failed to unsubscribe from key ${key}:`, error);
1474
+ this.debug.warn(`Failed to unsubscribe from key ${key}:`, error);
1133
1475
  }
1134
1476
  }
1135
1477
 
@@ -1145,8 +1487,24 @@ export class DistributedStorageManager extends EventEmitter {
1145
1487
  * @returns {Promise<Object|null>} Key information or null if not found
1146
1488
  */
1147
1489
  async getKeyInfo(key) {
1148
- const keyId = await this.webDHT.generateKeyId(key);
1149
- const metadata = this.storageMetadata.get(keyId);
1490
+ // Resolve the key to its actual storage location
1491
+ const resolved = await this.resolveKey(key);
1492
+ const { fullKey, keyId } = resolved;
1493
+
1494
+ let metadata = this.storageMetadata.get(keyId);
1495
+
1496
+ // If metadata is not found locally, try to retrieve it from the network
1497
+ if (!metadata) {
1498
+ try {
1499
+ const existingData = await this.retrieve(fullKey);
1500
+ if (existingData !== null) {
1501
+ // Metadata should now be available after retrieve
1502
+ metadata = this.storageMetadata.get(keyId);
1503
+ }
1504
+ } catch (error) {
1505
+ this.debug.warn(`Failed to retrieve metadata for key ${key}:`, error);
1506
+ }
1507
+ }
1150
1508
 
1151
1509
  if (!metadata) {
1152
1510
  return null;
@@ -1180,7 +1538,7 @@ export class DistributedStorageManager extends EventEmitter {
1180
1538
  }
1181
1539
 
1182
1540
  this.enabled = true;
1183
- console.log('📦 Distributed storage enabled');
1541
+ this.debug.log('📦 Distributed storage enabled');
1184
1542
  this.emit('storageEnabled');
1185
1543
  }
1186
1544
 
@@ -1190,7 +1548,7 @@ export class DistributedStorageManager extends EventEmitter {
1190
1548
  */
1191
1549
  async disable() {
1192
1550
  this.enabled = false;
1193
- console.log('📦 Distributed storage disabled');
1551
+ this.debug.log('📦 Distributed storage disabled');
1194
1552
  this.emit('storageDisabled');
1195
1553
  }
1196
1554
 
@@ -1215,7 +1573,7 @@ export class DistributedStorageManager extends EventEmitter {
1215
1573
  try {
1216
1574
  await this.delete(metadata.key);
1217
1575
  } catch (error) {
1218
- console.warn(`Failed to delete key ${metadata.key} during clear:`, error);
1576
+ this.debug.warn(`Failed to delete key ${metadata.key} during clear:`, error);
1219
1577
  }
1220
1578
  }
1221
1579
  }
@@ -1225,8 +1583,10 @@ export class DistributedStorageManager extends EventEmitter {
1225
1583
  this.accessControl.clear();
1226
1584
  this.crdtStates.clear();
1227
1585
  this.ownedKeys.clear();
1586
+ this.spaceOwnership.clear();
1587
+ this.keyToSpaceMapping.clear();
1228
1588
 
1229
- console.log('📦 All stored data cleared');
1589
+ this.debug.log('📦 All stored data cleared');
1230
1590
  this.emit('storageCleared');
1231
1591
  }
1232
1592
 
@@ -1240,8 +1600,15 @@ export class DistributedStorageManager extends EventEmitter {
1240
1600
 
1241
1601
  for (const keyId of this.ownedKeys) {
1242
1602
  const metadata = this.storageMetadata.get(keyId);
1243
- if (metadata && metadata.key.startsWith(prefix)) {
1244
- keys.push(metadata.key);
1603
+ if (metadata) {
1604
+ // Check both the full key and the base key for prefix match
1605
+ const fullKey = metadata.key;
1606
+ const baseKey = metadata.baseKey;
1607
+
1608
+ if (fullKey.startsWith(prefix) || baseKey.startsWith(prefix)) {
1609
+ // Return the base key for user convenience (since they stored with base key)
1610
+ keys.push(baseKey);
1611
+ }
1245
1612
  }
1246
1613
  }
1247
1614
 
@@ -1262,7 +1629,7 @@ export class DistributedStorageManager extends EventEmitter {
1262
1629
  await this.delete(key);
1263
1630
  deletedCount++;
1264
1631
  } catch (error) {
1265
- console.warn(`Failed to delete key ${key} during bulk delete:`, error);
1632
+ this.debug.warn(`Failed to delete key ${key} during bulk delete:`, error);
1266
1633
  }
1267
1634
  }
1268
1635