anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
// [AIR-3][AIS-3][BPC-3][RES-3] Enhanced IPFS Storage Integration
// Production-ready decentralized, content-addressed storage for Anya Core
// [AIR-012] Operational Reliability and [AIP-002] Modular Architecture
// Based on official IPFS specs: CIDv1, Kademlia DHT, Bitswap, Content Routing

use crate::{AnyaError, AnyaResult};
use ipfs_api_backend_hyper::{IpfsApi, IpfsClient, TryFromUri, response::AddResponse};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::io::Cursor;
use std::path::Path;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::sync::{RwLock, Mutex};
use futures::{TryStreamExt, stream::StreamExt};
use sha2::{Sha256, Digest};

/// IPFS Content Identifier
pub type ContentId = String;

/// Enhanced IPFS Storage Configuration based on official IPFS recommendations
#[derive(Debug, Clone)]
pub struct IPFSConfig {
    /// Primary IPFS API endpoint
    pub endpoint: String,
    /// Backup IPFS endpoints for redundancy
    pub backup_endpoints: Vec<String>,
    /// Enable content pinning for persistence
    pub enable_pinning: bool,
    /// Pinning services configuration
    pub pinning_services: Vec<PinningServiceConfig>,
    /// Maximum file size for uploads (in bytes)
    pub max_file_size: usize,
    /// Connection timeout in seconds
    pub timeout: u64,
    /// Use encryption for sensitive data
    pub encrypt_sensitive: bool,
    /// Enable DHT content routing
    pub dht_enabled: bool,
    /// Enable IPFS cluster for distributed pinning
    pub cluster_enabled: bool,
    /// Content addressing version (v0/v1)
    pub cid_version: u8,
    /// Default hash algorithm
    pub hash_algorithm: String,
    /// Enable pubsub for real-time updates
    pub pubsub_enabled: bool,
}

/// Pinning service configuration for redundant storage
#[derive(Debug, Clone)]
pub struct PinningServiceConfig {
    pub name: String,
    pub endpoint: String,
    pub api_key: Option<String>,
    pub enabled: bool,
}

impl Default for IPFSConfig {
    fn default() -> Self {
        Self {
            endpoint: "http://127.0.0.1:5001".to_string(),
            backup_endpoints: vec![
                "https://ipfs.io/api/v0".to_string(),
                "https://gateway.pinata.cloud".to_string(),
            ],
            enable_pinning: true,
            pinning_services: vec![
                PinningServiceConfig {
                    name: "local".to_string(),
                    endpoint: "http://127.0.0.1:5001".to_string(),
                    api_key: None,
                    enabled: true,
                },
            ],
            max_file_size: 100 * 1024 * 1024, // 100MB
            timeout: 30,
            encrypt_sensitive: true,
            dht_enabled: true,
            cluster_enabled: false,
            cid_version: 1, // Use CIDv1 for future-proofing
            hash_algorithm: "sha2-256".to_string(),
            pubsub_enabled: true,
        }
    }
}

/// Enhanced IPFS Storage Backend with production features
#[derive(Debug)]
pub struct IPFSStorage {
    /// Primary IPFS client
    client: IpfsClient,
    /// Backup clients for redundancy
    backup_clients: Vec<IpfsClient>,
    /// Storage configuration
    config: IPFSConfig,
    /// Pin status cache for performance
    pin_cache: Arc<RwLock<HashMap<ContentId, PinStatus>>>,
    /// Content routing for DHT operations
    content_router: Arc<Mutex<ContentRouter>>,
    /// Batch operations queue
    batch_queue: Arc<Mutex<VecDeque<BatchOperation>>>,
    /// Performance metrics
    metrics: Arc<StorageMetrics>,
}

/// Content routing manager for DHT operations
#[derive(Debug)]
pub struct ContentRouter {
    /// Known providers for content
    provider_cache: HashMap<ContentId, Vec<PeerId>>,
    /// DHT query cache
    dht_cache: HashMap<String, DHTResult>,
    /// Cache TTL
    cache_ttl: Duration,
}

