Merge rust-bitcoin/rust-bitcoin#7185: Refactor/bip32 flatten error constructors
What changed, and why it matters
This is a minor internal code cleanup in the Rust Bitcoin library. It changes how two functions (Xpriv::decode and Xpub::decode) build their error messages when an input is the wrong length. The behavior is the same before and after; only the style of the code changed. There is no security issue.
No action needed. This is a non-functional refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors error construction in key_expression/src/bip32.rs. Previously, Xpriv::decode and Xpub::decode wrapped a WrongExtendedKeyLength error directly in their respective decode errors inside a closure. Now they first map to XkeyDecodeError::WrongExtendedKeyLength and then map again to the outer XprivDecodeError::Xkey / XpubDecodeError::Xkey variant. This is a purely syntactic change following the project’s error-handling style guide. The produced error value and control flow are unchanged.
Changed components
key_expression/src/bip32.rsInspect captured patch +8 / −6
### key_expression/src/bip32.rs
@@ -982,9 +982,10 @@ impl Xpriv {
/// * [`XprivDecodeError::InvalidPrivateKeyPrefix`] if the private key bytes don't start with zero.
/// * [`XprivDecodeError::Secp256k1`] if the private key bytes are not a valid secp secret key.
pub fn decode(data: &[u8]) -> Result<Self, XprivDecodeError> {
- let data: &[u8; 78] = data.try_into().map_err(|_| {
- XprivDecodeError::Xkey(XkeyDecodeError::WrongExtendedKeyLength(data.len()))
- })?;
+ let data: &[u8; 78] = data
+ .try_into()
+ .map_err(|_| XkeyDecodeError::WrongExtendedKeyLength(data.len()))
+ .map_err(XprivDecodeError::Xkey)?;
Self::from_byte_array(data)
}
@@ -1203,9 +1204,10 @@ impl Xpub {
/// * [`XpubDecodeError::UnknownVersion`] if the decoded network value is not main or testnet.
/// * [`XpubDecodeError::Secp256k1`] if the public key bytes are not a valid secp public key.
pub fn decode(data: &[u8]) -> Result<Self, XpubDecodeError> {
- let data: &[u8; 78] = data.try_into().map_err(|_| {
- XpubDecodeError::Xkey(XkeyDecodeError::WrongExtendedKeyLength(data.len()))
- })?;
+ let data: &[u8; 78] = data
+ .try_into()
+ .map_err(|_| XkeyDecodeError::WrongExtendedKeyLength(data.len()))
+ .map_err(XpubDecodeError::Xkey)?;
Self::from_byte_array(data)
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.