use super::{Builder, Error, Instruction, ScriptBuf, ScriptPubKeyBuf};
use crate::internal_macros;
use crate::key::{PubkeyHash, WPubkeyHash};
use crate::script::witness_program::{WitnessProgram, P2A_PROGRAM};
use crate::script::witness_version::WitnessVersion;
use crate::script::{self, ScriptHash, WScriptHash};
mod sealed {
pub trait Sealed {}
impl<T> Sealed for super::ScriptBuf<T> {}
}
internal_macros::define_extension_trait! {
pub trait ScriptBufExt<T> impl<T> for ScriptBuf<T> {
fn builder() -> Builder<T> { Builder::new() }
fn push_int(&mut self, n: i32) -> Result<(), Error> {
primitives::script::ScriptBuf::push_int(self, n).map_err(Into::into)
}
fn push_int_unchecked(&mut self, n: i64) {
primitives::script::ScriptBuf::push_int_unchecked(self, n)
}
fn push_int_non_minimal(&mut self, data: i64) {
primitives::script::ScriptBuf::push_int_non_minimal(self, data)
}
fn push_opcode(&mut self, data: crate::opcodes::Opcode) {
primitives::script::ScriptBuf::push_opcode(self, data)
}
fn push_slice<D: AsRef<[u8]>>(&mut self, data: D) {
primitives::script::ScriptBuf::push_slice(self, data)
}
fn push_slice_non_minimal<D: AsRef<[u8]>>(&mut self, data: D) {
primitives::script::ScriptBuf::push_slice_non_minimal(self, data)
}
fn push_instruction(&mut self, instruction: Instruction<'_>) {
primitives::script::ScriptBuf::push_instruction(self, instruction)
}
fn scan_and_push_verify(&mut self) {
primitives::script::ScriptBuf::scan_and_push_verify(self)
}
fn new_p2wpkh(pubkey_hash: WPubkeyHash) -> Self {
script::new_witness_program_unchecked(WitnessVersion::V0, pubkey_hash.to_byte_array())
}
}
}
crate::internal_macros::define_extension_trait! {
pub trait ScriptPubKeyBufExt impl for ScriptPubKeyBuf {
fn new_op_return<T: AsRef<[u8]>>(data: T) -> Self {
Builder::new().push_opcode(crate::opcodes::all::OP_RETURN).push_slice(data).into_script()
}
fn new_p2pkh(pubkey_hash: PubkeyHash) -> Self {
Builder::new()
.push_opcode(crate::opcodes::all::OP_DUP)
.push_opcode(crate::opcodes::all::OP_HASH160)
.push_slice(pubkey_hash)
.push_opcode(crate::opcodes::all::OP_EQUALVERIFY)
.push_opcode(crate::opcodes::all::OP_CHECKSIG)
.into_script()
}
fn new_p2sh(script_hash: ScriptHash) -> Self {
Builder::new()
.push_opcode(crate::opcodes::all::OP_HASH160)
.push_slice(script_hash.to_byte_array())
.push_opcode(crate::opcodes::all::OP_EQUAL)
.into_script()
}
fn new_p2wsh(script_hash: WScriptHash) -> Self {
script::new_witness_program_unchecked(WitnessVersion::V0, script_hash.to_byte_array())
}
fn new_p2a() -> Self {
script::new_witness_program_unchecked(WitnessVersion::V1, P2A_PROGRAM)
}
fn new_witness_program(witness_program: &WitnessProgram) -> Self {
Builder::new()
.push_opcode(witness_program.version().into())
.push_slice(witness_program.program())
.into_script()
}
}
}