Skip to content

Commit

Permalink
Merge rust-bitcoin#2949: OP_RETURN standardness check
Browse files Browse the repository at this point in the history
15f6bac api: Run just check-api (Ryan Breen)
9684d49 Add is_standard_op_return (Ryan Breen)

Pull request description:

  This is the suggestion for rust-bitcoin#2292 to check OP_RETURN length

ACKs for top commit:
  apoelstra:
    ACK 15f6bac

Tree-SHA512: e346b5eff7cc40b98a08948c83cb5c064184541d819c37a977e432ec09df7f9e1a074f16a4df598142784bd875f1379e2b0848fe898923e4e12829f85b4c4520
  • Loading branch information
apoelstra committed Jul 2, 2024
2 parents e1b357c + 15f6bac commit db72ea8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/bitcoin/all-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8209,6 +8209,7 @@ pub fn bitcoin::blockdata::script::Script::is_p2wpkh(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_p2wsh(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_provably_unspendable(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_push_only(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_standard_op_return(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_witness_program(&self) -> bool
pub fn bitcoin::blockdata::script::Script::len(&self) -> usize
pub fn bitcoin::blockdata::script::Script::minimal_non_dust(&self) -> bitcoin_units::amount::Amount
Expand Down
1 change: 1 addition & 0 deletions api/bitcoin/default-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7831,6 +7831,7 @@ pub fn bitcoin::blockdata::script::Script::is_p2wpkh(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_p2wsh(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_provably_unspendable(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_push_only(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_standard_op_return(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_witness_program(&self) -> bool
pub fn bitcoin::blockdata::script::Script::len(&self) -> usize
pub fn bitcoin::blockdata::script::Script::minimal_non_dust(&self) -> bitcoin_units::amount::Amount
Expand Down
1 change: 1 addition & 0 deletions api/bitcoin/no-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7182,6 +7182,7 @@ pub fn bitcoin::blockdata::script::Script::is_p2wpkh(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_p2wsh(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_provably_unspendable(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_push_only(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_standard_op_return(&self) -> bool
pub fn bitcoin::blockdata::script::Script::is_witness_program(&self) -> bool
pub fn bitcoin::blockdata::script::Script::len(&self) -> usize
pub fn bitcoin::blockdata::script::Script::minimal_non_dust(&self) -> bitcoin_units::amount::Amount
Expand Down
14 changes: 13 additions & 1 deletion bitcoin/src/blockdata/script/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions bitcoin/src/blockdata/script/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit db72ea8

Please sign in to comment.