@@ -643,6 +643,7 @@ struct IrrevocablyResolvedHTLC {
643
643
/// was not present in the confirmed commitment transaction), HTLC-Success, or HTLC-Timeout
644
644
/// transaction.
645
645
resolving_txid : Option < Txid > , // Added as optional, but always filled in, in 0.0.110
646
+ resolving_tx : Option < Transaction > ,
646
647
/// Only set if the HTLC claim was ours using a payment preimage
647
648
payment_preimage : Option < PaymentPreimage > ,
648
649
}
@@ -658,6 +659,7 @@ impl Writeable for IrrevocablyResolvedHTLC {
658
659
( 0 , mapped_commitment_tx_output_idx, required) ,
659
660
( 1 , self . resolving_txid, option) ,
660
661
( 2 , self . payment_preimage, option) ,
662
+ ( 3 , self . resolving_tx, option) ,
661
663
} ) ;
662
664
Ok ( ( ) )
663
665
}
@@ -668,15 +670,18 @@ impl Readable for IrrevocablyResolvedHTLC {
668
670
let mut mapped_commitment_tx_output_idx = 0 ;
669
671
let mut resolving_txid = None ;
670
672
let mut payment_preimage = None ;
673
+ let mut resolving_tx = None ;
671
674
read_tlv_fields ! ( reader, {
672
675
( 0 , mapped_commitment_tx_output_idx, required) ,
673
676
( 1 , resolving_txid, option) ,
674
677
( 2 , payment_preimage, option) ,
678
+ ( 3 , resolving_tx, option) ,
675
679
} ) ;
676
680
Ok ( Self {
677
681
commitment_tx_output_idx : if mapped_commitment_tx_output_idx == u32:: max_value ( ) { None } else { Some ( mapped_commitment_tx_output_idx) } ,
678
682
resolving_txid,
679
683
payment_preimage,
684
+ resolving_tx,
680
685
} )
681
686
}
682
687
}
@@ -1511,23 +1516,26 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1511
1516
if let Some ( v) = htlc. transaction_output_index { v } else { return None ; } ;
1512
1517
1513
1518
let mut htlc_spend_txid_opt = None ;
1519
+ let mut htlc_spend_tx_opt = None ;
1514
1520
let mut holder_timeout_spend_pending = None ;
1515
1521
let mut htlc_spend_pending = None ;
1516
1522
let mut holder_delayed_output_pending = None ;
1517
1523
for event in self . onchain_events_awaiting_threshold_conf . iter ( ) {
1518
1524
match event. event {
1519
1525
OnchainEvent :: HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
1520
1526
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 ( ) ;
1523
1530
debug_assert ! ( holder_timeout_spend_pending. is_none( ) ) ;
1524
1531
debug_assert_eq ! ( htlc_value_satoshis. unwrap( ) , htlc. amount_msat / 1000 ) ;
1525
1532
holder_timeout_spend_pending = Some ( event. confirmation_threshold ( ) ) ;
1526
1533
} ,
1527
1534
OnchainEvent :: HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. }
1528
1535
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 ( ) ;
1531
1539
debug_assert ! ( htlc_spend_pending. is_none( ) ) ;
1532
1540
htlc_spend_pending = Some ( ( event. confirmation_threshold ( ) , preimage. is_some ( ) ) ) ;
1533
1541
} ,
@@ -1542,20 +1550,26 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1542
1550
}
1543
1551
let htlc_resolved = self . htlcs_resolved_on_chain . iter ( )
1544
1552
. 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 ( ) ;
1547
1556
true
1548
1557
} else { false } ) ;
1549
1558
debug_assert ! ( holder_timeout_spend_pending. is_some( ) as u8 + htlc_spend_pending. is_some( ) as u8 + htlc_resolved. is_some( ) as u8 <= 1 ) ;
1550
1559
1560
+ let htlc_commitment_outpoint = BitcoinOutPoint :: new ( confirmed_txid. unwrap ( ) , htlc_commitment_tx_output_idx) ;
1551
1561
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 ) )
1557
1571
} else {
1558
- BitcoinOutPoint :: new ( confirmed_txid . unwrap ( ) , htlc_commitment_tx_output_idx )
1572
+ htlc_commitment_outpoint
1559
1573
} ;
1560
1574
let htlc_output_spend_pending = self . onchain_tx_handler . is_output_spend_pending ( & htlc_output_to_spend) ;
1561
1575
@@ -1579,8 +1593,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1579
1593
} = & event. event {
1580
1594
if event. transaction . as_ref ( ) . map ( |tx| tx. input . iter ( ) . any ( |inp| {
1581
1595
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
1584
1597
} else {
1585
1598
Some ( inp. previous_output . txid ) == confirmed_txid &&
1586
1599
inp. previous_output . vout == htlc_commitment_tx_output_idx
@@ -3055,7 +3068,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3055
3068
htlc_value_satoshis,
3056
3069
} ) ) ;
3057
3070
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 ,
3059
3074
payment_preimage : None ,
3060
3075
} ) ;
3061
3076
} ,
@@ -3067,7 +3082,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3067
3082
} ,
3068
3083
OnchainEvent :: HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. } => {
3069
3084
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 ,
3071
3088
payment_preimage : preimage,
3072
3089
} ) ;
3073
3090
} ,
0 commit comments