Skip to content

Commit 0c63591

Browse files
authored
Merge pull request #110 from pythcoiner/issue_109
ledger: fix change detection logic in Ledger::display_address()
2 parents 76f3ab6 + 0026747 commit 0c63591

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

cli/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub mod command {
6868
{
6969
if let Ok((device, _)) = device.wait_confirm().await {
7070
let mut bb02 = BitBox02::from(device).with_network(network);
71-
if let Some(ref policy) = wallet.as_ref().map(|w| w.policy).flatten() {
71+
if let Some(policy) = wallet.as_ref().and_then(|w| w.policy) {
7272
bb02 = bb02.with_policy(policy)?;
7373
}
7474
hws.push(bb02.into());

src/ledger.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ use ledger_bitcoin_client::{
2626
WalletPolicy, WalletPubKey,
2727
};
2828

29-
use crate::{parse_version, utils, AddressScript, DeviceKind, Error as HWIError, HWI};
29+
use crate::{
30+
parse_version, utils, AddressScript, DeviceKind, Error as HWIError, CHANGE_INDEX, HWI,
31+
RECV_INDEX,
32+
};
3033

3134
pub use hidapi::{DeviceInfo, HidApi};
3235
pub use ledger_bitcoin_client::async_client::Transport;
@@ -111,11 +114,14 @@ impl<T: Transport + Sync + Send> HWI for Ledger<T> {
111114
let wallet =
112115
WalletPolicy::new("".into(), WalletVersion::V2, descriptor_template, keys);
113116

117+
if ![RECV_INDEX, CHANGE_INDEX].contains(&normal_children[0]) {
118+
return Err(HWIError::Bip86ChangeIndex);
119+
}
114120
self.client
115121
.get_wallet_address(
116122
&wallet,
117123
None,
118-
normal_children[0] == ChildNumber::from_normal_idx(0).unwrap(),
124+
normal_children[0] == CHANGE_INDEX,
119125
normal_children[1].into(),
120126
true,
121127
)

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ pub mod utils;
1313

1414
use async_trait::async_trait;
1515
use bitcoin::{
16-
bip32::{DerivationPath, Fingerprint, Xpub},
16+
bip32::{ChildNumber, DerivationPath, Fingerprint, Xpub},
1717
psbt::Psbt,
1818
};
1919

2020
use std::{cmp::Ordering, fmt::Debug, str::FromStr};
2121

22+
const RECV_INDEX: ChildNumber = ChildNumber::Normal { index: 0 };
23+
const CHANGE_INDEX: ChildNumber = ChildNumber::Normal { index: 1 };
24+
2225
#[derive(Debug, Clone)]
2326
pub enum Error {
2427
ParsingPolicy(bip389::ParseError),
@@ -34,6 +37,7 @@ pub enum Error {
3437
Unexpected(&'static str),
3538
UserRefused,
3639
NetworkMismatch,
40+
Bip86ChangeIndex,
3741
}
3842

3943
impl std::fmt::Display for Error {
@@ -52,6 +56,9 @@ impl std::fmt::Display for Error {
5256
Error::Unexpected(e) => write!(f, "{}", e),
5357
Error::UserRefused => write!(f, "User refused operation"),
5458
Error::NetworkMismatch => write!(f, "Device network is different"),
59+
Error::Bip86ChangeIndex => {
60+
write!(f, "Ledger devices only accept 0 or 1 as`change` index value for BIP86 derivation path")
61+
}
5562
}
5663
}
5764
}

0 commit comments

Comments
 (0)