From c8a6d4104e01056cdcdb5608fbb34bde045c1080 Mon Sep 17 00:00:00 2001 From: Alex Emmet <111742636+Alex-Emmet@users.noreply.github.com> Date: Sun, 5 Apr 2026 00:27:04 +0200 Subject: [PATCH] ... --- iota-storage/src/util/config_util.rs | 20 ++++++++- iota-util/src/file_util.rs | 9 ---- omikron-connector/src/omikron_connection.rs | 50 ++++++++++----------- 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/iota-storage/src/util/config_util.rs b/iota-storage/src/util/config_util.rs index 0ad7827..6d4082b 100644 --- a/iota-storage/src/util/config_util.rs +++ b/iota-storage/src/util/config_util.rs @@ -19,11 +19,26 @@ impl ConfigUtil { } pub fn clear(&mut self) { self.config = JsonValue::new_object(); + self.unique = false; } pub fn load(&mut self) { let s = load_file("", "config.json"); - if !s.is_empty() { - self.config = json::parse(&s).unwrap_or(JsonValue::new_object()); + if s.is_empty() { + // File might be missing or empty/being written. + // We don't want to wipe the current config if it already has data. + // But if it's the first load, it will stay empty. + return; + } + + match json::parse(&s) { + Ok(parsed) => { + self.config = parsed; + self.unique = false; + } + Err(e) => { + eprintln!("Failed to parse config.json: {}. Content: '{}'", e, s); + // Keep the current config rather than wiping it. + } } } @@ -55,6 +70,7 @@ impl ConfigUtil { pub fn update(&mut self) { if self.unique { save_file("", "config.json", &self.config.to_string()); + self.unique = false; } } } diff --git a/iota-util/src/file_util.rs b/iota-util/src/file_util.rs index 1326feb..b0cd3a3 100755 --- a/iota-util/src/file_util.rs +++ b/iota-util/src/file_util.rs @@ -9,8 +9,6 @@ use uuid::Uuid; use walkdir::WalkDir; use zip::ZipArchive; - - #[allow(dead_code)] pub fn delete_directory(path: &str) -> bool { let dir = Path::new(&get_directory()).join(path); @@ -96,17 +94,10 @@ pub fn load_file(path: &str, name: &str) -> String { let file_path = dir.join(name); if !dir.exists() { - if let Err(e) = fs::create_dir_all(&dir) { - println!("[IMPORTANT] Couldn't create directories: {}", e); - return String::new(); - } return String::new(); } if !file_path.exists() { - if let Err(e) = File::create(&file_path) { - println!("[IMPORTANT] Couldn't create file: {}", e); - } return String::new(); } diff --git a/omikron-connector/src/omikron_connection.rs b/omikron-connector/src/omikron_connection.rs index def2cc6..48da9fb 100755 --- a/omikron-connector/src/omikron_connection.rs +++ b/omikron-connector/src/omikron_connection.rs @@ -1,15 +1,15 @@ +use dashmap::DashMap; +use iota_logger::{log, log_cv_in, log_cv_out, log_t}; +use iota_state::{ACTIVE_TASKS, SHUTDOWN}; use iota_storage::users::contact::Contact; use iota_storage::util::chat_files::{MessageState, change_message_state}; use iota_storage::util::chats_util::{get_user, mod_user}; use iota_storage::util::communities_util::CommunitiesUtil; +use iota_storage::util::config_util::CONFIG; +use iota_storage::util::{chat_files, chats_util}; +use iota_util::crypto_helper; use iota_util::crypto_util::{DataFormat, SecurePayload}; use iota_util::file_util::{get_children, load_file, save_file}; -use iota_storage::util::{chat_files, chats_util}; -use iota_storage::util::config_util::CONFIG; -use iota_util::crypto_helper; -use iota_state::{ACTIVE_TASKS, SHUTDOWN}; -use iota_logger::{log, log_cv_in, log_cv_out, log_t}; -use dashmap::DashMap; use json::JsonValue; use std::collections::HashMap; use std::sync::{Arc, LazyLock}; @@ -225,15 +225,15 @@ impl OmikronConnection { *self.sender.write().await = Some(sender_arc.clone()); *self.state.write().await = ConnectionState::Connected { identified: false }; - // Handle registration/identification - self.handle_authentication().await; - // Start read loop let read_self = self.clone(); let read_handle = tokio::spawn(async move { read_self.read_loop(&mut receiver).await; }); + // Handle registration/identification + self.handle_authentication().await; + // Start heartbeat let heartbeat_self = self.clone(); let heartbeat_handle = tokio::spawn(async move { @@ -280,26 +280,26 @@ impl OmikronConnection { let private_key = conf.get_private_key(); drop(conf); - if iota_id == 0 || public_key.is_none() || private_key.is_none() { + if iota_id == 0 { log_t!("iota_register_new"); - let key_pair = crypto_helper::generate_keypair(); - let public_key_base64 = crypto_helper::public_key_to_base64(&key_pair.public); - let _private_key_base64 = crypto_helper::secret_key_to_base64(&key_pair.secret); + let (pub_k, priv_k) = if let (Some(pk), Some(sk)) = (public_key, private_key) { + (pk, sk) + } else { + let key_pair = crypto_helper::generate_keypair(); + let public_key_base64 = crypto_helper::public_key_to_base64(&key_pair.public); + let private_key_base64 = crypto_helper::secret_key_to_base64(&key_pair.secret); - let mut conf_write = CONFIG.write().await; - // NOTE: - // Intentionally not storing the generated private/public keys directly into the - // config file here to avoid persisting sensitive material in plaintext. If you - // want to persist them, uncomment the two lines below and accept the security - // implications (they will be saved by `conf_write.update()`). - // conf_write.change("public_key", DataValue::Str(public_key_base64.clone())); - // conf_write.change("private_key", DataValue::Str(private_key_base64)); - conf_write.update(); - drop(conf_write); + let mut conf_write = CONFIG.write().await; + conf_write.change("public_key", JsonValue::from(public_key_base64.clone())); + conf_write.change("private_key", JsonValue::from(private_key_base64.clone())); + conf_write.update(); + drop(conf_write); + (public_key_base64, private_key_base64) + }; let register_msg = CommunicationValue::new(CommunicationType::register_iota) - .add_data(DataTypes::public_key, DataValue::Str(public_key_base64)); + .add_data(DataTypes::public_key, DataValue::Str(pub_k)); let msg_id = register_msg.get_id(); @@ -311,7 +311,7 @@ impl OmikronConnection { return false; } - let iota_value = cv.get_data(DataTypes::register_id); + let iota_value = cv.get_data(DataTypes::iota_id); let iota_id = iota_value.as_number().unwrap_or(0); if iota_id != 0 {