From 02e28bbd3e72ae5aacde38552ac5dc892639a20e Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Sat, 8 Mar 2025 19:10:51 +0100 Subject: [PATCH] gui: implement Daemon.update_wallet_metadata() for lianad --- liana-gui/src/app/settings.rs | 19 +++++++++++++++++++ liana-gui/src/daemon/mod.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/liana-gui/src/app/settings.rs b/liana-gui/src/app/settings.rs index 27471b5b..7657de98 100644 --- a/liana-gui/src/app/settings.rs +++ b/liana-gui/src/app/settings.rs @@ -107,6 +107,25 @@ impl WalletSetting { } map } + + pub fn update_alias(&mut self, key: &Fingerprint, alias: &str) { + let key_aliases = self.keys_aliases(); + if key_aliases.contains_key(key) { + self.keys = self + .keys + .clone() + .into_iter() + .map(|mut ks| { + if ks.master_fingerprint == *key { + ks.name = alias.into(); + ks + } else { + ks + } + }) + .collect(); + } + } } #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)] diff --git a/liana-gui/src/daemon/mod.rs b/liana-gui/src/daemon/mod.rs index 300fc918..e2706271 100644 --- a/liana-gui/src/daemon/mod.rs +++ b/liana-gui/src/daemon/mod.rs @@ -21,6 +21,7 @@ use lianad::{ StartupError, }; +use crate::app::settings::Settings; use crate::{hw::HardwareWalletConfig, node}; #[derive(Debug)] @@ -364,12 +365,33 @@ pub trait Daemon: Debug { Ok(events) } - /// Implemented by LianaLite backend + /// Reimplemented by LianaLite backend async fn update_wallet_metadata( &self, - _fingerprint_aliases: &HashMap, + fingerprint_aliases: &HashMap, _hws: &[HardwareWalletConfig], ) -> Result<(), DaemonError> { + if let Some(datadir) = self + .config() + .ok_or(DaemonError::Unexpected("Config missing".into()))? + .data_dir() + { + let network = self.get_info().await?.network; + let mut settings = Settings::from_file(datadir, network) + .map_err(|_| DaemonError::Unexpected("Fail to read Settings from file".into()))?; + let wallet = if settings.wallets.len() == 1 { + settings.wallets.get_mut(0).expect("already checked") + } else { + return Err(DaemonError::Unexpected( + "Settings file contains more than one wallet".into(), + )); + }; + for fg in wallet.keys_aliases().keys() { + if fingerprint_aliases.contains_key(fg) { + wallet.update_alias(fg, fingerprint_aliases.get(fg).expect("checked")); + } + } + } Ok(()) } }