@@ -559,6 +559,12 @@ impl<Signer: Sign> Channel<Signer> {
559
559
let mut secp_ctx = Secp256k1 :: new ( ) ;
560
560
secp_ctx. seeded_randomize ( & keys_provider. get_secure_random_bytes ( ) ) ;
561
561
562
+ let shutdown_scriptpubkey = if config. channel_options . commit_upfront_shutdown_pubkey {
563
+ Some ( keys_provider. get_shutdown_scriptpubkey ( ) )
564
+ } else {
565
+ None
566
+ } ;
567
+
562
568
Ok ( Channel {
563
569
user_id,
564
570
config : config. channel_options . clone ( ) ,
@@ -571,7 +577,7 @@ impl<Signer: Sign> Channel<Signer> {
571
577
latest_monitor_update_id : 0 ,
572
578
573
579
holder_signer,
574
- shutdown_scriptpubkey : Some ( keys_provider . get_shutdown_scriptpubkey ( ) ) ,
580
+ shutdown_scriptpubkey,
575
581
destination_script : keys_provider. get_destination_script ( ) ,
576
582
577
583
cur_holder_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
@@ -801,6 +807,12 @@ impl<Signer: Sign> Channel<Signer> {
801
807
let mut secp_ctx = Secp256k1 :: new ( ) ;
802
808
secp_ctx. seeded_randomize ( & keys_provider. get_secure_random_bytes ( ) ) ;
803
809
810
+ let shutdown_scriptpubkey = if config. channel_options . commit_upfront_shutdown_pubkey {
811
+ Some ( keys_provider. get_shutdown_scriptpubkey ( ) )
812
+ } else {
813
+ None
814
+ } ;
815
+
804
816
let chan = Channel {
805
817
user_id,
806
818
config : local_config,
@@ -812,7 +824,7 @@ impl<Signer: Sign> Channel<Signer> {
812
824
latest_monitor_update_id : 0 ,
813
825
814
826
holder_signer,
815
- shutdown_scriptpubkey : Some ( keys_provider . get_shutdown_scriptpubkey ( ) ) ,
827
+ shutdown_scriptpubkey,
816
828
destination_script : keys_provider. get_destination_script ( ) ,
817
829
818
830
cur_holder_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
@@ -1154,6 +1166,7 @@ impl<Signer: Sign> Channel<Signer> {
1154
1166
} , ( ) ) ) ;
1155
1167
}
1156
1168
1169
+ assert ! ( self . shutdown_scriptpubkey. is_some( ) ) ;
1157
1170
if value_to_self as u64 > self . holder_dust_limit_satoshis {
1158
1171
txouts. push ( ( TxOut {
1159
1172
script_pubkey : self . get_closing_scriptpubkey ( ) ,
@@ -3023,6 +3036,7 @@ impl<Signer: Sign> Channel<Signer> {
3023
3036
self . channel_state &= !( ChannelState :: PeerDisconnected as u32 ) ;
3024
3037
3025
3038
let shutdown_msg = if self . channel_state & ( ChannelState :: LocalShutdownSent as u32 ) != 0 {
3039
+ assert ! ( self . shutdown_scriptpubkey. is_some( ) ) ;
3026
3040
Some ( msgs:: Shutdown {
3027
3041
channel_id : self . channel_id ,
3028
3042
scriptpubkey : self . get_closing_scriptpubkey ( ) ,
@@ -3134,6 +3148,7 @@ impl<Signer: Sign> Channel<Signer> {
3134
3148
if self . feerate_per_kw > proposed_feerate {
3135
3149
proposed_feerate = self . feerate_per_kw ;
3136
3150
}
3151
+ assert ! ( self . shutdown_scriptpubkey. is_some( ) ) ;
3137
3152
let tx_weight = self . get_closing_transaction_weight ( Some ( & self . get_closing_scriptpubkey ( ) ) , Some ( self . counterparty_shutdown_scriptpubkey . as_ref ( ) . unwrap ( ) ) ) ;
3138
3153
let proposed_total_fee_satoshis = proposed_feerate as u64 * tx_weight / 1000 ;
3139
3154
@@ -3152,8 +3167,12 @@ impl<Signer: Sign> Channel<Signer> {
3152
3167
} )
3153
3168
}
3154
3169
3155
- pub fn shutdown < F : Deref > ( & mut self , fee_estimator : & F , _their_features : & InitFeatures , msg : & msgs:: Shutdown ) -> Result < ( Option < msgs:: Shutdown > , Option < msgs:: ClosingSigned > , Vec < ( HTLCSource , PaymentHash ) > ) , ChannelError >
3156
- where F :: Target : FeeEstimator
3170
+ pub fn shutdown < F : Deref , K : Deref > (
3171
+ & mut self , fee_estimator : & F , keys_provider : & K , _their_features : & InitFeatures , msg : & msgs:: Shutdown
3172
+ ) -> Result < ( Option < msgs:: Shutdown > , Option < msgs:: ClosingSigned > , Option < ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) , ChannelError >
3173
+ where
3174
+ F :: Target : FeeEstimator ,
3175
+ K :: Target : KeysInterface < Signer = Signer >
3157
3176
{
3158
3177
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
3159
3178
return Err ( ChannelError :: Close ( "Peer sent shutdown when we needed a channel_reestablish" . to_owned ( ) ) ) ;
@@ -3203,23 +3222,36 @@ impl<Signer: Sign> Channel<Signer> {
3203
3222
_ => true
3204
3223
}
3205
3224
} ) ;
3225
+
3206
3226
// If we have any LocalAnnounced updates we'll probably just get back a update_fail_htlc
3207
3227
// immediately after the commitment dance, but we can send a Shutdown cause we won't send
3208
3228
// any further commitment updates after we set LocalShutdownSent.
3209
-
3210
- let shutdown = if ( self . channel_state & ChannelState :: LocalShutdownSent as u32 ) == ChannelState :: LocalShutdownSent as u32 {
3211
- None
3212
- } else {
3229
+ let send_shutdown = ( self . channel_state & ChannelState :: LocalShutdownSent as u32 ) != ChannelState :: LocalShutdownSent as u32 ;
3230
+ let monitor_update = match self . shutdown_scriptpubkey {
3231
+ Some ( _) => None ,
3232
+ None => {
3233
+ assert ! ( send_shutdown) ;
3234
+ self . shutdown_scriptpubkey = Some ( keys_provider. get_shutdown_scriptpubkey ( ) ) ;
3235
+ self . latest_monitor_update_id += 1 ;
3236
+ Some ( ChannelMonitorUpdate {
3237
+ update_id : self . latest_monitor_update_id ,
3238
+ updates : vec ! [ ChannelMonitorUpdateStep :: ShutdownScript {
3239
+ scriptpubkey: self . get_closing_scriptpubkey( ) ,
3240
+ } ] ,
3241
+ } )
3242
+ } ,
3243
+ } ;
3244
+ let shutdown = if send_shutdown {
3213
3245
Some ( msgs:: Shutdown {
3214
3246
channel_id : self . channel_id ,
3215
3247
scriptpubkey : self . get_closing_scriptpubkey ( ) ,
3216
3248
} )
3217
- } ;
3249
+ } else { None } ;
3218
3250
3219
3251
self . channel_state |= ChannelState :: LocalShutdownSent as u32 ;
3220
3252
self . update_time_counter += 1 ;
3221
3253
3222
- Ok ( ( shutdown, self . maybe_propose_first_closing_signed ( fee_estimator) , dropped_outbound_htlcs) )
3254
+ Ok ( ( shutdown, self . maybe_propose_first_closing_signed ( fee_estimator) , monitor_update , dropped_outbound_htlcs) )
3223
3255
}
3224
3256
3225
3257
fn build_signed_closing_transaction ( & self , tx : & mut Transaction , counterparty_sig : & Signature , sig : & Signature ) {
@@ -3294,6 +3326,7 @@ impl<Signer: Sign> Channel<Signer> {
3294
3326
3295
3327
macro_rules! propose_new_feerate {
3296
3328
( $new_feerate: expr) => {
3329
+ assert!( self . shutdown_scriptpubkey. is_some( ) ) ;
3297
3330
let tx_weight = self . get_closing_transaction_weight( Some ( & self . get_closing_scriptpubkey( ) ) , Some ( self . counterparty_shutdown_scriptpubkey. as_ref( ) . unwrap( ) ) ) ;
3298
3331
let ( closing_tx, used_total_fee) = self . build_closing_transaction( $new_feerate as u64 * tx_weight / 1000 , false ) ;
3299
3332
let sig = self . holder_signer
@@ -3792,7 +3825,10 @@ impl<Signer: Sign> Channel<Signer> {
3792
3825
htlc_basepoint : keys. htlc_basepoint ,
3793
3826
first_per_commitment_point,
3794
3827
channel_flags : if self . config . announced_channel { 1 } else { 0 } ,
3795
- shutdown_scriptpubkey : OptionalField :: Present ( if self . config . commit_upfront_shutdown_pubkey { self . get_closing_scriptpubkey ( ) } else { Builder :: new ( ) . into_script ( ) } )
3828
+ shutdown_scriptpubkey : OptionalField :: Present ( match & self . shutdown_scriptpubkey {
3829
+ Some ( script) => script. clone ( ) . into_inner ( ) ,
3830
+ None => Builder :: new ( ) . into_script ( ) ,
3831
+ } ) ,
3796
3832
}
3797
3833
}
3798
3834
@@ -3825,7 +3861,10 @@ impl<Signer: Sign> Channel<Signer> {
3825
3861
delayed_payment_basepoint : keys. delayed_payment_basepoint ,
3826
3862
htlc_basepoint : keys. htlc_basepoint ,
3827
3863
first_per_commitment_point,
3828
- shutdown_scriptpubkey : OptionalField :: Present ( if self . config . commit_upfront_shutdown_pubkey { self . get_closing_scriptpubkey ( ) } else { Builder :: new ( ) . into_script ( ) } )
3864
+ shutdown_scriptpubkey : OptionalField :: Present ( match & self . shutdown_scriptpubkey {
3865
+ Some ( script) => script. clone ( ) . into_inner ( ) ,
3866
+ None => Builder :: new ( ) . into_script ( ) ,
3867
+ } ) ,
3829
3868
}
3830
3869
}
3831
3870
@@ -4333,7 +4372,8 @@ impl<Signer: Sign> Channel<Signer> {
4333
4372
4334
4373
/// Begins the shutdown process, getting a message for the remote peer and returning all
4335
4374
/// holding cell HTLCs for payment failure.
4336
- pub fn get_shutdown ( & mut self ) -> Result < ( msgs:: Shutdown , Vec < ( HTLCSource , PaymentHash ) > ) , APIError > {
4375
+ pub fn get_shutdown < K : Deref > ( & mut self , keys_provider : & K ) -> Result < ( msgs:: Shutdown , Option < ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) , APIError >
4376
+ where K :: Target : KeysInterface < Signer = Signer > {
4337
4377
for htlc in self . pending_outbound_htlcs . iter ( ) {
4338
4378
if let OutboundHTLCState :: LocalAnnounced ( _) = htlc. state {
4339
4379
return Err ( APIError :: APIMisuseError { err : "Cannot begin shutdown with pending HTLCs. Process pending events first" . to_owned ( ) } ) ;
@@ -4352,7 +4392,16 @@ impl<Signer: Sign> Channel<Signer> {
4352
4392
return Err ( APIError :: ChannelUnavailable { err : "Cannot begin shutdown while peer is disconnected or we're waiting on a monitor update, maybe force-close instead?" . to_owned ( ) } ) ;
4353
4393
}
4354
4394
4355
- let closing_script = self . get_closing_scriptpubkey ( ) ;
4395
+ let monitor_update = if self . shutdown_scriptpubkey . is_none ( ) {
4396
+ self . shutdown_scriptpubkey = Some ( keys_provider. get_shutdown_scriptpubkey ( ) ) ;
4397
+ self . latest_monitor_update_id += 1 ;
4398
+ Some ( ChannelMonitorUpdate {
4399
+ update_id : self . latest_monitor_update_id ,
4400
+ updates : vec ! [ ChannelMonitorUpdateStep :: ShutdownScript {
4401
+ scriptpubkey: self . get_closing_scriptpubkey( ) ,
4402
+ } ] ,
4403
+ } )
4404
+ } else { None } ;
4356
4405
4357
4406
// From here on out, we may not fail!
4358
4407
if self . channel_state < ChannelState :: FundingSent as u32 {
@@ -4378,8 +4427,8 @@ impl<Signer: Sign> Channel<Signer> {
4378
4427
4379
4428
Ok ( ( msgs:: Shutdown {
4380
4429
channel_id : self . channel_id ,
4381
- scriptpubkey : closing_script ,
4382
- } , dropped_outbound_htlcs) )
4430
+ scriptpubkey : self . get_closing_scriptpubkey ( ) ,
4431
+ } , monitor_update , dropped_outbound_htlcs) )
4383
4432
}
4384
4433
4385
4434
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
0 commit comments