From 727354254680b9ac025c5a44580f1cda9fa9f371 Mon Sep 17 00:00:00 2001 From: edouardparis Date: Fri, 20 Sep 2024 13:26:58 +0200 Subject: [PATCH] hw: check if app is open before sending old version error message We check if we are able to fetch master fingerprint to see if the bitcoin app is open. The error is then displayed with the generic connection error message. --- gui/src/hw.rs | 178 ++++++++++++++++++++++---------------------------- 1 file changed, 79 insertions(+), 99 deletions(-) diff --git a/gui/src/hw.rs b/gui/src/hw.rs index eb21c1a45..cebbb27b6 100644 --- a/gui/src/hw.rs +++ b/gui/src/hw.rs @@ -24,6 +24,7 @@ pub enum UnsupportedReason { Method(&'static str), NotPartOfWallet(Fingerprint), WrongNetwork, + AppIsNotOpen, } // Todo drop the Clone, to remove the Mutex on HardwareWallet::Locked @@ -488,61 +489,24 @@ async fn refresh(mut state: State) -> (HardwareWalletMessage, State) { } match ledger::LedgerSimulator::try_connect().await { - Ok(mut device) => { + Ok(device) => { let id = "ledger-simulator".to_string(); if state.connected_supported_hws.contains(&id) { still.push(id); } else { - match device.get_master_fingerprint().await { - Ok(fingerprint) => { - let version = device.get_version().await.ok(); - if ledger_version_supported(version.as_ref()) { - let mut registered = false; - if let Some(w) = &state.wallet { - if let Some(cfg) = w - .hardware_wallets - .iter() - .find(|cfg| cfg.fingerprint == fingerprint) - { - device = device - .with_wallet( - &w.name, - &w.main_descriptor.to_string(), - Some(cfg.token()), - ) - .expect("Configuration must be correct"); - registered = true; - } - } - hws.push(HardwareWallet::Supported { - id, - kind: device.device_kind(), - fingerprint, - device: Arc::new(device), - version, - registered: Some(registered), - alias: state.keys_aliases.get(&fingerprint).cloned(), - }); - } else { - hws.push(HardwareWallet::Unsupported { - id, - kind: device.device_kind(), - version, - reason: UnsupportedReason::Version { - minimal_supported_version: "2.1.0", - }, - }); - } + match handle_ledger_device( + id, + device, + state.wallet.as_ref().map(|w| w.as_ref()), + &state.keys_aliases, + ) + .await + { + Ok(hw) => { + hws.push(hw); } - Err(_) => { - hws.push(HardwareWallet::Unsupported { - id, - kind: device.device_kind(), - version: None, - reason: UnsupportedReason::Version { - minimal_supported_version: "2.1.0", - }, - }); + Err(e) => { + warn!("{:?}", e); } } } @@ -659,57 +623,21 @@ async fn refresh(mut state: State) -> (HardwareWalletMessage, State) { still.push(id); continue; } + match ledger::Ledger::::connect(api, detected) { - Ok(mut device) => match device.get_master_fingerprint().await { - Ok(fingerprint) => { - let version = device.get_version().await.ok(); - if ledger_version_supported(version.as_ref()) { - let mut registered = false; - if let Some(w) = &state.wallet { - if let Some(cfg) = w - .hardware_wallets - .iter() - .find(|cfg| cfg.fingerprint == fingerprint) - { - device = device - .with_wallet( - &w.name, - &w.main_descriptor.to_string(), - Some(cfg.token()), - ) - .expect("Configuration must be correct"); - registered = true; - } - } - hws.push(HardwareWallet::Supported { - id, - kind: device.device_kind(), - fingerprint, - device: Arc::new(device), - version, - registered: Some(registered), - alias: state.keys_aliases.get(&fingerprint).cloned(), - }); - } else { - hws.push(HardwareWallet::Unsupported { - id, - kind: device.device_kind(), - version, - reason: UnsupportedReason::Version { - minimal_supported_version: "2.1.0", - }, - }); - } + Ok(device) => match handle_ledger_device( + id, + device, + state.wallet.as_ref().map(|w| w.as_ref()), + &state.keys_aliases, + ) + .await + { + Ok(hw) => { + hws.push(hw); } - Err(_) => { - hws.push(HardwareWallet::Unsupported { - id, - kind: device.device_kind(), - version: None, - reason: UnsupportedReason::Version { - minimal_supported_version: "2.1.0", - }, - }); + Err(e) => { + warn!("{:?}", e); } }, Err(HWIError::DeviceNotFound) => {} @@ -757,6 +685,58 @@ async fn refresh(mut state: State) -> (HardwareWalletMessage, State) { ) } +async fn handle_ledger_device<'a, T: async_hwi::ledger::Transport + Sync + Send + 'static>( + id: String, + mut device: ledger::Ledger, + wallet: Option<&'a Wallet>, + keys_aliases: &'a HashMap, +) -> Result { + match device.get_master_fingerprint().await { + Ok(fingerprint) => { + let version = device.get_version().await.ok(); + if ledger_version_supported(version.as_ref()) { + let mut registered = false; + if let Some(w) = &wallet { + if let Some(cfg) = w + .hardware_wallets + .iter() + .find(|cfg| cfg.fingerprint == fingerprint) + { + device = device + .with_wallet(&w.name, &w.main_descriptor.to_string(), Some(cfg.token())) + .expect("Configuration must be correct"); + registered = true; + } + } + Ok(HardwareWallet::Supported { + id, + kind: device.device_kind(), + fingerprint, + device: Arc::new(device), + version, + registered: Some(registered), + alias: keys_aliases.get(&fingerprint).cloned(), + }) + } else { + Ok(HardwareWallet::Unsupported { + id, + kind: device.device_kind(), + version, + reason: UnsupportedReason::Version { + minimal_supported_version: "2.1.0", + }, + }) + } + } + Err(_) => Ok(HardwareWallet::Unsupported { + id, + kind: device.device_kind(), + version: None, + reason: UnsupportedReason::AppIsNotOpen, + }), + } +} + async fn handle_jade_device( id: String, network: Network,