Implement `AsRef<PushBytes>` for generic hashes
What changed, and why it matters
This commit is a small convenience improvement, not a security fix. It lets several common Bitcoin hash types be directly used as 'pushable bytes' in Bitcoin scripts, so developers can write slightly cleaner code when building transactions. There is no vulnerability being patched.
No security action required. Treat as a normal API-convenience commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change adds AsRef<PushBytes> implementations for ripemd160::Hash, hash160::Hash, sha1::Hash, sha256::Hash, and sha256d::Hash in rust-bitcoin. It also relaxes the internal impl_asref_push_bytes! macro to accept arbitrary type tokens (ty) instead of only identifiers. Functionally equivalent conversions already existed via &[u8; N], so this is purely ergonomic and signals intended usage. No unsafe code, no boundary checks removed, no bug fixed.
Changed components
bitcoin/src/blockdata/script/push_bytes.rsbitcoin/src/internal_macros.rsInspect captured patch +10 / −1
diff --git a/bitcoin/src/blockdata/script/push_bytes.rs b/bitcoin/src/blockdata/script/push_bytes.rs
index 8591268f..43265c11 100644
--- a/bitcoin/src/blockdata/script/push_bytes.rs
+++ b/bitcoin/src/blockdata/script/push_bytes.rs
@@ -6,6 +6,7 @@ use core::fmt;
use core::ops::{Deref, DerefMut};
use crate::crypto::{ecdsa, taproot};
+use crate::internal_macros::impl_asref_push_bytes;
use crate::prelude::{Borrow, BorrowMut};
use crate::script;
@@ -436,6 +437,14 @@ impl AsRef<PushBytes> for taproot::SerializedSignature {
}
}
+impl_asref_push_bytes! {
+ hashes::ripemd160::Hash,
+ hashes::hash160::Hash,
+ hashes::sha1::Hash,
+ hashes::sha256::Hash,
+ hashes::sha256d::Hash,
+}
+
/// Possible errors that can arise from [`PushBytes::read_scriptint`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
diff --git a/bitcoin/src/internal_macros.rs b/bitcoin/src/internal_macros.rs
index d3fd16d6..8c614c6f 100644
--- a/bitcoin/src/internal_macros.rs
+++ b/bitcoin/src/internal_macros.rs
@@ -48,7 +48,7 @@ include!("../include/array_newtype.rs");
#[rustfmt::skip]
macro_rules! impl_asref_push_bytes {
- ($($hashtype:ident),*) => {
+ ($($hashtype:ty),* $(,)?) => {
$(
impl AsRef<$crate::script::PushBytes> for $hashtype {
fn as_ref(&self) -> &$crate::script::PushBytes {
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.