Skip to content

Commit

Permalink
hw: check if app is open before sending old version error message
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
edouardparis committed Sep 20, 2024
1 parent 6e55191 commit 7273542
Showing 1 changed file with 79 additions and 99 deletions.
178 changes: 79 additions & 99 deletions gui/src/hw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -659,57 +623,21 @@ async fn refresh(mut state: State) -> (HardwareWalletMessage, State) {
still.push(id);
continue;
}

match ledger::Ledger::<ledger::TransportHID>::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) => {}
Expand Down Expand Up @@ -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<T>,
wallet: Option<&'a Wallet>,
keys_aliases: &'a HashMap<Fingerprint, String>,
) -> Result<HardwareWallet, HWIError> {
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,
Expand Down

0 comments on commit 7273542

Please sign in to comment.