diff --git a/src/peer_channels.rs b/src/peer_channels.rs index 489a623d7..a569a1483 100644 --- a/src/peer_channels.rs +++ b/src/peer_channels.rs @@ -9,6 +9,7 @@ use iroh_net::magic_endpoint::accept_conn; use iroh_net::NodeId; use iroh_net::{derp::DerpMode, key::SecretKey, MagicEndpoint}; +use crate::config::Config; use crate::contact::ContactId; use crate::context::Context; use crate::message::{Message, MsgId}; @@ -18,7 +19,7 @@ use crate::webxdc::StatusUpdateItem; impl Context { /// Create magic endpoint and gossip for the context. pub async fn create_gossip(&self) -> Result<()> { - let secret_key: SecretKey = self.generate_iroh_keypair(); + let secret_key: SecretKey = self.get_or_generate_iroh_keypair().await?; println!("> our secret key: {}", base32::fmt(secret_key.to_bytes())); if self.endpoint.lock().await.is_some() { @@ -145,9 +146,17 @@ impl Context { Ok(()) } - /// Get the iroh gossip secret key from the database or create one. - pub fn generate_iroh_keypair(&self) -> SecretKey { - SecretKey::generate() + /// Get the iroh gossip secret key from the database or generate a new one and persist it. + pub async fn get_or_generate_iroh_keypair(&self) -> Result { + match self.get_config_parsed(Config::IrohSecretKey).await? { + Some(key) => Ok(key), + None => { + let key = SecretKey::generate(); + self.set_config(Config::IrohSecretKey, Some(&key.to_string())) + .await?; + Ok(key) + } + } } }