1
1
use std:: marker:: PhantomData ;
2
2
3
+ use bitcoin:: blockdata:: opcodes:: all:: { OP_CHECKSIG , OP_CHECKSIGADD , OP_EQUAL } ;
4
+ use bitcoin:: blockdata:: script:: Builder ;
3
5
use bitcoin:: blockdata:: transaction:: { TxIn , TxOut } ;
4
6
use bitcoin:: blockdata:: witness:: Witness ;
5
7
use bitcoin:: secp256k1:: rand:: thread_rng;
6
8
use bitcoin:: secp256k1:: Secp256k1 ;
7
9
use bitcoin:: util:: psbt:: PartiallySignedTransaction ;
10
+ use bitcoin:: Script ;
8
11
use bitcoin:: { Amount , KeyPair , XOnlyPublicKey } ;
9
12
10
13
use crate :: script;
@@ -21,23 +24,25 @@ pub struct Lock;
21
24
22
25
impl SubTransaction for Lock {
23
26
fn finalize ( psbt : & mut PartiallySignedTransaction ) -> Result < ( ) , FError > {
24
- let ( pubkey, full_sig) = psbt. inputs [ 0 ]
25
- . partial_sigs
26
- . iter ( )
27
- . next ( )
28
- . ok_or ( FError :: MissingSignature ) ?;
29
- psbt. inputs [ 0 ] . final_script_witness = Some ( Witness :: from_vec ( vec ! [
30
- full_sig. to_vec( ) ,
31
- pubkey. serialize( ) . to_vec( ) ,
32
- ] ) ) ;
27
+ //let (pubkey, full_sig) = psbt.inputs[0]
28
+ // .partial_sigs
29
+ // .iter()
30
+ // .next()
31
+ // .ok_or(FError::MissingSignature)?;
32
+ //psbt.inputs[0].final_script_witness = Some(Witness::from_vec(vec![
33
+ // full_sig.to_vec(),
34
+ // pubkey.serialize().to_vec(),
35
+ //]));
36
+ let sig = psbt. inputs [ 0 ] . tap_key_sig . ok_or ( FError :: MissingSignature ) ?;
37
+ psbt. inputs [ 0 ] . final_script_witness = Some ( Witness :: from_vec ( vec ! [ sig. to_vec( ) ] ) ) ;
33
38
Ok ( ( ) )
34
39
}
35
40
}
36
41
37
42
impl Lockable < Bitcoin < Taproot > , MetadataOutput > for Tx < Lock > {
38
43
fn initialize (
39
44
prev : & impl Fundable < Bitcoin < Taproot > , MetadataOutput > ,
40
- _lock : script:: DataLock < Bitcoin < Taproot > > ,
45
+ lock : script:: DataLock < Bitcoin < Taproot > > ,
41
46
target_amount : Amount ,
42
47
) -> Result < Self , FError > {
43
48
let secp = Secp256k1 :: new ( ) ;
@@ -46,9 +51,37 @@ impl Lockable<Bitcoin<Taproot>, MetadataOutput> for Tx<Lock> {
46
51
let untweaked_public_key =
47
52
XOnlyPublicKey :: from_keypair ( & KeyPair :: new ( & secp, & mut thread_rng ( ) ) ) ;
48
53
let spend_info = TaprootBuilder :: new ( )
49
- // FIXME add script path for by and cancel
54
+ // Buy script
55
+ . add_leaf (
56
+ 1 ,
57
+ Builder :: new ( )
58
+ . push_slice ( lock. success . alice . serialize ( ) . as_ref ( ) )
59
+ . push_opcode ( OP_CHECKSIG )
60
+ . push_slice ( lock. success . bob . serialize ( ) . as_ref ( ) )
61
+ . push_opcode ( OP_CHECKSIGADD )
62
+ . push_int ( 2 )
63
+ . push_opcode ( OP_EQUAL )
64
+ . into_script ( ) ,
65
+ )
66
+ // FIXME
67
+ . unwrap ( )
68
+ // Cancel script
69
+ . add_leaf (
70
+ 1 ,
71
+ Builder :: new ( )
72
+ . push_slice ( lock. failure . alice . serialize ( ) . as_ref ( ) )
73
+ . push_opcode ( OP_CHECKSIG )
74
+ . push_slice ( lock. failure . bob . serialize ( ) . as_ref ( ) )
75
+ . push_opcode ( OP_CHECKSIGADD )
76
+ . push_int ( 1 ) // FIXME this is just for making different script (same keys for now between success and failure)
77
+ . push_opcode ( OP_EQUAL )
78
+ . into_script ( ) ,
79
+ )
80
+ // FIXME
81
+ . unwrap ( )
50
82
. finalize ( & secp, untweaked_public_key)
51
83
. expect ( "Valid taproot FIXME" ) ;
84
+ println ! ( "{:#?}" , spend_info) ;
52
85
let tweaked_pubkey = spend_info. output_key ( ) ;
53
86
let output_metadata = prev. get_consumable_output ( ) ?;
54
87
@@ -61,13 +94,13 @@ impl Lockable<Bitcoin<Taproot>, MetadataOutput> for Tx<Lock> {
61
94
lock_time : 0 ,
62
95
input : vec ! [ TxIn {
63
96
previous_output: output_metadata. out_point,
64
- script_sig: bitcoin :: Script :: default ( ) ,
97
+ script_sig: Script :: default ( ) ,
65
98
sequence: CSVTimelock :: disable( ) ,
66
99
witness: Witness :: new( ) ,
67
100
} ] ,
68
101
output : vec ! [ TxOut {
69
102
value: target_amount. as_sat( ) ,
70
- script_pubkey: bitcoin :: Script :: new_v1_p2tr_tweaked( tweaked_pubkey) ,
103
+ script_pubkey: Script :: new_v1_p2tr_tweaked( tweaked_pubkey) ,
71
104
} ] ,
72
105
} ;
73
106
0 commit comments