Skip to content

Commit

Permalink
Merge #1557: move the unlocking of bitbox to a method
Browse files Browse the repository at this point in the history
3ced1db move the unlocking of bb to a method (edouardparis)

Pull request description:

  This is preparatory work for #1550

ACKs for top commit:
  edouardparis:
    Self-ACK 3ced1db

Tree-SHA512: 1ff5aef26ed13446750a5f1c339d47b35a15227df98bb57321f81520294f35fd23fd226f3ccf515a56f07b8ec5b4f1d9c0fcb854f743d862389ab4605afcd3df
  • Loading branch information
edouardparis committed Jan 28, 2025
2 parents 3c8e412 + 3ced1db commit 71959b4
Showing 1 changed file with 60 additions and 49 deletions.
109 changes: 60 additions & 49 deletions liana-gui/src/hw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,56 +235,16 @@ impl HardwareWallets {
None => {}
Some(LockedDevice::BitBox02(bb)) => {
let id = id.to_string();
let id_cloned = id.clone();
let network = self.network;
let wallet = self.wallet.clone();
cmds.push(Command::perform(
async move {
let paired_bb = bb.wait_confirm().await?;
let mut bitbox2 =
BitBox02::from(paired_bb).with_network(network);
let fingerprint =
bitbox2.get_master_fingerprint().await?;
let mut registered = false;
let version = bitbox2.get_version().await.ok();
if let Some(wallet) = &wallet {
let desc = wallet.main_descriptor.to_string();
bitbox2 = bitbox2.with_policy(&desc)?;
registered =
bitbox2.is_policy_registered(&desc).await?;
if wallet.descriptor_keys().contains(&fingerprint) {
Ok(HardwareWallet::Supported {
id: id.clone(),
kind: DeviceKind::BitBox02,
fingerprint,
device: bitbox2.into(),
version,
registered: Some(registered),
alias: None,
})
} else {
Ok(HardwareWallet::Unsupported {
id: id.clone(),
kind: DeviceKind::BitBox02,
version,
reason: UnsupportedReason::NotPartOfWallet(
fingerprint,
),
})
}
} else {
Ok(HardwareWallet::Supported {
id: id.clone(),
kind: DeviceKind::BitBox02,
fingerprint,
device: bitbox2.into(),
version,
registered: Some(registered),
alias: None,
})
}
(
id.clone(),
unlock_bitbox(id, network, bb, wallet).await,
)
},
|res| HardwareWalletMessage::Unlocked(id_cloned, res),
|(id, res)| HardwareWalletMessage::Unlocked(id, res),
));
}
Some(LockedDevice::Jade(device)) => {
Expand All @@ -294,17 +254,22 @@ impl HardwareWallets {
let wallet = self.wallet.clone();
cmds.push(Command::perform(
async move {
device.auth().await?;
handle_jade_device(
if let Err(e) = device.auth().await {
return (id_cloned, Err(e.into()));
}
let res = handle_jade_device(
id,
network,
device,
wallet.as_ref().map(|w| w.as_ref()),
None,
)
.await
.await;
(id_cloned, res)
},
|(id_cloned, res)| {
HardwareWalletMessage::Unlocked(id_cloned, res)
},
|res| HardwareWalletMessage::Unlocked(id_cloned, res),
));
}
}
Expand Down Expand Up @@ -363,6 +328,52 @@ impl HardwareWallets {
}
}

async fn unlock_bitbox(
id: String,
network: Network,
bb: Box<PairingBitbox02<runtime::TokioRuntime>>,
wallet: Option<Arc<Wallet>>,
) -> Result<HardwareWallet, async_hwi::Error> {
let paired_bb = bb.wait_confirm().await?;
let mut bitbox2 = BitBox02::from(paired_bb).with_network(network);
let fingerprint = bitbox2.get_master_fingerprint().await?;
let mut registered = false;
let version = bitbox2.get_version().await.ok();
if let Some(wallet) = &wallet {
let desc = wallet.main_descriptor.to_string();
bitbox2 = bitbox2.with_policy(&desc)?;
registered = bitbox2.is_policy_registered(&desc).await?;
if wallet.descriptor_keys().contains(&fingerprint) {
Ok(HardwareWallet::Supported {
id: id.clone(),
kind: DeviceKind::BitBox02,
fingerprint,
device: bitbox2.into(),
version,
registered: Some(registered),
alias: None,
})
} else {
Ok(HardwareWallet::Unsupported {
id: id.clone(),
kind: DeviceKind::BitBox02,
version,
reason: UnsupportedReason::NotPartOfWallet(fingerprint),
})
}
} else {
Ok(HardwareWallet::Supported {
id: id.clone(),
kind: DeviceKind::BitBox02,
fingerprint,
device: bitbox2.into(),
version,
registered: Some(registered),
alias: None,
})
}
}

struct State {
network: Network,
keys_aliases: HashMap<Fingerprint, String>,
Expand Down

0 comments on commit 71959b4

Please sign in to comment.