diff --git a/bitcoin/src/blockdata/script/borrowed.rs b/bitcoin/src/blockdata/script/borrowed.rs index dc8d57f3d..e830357af 100644 --- a/bitcoin/src/blockdata/script/borrowed.rs +++ b/bitcoin/src/blockdata/script/borrowed.rs @@ -349,7 +349,10 @@ impl Script { && self.0[1] == OP_PUSHBYTES_32.to_u8() } - /// Check if this is an OP_RETURN output. + /// 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] pub fn is_op_return(&self) -> bool { match self.0.first() { @@ -358,6 +361,15 @@ impl Script { } } + /// 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 + /// changes. + #[inline] + pub fn is_standard_op_return(&self) -> bool { + self.is_op_return() && self.0.len() <= 80 + } + /// Checks whether a script is trivially known to have no satisfying input. /// /// This method has potentially confusing semantics and an unclear purpose, so it's going to be diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs index beaa3d1e3..b7b8902fc 100644 --- a/bitcoin/src/blockdata/script/tests.rs +++ b/bitcoin/src/blockdata/script/tests.rs @@ -406,6 +406,20 @@ fn op_return_test() { assert!(!ScriptBuf::from_hex("").unwrap().is_op_return()); } +#[test] +fn standard_op_return_test() { + assert!(ScriptBuf::from_hex("6aa9149eb21980dc9d413d8eac27314938b9da920ee53e87") + .unwrap() + .is_standard_op_return()); + assert!(ScriptBuf::from_hex("6a48656c6c6f2c2074686973206973206d7920666972737420636f6e747269627574696f6e20746f207275737420626974636f696e2e20506c6561736520617070726f7665206d79205052206672656e") + .unwrap() + .is_standard_op_return()); + + assert!(!ScriptBuf::from_hex("6a48656c6c6f2c2074686973206973206d7920666972737420636f6e747269627574696f6e20746f207275737420626974636f696e2e20506c6561736520617070726f7665206d79205052206672656e21") + .unwrap() + .is_standard_op_return()); +} + #[test] fn multisig() { // First multisig? 1-of-2