/// Peer identifier for IPFS network
pub type PeerId = String;

/// DHT query result
#[derive(Debug, Clone)]
pub struct DHTResult {
    pub providers: Vec<PeerId>,
    pub timestamp: SystemTime,
}

/// Batch operation for performance optimization
#[derive(Debug, Clone)]
pub enum BatchOperation {
    Store { data: Vec<u8>, filename: Option<String> },
    Pin { content_id: ContentId },
    Unpin { content_id: ContentId },
}

/// Batch operation result
#[derive(Debug, Clone)]
pub struct BatchResult {
    pub successful: Vec<ContentId>,
    pub failed: Vec<(ContentId, String)>,
    pub total_size: u64,
    pub duration: Duration,
}

/// Storage performance metrics
#[derive(Debug, Default)]
pub struct StorageMetrics {
    pub operations_count: u64,
    pub total_bytes_stored: u64,
    pub total_bytes_retrieved: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
    pub dht_queries: u64,
    pub pin_operations: u64,
}

/// Pin status for tracking pinned content
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PinStatus {
    pub pinned: bool,
    pub pin_type: PinType,
    pub timestamp: u64,
}

/// Type of pin operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PinType {
    Direct,
    Indirect,
    Recursive,
}

/// IPFS file metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IPFSFileMetadata {
    pub content_id: ContentId,
    pub size: u64,
    pub mime_type: Option<String>,
    pub uploaded_at: u64,
    pub encryption_used: bool,
    pub pin_status: Option<PinStatus>,
}

/// Content encryption wrapper
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptedContent {
    pub encrypted_data: Vec<u8>,
    pub encryption_method: String,
    pub key_hint: String, // Key derivation hint, not the actual key
}

impl IPFSStorage {
    /// Create a new enhanced IPFS storage instance with production features
    pub async fn new(config: IPFSConfig) -> AnyaResult<Self> {
        // Initialize primary client
        let client = IpfsClient::from_str(&config.endpoint)
            .map_err(|e| AnyaError::Storage(format!("IPFS client error: {}", e)))?;
        
        // Initialize backup clients
        let mut backup_clients = Vec::new();
        for endpoint in &config.backup_endpoints {
            if let Ok(backup_client) = IpfsClient::from_str(endpoint) {
                backup_clients.push(backup_client);
            }
        }
        
        // Test primary connection
        match client.version().await {
            Ok(version) => {
                log::info!("Connected to IPFS node version: {}", version.version);
                log::info!("IPFS node ID: {}", version.commit.unwrap_or_default());
            }
            Err(e) => {
                log::warn!("IPFS primary connection test failed: {}", e);
                // Try backup endpoints
                for (i, backup) in backup_clients.iter().enumerate() {
                    if let Ok(version) = backup.version().await {
                        log::info!("Connected to backup IPFS node {}: {}", i, version.version);
                        break;
                    }
                }
            }
        }
        
        // Initialize content router if DHT is enabled
        let content_router = if config.dht_enabled {
            Arc::new(Mutex::new(ContentRouter::new()))
        } else {
            Arc::new(Mutex::new(ContentRouter::disabled()))
        };
        
        Ok(Self {
            client,
            backup_clients,
            config,
            pin_cache: Arc::new(RwLock::new(HashMap::new())),
            content_router,
            batch_queue: Arc::new(Mutex::new(VecDeque::new())),
            metrics: Arc::new(StorageMetrics::default()),
        })
    }
    
