Skip to content

Commit

Permalink
maintain provably unspendable semantincs
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Jan 14, 2025
1 parent 56e03c8 commit 83abd5b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl TxOutValue {
"fee"
} else if script.is_empty() {
"empty"
} else if script.is_op_return() {
} else if crate::util::provably_unspendable(&script) {
"op_return"
} else if script.is_p2pk() {
"p2pk"
Expand All @@ -319,7 +319,7 @@ impl TxOutValue {
"v0_p2wsh"
} else if script.is_p2tr() {
"v1_p2tr"
} else if script.is_op_return() {
} else if crate::util::provably_unspendable(&script) {
"provably_unspendable"
} else {
"unknown"
Expand Down
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use self::block::{
BlockHeaderMeta, BlockId, BlockMeta, BlockStatus, HeaderEntry, HeaderList, DEFAULT_BLOCKHASH,
};
pub use self::fees::get_tx_fee;
pub(crate) use self::script::provably_unspendable;
pub use self::script::{get_innerscripts, ScriptToAddr, ScriptToAsm};
pub use self::transaction::{
extract_tx_prevouts, get_prev_outpoints, has_prevout, is_coinbase, is_spendable,
Expand Down
15 changes: 15 additions & 0 deletions src/util/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,18 @@ pub fn get_innerscripts(txin: &TxIn, prevout: &TxOut) -> InnerScripts {
witness_script,
}
}

/// Compatible with Script::is_provably_unspendable on rust-bitcoin v0.31 and before because it checks also IllegalOp class
pub(crate) fn provably_unspendable(script: &Script) -> bool {
use bitcoin::blockdata::opcodes::Class::{IllegalOp, ReturnOp};

match script.as_bytes().first() {
Some(b) => {
let first = bitcoin::Opcode::from(*b);
let class = first.classify(bitcoin::blockdata::opcodes::ClassifyContext::Legacy);

class == ReturnOp || class == IllegalOp
}
None => false,
}
}
2 changes: 1 addition & 1 deletion src/util/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn has_prevout(txin: &TxIn) -> bool {

pub fn is_spendable(txout: &TxOut) -> bool {
#[cfg(not(feature = "liquid"))]
return !txout.script_pubkey.is_op_return();
return !crate::util::provably_unspendable(&txout.script_pubkey);
#[cfg(feature = "liquid")]
return !txout.is_fee() && !txout.script_pubkey.is_provably_unspendable();
}
Expand Down

0 comments on commit 83abd5b

Please sign in to comment.