@@ -643,6 +643,7 @@ struct IrrevocablyResolvedHTLC {
643643 /// was not present in the confirmed commitment transaction), HTLC-Success, or HTLC-Timeout
644644 /// transaction.
645645 resolving_txid : Option < Txid > , // Added as optional, but always filled in, in 0.0.110
646+ resolving_tx : Option < Transaction > ,
646647 /// Only set if the HTLC claim was ours using a payment preimage
647648 payment_preimage : Option < PaymentPreimage > ,
648649}
@@ -658,6 +659,7 @@ impl Writeable for IrrevocablyResolvedHTLC {
658659 ( 0 , mapped_commitment_tx_output_idx, required) ,
659660 ( 1 , self . resolving_txid, option) ,
660661 ( 2 , self . payment_preimage, option) ,
662+ ( 3 , self . resolving_tx, option) ,
661663 } ) ;
662664 Ok ( ( ) )
663665 }
@@ -668,15 +670,18 @@ impl Readable for IrrevocablyResolvedHTLC {
668670 let mut mapped_commitment_tx_output_idx = 0 ;
669671 let mut resolving_txid = None ;
670672 let mut payment_preimage = None ;
673+ let mut resolving_tx = None ;
671674 read_tlv_fields ! ( reader, {
672675 ( 0 , mapped_commitment_tx_output_idx, required) ,
673676 ( 1 , resolving_txid, option) ,
674677 ( 2 , payment_preimage, option) ,
678+ ( 3 , resolving_tx, option) ,
675679 } ) ;
676680 Ok ( Self {
677681 commitment_tx_output_idx : if mapped_commitment_tx_output_idx == u32:: max_value ( ) { None } else { Some ( mapped_commitment_tx_output_idx) } ,
678682 resolving_txid,
679683 payment_preimage,
684+ resolving_tx,
680685 } )
681686 }
682687}
@@ -1511,23 +1516,26 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
15111516 if let Some ( v) = htlc. transaction_output_index { v } else { return None ; } ;
15121517
15131518 let mut htlc_spend_txid_opt = None ;
1519+ let mut htlc_spend_tx_opt = None ;
15141520 let mut holder_timeout_spend_pending = None ;
15151521 let mut htlc_spend_pending = None ;
15161522 let mut holder_delayed_output_pending = None ;
15171523 for event in self . onchain_events_awaiting_threshold_conf . iter ( ) {
15181524 match event. event {
15191525 OnchainEvent :: HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
15201526 if commitment_tx_output_idx == Some ( htlc_commitment_tx_output_idx) => {
1521- debug_assert ! ( htlc_spend_txid_opt. is_none( ) ) ;
1522- htlc_spend_txid_opt = event. transaction . as_ref ( ) . map ( |tx| tx. txid ( ) ) ;
1527+ htlc_spend_txid_opt = Some ( & event. txid ) ;
1528+ debug_assert ! ( htlc_spend_tx_opt. is_none( ) ) ;
1529+ htlc_spend_tx_opt = event. transaction . as_ref ( ) ;
15231530 debug_assert ! ( holder_timeout_spend_pending. is_none( ) ) ;
15241531 debug_assert_eq ! ( htlc_value_satoshis. unwrap( ) , htlc. amount_msat / 1000 ) ;
15251532 holder_timeout_spend_pending = Some ( event. confirmation_threshold ( ) ) ;
15261533 } ,
15271534 OnchainEvent :: HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. }
15281535 if commitment_tx_output_idx == htlc_commitment_tx_output_idx => {
1529- debug_assert ! ( htlc_spend_txid_opt. is_none( ) ) ;
1530- htlc_spend_txid_opt = event. transaction . as_ref ( ) . map ( |tx| tx. txid ( ) ) ;
1536+ htlc_spend_txid_opt = Some ( & event. txid ) ;
1537+ debug_assert ! ( htlc_spend_tx_opt. is_none( ) ) ;
1538+ htlc_spend_tx_opt = event. transaction . as_ref ( ) ;
15311539 debug_assert ! ( htlc_spend_pending. is_none( ) ) ;
15321540 htlc_spend_pending = Some ( ( event. confirmation_threshold ( ) , preimage. is_some ( ) ) ) ;
15331541 } ,
@@ -1542,20 +1550,26 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
15421550 }
15431551 let htlc_resolved = self . htlcs_resolved_on_chain . iter ( )
15441552 . find ( |v| if v. commitment_tx_output_idx == Some ( htlc_commitment_tx_output_idx) {
1545- debug_assert ! ( htlc_spend_txid_opt. is_none( ) ) ;
1546- htlc_spend_txid_opt = v. resolving_txid ;
1553+ htlc_spend_txid_opt = v. resolving_txid . as_ref ( ) ;
1554+ debug_assert ! ( htlc_spend_tx_opt. is_none( ) ) ;
1555+ htlc_spend_tx_opt = v. resolving_tx . as_ref ( ) ;
15471556 true
15481557 } else { false } ) ;
15491558 debug_assert ! ( holder_timeout_spend_pending. is_some( ) as u8 + htlc_spend_pending. is_some( ) as u8 + htlc_resolved. is_some( ) as u8 <= 1 ) ;
15501559
1560+ let htlc_commitment_outpoint = BitcoinOutPoint :: new ( confirmed_txid. unwrap ( ) , htlc_commitment_tx_output_idx) ;
15511561 let htlc_output_to_spend =
1552- if let Some ( txid) = htlc_spend_txid_opt {
1553- debug_assert ! (
1554- self . onchain_tx_handler. channel_transaction_parameters. opt_anchors. is_none( ) ,
1555- "This code needs updating for anchors" ) ;
1556- BitcoinOutPoint :: new ( txid, 0 )
1562+ if let Some ( ref tx) = htlc_spend_tx_opt {
1563+ // Because HTLCs are signed with SIGHASH_SINGLE|ANYONECANPAY under BIP-0143, we can
1564+ // locate the correct output by ensuring its adjacent input spends the HTLC output in
1565+ // the commitment.
1566+ let htlc_input_idx_opt = tx. input . iter ( ) . enumerate ( )
1567+ . find ( |( _, input) | input. previous_output == htlc_commitment_outpoint)
1568+ . map ( |( idx, _) | idx as u32 ) ;
1569+ debug_assert ! ( htlc_input_idx_opt. is_some( ) ) ;
1570+ BitcoinOutPoint :: new ( * htlc_spend_txid_opt. unwrap ( ) , htlc_input_idx_opt. unwrap_or ( 0 ) )
15571571 } else {
1558- BitcoinOutPoint :: new ( confirmed_txid . unwrap ( ) , htlc_commitment_tx_output_idx )
1572+ htlc_commitment_outpoint
15591573 } ;
15601574 let htlc_output_spend_pending = self . onchain_tx_handler . is_output_spend_pending ( & htlc_output_to_spend) ;
15611575
@@ -1579,8 +1593,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
15791593 } = & event. event {
15801594 if event. transaction . as_ref ( ) . map ( |tx| tx. input . iter ( ) . any ( |inp| {
15811595 if let Some ( htlc_spend_txid) = htlc_spend_txid_opt {
1582- Some ( tx. txid ( ) ) == htlc_spend_txid_opt ||
1583- inp. previous_output . txid == htlc_spend_txid
1596+ tx. txid ( ) == * htlc_spend_txid || inp. previous_output . txid == * htlc_spend_txid
15841597 } else {
15851598 Some ( inp. previous_output . txid ) == confirmed_txid &&
15861599 inp. previous_output . vout == htlc_commitment_tx_output_idx
@@ -3055,7 +3068,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30553068 htlc_value_satoshis,
30563069 } ) ) ;
30573070 self . htlcs_resolved_on_chain . push ( IrrevocablyResolvedHTLC {
3058- commitment_tx_output_idx, resolving_txid : Some ( entry. txid ) ,
3071+ commitment_tx_output_idx,
3072+ resolving_txid : Some ( entry. txid ) ,
3073+ resolving_tx : entry. transaction ,
30593074 payment_preimage : None ,
30603075 } ) ;
30613076 } ,
@@ -3067,7 +3082,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30673082 } ,
30683083 OnchainEvent :: HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. } => {
30693084 self . htlcs_resolved_on_chain . push ( IrrevocablyResolvedHTLC {
3070- commitment_tx_output_idx : Some ( commitment_tx_output_idx) , resolving_txid : Some ( entry. txid ) ,
3085+ commitment_tx_output_idx : Some ( commitment_tx_output_idx) ,
3086+ resolving_txid : Some ( entry. txid ) ,
3087+ resolving_tx : entry. transaction ,
30713088 payment_preimage : preimage,
30723089 } ) ;
30733090 } ,
0 commit comments