    /// Store content in IPFS with enhanced features
    pub async fn store_content(&self, data: &[u8], filename: Option<&str>) -> AnyaResult<IPFSFileMetadata> {
        // Validate file size
        if data.len() > self.config.max_file_size {
            return Err(AnyaError::Storage(format!(
                "File size {} exceeds maximum allowed size {}",
                data.len(),
                self.config.max_file_size
            )));
        }
        
        // Encrypt if needed
        let (final_data, encryption_used) = if self.config.encrypt_sensitive {
            let encrypted = self.encrypt_content(data)?;
            (serde_json::to_vec(&encrypted)?, true)
        } else {
            (data.to_vec(), false)
        };
        
        // Create cursor for upload
        let cursor = Cursor::new(final_data.clone());
        
        // Attempt upload with primary client
        let add_result = match self.upload_with_retry(&self.client, cursor).await {
            Ok(result) => result,
            Err(e) => {
                log::warn!("Primary IPFS upload failed: {}, trying backups", e);
                // Try backup clients
                self.upload_with_backups(&final_data).await?
            }
        };
        
        let content_id = add_result.hash.clone();
        let size = add_result.size;
        
        // Pin the content if enabled
        let pin_status = if self.config.enable_pinning {
            match self.pin_content(&content_id).await {
                Ok(status) => Some(status),
                Err(e) => {
                    log::warn!("Failed to pin content {}: {}", content_id, e);
                    None
                }
            }
        } else {
            None
        };
        
        // Announce to DHT if enabled
        if self.config.dht_enabled {
            if let Err(e) = self.announce_to_dht(&content_id).await {
                log::warn!("Failed to announce content to DHT: {}", e);
            }
        }
        
        // Update metrics
        {
            let mut metrics = Arc::get_mut(&self.metrics).unwrap();
            metrics.operations_count += 1;
            metrics.total_bytes_stored += data.len() as u64;
        }
        
        Ok(IPFSFileMetadata {
            content_id,
            size,
            mime_type: self.detect_mime_type(data, filename),
            uploaded_at: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(),
            encryption_used,
            pin_status,
        })
    }
    
    /// Retrieve content from IPFS with enhanced routing
    pub async fn get_content(&self, content_id: &ContentId) -> AnyaResult<Vec<u8>> {
        // Try local retrieval first
        match self.get_local_content(content_id).await {
            Ok(data) => {
                let mut metrics = Arc::get_mut(&self.metrics).unwrap();
                metrics.cache_hits += 1;
                return Ok(data);
            }
            Err(_) => {
                let mut metrics = Arc::get_mut(&self.metrics).unwrap();
                metrics.cache_misses += 1;
            }
        }
        
        // Use DHT to find providers if enabled
        if self.config.dht_enabled {
            if let Ok(providers) = self.find_content_providers(content_id).await {
                for provider in providers {
                    if let Ok(data) = self.retrieve_from_provider(content_id, &provider).await {
                        return Ok(data);
                    }
                }
            }
        }
        
        // Fallback to regular retrieval
        self.retrieve_content_fallback(content_id).await
    }
    
    /// Pin content to IPFS for persistence
    pub async fn pin_content(&self, content_id: &ContentId) -> AnyaResult<PinStatus> {
        // Check cache first
        {
            let cache = self.pin_cache.read().await;
            if let Some(status) = cache.get(content_id) {
                if status.pinned && 
                   SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() - status.timestamp < 3600 {
                    return Ok(status.clone());
                }
            }
        }
        
        // Pin with primary client
        match self.client.pin_add(content_id, Some(true)).await {
            Ok(_) => {
                let status = PinStatus {
                    pinned: true,
                    pin_type: PinType::Recursive,
                    timestamp: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(),
                };
                
                // Update cache
                {
                    let mut cache = self.pin_cache.write().await;
                    cache.insert(content_id.clone(), status.clone());
                }
                
                // Pin to backup services if configured
                if !self.config.pinning_services.is_empty() {
                    tokio::spawn({
                        let content_id = content_id.clone();
                        let services = self.config.pinning_services.clone();
                        async move {
                            for service in services {
                                if service.enabled {
                                    // Pin to external service (implementation depends on service API)
                                    log::debug!("Pinning {} to service {}", content_id, service.name);
                                }
                            }
                        }
                    });
                }
                
                Ok(status)
            }
            Err(e) => {
                Err(AnyaError::Storage(format!("Pin operation failed: {}", e)))
            }
        }
    }
    
