Add with_compressedness to LegacyPublicKey
What changed, and why it matters
This commit adds a small convenience method called `with_compressedness` to a public-key type in the rust-bitcoin library. It lets callers flip whether a Bitcoin public key is stored in compressed or uncompressed form without having to reach into lower-level secp256k1 cryptography types. The change is purely an API usability improvement and does not fix any bug or vulnerability.
No security action required. Treat as a normal feature/API improvement.
Security signals we found
No security-relevant change identified
API ergonomics / developer-experience improvement only
No validation, parsing, or cryptographic logic modified
Evidence from the diff
The patch introduces LegacyPublicKey::with_compressedness(self, compressed: bool) -> Self, which returns a new LegacyPublicKey sharing the same inner secp256k1::PublicKey but with the requested compressed flag. Existing test code is updated to use the new helper instead of LegacyPublicKey::from_secp_uncompressed(...). There is no change to serialization, validation, or cryptographic behavior.
Changed components
rust-bitcoin crypto cratecrypto/src/key.rsLegacyPublicKey typeInspect captured patch +12 / −3
diff --git a/crypto/src/key.rs b/crypto/src/key.rs
index 63f05510..c50abd3a 100644
--- a/crypto/src/key.rs
+++ b/crypto/src/key.rs
@@ -124,6 +124,15 @@ mod encapsulate {
Self { compressed: false, inner: key.into() }
}
+ /// Sets the compressedness of this [`LegacyPublicKey`].
+ ///
+ /// This returns a new `LegacyPublicKey` with the same inner value, but the given
+ /// compressedness.
+ #[must_use]
+ pub fn with_compressedness(self, compressed: bool) -> Self {
+ Self { compressed, inner: self.to_inner() }
+ }
+
/// Returns the inner secp256k1 public key.
#[inline]
pub fn to_inner(self) -> secp256k1::PublicKey { self.inner }
@@ -1852,7 +1861,7 @@ mod tests {
let wk = KEY_WIF.parse::<WifKey>().unwrap();
let pk = LegacyPublicKey::from_private_key(&wk.private_key);
- let pk_u = LegacyPublicKey::from_secp_uncompressed(pk.to_inner());
+ let pk_u = pk.with_compressedness(false);
assert_tokens(&wk, &[Token::BorrowedStr(KEY_WIF)]);
assert_tokens(&pk.compact(), &[Token::BorrowedBytes(&PK_BYTES[..])]);
@@ -1866,7 +1875,7 @@ mod tests {
let key1 = "02ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8"
.parse::<LegacyPublicKey>()
.unwrap();
- let key2 = LegacyPublicKey::from_secp_uncompressed(key1.to_inner());
+ let key2 = key1.with_compressedness(false);
let arrayvec1 = ArrayVec::from_slice(
&hex::decode_to_array::<33>(
"02ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8",
@@ -2190,7 +2199,7 @@ mod tests {
let deser = LegacyPublicKey::from_slice(serialized).unwrap();
assert_eq!(deser, key);
- let key = LegacyPublicKey::from_secp_uncompressed(key.to_inner());
+ let key = key.with_compressedness(false);
let serialized = &key.to_bytes();
assert_eq!(serialized.len(), 65);
let deser = LegacyPublicKey::from_slice(serialized).unwrap();
Why this scored 18/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.