@@ -3562,29 +3562,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35623562 } => {
35633563 let channel_id = self . channel_id ;
35643564 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- }
35823565 ret. push ( Event :: BumpTransaction ( BumpTransactionEvent :: HTLCResolution {
35833566 channel_id,
35843567 counterparty_node_id,
35853568 claim_id,
35863569 target_feerate_sat_per_1000_weight,
3587- htlc_descriptors,
3570+ htlc_descriptors : htlcs ,
35883571 tx_lock_time,
35893572 } ) ) ;
35903573 }
@@ -3973,14 +3956,50 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
39733956 ( claimable_outpoints, outputs_to_watch)
39743957 }
39753958
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+ debug_assert_eq ! ( holder_tx. htlcs( ) . len( ) , holder_tx. counterparty_htlc_sigs. len( ) ) ;
3965+ for ( htlc, counterparty_sig) in holder_tx. htlcs ( ) . iter ( ) . zip ( holder_tx. counterparty_htlc_sigs . iter ( ) ) {
3966+ assert ! ( htlc. transaction_output_index. is_some( ) , "Expected transaction output index for non-dust HTLC" ) ;
3967+
3968+ let preimage = if htlc. offered {
3969+ None
3970+ } else if let Some ( ( preimage, _) ) = self . payment_preimages . get ( & htlc. payment_hash ) {
3971+ Some ( * preimage)
3972+ } else {
3973+ // We can't build an HTLC-Success transaction without the preimage
3974+ continue ;
3975+ } ;
3976+
3977+ htlcs. push ( HTLCDescriptor {
3978+ // TODO(splicing): Consider alternative funding scopes.
3979+ channel_derivation_parameters : ChannelDerivationParameters {
3980+ value_satoshis : self . funding . channel_parameters . channel_value_satoshis ,
3981+ keys_id : self . channel_keys_id ,
3982+ transaction_parameters : self . funding . channel_parameters . clone ( ) ,
3983+ } ,
3984+ commitment_txid : tx. txid ( ) ,
3985+ per_commitment_number : tx. commitment_number ( ) ,
3986+ per_commitment_point : tx. per_commitment_point ( ) ,
3987+ feerate_per_kw : tx. feerate_per_kw ( ) ,
3988+ htlc : htlc. clone ( ) ,
3989+ preimage,
3990+ counterparty_sig : * counterparty_sig,
3991+ } ) ;
3992+ }
3993+
3994+ htlcs
3995+ }
3996+
39763997 // Returns (1) `PackageTemplate`s that can be given to the OnchainTxHandler, so that the handler can
39773998 // broadcast transactions claiming holder HTLC commitment outputs and (2) a holder revokable
39783999 // script so we can detect whether a holder transaction has been seen on-chain.
39794000 fn get_broadcasted_holder_claims (
39804001 & self , holder_tx : & HolderCommitmentTransaction , conf_height : u32 ,
39814002 ) -> ( Vec < PackageTemplate > , Option < ( ScriptBuf , PublicKey , RevocationKey ) > ) {
3982- let mut claim_requests = Vec :: with_capacity ( holder_tx. htlcs ( ) . len ( ) ) ;
3983-
39844003 let tx = holder_tx. trust ( ) ;
39854004 let keys = tx. keys ( ) ;
39864005 let redeem_script = chan_utils:: get_revokeable_redeemscript (
@@ -3990,36 +4009,22 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
39904009 redeem_script. to_p2wsh ( ) , holder_tx. per_commitment_point ( ) , keys. revocation_key . clone ( ) ,
39914010 ) ) ;
39924011
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)
4012+ let claim_requests = self . get_broadcasted_holder_htlc_descriptors ( holder_tx) . into_iter ( )
4013+ . map ( |htlc_descriptor| {
4014+ let counterparty_spendable_height = if htlc_descriptor. htlc . offered {
4015+ conf_height
40014016 } 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 )
4017+ htlc_descriptor. htlc . cltv_expiry
40124018 } ;
4013- let htlc_package = PackageTemplate :: build_package (
4014- txid, transaction_output_index,
4015- PackageSolvingData :: HolderHTLCOutput ( htlc_output) ,
4019+ let transaction_output_index = htlc_descriptor. htlc . transaction_output_index
4020+ . expect ( "Expected transaction output index for non-dust HTLC" ) ;
4021+ PackageTemplate :: build_package (
4022+ tx. txid ( ) , transaction_output_index,
4023+ PackageSolvingData :: HolderHTLCOutput ( HolderHTLCOutput :: build ( htlc_descriptor) ) ,
40164024 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- }
4025+ )
4026+ } )
4027+ . collect ( ) ;
40234028
40244029 ( claim_requests, broadcasted_holder_revokable_script)
40254030 }
@@ -4153,33 +4158,36 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
41534158 & mut self , logger : & WithChannelMonitor < L >
41544159 ) -> Vec < Transaction > where L :: Target : Logger {
41554160 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 ( ) ;
4161+ let commitment_tx = {
4162+ let sig = self . onchain_tx_handler . signer . unsafe_sign_holder_commitment (
4163+ & self . funding . channel_parameters , & self . funding . current_holder_commitment . tx ,
4164+ & self . onchain_tx_handler . secp_ctx ,
4165+ ) . expect ( "sign holder commitment" ) ;
4166+ self . funding . current_holder_commitment . tx . add_holder_sig ( & self . funding . redeem_script , sig)
4167+ } ;
41584168 let mut holder_transactions = vec ! [ commitment_tx] ;
41594169 // When anchor outputs are present, the HTLC transactions are only final once the commitment
41604170 // transaction confirms due to the CSV 1 encumberance.
41614171 if self . channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
41624172 return holder_transactions;
41634173 }
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
4174+
4175+ self . get_broadcasted_holder_htlc_descriptors ( & self . funding . current_holder_commitment . tx )
4176+ . into_iter ( )
4177+ . for_each ( |htlc_descriptor| {
4178+ let txid = self . funding . current_holder_commitment . tx . trust ( ) . txid ( ) ;
4179+ let vout = htlc_descriptor . htlc . transaction_output_index
4180+ . expect ( "Expected transaction output index for non-dust HTLC" ) ;
4181+ let htlc_output = HolderHTLCOutput :: build ( htlc_descriptor ) ;
4182+ if let Some ( htlc_tx) = htlc_output . get_maybe_signed_htlc_tx (
4183+ & mut self . onchain_tx_handler , & :: bitcoin:: OutPoint { txid, vout } ,
41744184 ) {
41754185 if htlc_tx. is_fully_signed ( ) {
41764186 holder_transactions. push ( htlc_tx. 0 ) ;
41774187 }
41784188 }
4179- } else {
4180- debug_assert ! ( false , "Expected transaction output index for non-dust HTLC" ) ;
4181- }
4182- }
4189+ } ) ;
4190+
41834191 holder_transactions
41844192 }
41854193
0 commit comments