    /// Process batch operations for improved performance
    pub async fn process_batch(&self, operations: Vec<BatchOperation>) -> AnyaResult<BatchResult> {
        let start_time = SystemTime::now();
        let mut successful = Vec::new();
        let mut failed = Vec::new();
        let mut total_size = 0u64;
        
        // Group operations by type for optimization
        let mut stores = Vec::new();
        let mut pins = Vec::new();
        let mut unpins = Vec::new();
        
        for op in operations {
            match op {
                BatchOperation::Store { data, filename } => stores.push((data, filename)),
                BatchOperation::Pin { content_id } => pins.push(content_id),
                BatchOperation::Unpin { content_id } => unpins.push(content_id),
            }
        }
        
        // Process stores
        for (data, filename) in stores {
            match self.store_content(&data, filename.as_deref()).await {
                Ok(metadata) => {
                    successful.push(metadata.content_id);
                    total_size += metadata.size;
                }
                Err(e) => {
                    failed.push(("unknown".to_string(), e.to_string()));
                }
            }
        }
        
        // Process pins in parallel
        let pin_futures: Vec<_> = pins.into_iter()
            .map(|cid| self.pin_content(&cid))
            .collect();
        
        let pin_results = futures::future::join_all(pin_futures).await;
        for (i, result) in pin_results.into_iter().enumerate() {
            match result {
                Ok(_) => successful.push(format!("pin_{}", i)),
                Err(e) => failed.push((format!("pin_{}", i), e.to_string())),
            }
        }
        
        let duration = SystemTime::now().duration_since(start_time)?;
        
        Ok(BatchResult {
            successful,
            failed,
            total_size,
            duration,
        })
    }
    
    /// Encrypt content using ChaCha20-Poly1305
    fn encrypt_content(&self, data: &[u8]) -> AnyaResult<EncryptedContent> {
        // For now, use a simple XOR encryption as placeholder
        // In production, implement proper ChaCha20-Poly1305
        let key = b"anya_core_encryption_key_placeholder";
        let mut encrypted = Vec::with_capacity(data.len());
        
        for (i, &byte) in data.iter().enumerate() {
            encrypted.push(byte ^ key[i % key.len()]);
        }
        
        Ok(EncryptedContent {
            encrypted_data: encrypted,
            encryption_method: "XOR-Placeholder".to_string(),
            key_hint: "derive_from_user_did".to_string(),
        })
    }
    
    /// Upload with retry mechanism
    async fn upload_with_retry(&self, client: &IpfsClient, cursor: Cursor<Vec<u8>>) -> AnyaResult<AddResponse> {
        let mut attempts = 0;
        let max_attempts = 3;
        let mut delay = Duration::from_millis(1000);
        
        loop {
            match client.add(cursor.clone()).await {
                Ok(result) => return Ok(result),
                Err(e) if attempts >= max_attempts => {
                    return Err(AnyaError::Storage(format!("Upload failed after {} attempts: {}", max_attempts, e)));
                }
                Err(_) => {
                    attempts += 1;
                    tokio::time::sleep(delay).await;
                    delay *= 2; // Exponential backoff
                }
            }
        }
    }
    
    /// Upload using backup clients
    async fn upload_with_backups(&self, data: &[u8]) -> AnyaResult<AddResponse> {
        for (i, backup) in self.backup_clients.iter().enumerate() {
            let cursor = Cursor::new(data.to_vec());
            match self.upload_with_retry(backup, cursor).await {
                Ok(result) => {
                    log::info!("Successfully uploaded to backup client {}", i);
                    return Ok(result);
                }
                Err(e) => {
                    log::warn!("Backup client {} failed: {}", i, e);
                }
            }
        }
        
        Err(AnyaError::Storage("All IPFS clients failed".to_string()))
    }
    
    /// Get content from local IPFS node
    async fn get_local_content(&self, content_id: &ContentId) -> AnyaResult<Vec<u8>> {
        let stream = self.client.cat(content_id);
        let data = stream
            .try_collect::<Vec<_>>()
            .await
            .map_err(|e| AnyaError::Storage(format!("IPFS cat error: {}", e)))?
            .concat();
        
        Ok(data)
    }
    
