Remove dependence on PushBytes for SerializedLegacyPublicKey
What changed, and why it matters
This is a small internal code cleanup in the rust-bitcoin library. It changes how one public-key wrapper type exposes its underlying byte slice, removing a dependency on another type (PushBytes) so the code can be moved between crates more easily. There is no user-visible behavior change and no security fix.
No security action required. Treat as a normal refactoring/reorganization commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors SerializedLegacyPublicKey in bitcoin/src/crypto/key.rs. Previously, Deref<Target=[u8]> was implemented outside the encapsulating module and relied on Borrow<PushBytes>, which in turn required the PushBytes type from bitcoin. The patch moves the Deref implementation into the serialized_legacy_public_key module, makes [u8] the primary reference target, and re-implements Borrow<PushBytes> and AsRef<PushBytes> on top of Deref. The observable trait behavior remains equivalent: the type still dereferences to [u8], can be borrowed as [u8], and can be borrowed/as-ref’d as PushBytes via a fallible conversion that is asserted to succeed.
Changed components
bitcoin/src/crypto/key.rsSerializedLegacyPublicKeyInspect captured patch +14 / −26
diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs
index a6af6021..cffb3c1d 100644
--- a/bitcoin/src/crypto/key.rs
+++ b/bitcoin/src/crypto/key.rs
@@ -6,6 +6,7 @@
//! (de)serialized.
use core::borrow::Borrow;
+use core::ops::Deref;
use crate::internal_macros::define_extension_trait;
use crate::script::{self, PushBytes, WitnessScriptBuf};
@@ -32,8 +33,6 @@ pub use serialized_legacy_public_key::SerializedLegacyPublicKey;
mod serialized_legacy_public_key {
use internals::array_vec::ArrayVec;
- use crate::script::PushBytes;
-
/// A serialized form of `LegacyPublicKey`.
///
/// This type contains the legacy public key in serialized as either compressed or
@@ -53,39 +52,28 @@ mod serialized_legacy_public_key {
}
}
- // Keep the proof close to the type definition
- impl core::borrow::Borrow<PushBytes> for SerializedLegacyPublicKey {
- fn borrow(&self) -> &PushBytes {
- <&PushBytes>::try_from(&*self.0).expect("65 <= u32::MAX")
- }
+ impl core::ops::Deref for SerializedLegacyPublicKey {
+ type Target = [u8];
+
+ #[inline]
+ fn deref(&self) -> &Self::Target { &self.0 }
}
}
-impl core::ops::Deref for SerializedLegacyPublicKey {
- type Target = [u8];
-
- #[inline]
- fn deref(&self) -> &Self::Target {
- <Self as Borrow<PushBytes>>::borrow(self).as_bytes()
- }
+impl AsRef<[u8]> for SerializedLegacyPublicKey {
+ fn as_ref(&self) -> &[u8] { self }
}
-impl AsRef<PushBytes> for SerializedLegacyPublicKey {
- fn as_ref(&self) -> &PushBytes {
- self.borrow()
- }
+impl Borrow<[u8]> for SerializedLegacyPublicKey {
+ fn borrow(&self) -> &[u8] { self }
}
-impl AsRef<[u8]> for SerializedLegacyPublicKey {
- fn as_ref(&self) -> &[u8] {
- self
- }
+impl AsRef<PushBytes> for SerializedLegacyPublicKey {
+ fn as_ref(&self) -> &PushBytes { self.borrow() }
}
-impl Borrow<[u8]> for SerializedLegacyPublicKey {
- fn borrow(&self) -> &[u8] {
- self
- }
+impl Borrow<PushBytes> for SerializedLegacyPublicKey {
+ fn borrow(&self) -> &PushBytes { <&PushBytes>::try_from(self.deref()).expect("65 <= u32::MAX") }
}
#[deprecated(since = "TBD", note = "use `LegacyPublicKey` instead")]
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.