Skip to content

Commit 99cea81

Browse files
committed
test_common: Add create_txin.
1 parent fd39d14 commit 99cea81

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

src/test_common/mod.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@
55
//! This crate is in `src/` directory because unit tests can't access `tests/`
66
//! directory.
77
8-
use bitcoin::{absolute, Address, Network, Transaction, TxIn, TxOut, XOnlyPublicKey};
8+
use bitcoin::{
9+
absolute,
10+
key::UntweakedPublicKey,
11+
opcodes::all::OP_EQUAL,
12+
taproot::{LeafVersion, TaprootBuilder},
13+
Address, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Txid, Witness, WitnessProgram,
14+
XOnlyPublicKey,
15+
};
916
use secp256k1::Secp256k1;
17+
use std::str::FromStr;
1018

1119
#[allow(unused)]
1220
pub fn get_temp_address() -> Address {
@@ -21,6 +29,47 @@ pub fn get_temp_address() -> Address {
2129
Address::p2tr(&secp, xonly_public_key, None, Network::Regtest)
2230
}
2331

32+
#[allow(unused)]
33+
pub fn create_txin(txid: Txid) -> TxIn {
34+
let secp = bitcoin::secp256k1::Secp256k1::new();
35+
let internal_key = UntweakedPublicKey::from(
36+
bitcoin::secp256k1::PublicKey::from_str(
37+
"0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0",
38+
)
39+
.unwrap(),
40+
);
41+
42+
let mut script = ScriptBuf::new();
43+
script.push_slice([12, 34]);
44+
script.push_instruction(bitcoin::script::Instruction::Op(OP_EQUAL));
45+
46+
let taproot_builder = TaprootBuilder::new().add_leaf(0, script.clone()).unwrap();
47+
let taproot_spend_info = taproot_builder.finalize(&secp, internal_key).unwrap();
48+
49+
let witness_program =
50+
WitnessProgram::p2tr(&secp, internal_key, taproot_spend_info.merkle_root());
51+
52+
let mut control_block_bytes = Vec::new();
53+
taproot_spend_info
54+
.control_block(&(script.clone(), LeafVersion::TapScript))
55+
.unwrap()
56+
.encode(&mut control_block_bytes)
57+
.unwrap();
58+
59+
let mut script2 = ScriptBuf::new();
60+
script2.push_slice([12, 34]);
61+
let mut witness = Witness::new();
62+
witness.push(script2);
63+
witness.push(script.to_bytes());
64+
witness.push(control_block_bytes);
65+
66+
TxIn {
67+
previous_output: OutPoint { txid, vout: 0 },
68+
witness,
69+
..Default::default()
70+
}
71+
}
72+
2473
#[allow(unused)]
2574
pub fn create_transaction(tx_ins: Vec<TxIn>, tx_outs: Vec<TxOut>) -> Transaction {
2675
bitcoin::Transaction {

0 commit comments

Comments
 (0)