DerivationPath: support 'h' in Display output for hardened components

Aligns with ChildNumber’s format and improves consistency when debugging or comparing derivation paths.

Resolves: #4618
This commit is contained in:
vicjuma 2025-06-14 01:42:01 +03:00
parent 052514e6ff
commit 4284deed29
1 changed files with 17 additions and 2 deletions

View File

@ -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";