Bring back code to persist iroh secret

This commit is contained in:
adz
2024-03-11 13:35:48 +01:00
parent 6d8dcdb40d
commit dae6d6e450

View File

@@ -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<SecretKey> {
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)
}
}
}
}