Move is_op_return and new_op_return to primitives
What changed, and why it matters
This commit is a simple code reorganization: two helper functions for working with OP_RETURN outputs are moved from one internal module to another. The actual behavior of the functions is unchanged, and there is no security fix or vulnerability introduced.
No action required; this is a benign refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit moves is_op_return and new_op_return from extension traits in bitcoin/src/blockdata/script/ to inherent impls in primitives/src/script/. The implementations are copied verbatim, imports are updated, and a now-unused extension trait import is removed from bip158.rs. This is a pure refactor with no functional change.
Changed components
bitcoin/src/blockdata/script/borrowed.rsbitcoin/src/blockdata/script/owned.rsprimitives/src/script/borrowed.rsprimitives/src/script/owned.rsbitcoin/src/bip158.rsInspect captured patch +17 / −17
diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs
index 8a2b515b..4731f155 100644
--- a/bitcoin/src/bip158.rs
+++ b/bitcoin/src/bip158.rs
@@ -46,7 +46,7 @@ use io::{BufRead, Write};
use crate::block::{Block, BlockHash, Checked};
use crate::encoding::{CompactSizeEncoder, CompactSizeU64Decoder, ExactSizeEncoder as _};
use crate::prelude::{BTreeSet, Borrow, Vec};
-use crate::script::{ScriptPubKey, ScriptPubKeyExt as _};
+use crate::script::ScriptPubKey;
use crate::transaction::OutPoint;
use crate::ToU64 as _;
diff --git a/bitcoin/src/blockdata/script/borrowed.rs b/bitcoin/src/blockdata/script/borrowed.rs
index 84cf2b67..f3ee64c3 100644
--- a/bitcoin/src/blockdata/script/borrowed.rs
+++ b/bitcoin/src/blockdata/script/borrowed.rs
@@ -287,15 +287,6 @@ internal_macros::define_extension_trait! {
&& self.as_bytes()[1] == OP_PUSHBYTES_32.to_u8()
}
- /// Check if this is a consensus-valid OP_RETURN output.
- ///
- /// To validate if the OP_RETURN obeys Bitcoin Core's current standardness policy, use
- /// [`is_standard_op_return()`](Self::is_standard_op_return) instead.
- #[inline]
- fn is_op_return(&self) -> bool {
- self.as_bytes().first().is_some_and(|&b| b == OP_RETURN.to_u8())
- }
-
/// Check if this is an OP_RETURN that obeys Bitcoin Core standardness policy.
///
/// What this function considers to be standard may change without warning pending Bitcoin Core
diff --git a/bitcoin/src/blockdata/script/owned.rs b/bitcoin/src/blockdata/script/owned.rs
index ffd5a297..a31c22a4 100644
--- a/bitcoin/src/blockdata/script/owned.rs
+++ b/bitcoin/src/blockdata/script/owned.rs
@@ -126,11 +126,6 @@ internal_macros::define_extension_trait! {
crate::internal_macros::define_extension_trait! {
/// Extension functionality for the [`ScriptPubKeyBuf`] type.
pub trait ScriptPubKeyBufExt impl for ScriptPubKeyBuf {
- /// Generates OP_RETURN-type of scriptPubkey for the given data.
- fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self {
- Builder::new().push_opcode(OP_RETURN).push_slice(data).into_script()
- }
-
/// Generates P2PK-type of scriptPubkey.
fn new_p2pk(pubkey: LegacyPublicKey) -> Self {
Builder::new().push_key(pubkey).push_opcode(OP_CHECKSIG).into_script()
diff --git a/primitives/src/script/borrowed.rs b/primitives/src/script/borrowed.rs
index 851e8093..32453c48 100644
--- a/primitives/src/script/borrowed.rs
+++ b/primitives/src/script/borrowed.rs
@@ -13,7 +13,7 @@ use arbitrary::{Arbitrary, Unstructured};
use encoding::{BytesEncoder, CompactSizeEncoder, Encode, Encoder2};
use super::{ScriptBuf, P2A_PROGRAM};
-use crate::opcodes::all::{OP_CHECKSIG, OP_DUP, OP_EQUAL, OP_EQUALVERIFY, OP_HASH160};
+use crate::opcodes::all::{OP_CHECKSIG, OP_DUP, OP_EQUAL, OP_EQUALVERIFY, OP_HASH160, OP_RETURN};
use crate::opcodes::{Opcode, OP_PUSHBYTES_2, OP_PUSHBYTES_20, OP_PUSHBYTES_32};
use crate::prelude::{Box, ToOwned, Vec};
use crate::script::ScriptHashableTag;
@@ -278,6 +278,15 @@ impl ScriptPubKey {
&& self.as_bytes()[1] == OP_PUSHBYTES_2.to_u8()
&& self.as_bytes()[2..] == P2A_PROGRAM
}
+
+ /// Check if this is a consensus-valid `OP_RETURN` output.
+ ///
+ /// To validate if the `OP_RETURN` obeys Bitcoin Core's current standardness policy, use
+ /// `bitcoin::ScriptPubKeyExt::is_standard_op_return()` instead.
+ #[inline]
+ pub fn is_op_return(&self) -> bool {
+ self.as_bytes().first().is_some_and(|&b| b == OP_RETURN.to_u8())
+ }
}
impl<T> Encode for Script<T> {
diff --git a/primitives/src/script/owned.rs b/primitives/src/script/owned.rs
index 0ae94588..1388dbc7 100644
--- a/primitives/src/script/owned.rs
+++ b/primitives/src/script/owned.rs
@@ -8,7 +8,7 @@ use arbitrary::{Arbitrary, Unstructured};
use encoding::{ByteVecDecoder, DecoderStatus};
use super::{Script, ScriptBufDecoderError, P2A_PROGRAM};
-use crate::opcodes::all::{OP_1, OP_1NEGATE, OP_EQUAL, OP_HASH160};
+use crate::opcodes::all::{OP_1, OP_1NEGATE, OP_EQUAL, OP_HASH160, OP_RETURN};
use crate::opcodes::{self, Opcode};
use crate::prelude::{Box, Vec};
use crate::script::{Builder, PushBytes, ScriptHash, WScriptHash};
@@ -261,6 +261,11 @@ impl<T> ScriptBuf<T> {
}
impl ScriptPubKeyBuf {
+ /// Generates OP_RETURN-type of scriptPubkey for the given data.
+ pub fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self {
+ Builder::new().push_opcode(OP_RETURN).push_slice(data).into_script()
+ }
+
/// Generates P2SH-type of scriptPubkey with a given hash of the redeem script.
pub fn new_p2sh(script_hash: ScriptHash) -> Self {
Builder::new()
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.