Merge #984: descriptors: fix derived key parsing and formatting

e9bd8805e5917b05c483db5ba6b7ff34d98dc36d descriptors: fix derived key parsing and formatting (Antoine Poinsot)

Pull request description:

  This was untested, what could go wrong? Well, it was wrong of course!

ACKs for top commit:
  darosior:
    self-ACK e9bd8805e5917b05c483db5ba6b7ff34d98dc36d

Tree-SHA512: 87fb1c84d5b03e59970183882dd63020687011eb5d08e1e31bad04609bdaef0c5a6bf0b5e63be3f2248dd35249cde7d43c83b7c19415cde31bd0a08ec46315be
This commit is contained in:
Antoine Poinsot 2024-02-27 14:19:46 +01:00
commit 0f35a88a94
No known key found for this signature in database
GPG Key ID: E13FC145CD3F4304

View File

@ -41,7 +41,9 @@ impl fmt::Display for DerivedPublicKey {
for byte in fingerprint.as_bytes().iter() {
write!(f, "{:02x}", byte)?;
}
write!(f, "/{}", deriv_path)?;
for child in deriv_path {
write!(f, "/{}", child)?;
}
write!(f, "]{}", self.key)
}
}
@ -78,7 +80,10 @@ impl str::FromStr for DerivedPublicKey {
}
let fingerprint = bip32::Fingerprint::from_str(&fg_deriv[..8])
.map_err(|_| DescKeyError::DerivedKeyParsing)?;
let deriv_path = bip32::DerivationPath::from_str(&fg_deriv[9..])
let deriv_path = fg_deriv[9..]
.split('/')
.map(bip32::ChildNumber::from_str)
.collect::<Result<bip32::DerivationPath, _>>()
.map_err(|_| DescKeyError::DerivedKeyParsing)?;
if deriv_path.into_iter().any(bip32::ChildNumber::is_hardened) {
return Err(DescKeyError::DerivedKeyParsing);
@ -134,3 +139,17 @@ impl ToPublicKey for DerivedPublicKey {
*hash
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
#[test]
fn derived_pubkey_roundtrip() {
let der_pub_str =
"[7c461e5d/0/42]03cd3dc23adaab61731285f8f7bf2f85150bb7c0a379aea48fad5bc82c35e771a2";
let der_pub = DerivedPublicKey::from_str(der_pub_str).unwrap();
assert_eq!(der_pub.to_string(), der_pub_str);
}
}