Merge rust-bitcoin/rust-bitcoin#7220: refactor(bip32)!: replace KeySource with KeyOrigin
What changed, and why it matters
This is a routine code cleanup in a Bitcoin library. It renames a simple data container from KeySource to KeyOrigin and gives it named fields (fingerprint and path) instead of numbered tuple slots (.0 and .1). The serialized format is intentionally kept identical, so existing saved data should still load. There is no security bug being fixed here.
No security action required. Treat as a normal API-breaking release note for downstream consumers updating from KeySource to KeyOrigin.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit replaces the type alias pub type KeySource = (Fingerprint, RelativeDerivationPath) with a named struct KeyOrigin { fingerprint, path }. It adds explicit serde Serialize/Deserialize implementations that preserve the previous tuple encoding, plus an Arbitrary impl and a regression test verifying tuple-compatible JSON and bincode serialization. This is a breaking API refactor in preparation for BIP-0380; no vulnerability is addressed.
Changed components
key_expression/src/bip32.rsbitcoin/CHANGELOG.mdkey_expression/CHANGELOG.mdInspect captured patch +84 / −3
### bitcoin/CHANGELOG.md
@@ -2,6 +2,9 @@
## [Unreleased]
+- BREAKING: Remove `bip32::KeySource` in favor of `bip32::KeyOrigin`. Replace `(fingerprint, path)`
+ construction with `KeyOrigin { fingerprint, path }` and `.0`/`.1` access with `.fingerprint`/`.path`.
+ The serde tuple representation is unchanged.
- `pow`'s internal `U256` helper (used only by `U256Wrapper` byte-array conversions) is now sourced
from `bitcoin-internals` instead of a duplicated `include!` module.
- BREAKING: `bip32::Xpriv`/`Xpub` fields are private; use metadata getters, checked `from_parts`
### key_expression/CHANGELOG.md
@@ -2,6 +2,9 @@
## [Unreleased]
+- BREAKING: Remove `bip32::KeySource` in favor of `bip32::KeyOrigin`. Replace `(fingerprint, path)`
+ construction with `KeyOrigin { fingerprint, path }` and `.0`/`.1` access with `.fingerprint`/`.path`.
+ The serde tuple representation is unchanged.
- Make `Xpriv` debug formatting redact the private-key field consistently with and without `std`.
- Add checked `XprivParts`/`XpubParts` construction, xkey metadata getters, and a `secp256k1` crate re-export.
- BREAKING: Make `Xpriv`/`Xpub` fields private; use metadata getters, checked `from_parts` construction,
### key_expression/src/bip32.rs
@@ -828,10 +828,55 @@ impl fmt::Debug for RelativeDerivationPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self, f) }
}
-/// Full information on the used extended public key: fingerprint of the
-/// master extended public key and a derivation path from it.
+/// A key's origin: the master key fingerprint and the relative derivation path from it.
+///
+/// The fingerprint is an identifier, not authenticated proof of ancestry. The path has no `m`
+/// prefix; an empty path is valid and refers to the master key itself.
+///
+/// With `serde`, this uses the same `(fingerprint, path)` tuple representation as the former
+/// `KeySource` alias in both human-readable and binary formats, not a named-field object.
+///
+/// ```
+/// use bitcoin_key_expression::bip32::KeyOrigin;
+///
+/// let origin = KeyOrigin {
+/// fingerprint: "d34db33f".parse().unwrap(),
+/// path: "84'/0'/0'/0/1".parse().unwrap(),
+/// };
+/// assert_eq!(origin.path.len(), 5);
+/// ```
+#[cfg(feature = "alloc")]
+#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct KeyOrigin {
+ /// The master key's fingerprint.
+ pub fingerprint: Fingerprint,
+ /// The relative derivation path from the master key.
+ pub path: RelativeDerivationPath,
+}
+
#[cfg(feature = "alloc")]
-pub type KeySource = (Fingerprint, RelativeDerivationPath);
+#[cfg(feature = "serde")]
+impl serde::Serialize for KeyOrigin {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ (&self.fingerprint, &self.path).serialize(serializer)
+ }
+}
+
+#[cfg(feature = "alloc")]
+#[cfg(feature = "serde")]
+impl<'de> serde::Deserialize<'de> for KeyOrigin {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ let (fingerprint, path) =
+ <(Fingerprint, RelativeDerivationPath)>::deserialize(deserializer)?;
+ Ok(Self { fingerprint, path })
+ }
+}
impl Xpriv {
/// Constructs an extended private key from its components.
@@ -1881,6 +1926,14 @@ impl<'a> Arbitrary<'a> for AbsoluteDerivationPath {
}
}
+#[cfg(feature = "alloc")]
+#[cfg(feature = "arbitrary")]
+impl<'a> Arbitrary<'a> for KeyOrigin {
+ fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
+ Ok(Self { fingerprint: u.arbitrary()?, path: u.arbitrary()? })
+ }
+}
+
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for Fingerprint {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
@@ -2688,6 +2741,28 @@ mod tests {
serde_round_trip!("m/0'/1".parse::<AbsoluteDerivationPath>().unwrap());
}
+ #[test]
+ #[cfg(feature = "serde")]
+ fn key_origin_serde_tuple_compatibility() {
+ for path in ["", "84'/0/1'"] {
+ let tuple = (
+ Fingerprint::from([1u8, 2, 3, 42]),
+ path.parse::<RelativeDerivationPath>().unwrap(),
+ );
+ let origin = KeyOrigin { fingerprint: tuple.0, path: tuple.1.clone() };
+
+ let tuple_json = serde_json::to_string(&tuple).unwrap();
+ let origin_json = serde_json::to_string(&origin).unwrap();
+ assert_eq!(tuple_json, origin_json);
+ assert_eq!(serde_json::from_str::<KeyOrigin>(&tuple_json).unwrap(), origin);
+
+ let tuple_bytes = bincode::serialize(&tuple).unwrap();
+ let origin_bytes = bincode::serialize(&origin).unwrap();
+ assert_eq!(tuple_bytes, origin_bytes);
+ assert_eq!(bincode::deserialize::<KeyOrigin>(&tuple_bytes).unwrap(), origin);
+ }
+ }
+
#[test]
#[cfg(feature = "serde")]
pub fn encode_fingerprint_chaincode() {Why this scored 19/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.