Skip to content

Commit 4284dee

Browse files
committed
DerivationPath: support 'h' in Display output for hardened components
Aligns with ChildNumber’s format and improves consistency when debugging or comparing derivation paths. Resolves: rust-bitcoin#4618
1 parent 052514e commit 4284dee

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

bitcoin/src/bip32.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,19 @@ impl fmt::Display for DerivationPath {
482482
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
483483
let mut iter = self.0.iter();
484484
if let Some(first_element) = iter.next() {
485-
write!(f, "{}", first_element)?;
485+
if f.alternate() {
486+
write!(f, "{:#}", first_element)?;
487+
} else {
488+
write!(f, "{}", first_element)?;
489+
}
486490
}
487491
for cn in iter {
488492
f.write_str("/")?;
489-
write!(f, "{}", cn)?;
493+
if f.alternate() {
494+
write!(f, "{:#}", cn)?;
495+
} else {
496+
write!(f, "{}", cn)?;
497+
}
490498
}
491499
Ok(())
492500
}
@@ -1108,6 +1116,13 @@ mod tests {
11081116
}
11091117
}
11101118

1119+
#[test]
1120+
fn test_derivation_path_display() {
1121+
let path = DerivationPath::from_str("m/84'/0'/0'/0/0").unwrap();
1122+
assert_eq!(format!("{}", path), "84'/0'/0'/0/0");
1123+
assert_eq!(format!("{:#}", path), "84h/0h/0h/0/0");
1124+
}
1125+
11111126
#[test]
11121127
fn parse_derivation_path_out_of_range() {
11131128
let invalid_path = "2147483648";

0 commit comments

Comments
 (0)