//! Configuration for git-remote-iroh. //! //! Stores the iroh secret key so the node ID is persistent. use anyhow::{Context, Result}; use iroh::SecretKey; use std::path::PathBuf; const CONFIG_DIR: &str = ".git-remote-iroh"; const KEY_FILE: &str = "secret_key"; /// Returns the config directory (~/.git-remote-iroh/). pub fn config_dir() -> Result { let home = std::env::var("HOME").context("HOME not set")?; Ok(PathBuf::from(home).join(CONFIG_DIR)) } /// Loads or generates a persistent iroh secret key. pub fn load_or_create_key() -> Result { let dir = config_dir()?; let key_path = dir.join(KEY_FILE); if key_path.exists() { let bytes = std::fs::read(&key_path).context("failed to read iroh key")?; let arr: [u8; 32] = bytes .try_into() .map_err(|_| anyhow::anyhow!("invalid key length"))?; Ok(SecretKey::from(arr)) } else { std::fs::create_dir_all(&dir).context("failed to create config dir")?; let key = SecretKey::generate(&mut rand::rng()); std::fs::write(&key_path, key.to_bytes()).context("failed to write iroh key")?; Ok(key) } }