@@ -595,6 +595,12 @@ impl<Signer: Sign> Channel<Signer> {
595
595
let mut secp_ctx = Secp256k1 :: new ( ) ;
596
596
secp_ctx. seeded_randomize ( & keys_provider. get_secure_random_bytes ( ) ) ;
597
597
598
+ let shutdown_scriptpubkey = if config. channel_options . commit_upfront_shutdown_pubkey {
599
+ Some ( keys_provider. get_shutdown_scriptpubkey ( ) )
600
+ } else {
601
+ None
602
+ } ;
603
+
598
604
Ok ( Channel {
599
605
user_id,
600
606
config : config. channel_options . clone ( ) ,
@@ -607,7 +613,7 @@ impl<Signer: Sign> Channel<Signer> {
607
613
latest_monitor_update_id : 0 ,
608
614
609
615
holder_signer,
610
- shutdown_scriptpubkey : Some ( keys_provider . get_shutdown_scriptpubkey ( ) ) ,
616
+ shutdown_scriptpubkey,
611
617
destination_script : keys_provider. get_destination_script ( ) ,
612
618
613
619
cur_holder_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
@@ -840,6 +846,12 @@ impl<Signer: Sign> Channel<Signer> {
840
846
let mut secp_ctx = Secp256k1 :: new ( ) ;
841
847
secp_ctx. seeded_randomize ( & keys_provider. get_secure_random_bytes ( ) ) ;
842
848
849
+ let shutdown_scriptpubkey = if config. channel_options . commit_upfront_shutdown_pubkey {
850
+ Some ( keys_provider. get_shutdown_scriptpubkey ( ) )
851
+ } else {
852
+ None
853
+ } ;
854
+
843
855
let chan = Channel {
844
856
user_id,
845
857
config : local_config,
@@ -851,7 +863,7 @@ impl<Signer: Sign> Channel<Signer> {
851
863
latest_monitor_update_id : 0 ,
852
864
853
865
holder_signer,
854
- shutdown_scriptpubkey : Some ( keys_provider . get_shutdown_scriptpubkey ( ) ) ,
866
+ shutdown_scriptpubkey,
855
867
destination_script : keys_provider. get_destination_script ( ) ,
856
868
857
869
cur_holder_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
@@ -1196,6 +1208,7 @@ impl<Signer: Sign> Channel<Signer> {
1196
1208
} , ( ) ) ) ;
1197
1209
}
1198
1210
1211
+ assert ! ( self . shutdown_scriptpubkey. is_some( ) ) ;
1199
1212
if value_to_self as u64 > self . holder_dust_limit_satoshis {
1200
1213
txouts. push ( ( TxOut {
1201
1214
script_pubkey : self . get_closing_scriptpubkey ( ) ,
@@ -3082,6 +3095,7 @@ impl<Signer: Sign> Channel<Signer> {
3082
3095
self . channel_state &= !( ChannelState :: PeerDisconnected as u32 ) ;
3083
3096
3084
3097
let shutdown_msg = if self . channel_state & ( ChannelState :: LocalShutdownSent as u32 ) != 0 {
3098
+ assert ! ( self . shutdown_scriptpubkey. is_some( ) ) ;
3085
3099
Some ( msgs:: Shutdown {
3086
3100
channel_id : self . channel_id ,
3087
3101
scriptpubkey : self . get_closing_scriptpubkey ( ) ,
@@ -3193,6 +3207,7 @@ impl<Signer: Sign> Channel<Signer> {
3193
3207
if self . feerate_per_kw > proposed_feerate {
3194
3208
proposed_feerate = self . feerate_per_kw ;
3195
3209
}
3210
+ assert ! ( self . shutdown_scriptpubkey. is_some( ) ) ;
3196
3211
let tx_weight = self . get_closing_transaction_weight ( Some ( & self . get_closing_scriptpubkey ( ) ) , Some ( self . counterparty_shutdown_scriptpubkey . as_ref ( ) . unwrap ( ) ) ) ;
3197
3212
let proposed_total_fee_satoshis = proposed_feerate as u64 * tx_weight / 1000 ;
3198
3213
@@ -3211,8 +3226,12 @@ impl<Signer: Sign> Channel<Signer> {
3211
3226
} )
3212
3227
}
3213
3228
3214
- 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 >
3215
- where F :: Target : FeeEstimator
3229
+ pub fn shutdown < F : Deref , K : Deref > (
3230
+ & mut self , fee_estimator : & F , keys_provider : & K , _their_features : & InitFeatures , msg : & msgs:: Shutdown
3231
+ ) -> Result < ( Option < msgs:: Shutdown > , Option < msgs:: ClosingSigned > , Option < ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) , ChannelError >
3232
+ where
3233
+ F :: Target : FeeEstimator ,
3234
+ K :: Target : KeysInterface < Signer = Signer >
3216
3235
{
3217
3236
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
3218
3237
return Err ( ChannelError :: Close ( "Peer sent shutdown when we needed a channel_reestablish" . to_owned ( ) ) ) ;
@@ -3262,23 +3281,36 @@ impl<Signer: Sign> Channel<Signer> {
3262
3281
_ => true
3263
3282
}
3264
3283
} ) ;
3284
+
3265
3285
// If we have any LocalAnnounced updates we'll probably just get back a update_fail_htlc
3266
3286
// immediately after the commitment dance, but we can send a Shutdown cause we won't send
3267
3287
// any further commitment updates after we set LocalShutdownSent.
3268
-
3269
- let shutdown = if ( self . channel_state & ChannelState :: LocalShutdownSent as u32 ) == ChannelState :: LocalShutdownSent as u32 {
3270
- None
3271
- } else {
3288
+ let send_shutdown = ( self . channel_state & ChannelState :: LocalShutdownSent as u32 ) != ChannelState :: LocalShutdownSent as u32 ;
3289
+ let monitor_update = match self . shutdown_scriptpubkey {
3290
+ Some ( _) => None ,
3291
+ None => {
3292
+ assert ! ( send_shutdown) ;
3293
+ self . shutdown_scriptpubkey = Some ( keys_provider. get_shutdown_scriptpubkey ( ) ) ;
3294
+ self . latest_monitor_update_id += 1 ;
3295
+ Some ( ChannelMonitorUpdate {
3296
+ update_id : self . latest_monitor_update_id ,
3297
+ updates : vec ! [ ChannelMonitorUpdateStep :: ShutdownScript {
3298
+ scriptpubkey: self . get_closing_scriptpubkey( ) ,
3299
+ } ] ,
3300
+ } )
3301
+ } ,
3302
+ } ;
3303
+ let shutdown = if send_shutdown {
3272
3304
Some ( msgs:: Shutdown {
3273
3305
channel_id : self . channel_id ,
3274
3306
scriptpubkey : self . get_closing_scriptpubkey ( ) ,
3275
3307
} )
3276
- } ;
3308
+ } else { None } ;
3277
3309
3278
3310
self . channel_state |= ChannelState :: LocalShutdownSent as u32 ;
3279
3311
self . update_time_counter += 1 ;
3280
3312
3281
- Ok ( ( shutdown, self . maybe_propose_first_closing_signed ( fee_estimator) , dropped_outbound_htlcs) )
3313
+ Ok ( ( shutdown, self . maybe_propose_first_closing_signed ( fee_estimator) , monitor_update , dropped_outbound_htlcs) )
3282
3314
}
3283
3315
3284
3316
fn build_signed_closing_transaction ( & self , tx : & mut Transaction , counterparty_sig : & Signature , sig : & Signature ) {
@@ -3353,6 +3385,7 @@ impl<Signer: Sign> Channel<Signer> {
3353
3385
3354
3386
macro_rules! propose_new_feerate {
3355
3387
( $new_feerate: expr) => {
3388
+ assert!( self . shutdown_scriptpubkey. is_some( ) ) ;
3356
3389
let tx_weight = self . get_closing_transaction_weight( Some ( & self . get_closing_scriptpubkey( ) ) , Some ( self . counterparty_shutdown_scriptpubkey. as_ref( ) . unwrap( ) ) ) ;
3357
3390
let ( closing_tx, used_total_fee) = self . build_closing_transaction( $new_feerate as u64 * tx_weight / 1000 , false ) ;
3358
3391
let sig = self . holder_signer
@@ -3851,7 +3884,10 @@ impl<Signer: Sign> Channel<Signer> {
3851
3884
htlc_basepoint : keys. htlc_basepoint ,
3852
3885
first_per_commitment_point,
3853
3886
channel_flags : if self . config . announced_channel { 1 } else { 0 } ,
3854
- shutdown_scriptpubkey : OptionalField :: Present ( if self . config . commit_upfront_shutdown_pubkey { self . get_closing_scriptpubkey ( ) } else { Builder :: new ( ) . into_script ( ) } )
3887
+ shutdown_scriptpubkey : OptionalField :: Present ( match & self . shutdown_scriptpubkey {
3888
+ Some ( script) => script. clone ( ) . into_inner ( ) ,
3889
+ None => Builder :: new ( ) . into_script ( ) ,
3890
+ } ) ,
3855
3891
}
3856
3892
}
3857
3893
@@ -3884,7 +3920,10 @@ impl<Signer: Sign> Channel<Signer> {
3884
3920
delayed_payment_basepoint : keys. delayed_payment_basepoint ,
3885
3921
htlc_basepoint : keys. htlc_basepoint ,
3886
3922
first_per_commitment_point,
3887
- shutdown_scriptpubkey : OptionalField :: Present ( if self . config . commit_upfront_shutdown_pubkey { self . get_closing_scriptpubkey ( ) } else { Builder :: new ( ) . into_script ( ) } )
3923
+ shutdown_scriptpubkey : OptionalField :: Present ( match & self . shutdown_scriptpubkey {
3924
+ Some ( script) => script. clone ( ) . into_inner ( ) ,
3925
+ None => Builder :: new ( ) . into_script ( ) ,
3926
+ } ) ,
3888
3927
}
3889
3928
}
3890
3929
@@ -4392,7 +4431,8 @@ impl<Signer: Sign> Channel<Signer> {
4392
4431
4393
4432
/// Begins the shutdown process, getting a message for the remote peer and returning all
4394
4433
/// holding cell HTLCs for payment failure.
4395
- pub fn get_shutdown ( & mut self ) -> Result < ( msgs:: Shutdown , Vec < ( HTLCSource , PaymentHash ) > ) , APIError > {
4434
+ pub fn get_shutdown < K : Deref > ( & mut self , keys_provider : & K ) -> Result < ( msgs:: Shutdown , Option < ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) , APIError >
4435
+ where K :: Target : KeysInterface < Signer = Signer > {
4396
4436
for htlc in self . pending_outbound_htlcs . iter ( ) {
4397
4437
if let OutboundHTLCState :: LocalAnnounced ( _) = htlc. state {
4398
4438
return Err ( APIError :: APIMisuseError { err : "Cannot begin shutdown with pending HTLCs. Process pending events first" . to_owned ( ) } ) ;
@@ -4411,7 +4451,16 @@ impl<Signer: Sign> Channel<Signer> {
4411
4451
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 ( ) } ) ;
4412
4452
}
4413
4453
4414
- let closing_script = self . get_closing_scriptpubkey ( ) ;
4454
+ let monitor_update = if self . shutdown_scriptpubkey . is_none ( ) {
4455
+ self . shutdown_scriptpubkey = Some ( keys_provider. get_shutdown_scriptpubkey ( ) ) ;
4456
+ self . latest_monitor_update_id += 1 ;
4457
+ Some ( ChannelMonitorUpdate {
4458
+ update_id : self . latest_monitor_update_id ,
4459
+ updates : vec ! [ ChannelMonitorUpdateStep :: ShutdownScript {
4460
+ scriptpubkey: self . get_closing_scriptpubkey( ) ,
4461
+ } ] ,
4462
+ } )
4463
+ } else { None } ;
4415
4464
4416
4465
// From here on out, we may not fail!
4417
4466
if self . channel_state < ChannelState :: FundingSent as u32 {
@@ -4437,8 +4486,8 @@ impl<Signer: Sign> Channel<Signer> {
4437
4486
4438
4487
Ok ( ( msgs:: Shutdown {
4439
4488
channel_id : self . channel_id ,
4440
- scriptpubkey : closing_script ,
4441
- } , dropped_outbound_htlcs) )
4489
+ scriptpubkey : self . get_closing_scriptpubkey ( ) ,
4490
+ } , monitor_update , dropped_outbound_htlcs) )
4442
4491
}
4443
4492
4444
4493
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
0 commit comments