rad-id-sync
This project has not described itself yet.
Brought by drakestapleton
rad-id-sync / src / identity.rs
| 1 | use serde::{Deserialize, Serialize}; |
| 2 | use std::fs; |
| 3 | use std::path::PathBuf; |
| 4 | |
| 5 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 6 | #[serde(rename_all = "snake_case")] |
| 7 | pub enum EntityType { |
| 8 | AutonomousAgent, |
| 9 | HumanOperator, |
| 10 | NodeBridge, |
| 11 | } |
| 12 | |
| 13 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 14 | pub struct KeyRecord { |
| 15 | pub key_type: String, // e.g. "ssh-ed25519", "radicle-ed25519" |
| 16 | pub public_key: String, |
| 17 | pub finger_print: String, |
| 18 | pub added_at: String, |
| 19 | pub status: String, // "active", "rotated", "revoked" |
| 20 | } |
| 21 | |
| 22 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 23 | pub struct IdentityAnchor { |
| 24 | pub did: String, // e.g. "did:rad:aien:spark-01" |
| 25 | pub name: String, // "AIEN" or "Drake Stapleton" |
| 26 | pub entity_type: EntityType, |
| 27 | pub primary_radicle_key: Option<String>, |
| 28 | pub primary_ssh_key: Option<String>, |
| 29 | pub key_history: Vec<KeyRecord>, |
| 30 | pub last_synced_at: String, |
| 31 | pub metadata: std::collections::HashMap<String, String>, |
| 32 | } |
| 33 | |
| 34 | impl IdentityAnchor { |
| 35 | pub fn default_agent() -> Self { |
| 36 | let mut meta = std::collections::HashMap::new(); |
| 37 | meta.insert("harness".to_string(), "aien-mojo-1.0".to_string()); |
| 38 | meta.insert("host".to_string(), "spark-gb10".to_string()); |
| 39 | |
| 40 | Self { |
| 41 | did: "did:rad:aien:spark-master".to_string(), |
| 42 | name: "AIEN".to_string(), |
| 43 | entity_type: EntityType::AutonomousAgent, |
| 44 | primary_radicle_key: None, |
| 45 | primary_ssh_key: None, |
| 46 | key_history: Vec::new(), |
| 47 | last_synced_at: chrono::Utc::now().to_rfc3339(), |
| 48 | metadata: meta, |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | pub fn anchor_file_path() -> PathBuf { |
| 53 | let home = std::env::var("HOME").unwrap_or_else(|_| "/home/drakestapleton".to_string()); |
| 54 | PathBuf::from(home).join(".radicle").join("identity_anchor.json") |
| 55 | } |
| 56 | |
| 57 | pub fn load_or_init() -> Result<Self, String> { |
| 58 | let path = Self::anchor_file_path(); |
| 59 | if path.exists() { |
| 60 | let data = fs::read_to_string(&path).map_err(|e| format!("Failed to read anchor: {}", e))?; |
| 61 | serde_json::from_str(&data).map_err(|e| format!("Failed to parse anchor: {}", e)) |
| 62 | } else { |
| 63 | let anchor = Self::default_agent(); |
| 64 | anchor.save()?; |
| 65 | Ok(anchor) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn save(&self) -> Result<(), String> { |
| 70 | let path = Self::anchor_file_path(); |
| 71 | if let Some(parent) = path.parent() { |
| 72 | fs::create_dir_all(parent).map_err(|e| format!("Failed to create parent dir: {}", e))?; |
| 73 | } |
| 74 | let data = serde_json::to_string_pretty(self).map_err(|e| format!("Failed to serialize: {}", e))?; |
| 75 | fs::write(&path, data).map_err(|e| format!("Failed to write anchor: {}", e)) |
| 76 | } |
| 77 | } |