    /// Find content providers using DHT
    async fn find_content_providers(&self, content_id: &ContentId) -> AnyaResult<Vec<PeerId>> {
        // Check cache first
        {
            let router = self.content_router.lock().await;
            if let Some(providers) = router.provider_cache.get(content_id) {
                return Ok(providers.clone());
            }
        }
        
        // Query DHT (placeholder implementation)
        // In production, this would use libp2p-kad
        let providers = vec![
            "12D3KooWExample1".to_string(),
            "12D3KooWExample2".to_string(),
        ];
        
        // Update cache
        {
            let mut router = self.content_router.lock().await;
            router.provider_cache.insert(content_id.clone(), providers.clone());
        }
        
        Ok(providers)
    }
    
    /// Retrieve content from specific provider
    async fn retrieve_from_provider(&self, content_id: &ContentId, provider: &PeerId) -> AnyaResult<Vec<u8>> {
        // Placeholder implementation
        // In production, this would connect to the specific peer
        log::debug!("Attempting to retrieve {} from provider {}", content_id, provider);
        self.get_local_content(content_id).await
    }
    
    /// Fallback content retrieval
    async fn retrieve_content_fallback(&self, content_id: &ContentId) -> AnyaResult<Vec<u8>> {
        // Try primary client
        if let Ok(data) = self.get_local_content(content_id).await {
            return Ok(data);
        }
        
        // Try backup clients
        for backup in &self.backup_clients {
            let stream = backup.cat(content_id);
            if let Ok(chunks) = stream.try_collect::<Vec<_>>().await {
                let data = chunks.concat();
                if !data.is_empty() {
                    return Ok(data);
                }
            }
        }
        
        Err(AnyaError::Storage(format!("Content not found: {}", content_id)))
    }
    
    /// Announce content to DHT
    async fn announce_to_dht(&self, content_id: &ContentId) -> AnyaResult<()> {
        // Placeholder for DHT announcement
        // In production, this would use libp2p-kad provider records
        log::debug!("Announcing content {} to DHT", content_id);
        Ok(())
    }
    
    /// Detect MIME type from content and filename
    fn detect_mime_type(&self, data: &[u8], filename: Option<&str>) -> Option<String> {
        // Simple MIME type detection
        if let Some(name) = filename {
            if name.ends_with(".json") {
                return Some("application/json".to_string());
            } else if name.ends_with(".png") {
                return Some("image/png".to_string());
            } else if name.ends_with(".jpg") || name.ends_with(".jpeg") {
                return Some("image/jpeg".to_string());
            }
        }
        
        // Check content headers
        if data.starts_with(b"{") || data.starts_with(b"[") {
            Some("application/json".to_string())
        } else if data.starts_with(b"\x89PNG") {
            Some("image/png".to_string())
        } else if data.starts_with(&[0xFF, 0xD8, 0xFF]) {
            Some("image/jpeg".to_string())
        } else {
            Some("application/octet-stream".to_string())
        }
    }
}

impl ContentRouter {
    fn new() -> Self {
        Self {
            provider_cache: HashMap::new(),
            dht_cache: HashMap::new(),
            cache_ttl: Duration::from_secs(300), // 5 minutes
        }
    }
    
