Merge rust-bitcoin/rust-bitcoin#7083: key_expression: Add Xpriv::from_byte_array and Xpub::from_byte_array
What changed, and why it matters
This commit adds two new helper functions, Xpriv::from_byte_array and Xpub::from_byte_array, for decoding Bitcoin extended keys from a fixed 78-byte array. It also moves the length check out of an internal helper so the new functions can rely on Rust's type system for length safety. The existing decode(slice) API is preserved and still validates length. There is no security bug or vulnerability here—this is a routine, purely additive API improvement with added tests.
No security action needed. This is a benign API enhancement. Normal code-review approval is sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors BIP-32 extended key decoding in key_expression/src/bip32.rs. It introduces public from_byte_array methods on Xpriv and Xpub that accept &[u8; 78], delegates the existing decode(&[u8]) methods to them after a length check, and changes the internal Common::decode to assume a 78-byte input. The behavior is unchanged for callers: wrong-length inputs still return WrongExtendedKeyLength. New unit tests verify parity between decode and from_byte_array and that wrong lengths are rejected.
Changed components
key_expression/src/bip32.rsInspect captured patch +71 / −5
### key_expression/src/bip32.rs
@@ -831,6 +831,18 @@ 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()))
+ })?;
+ Self::from_byte_array(data)
+ }
+
+ /// Decodes an extended private key from its 78-byte BIP-0032 encoding.
+ ///
+ /// # Errors
+ ///
+ /// Same as [`Xpriv::decode`], except that the length is checked by the type system.
+ pub fn from_byte_array(data: &[u8; 78]) -> Result<Self, XprivDecodeError> {
let Common { network, depth, parent_fingerprint, child_number, chain_code, key } =
Common::decode(data).map_err(XprivDecodeError::Xkey)?;
@@ -992,6 +1004,18 @@ 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()))
+ })?;
+ Self::from_byte_array(data)
+ }
+
+ /// Decodes an extended public key from its 78-byte BIP-0032 encoding.
+ ///
+ /// # Errors
+ ///
+ /// Same as [`Xpub::decode`], except that the length is checked by the type system.
+ pub fn from_byte_array(data: &[u8; 78]) -> Result<Self, XpubDecodeError> {
let Common { network, depth, parent_fingerprint, child_number, chain_code, key } =
Common::decode(data).map_err(XpubDecodeError::Xkey)?;
@@ -1105,15 +1129,11 @@ impl Common {
///
/// # Errors
///
- /// * [`XkeyDecodeError::WrongExtendedKeyLength`] if the data is not the correct length.
/// * [`XkeyDecodeError::NonZeroParentFingerprintForMasterKey`] if the depth is 0 and the master key
/// fingerprint is non-zero.
/// * [`XkeyDecodeError::NonZeroChildNumberForMasterKey`] if the depth is 0 and the child number is
/// non-zero.
- fn decode(data: &[u8]) -> Result<Self, XkeyDecodeError> {
- let data: &[u8; 78] =
- data.try_into().map_err(|_| XkeyDecodeError::WrongExtendedKeyLength(data.len()))?;
-
+ fn decode(data: &[u8; 78]) -> Result<Self, XkeyDecodeError> {
let (&network, data) = data.split_array::<4, 74>();
let (&depth, data) = data.split_first::<73>();
let (&parent_fingerprint, data) = data.split_array::<4, 69>();
@@ -2199,6 +2219,52 @@ mod tests {
}
}
+ #[test]
+ fn decode_matches_from_byte_array() {
+ let xpriv = "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi".parse::<Xpriv>().unwrap();
+ let xpub = Xpub::from_xpriv(&xpriv);
+
+ assert_eq!(Xpriv::decode(&xpriv.encode()), Ok(xpriv));
+ assert_eq!(Xpriv::from_byte_array(&xpriv.encode()), Ok(xpriv));
+ assert_eq!(Xpub::decode(&xpub.encode()), Ok(xpub));
+ assert_eq!(Xpub::from_byte_array(&xpub.encode()), Ok(xpub));
+ }
+
+ #[test]
+ fn reject_decode_with_wrong_length() {
+ for len in [0usize, 77, 79] {
+ let data = vec![0u8; len];
+
+ assert_eq!(
+ Xpriv::decode(&data),
+ Err(XprivDecodeError::Xkey(XkeyDecodeError::WrongExtendedKeyLength(len)))
+ );
+ assert_eq!(
+ Xpub::decode(&data),
+ Err(XpubDecodeError::Xkey(XkeyDecodeError::WrongExtendedKeyLength(len)))
+ );
+ }
+ }
+
+ #[test]
+ fn reject_base58_payload_with_wrong_length() {
+ for len in [0usize, 77, 79] {
+ let encoded = base58::Base58CkString::encode(&vec![0u8; len]).unwrap();
+
+ match encoded.as_str().parse::<Xpriv>() {
+ Err(ParseXprivError::InvalidBase58PayloadLength(e)) =>
+ assert_eq!(e.invalid_base58_payload_length(), len),
+ result => panic!("Expected InvalidBase58PayloadLength error, got {:?}", result),
+ }
+
+ match encoded.as_str().parse::<Xpub>() {
+ Err(ParseXpubError::InvalidBase58PayloadLength(e)) =>
+ assert_eq!(e.invalid_base58_payload_length(), len),
+ result => panic!("Expected InvalidBase58PayloadLength error, got {:?}", result),
+ }
+ }
+ }
+
#[test]
fn reject_xpriv_with_zero_depth_and_non_zero_index() {
let result = "xprv9s21ZrQH4r4TsiLvyLXqM9P7k1K3EYhA1kkD6xuquB5i39AU8KF42acDyL3qsDbU9NmZn6MsGSUYZEsuoePmjzsB3eFKSUEh3Gu1N3cqVUN".parse::<Xpriv>();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.