5
5
//! This crate is in `src/` directory because unit tests can't access `tests/`
6
6
//! directory.
7
7
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
+ } ;
9
16
use secp256k1:: Secp256k1 ;
17
+ use std:: str:: FromStr ;
10
18
11
19
#[ allow( unused) ]
12
20
pub fn get_temp_address ( ) -> Address {
@@ -21,6 +29,47 @@ pub fn get_temp_address() -> Address {
21
29
Address :: p2tr ( & secp, xonly_public_key, None , Network :: Regtest )
22
30
}
23
31
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
+
24
73
#[ allow( unused) ]
25
74
pub fn create_transaction ( tx_ins : Vec < TxIn > , tx_outs : Vec < TxOut > ) -> Transaction {
26
75
bitcoin:: Transaction {
0 commit comments