Move/add From<PushBytes> for SerializedSignature
What changed, and why it matters
This commit is a small internal code reorganization. It moves an existing conversion helper (so a serialized ECDSA signature can be viewed as a script 'push bytes' object) to a more central location, and adds an equivalent helper for Taproot signatures. There is no security fix or behavior change visible to users.
No security action required. Treat as a normal refactoring/relocation change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change relocates impl AsRef<PushBytes> for ecdsa::SerializedSignature from bitcoin/src/crypto/ecdsa.rs to bitcoin/src/blockdata/script/push_bytes.rs, and adds a matching impl AsRef<PushBytes> for taproot::SerializedSignature. Both implementations use try_from on the byte slice and unwrap with a length guarantee comment (73 bytes for ECDSA, 65 bytes for Taproot). The old ECDSA implementation used a different slicing approach via &<&PushBytes>::from(&self.data)[..self.len()].
Changed components
bitcoin/src/blockdata/script/push_bytes.rsbitcoin/src/crypto/ecdsa.rsInspect captured patch +15 / −6
diff --git a/bitcoin/src/blockdata/script/push_bytes.rs b/bitcoin/src/blockdata/script/push_bytes.rs
index 386d81e0..dd19edd1 100644
--- a/bitcoin/src/blockdata/script/push_bytes.rs
+++ b/bitcoin/src/blockdata/script/push_bytes.rs
@@ -5,6 +5,7 @@
use core::ops::{Deref, DerefMut};
use core::fmt;
+use crate::crypto::{ecdsa, taproot};
use crate::prelude::{Borrow, BorrowMut};
use crate::script;
@@ -418,6 +419,20 @@ impl BorrowMut<PushBytes> for PushBytesBuf {
fn borrow_mut(&mut self) -> &mut PushBytes { self.as_mut_push_bytes() }
}
+impl AsRef<PushBytes> for ecdsa::SerializedSignature {
+ #[inline]
+ fn as_ref(&self) -> &PushBytes {
+ <&PushBytes>::try_from(<Self as AsRef<[u8]>>::as_ref(self)).expect("max length 73 bytes is valid")
+ }
+}
+
+impl AsRef<PushBytes> for taproot::SerializedSignature {
+ #[inline]
+ fn as_ref(&self) -> &PushBytes {
+ <&PushBytes>::try_from(<Self as AsRef<[u8]>>::as_ref(self)).expect("max length 65 bytes is valid")
+ }
+}
+
/// Possible errors that can arise from [`PushBytes::read_scriptint`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
diff --git a/bitcoin/src/crypto/ecdsa.rs b/bitcoin/src/crypto/ecdsa.rs
index be82535f..8e975626 100644
--- a/bitcoin/src/crypto/ecdsa.rs
+++ b/bitcoin/src/crypto/ecdsa.rs
@@ -15,7 +15,6 @@ use internals::{impl_to_hex_from_lower_hex, write_err};
use io::Write;
use crate::prelude::{DisplayHex, Vec};
-use crate::script::PushBytes;
#[cfg(doc)]
use crate::script::ScriptPubKeyBufExt as _;
use crate::sighash::{EcdsaSighashType, NonStandardSighashTypeError};
@@ -195,11 +194,6 @@ impl AsRef<[u8]> for SerializedSignature {
fn as_ref(&self) -> &[u8] { &self.data[..self.len] }
}
-impl AsRef<PushBytes> for SerializedSignature {
- #[inline]
- fn as_ref(&self) -> &PushBytes { &<&PushBytes>::from(&self.data)[..self.len()] }
-}
-
impl core::borrow::Borrow<[u8]> for SerializedSignature {
#[inline]
fn borrow(&self) -> &[u8] { &self.data[..self.len] }
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.