anya_core/web5/
mod.rs

1//! Web5 Implementation Core [AIR-3][AIS-3][BPC-3][RES-3]
2
3// Re-export modules
4pub mod dwn; // Decentralized Web Node
5pub mod identity;
6pub mod protocols;
7pub mod vc; // Verifiable Credentials
8
9// Re-export important types for easy access
10// Legacy Web5Adapter removed. Use the canonical HTTP client adapter from src/web/web5_adapter.rs
11pub use identity::{DIDDocument, DIDManager, IdentityManager, Web5Error, Web5Result, DID};
12pub use protocols::{ProtocolDefinition, ProtocolHandler, ProtocolManager};
13
14use std::collections::HashMap;
15
16/// Web5 configuration with focused parameters
17#[derive(Clone, Debug)]
18pub struct Web5Config {
19    /// Whether Web5 functionality is enabled
20    pub enabled: bool,
21    /// Default DID method to use (e.g., "ion", "key", "web")
22    pub did_method: String,
23    /// DWN endpoint URL (if applicable)
24    pub dwn_url: Option<String>,
25    /// Whether to use local storage for DIDs
26    pub use_local_storage: bool,
27}
28
29impl Default for Web5Config {
30    fn default() -> Self {
31        Self {
32            enabled: true,
33            did_method: "ion".to_string(),
34            dwn_url: None,
35            use_local_storage: true,
36        }
37    }
38}
39
40/// Web5Manager: Lightweight coordinator for Web5 functionality following hexagonal architecture
41/// Implements ports and adapters pattern for clean interfaces
42pub struct Web5Manager {
43    /// Configuration
44    config: Web5Config,
45    /// DID manager - Core identity functionality
46    did_manager: identity::DIDManager,
47    /// Protocol manager - Core protocol functionality
48    protocol_manager: protocols::ProtocolManager,
49}
50
51impl Web5Manager {
52    /// Create a new Web5 manager with the specified configuration
53    pub fn new(config: Web5Config) -> Web5Result<Self> {
54        let did_manager = identity::DIDManager::new(&config.did_method);
55        let protocol_manager = protocols::ProtocolManager::new();
56
57        Ok(Self {
58            config,
59            did_manager,
60            protocol_manager,
61        })
62    }
63
64    /// Access the DID manager component
65    pub fn did_manager(&self) -> &identity::DIDManager {
66        &self.did_manager
67    }
68
69    /// Access the protocol manager component
70    pub fn protocol_manager(&self) -> &protocols::ProtocolManager {
71        &self.protocol_manager
72    }
73
74    /// Initialize the Web5 subsystem with default protocols
75    pub fn initialize(&mut self) -> Web5Result<()> {
76        // Register standard protocols
77        let profile_handler = protocols::ProfileProtocolHandler::new();
78        self.protocol_manager
79            .register_protocol(Box::new(profile_handler))?;
80
81        // Create default identity if none exists
82        if self.config.use_local_storage && self.did_manager.get_default_did()?.is_none() {
83            let did = self.did_manager.create_did()?;
84            self.did_manager.set_default_did(&did.id)?
85        }
86
87        Ok(())
88    }
89
90    /// Get the system status
91    pub fn status(&self) -> Web5Result<Web5Status> {
92        let did_count = self.did_manager.dids()?.len();
93        let protocol_count = self.protocol_manager.get_all_protocols().len();
94
95        Ok(Web5Status {
96            enabled: self.config.enabled,
97            did_count,
98            protocol_count,
99            dwn_connected: self.config.dwn_url.is_some(),
100        })
101    }
102
103    /// Get metrics for the Web5 system
104    pub fn get_metrics(&self) -> Web5Result<HashMap<String, String>> {
105        let mut metrics = HashMap::new();
106        metrics.insert(
107            "dids".to_string(),
108            self.did_manager.dids()?.len().to_string(),
109        );
110        metrics.insert(
111            "protocols".to_string(),
112            self.protocol_manager.get_all_protocols().len().to_string(),
113        );
114        metrics.insert(
115            "dwn_connected".to_string(),
116            self.config.dwn_url.is_some().to_string(),
117        );
118
119        Ok(metrics)
120    }
121}
122
123/// Web5 system status information
124#[derive(Clone, Debug)]
125pub struct Web5Status {
126    /// Whether Web5 is enabled
127    pub enabled: bool,
128    /// Number of DIDs managed
129    pub did_count: usize,
130    /// Number of protocols registered
131    pub protocol_count: usize,
132    /// Whether connected to a DWN
133    pub dwn_connected: bool,
134}
135
136#[cfg(test)]
137mod tests {
138    // [AIR-3][AIS-3][BPC-3][RES-3] Proper error handling organization
139    use super::*;
140
141    #[test]
142    fn test_web5_manager_creation() -> Result<(), Box<dyn std::error::Error>> {
143        let config = Web5Config::default();
144        let manager = Web5Manager::new(config)?;
145
146        assert!(manager.config.enabled);
147        assert_eq!(manager.config.did_method, "ion");
148        Ok(())
149    }
150
151    #[test]
152    fn test_web5_status() -> Result<(), Box<dyn std::error::Error>> {
153        let config = Web5Config::default();
154        let manager = Web5Manager::new(config)?;
155
156        let status = manager.status()?;
157        assert!(status.enabled);
158        assert_eq!(status.did_count, 0);
159        assert_eq!(status.protocol_count, 0);
160        assert!(!status.dwn_connected);
161        Ok(())
162    }
163}