From 4284deed29114c5e31ef7c29e28e352b860f74e7 Mon Sep 17 00:00:00 2001 From: vicjuma Date: Sat, 14 Jun 2025 01:42:01 +0300 Subject: [PATCH] DerivationPath: support 'h' in Display output for hardened components MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Aligns with ChildNumber’s format and improves consistency when debugging or comparing derivation paths. Resolves: #4618 --- bitcoin/src/bip32.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index c1bff9170..08466e3c8 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -482,11 +482,19 @@ impl fmt::Display for DerivationPath { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut iter = self.0.iter(); if let Some(first_element) = iter.next() { - write!(f, "{}", first_element)?; + if f.alternate() { + write!(f, "{:#}", first_element)?; + } else { + write!(f, "{}", first_element)?; + } } for cn in iter { f.write_str("/")?; - write!(f, "{}", cn)?; + if f.alternate() { + write!(f, "{:#}", cn)?; + } else { + write!(f, "{}", cn)?; + } } Ok(()) } @@ -1108,6 +1116,13 @@ mod tests { } } + #[test] + fn test_derivation_path_display() { + let path = DerivationPath::from_str("m/84'/0'/0'/0/0").unwrap(); + assert_eq!(format!("{}", path), "84'/0'/0'/0/0"); + assert_eq!(format!("{:#}", path), "84h/0h/0h/0/0"); + } + #[test] fn parse_derivation_path_out_of_range() { let invalid_path = "2147483648";