@@ -3562,29 +3562,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3562
3562
} => {
3563
3563
let channel_id = self . channel_id ;
3564
3564
let counterparty_node_id = self . counterparty_node_id ;
3565
- let mut htlc_descriptors = Vec :: with_capacity ( htlcs. len ( ) ) ;
3566
- for htlc in htlcs {
3567
- htlc_descriptors. push ( HTLCDescriptor {
3568
- channel_derivation_parameters : ChannelDerivationParameters {
3569
- keys_id : self . channel_keys_id ,
3570
- value_satoshis : self . funding . channel_parameters . channel_value_satoshis ,
3571
- transaction_parameters : self . funding . channel_parameters . clone ( ) ,
3572
- } ,
3573
- commitment_txid : htlc. commitment_txid ,
3574
- per_commitment_number : htlc. per_commitment_number ,
3575
- per_commitment_point : htlc. per_commitment_point ,
3576
- feerate_per_kw : 0 ,
3577
- htlc : htlc. htlc ,
3578
- preimage : htlc. preimage ,
3579
- counterparty_sig : htlc. counterparty_sig ,
3580
- } ) ;
3581
- }
3582
3565
ret. push ( Event :: BumpTransaction ( BumpTransactionEvent :: HTLCResolution {
3583
3566
channel_id,
3584
3567
counterparty_node_id,
3585
3568
claim_id,
3586
3569
target_feerate_sat_per_1000_weight,
3587
- htlc_descriptors,
3570
+ htlc_descriptors : htlcs ,
3588
3571
tx_lock_time,
3589
3572
} ) ) ;
3590
3573
}
@@ -3973,14 +3956,57 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3973
3956
( claimable_outpoints, outputs_to_watch)
3974
3957
}
3975
3958
3959
+ fn get_broadcasted_holder_htlc_descriptors (
3960
+ & self , holder_tx : & HolderCommitmentTransaction ,
3961
+ ) -> Vec < HTLCDescriptor > {
3962
+ let tx = holder_tx. trust ( ) ;
3963
+ let mut htlcs = Vec :: with_capacity ( holder_tx. htlcs ( ) . len ( ) ) ;
3964
+ for ( idx, htlc) in holder_tx. htlcs ( ) . iter ( ) . enumerate ( ) {
3965
+ assert ! ( htlc. transaction_output_index. is_some( ) , "Expected transaction output index for non-dust HTLC" ) ;
3966
+
3967
+ let counterparty_sig =
3968
+ if let Some ( counterparty_sig) = holder_tx. counterparty_htlc_sigs . get ( idx) {
3969
+ * counterparty_sig
3970
+ } else {
3971
+ debug_assert ! ( false , "All non-dust HTLCs must have a corresponding counterparty signature" ) ;
3972
+ continue ;
3973
+ } ;
3974
+
3975
+ let preimage = if htlc. offered {
3976
+ None
3977
+ } else if let Some ( ( preimage, _) ) = self . payment_preimages . get ( & htlc. payment_hash ) {
3978
+ Some ( * preimage)
3979
+ } else {
3980
+ // We can't build an HTLC-Success transaction without the preimage
3981
+ continue ;
3982
+ } ;
3983
+
3984
+ htlcs. push ( HTLCDescriptor {
3985
+ // TODO(splicing): Consider alternative funding scopes.
3986
+ channel_derivation_parameters : ChannelDerivationParameters {
3987
+ value_satoshis : self . funding . channel_parameters . channel_value_satoshis ,
3988
+ keys_id : self . channel_keys_id ,
3989
+ transaction_parameters : self . funding . channel_parameters . clone ( ) ,
3990
+ } ,
3991
+ commitment_txid : tx. txid ( ) ,
3992
+ per_commitment_number : tx. commitment_number ( ) ,
3993
+ per_commitment_point : tx. per_commitment_point ( ) ,
3994
+ feerate_per_kw : tx. feerate_per_kw ( ) ,
3995
+ htlc : htlc. clone ( ) ,
3996
+ preimage,
3997
+ counterparty_sig,
3998
+ } ) ;
3999
+ }
4000
+
4001
+ htlcs
4002
+ }
4003
+
3976
4004
// Returns (1) `PackageTemplate`s that can be given to the OnchainTxHandler, so that the handler can
3977
4005
// broadcast transactions claiming holder HTLC commitment outputs and (2) a holder revokable
3978
4006
// script so we can detect whether a holder transaction has been seen on-chain.
3979
4007
fn get_broadcasted_holder_claims (
3980
4008
& self , holder_tx : & HolderCommitmentTransaction , conf_height : u32 ,
3981
4009
) -> ( Vec < PackageTemplate > , Option < ( ScriptBuf , PublicKey , RevocationKey ) > ) {
3982
- let mut claim_requests = Vec :: with_capacity ( holder_tx. htlcs ( ) . len ( ) ) ;
3983
-
3984
4010
let tx = holder_tx. trust ( ) ;
3985
4011
let keys = tx. keys ( ) ;
3986
4012
let redeem_script = chan_utils:: get_revokeable_redeemscript (
@@ -3990,36 +4016,22 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3990
4016
redeem_script. to_p2wsh ( ) , holder_tx. per_commitment_point ( ) , keys. revocation_key . clone ( ) ,
3991
4017
) ) ;
3992
4018
3993
- let txid = tx. txid ( ) ;
3994
- for htlc in holder_tx. htlcs ( ) {
3995
- if let Some ( transaction_output_index) = htlc. transaction_output_index {
3996
- let ( htlc_output, counterparty_spendable_height) = if htlc. offered {
3997
- let htlc_output = HolderHTLCOutput :: build_offered (
3998
- htlc. amount_msat , htlc. cltv_expiry , self . channel_type_features ( ) . clone ( )
3999
- ) ;
4000
- ( htlc_output, conf_height)
4019
+ let claim_requests = self . get_broadcasted_holder_htlc_descriptors ( holder_tx) . into_iter ( )
4020
+ . map ( |htlc_descriptor| {
4021
+ let counterparty_spendable_height = if htlc_descriptor. htlc . offered {
4022
+ conf_height
4001
4023
} else {
4002
- let payment_preimage = if let Some ( ( preimage, _) ) = self . payment_preimages . get ( & htlc. payment_hash ) {
4003
- preimage. clone ( )
4004
- } else {
4005
- // We can't build an HTLC-Success transaction without the preimage
4006
- continue ;
4007
- } ;
4008
- let htlc_output = HolderHTLCOutput :: build_accepted (
4009
- payment_preimage, htlc. amount_msat , self . channel_type_features ( ) . clone ( )
4010
- ) ;
4011
- ( htlc_output, htlc. cltv_expiry )
4024
+ htlc_descriptor. htlc . cltv_expiry
4012
4025
} ;
4013
- let htlc_package = PackageTemplate :: build_package (
4014
- txid, transaction_output_index,
4015
- PackageSolvingData :: HolderHTLCOutput ( htlc_output) ,
4026
+ let transaction_output_index = htlc_descriptor. htlc . transaction_output_index
4027
+ . expect ( "Expected transaction output index for non-dust HTLC" ) ;
4028
+ PackageTemplate :: build_package (
4029
+ tx. txid ( ) , transaction_output_index,
4030
+ PackageSolvingData :: HolderHTLCOutput ( HolderHTLCOutput :: build ( htlc_descriptor) ) ,
4016
4031
counterparty_spendable_height,
4017
- ) ;
4018
- claim_requests. push ( htlc_package) ;
4019
- } else {
4020
- debug_assert ! ( false , "Expected transaction output index for non-dust HTLC" ) ;
4021
- }
4022
- }
4032
+ )
4033
+ } )
4034
+ . collect ( ) ;
4023
4035
4024
4036
( claim_requests, broadcasted_holder_revokable_script)
4025
4037
}
@@ -4153,33 +4165,36 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
4153
4165
& mut self , logger : & WithChannelMonitor < L >
4154
4166
) -> Vec < Transaction > where L :: Target : Logger {
4155
4167
log_debug ! ( logger, "Getting signed copy of latest holder commitment transaction!" ) ;
4156
- let commitment_tx = self . onchain_tx_handler . get_fully_signed_copy_holder_tx ( & self . funding . redeem_script ) ;
4157
- let txid = commitment_tx. compute_txid ( ) ;
4168
+ let commitment_tx = {
4169
+ let sig = self . onchain_tx_handler . signer . unsafe_sign_holder_commitment (
4170
+ & self . funding . channel_parameters , & self . funding . current_holder_commitment . tx ,
4171
+ & self . onchain_tx_handler . secp_ctx ,
4172
+ ) . expect ( "sign holder commitment" ) ;
4173
+ self . funding . current_holder_commitment . tx . add_holder_sig ( & self . funding . redeem_script , sig)
4174
+ } ;
4158
4175
let mut holder_transactions = vec ! [ commitment_tx] ;
4159
4176
// When anchor outputs are present, the HTLC transactions are only final once the commitment
4160
4177
// transaction confirms due to the CSV 1 encumberance.
4161
4178
if self . channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
4162
4179
return holder_transactions;
4163
4180
}
4164
- for htlc in self . funding . current_holder_commitment . tx . htlcs ( ) {
4165
- if let Some ( vout ) = htlc . transaction_output_index {
4166
- let preimage = if !htlc . offered {
4167
- if let Some ( ( preimage , _ ) ) = self . payment_preimages . get ( & htlc . payment_hash ) { Some ( preimage . clone ( ) ) } else {
4168
- // We can't build an HTLC-Success transaction without the preimage
4169
- continue ;
4170
- }
4171
- } else { None } ;
4172
- if let Some ( htlc_tx) = self . onchain_tx_handler . get_maybe_signed_htlc_tx (
4173
- & :: bitcoin:: OutPoint { txid, vout } , & preimage
4181
+
4182
+ self . get_broadcasted_holder_htlc_descriptors ( & self . funding . current_holder_commitment . tx )
4183
+ . into_iter ( )
4184
+ . for_each ( |htlc_descriptor| {
4185
+ let txid = self . funding . current_holder_commitment . tx . trust ( ) . txid ( ) ;
4186
+ let vout = htlc_descriptor . htlc . transaction_output_index
4187
+ . expect ( "Expected transaction output index for non-dust HTLC" ) ;
4188
+ let htlc_output = HolderHTLCOutput :: build ( htlc_descriptor ) ;
4189
+ if let Some ( htlc_tx) = htlc_output . get_maybe_signed_htlc_tx (
4190
+ & mut self . onchain_tx_handler , & :: bitcoin:: OutPoint { txid, vout } ,
4174
4191
) {
4175
4192
if htlc_tx. is_fully_signed ( ) {
4176
4193
holder_transactions. push ( htlc_tx. 0 ) ;
4177
4194
}
4178
4195
}
4179
- } else {
4180
- debug_assert ! ( false , "Expected transaction output index for non-dust HTLC" ) ;
4181
- }
4182
- }
4196
+ } ) ;
4197
+
4183
4198
holder_transactions
4184
4199
}
4185
4200
0 commit comments