@@ -303,7 +303,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
303
303
#[ cfg( not( test) ) ]
304
304
last_local_commitment_txn : Vec < Transaction > ,
305
305
306
- last_sent_closing_fee : Option < ( u64 , u64 ) > , // (feerate, fee)
306
+ last_sent_closing_fee : Option < ( u64 , u64 , Signature ) > , // (feerate, fee, our_sig )
307
307
308
308
/// The hash of the block in which the funding transaction reached our CONF_TARGET. We use this
309
309
/// to detect unconfirmation after a serialize-unserialize roundtrip where we may not see a full
@@ -2665,14 +2665,16 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2665
2665
let proposed_total_fee_satoshis = proposed_feerate * tx_weight / 1000 ;
2666
2666
2667
2667
let ( closing_tx, total_fee_satoshis) = self . build_closing_transaction ( proposed_total_fee_satoshis, false ) ;
2668
- let funding_redeemscript = self . get_funding_redeemscript ( ) ;
2669
- let sighash = hash_to_message ! ( & bip143:: SighashComponents :: new( & closing_tx) . sighash_all( & closing_tx. input[ 0 ] , & funding_redeemscript, self . channel_value_satoshis) [ ..] ) ;
2668
+ let our_sig = self . local_keys
2669
+ . sign_closing_transaction ( self . channel_value_satoshis , & self . get_funding_redeemscript ( ) , & closing_tx, & self . secp_ctx )
2670
+ . ok ( ) ;
2671
+ if our_sig. is_none ( ) { return None ; }
2670
2672
2671
- self . last_sent_closing_fee = Some ( ( proposed_feerate, total_fee_satoshis) ) ;
2673
+ self . last_sent_closing_fee = Some ( ( proposed_feerate, total_fee_satoshis, our_sig . clone ( ) . unwrap ( ) ) ) ;
2672
2674
Some ( msgs:: ClosingSigned {
2673
2675
channel_id : self . channel_id ,
2674
2676
fee_satoshis : total_fee_satoshis,
2675
- signature : self . secp_ctx . sign ( & sighash , & self . local_keys . funding_key ( ) ) ,
2677
+ signature : our_sig . unwrap ( ) ,
2676
2678
} )
2677
2679
}
2678
2680
@@ -2749,6 +2751,28 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2749
2751
Ok ( ( our_shutdown, self . maybe_propose_first_closing_signed ( fee_estimator) , dropped_outbound_htlcs) )
2750
2752
}
2751
2753
2754
+ fn build_signed_closing_transaction ( & self , tx : & mut Transaction , their_sig : & Signature , our_sig : & Signature ) {
2755
+ if tx. input . len ( ) != 1 { panic ! ( "Tried to sign closing transaction that had input count != 1!" ) ; }
2756
+ if tx. input [ 0 ] . witness . len ( ) != 0 { panic ! ( "Tried to re-sign closing transaction" ) ; }
2757
+ if tx. output . len ( ) > 2 { panic ! ( "Tried to sign bogus closing transaction" ) ; }
2758
+
2759
+ tx. input [ 0 ] . witness . push ( Vec :: new ( ) ) ; // First is the multisig dummy
2760
+
2761
+ let our_funding_key = PublicKey :: from_secret_key ( & self . secp_ctx , self . local_keys . funding_key ( ) ) . serialize ( ) ;
2762
+ let their_funding_key = self . their_funding_pubkey . unwrap ( ) . serialize ( ) ;
2763
+ if our_funding_key[ ..] < their_funding_key[ ..] {
2764
+ tx. input [ 0 ] . witness . push ( our_sig. serialize_der ( ) . to_vec ( ) ) ;
2765
+ tx. input [ 0 ] . witness . push ( their_sig. serialize_der ( ) . to_vec ( ) ) ;
2766
+ } else {
2767
+ tx. input [ 0 ] . witness . push ( their_sig. serialize_der ( ) . to_vec ( ) ) ;
2768
+ tx. input [ 0 ] . witness . push ( our_sig. serialize_der ( ) . to_vec ( ) ) ;
2769
+ }
2770
+ tx. input [ 0 ] . witness [ 1 ] . push ( SigHashType :: All as u8 ) ;
2771
+ tx. input [ 0 ] . witness [ 2 ] . push ( SigHashType :: All as u8 ) ;
2772
+
2773
+ tx. input [ 0 ] . witness . push ( self . get_funding_redeemscript ( ) . into_bytes ( ) ) ;
2774
+ }
2775
+
2752
2776
pub fn closing_signed ( & mut self , fee_estimator : & FeeEstimator , msg : & msgs:: ClosingSigned ) -> Result < ( Option < msgs:: ClosingSigned > , Option < Transaction > ) , ChannelError > {
2753
2777
if self . channel_state & BOTH_SIDES_SHUTDOWN_MASK != BOTH_SIDES_SHUTDOWN_MASK {
2754
2778
return Err ( ChannelError :: Close ( "Remote end sent us a closing_signed before both sides provided a shutdown" ) ) ;
@@ -2781,9 +2805,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2781
2805
} ,
2782
2806
} ;
2783
2807
2784
- if let Some ( ( _, last_fee) ) = self . last_sent_closing_fee {
2808
+ if let Some ( ( _, last_fee, our_sig ) ) = self . last_sent_closing_fee {
2785
2809
if last_fee == msg. fee_satoshis {
2786
- self . sign_commitment_transaction ( & mut closing_tx, & msg. signature ) ;
2810
+ self . build_signed_closing_transaction ( & mut closing_tx, & msg. signature , & our_sig ) ;
2787
2811
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
2788
2812
self . channel_update_count += 1 ;
2789
2813
return Ok ( ( None , Some ( closing_tx) ) ) ;
@@ -2794,9 +2818,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2794
2818
( $new_feerate: expr) => {
2795
2819
let closing_tx_max_weight = Self :: get_closing_transaction_weight( & self . get_closing_scriptpubkey( ) , self . their_shutdown_scriptpubkey. as_ref( ) . unwrap( ) ) ;
2796
2820
let ( closing_tx, used_total_fee) = self . build_closing_transaction( $new_feerate * closing_tx_max_weight / 1000 , false ) ;
2797
- sighash = hash_to_message!( & bip143:: SighashComponents :: new( & closing_tx) . sighash_all( & closing_tx. input[ 0 ] , & funding_redeemscript, self . channel_value_satoshis) [ ..] ) ;
2798
- let our_sig = self . secp_ctx. sign( & sighash, & self . local_keys. funding_key( ) ) ;
2799
- self . last_sent_closing_fee = Some ( ( $new_feerate, used_total_fee) ) ;
2821
+ let our_sig = self . local_keys
2822
+ . sign_closing_transaction( self . channel_value_satoshis, & funding_redeemscript, & closing_tx, & self . secp_ctx)
2823
+ . map_err( |_| ChannelError :: Close ( "External signer refused to sign closing transaction" ) ) ?;
2824
+ self . last_sent_closing_fee = Some ( ( $new_feerate, used_total_fee, our_sig. clone( ) ) ) ;
2800
2825
return Ok ( ( Some ( msgs:: ClosingSigned {
2801
2826
channel_id: self . channel_id,
2802
2827
fee_satoshis: used_total_fee,
@@ -2809,7 +2834,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2809
2834
if self . channel_outbound {
2810
2835
let our_max_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Normal ) ;
2811
2836
if proposed_sat_per_kw > our_max_feerate {
2812
- if let Some ( ( last_feerate, _) ) = self . last_sent_closing_fee {
2837
+ if let Some ( ( last_feerate, _, _ ) ) = self . last_sent_closing_fee {
2813
2838
if our_max_feerate <= last_feerate {
2814
2839
return Err ( ChannelError :: Close ( "Unable to come to consensus about closing feerate, remote wanted something higher than our Normal feerate" ) ) ;
2815
2840
}
@@ -2819,7 +2844,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2819
2844
} else {
2820
2845
let our_min_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
2821
2846
if proposed_sat_per_kw < our_min_feerate {
2822
- if let Some ( ( last_feerate, _) ) = self . last_sent_closing_fee {
2847
+ if let Some ( ( last_feerate, _, _ ) ) = self . last_sent_closing_fee {
2823
2848
if our_min_feerate >= last_feerate {
2824
2849
return Err ( ChannelError :: Close ( "Unable to come to consensus about closing feerate, remote wanted something lower than our Background feerate" ) ) ;
2825
2850
}
@@ -2828,7 +2853,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2828
2853
}
2829
2854
}
2830
2855
2831
- let our_sig = self . sign_commitment_transaction ( & mut closing_tx, & msg. signature ) ;
2856
+ let our_sig = self . local_keys
2857
+ . sign_closing_transaction ( self . channel_value_satoshis , & funding_redeemscript, & closing_tx, & self . secp_ctx )
2858
+ . map_err ( |_| ChannelError :: Close ( "External signer refused to sign closing transaction" ) ) ?;
2859
+ self . build_signed_closing_transaction ( & mut closing_tx, & msg. signature , & our_sig) ;
2860
+
2832
2861
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
2833
2862
self . channel_update_count += 1 ;
2834
2863
@@ -3855,10 +3884,11 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
3855
3884
}
3856
3885
3857
3886
match self . last_sent_closing_fee {
3858
- Some ( ( feerate, fee) ) => {
3887
+ Some ( ( feerate, fee, sig ) ) => {
3859
3888
1u8 . write ( writer) ?;
3860
3889
feerate. write ( writer) ?;
3861
3890
fee. write ( writer) ?;
3891
+ sig. write ( writer) ?;
3862
3892
} ,
3863
3893
None => 0u8 . write ( writer) ?,
3864
3894
}
@@ -4022,7 +4052,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
4022
4052
4023
4053
let last_sent_closing_fee = match <u8 as Readable < R > >:: read ( reader) ? {
4024
4054
0 => None ,
4025
- 1 => Some ( ( Readable :: read ( reader) ?, Readable :: read ( reader) ?) ) ,
4055
+ 1 => Some ( ( Readable :: read ( reader) ?, Readable :: read ( reader) ?, Readable :: read ( reader ) ? ) ) ,
4026
4056
_ => return Err ( DecodeError :: InvalidValue ) ,
4027
4057
} ;
4028
4058
0 commit comments