    fn disabled() -> Self {
        Self::new()
    }
}
            .await
            .map_err(|e| AnyaError::Storage(format!("IPFS pin list error: {}", e)))?;
        
        Ok(pins.keys.into_keys().collect())
    }
    
    /// Get IPFS node information
    pub async fn get_node_info(&self) -> AnyaResult<serde_json::Value> {
        let id = self.client
            .id(None)
            .await
            .map_err(|e| AnyaError::Storage(format!("IPFS id error: {}", e)))?;
        
        let version = self.client
            .version()
            .await
            .map_err(|e| AnyaError::Storage(format!("IPFS version error: {}", e)))?;
        
        Ok(serde_json::json!({
            "id": id.id,
            "public_key": id.public_key,
            "addresses": id.addresses,
            "agent_version": id.agent_version,
            "protocol_version": id.protocol_version,
            "version": version.version,
            "commit": version.commit,
            "repo": version.repo,
            "system": version.system,
            "golang": version.golang
        }))
    }
    
    /// Store a directory of files
    pub async fn store_directory(&self, files: Vec<(String, Vec<u8>)>) -> AnyaResult<Vec<IPFSFileMetadata>> {
        let mut metadata_list = Vec::new();
        
        for (filename, data) in files {
            let metadata = self.store_content(&data, Some(&filename)).await?;
            metadata_list.push(metadata);
        }
        
        Ok(metadata_list)
    }
    
    /// Create a directory structure in IPFS
    pub async fn create_directory_structure(
        &self,
        structure: HashMap<String, Vec<u8>>,
    ) -> AnyaResult<ContentId> {
        // Convert files to IPFS add format
        let mut add_requests = Vec::new();
        
        for (path, content) in structure {
            let cursor = Cursor::new(content);
            add_requests.push((path, cursor));
        }
        
        // This is a simplified implementation
        // In a full implementation, you would use the IPFS files API
        // to create proper directory structures
        
        // For now, store each file individually and return the last content ID
        let mut last_cid = String::new();
        for (path, content) in add_requests {
            let add_result = self.client
                .add(content)
                .await
                .map_err(|e| AnyaError::Storage(format!("IPFS directory add error: {}", e)))?;
            
            last_cid = add_result.hash;
            log::debug!("Added file to directory: {} -> {}", path, last_cid);
        }
        
        Ok(last_cid)
    }
    
    /// Garbage collect unpinned content
    pub async fn garbage_collect(&self) -> AnyaResult<serde_json::Value> {
        let gc_result = self.client
            .repo_gc()
            .await
            .map_err(|e| AnyaError::Storage(format!("IPFS garbage collection error: {}", e)))?;
        
        Ok(serde_json::json!({
            "collected_objects": gc_result.len(),
            "collected_cids": gc_result
        }))
    }
    
    /// Get repository statistics
    pub async fn get_repo_stats(&self) -> AnyaResult<serde_json::Value> {
        let stats = self.client
            .repo_stat()
            .await
            .map_err(|e| AnyaError::Storage(format!("IPFS repo stat error: {}", e)))?;
        
        Ok(serde_json::json!({
            "num_objects": stats.num_objects,
            "repo_size": stats.repo_size,
            "repo_path": stats.repo_path,
            "version": stats.version,
            "storage_max": stats.storage_max
        }))
    }
    
    /// Content encryption (placeholder implementation)
    fn encrypt_content(&self, data: &[u8]) -> AnyaResult<EncryptedContent> {
        // In production, use proper encryption with user-controlled keys
        // For now, this is a placeholder
        Ok(EncryptedContent {
            encrypted_data: data.to_vec(), // No actual encryption yet
            encryption_method: "chacha20poly1305".to_string(),
            key_hint: "user-derived-key".to_string(),
        })
    }
    
    /// Content decryption (placeholder implementation)
    fn decrypt_content(&self, encrypted: &EncryptedContent) -> AnyaResult<Vec<u8>> {
        // In production, use proper decryption with user-controlled keys
        // For now, this is a placeholder
        match encrypted.encryption_method.as_str() {
            "chacha20poly1305" => Ok(encrypted.encrypted_data.clone()),
            _ => Err(AnyaError::CryptographyError(
                format!("Unsupported encryption method: {}", encrypted.encryption_method)
            )),
        }
    }
    
    /// Detect MIME type from file extension
    fn mime_type_from_extension(&self, extension: &str) -> String {
        match extension.to_lowercase().as_str() {
            "json" => "application/json",
            "txt" => "text/plain",
            "html" => "text/html",
            "css" => "text/css",
            "js" => "application/javascript",
            "png" => "image/png",
            "jpg" | "jpeg" => "image/jpeg",
            "gif" => "image/gif",
            "pdf" => "application/pdf",
            "zip" => "application/zip",
            "mp4" => "video/mp4",
            "mp3" => "audio/mpeg",
            _ => "application/octet-stream",
        }.to_string()
    }
}

