script: Add Script::redeem_script inspector

This commit is contained in:
Steven Roose 2024-03-21 21:47:53 +00:00
parent 6a2fd96ff6
commit e48a2e4225
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
1 changed files with 19 additions and 0 deletions

View File

@ -394,6 +394,25 @@ impl Script {
} }
} }
/// Get redeemScript following BIP16 rules regarding P2SH spending.
///
/// This does not guarantee that this represents a P2SH input [`Script`].
/// It merely gets the last push of the script. Use
/// [`Script::is_p2sh`](crate::blockdata::script::Script::is_p2sh) on the
/// scriptPubKey to check whether it is actually a P2SH script.
pub fn redeem_script(&self) -> Option<&Script> {
// Script must consist entirely of pushes.
if self.instructions().any(|i| i.is_err() || i.unwrap().push_bytes().is_none()) {
return None;
}
if let Some(Ok(Instruction::PushBytes(b))) = self.instructions().last() {
Some(Script::from_bytes(b.as_bytes()))
} else {
None
}
}
/// Returns the minimum value an output with this script should have in order to be /// Returns the minimum value an output with this script should have in order to be
/// broadcastable on todays Bitcoin network. /// broadcastable on todays Bitcoin network.
#[deprecated(since = "0.32.0", note = "use minimal_non_dust and friends")] #[deprecated(since = "0.32.0", note = "use minimal_non_dust and friends")]