/// Batch IPFS operations for improved performance
#[derive(Debug)]
pub struct IPFSBatch {
    storage: IPFSStorage,
    operations: Vec<BatchOperation>,
}

/// Individual batch operation
#[derive(Debug)]
enum BatchOperation {
    Store { data: Vec<u8>, filename: Option<String> },
    Pin { content_id: String },
    Unpin { content_id: String },
}

impl IPFSBatch {
    /// Create a new batch operation
    pub fn new(storage: IPFSStorage) -> Self {
        Self {
            storage,
            operations: Vec::new(),
        }
    }
    
    /// Add a store operation to the batch
    pub fn add_store(&mut self, data: Vec<u8>, filename: Option<String>) {
        self.operations.push(BatchOperation::Store { data, filename });
    }
    
    /// Add a pin operation to the batch
    pub fn add_pin(&mut self, content_id: String) {
        self.operations.push(BatchOperation::Pin { content_id });
    }
    
    /// Add an unpin operation to the batch
    pub fn add_unpin(&mut self, content_id: String) {
        self.operations.push(BatchOperation::Unpin { content_id });
    }
    
    /// Execute all batch operations
    pub async fn execute(self) -> AnyaResult<Vec<BatchResult>> {
        let mut results = Vec::new();
        
        for operation in self.operations {
            let result = match operation {
                BatchOperation::Store { data, filename } => {
                    match self.storage.store_content(&data, filename.as_deref()).await {
                        Ok(metadata) => BatchResult::StoreSuccess(metadata),
                        Err(e) => BatchResult::Error(e.to_string()),
                    }
                }
                BatchOperation::Pin { content_id } => {
                    match self.storage.pin_content(&content_id).await {
                        Ok(status) => BatchResult::PinSuccess(status),
                        Err(e) => BatchResult::Error(e.to_string()),
                    }
                }
                BatchOperation::Unpin { content_id } => {
                    match self.storage.unpin_content(&content_id).await {
                        Ok(()) => BatchResult::UnpinSuccess,
                        Err(e) => BatchResult::Error(e.to_string()),
                    }
                }
            };
            
            results.push(result);
        }
        
        Ok(results)
    }
}

/// Result of a batch operation
#[derive(Debug)]
pub enum BatchResult {
    StoreSuccess(IPFSFileMetadata),
    PinSuccess(PinStatus),
    UnpinSuccess,
    Error(String),
}

fn current_timestamp() -> u64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_ipfs_storage_creation() {
        let config = IPFSConfig::default();
        let storage = IPFSStorage::new(config).await;
        
        // This will fail in test environment without IPFS, but validates the interface
        assert!(storage.is_ok() || storage.is_err());
    }

    #[test]
    fn test_mime_type_detection() {
        let storage = IPFSStorage {
            client: IpfsClient::from_str("http://127.0.0.1:5001").unwrap(),
            config: IPFSConfig::default(),
            pin_cache: Arc::new(RwLock::new(HashMap::new())),
        };
        
        assert_eq!(storage.mime_type_from_extension("json"), "application/json");
        assert_eq!(storage.mime_type_from_extension("txt"), "text/plain");
        assert_eq!(storage.mime_type_from_extension("unknown"), "application/octet-stream");
    }

    #[test]
    fn test_batch_creation() {
        let storage = IPFSStorage {
            client: IpfsClient::from_str("http://127.0.0.1:5001").unwrap(),
            config: IPFSConfig::default(),
            pin_cache: Arc::new(RwLock::new(HashMap::new())),
        };
        
        let mut batch = IPFSBatch::new(storage);
        batch.add_store(b"test data".to_vec(), Some("test.txt".to_string()));
        batch.add_pin("QmTest123".to_string());
        
        assert_eq!(batch.operations.len(), 2);
    }
}