@@ -647,6 +647,7 @@ struct IrrevocablyResolvedHTLC {
647
647
/// was not present in the confirmed commitment transaction), HTLC-Success, or HTLC-Timeout
648
648
/// transaction.
649
649
resolving_txid : Option < Txid > , // Added as optional, but always filled in, in 0.0.110
650
+ resolving_tx : Option < Transaction > ,
650
651
/// Only set if the HTLC claim was ours using a payment preimage
651
652
payment_preimage : Option < PaymentPreimage > ,
652
653
}
@@ -662,6 +663,7 @@ impl Writeable for IrrevocablyResolvedHTLC {
662
663
( 0 , mapped_commitment_tx_output_idx, required) ,
663
664
( 1 , self . resolving_txid, option) ,
664
665
( 2 , self . payment_preimage, option) ,
666
+ ( 3 , self . resolving_tx, option) ,
665
667
} ) ;
666
668
Ok ( ( ) )
667
669
}
@@ -672,15 +674,18 @@ impl Readable for IrrevocablyResolvedHTLC {
672
674
let mut mapped_commitment_tx_output_idx = 0 ;
673
675
let mut resolving_txid = None ;
674
676
let mut payment_preimage = None ;
677
+ let mut resolving_tx = None ;
675
678
read_tlv_fields ! ( reader, {
676
679
( 0 , mapped_commitment_tx_output_idx, required) ,
677
680
( 1 , resolving_txid, option) ,
678
681
( 2 , payment_preimage, option) ,
682
+ ( 3 , resolving_tx, option) ,
679
683
} ) ;
680
684
Ok ( Self {
681
685
commitment_tx_output_idx : if mapped_commitment_tx_output_idx == u32:: max_value ( ) { None } else { Some ( mapped_commitment_tx_output_idx) } ,
682
686
resolving_txid,
683
687
payment_preimage,
688
+ resolving_tx,
684
689
} )
685
690
}
686
691
}
@@ -1517,23 +1522,26 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1517
1522
if let Some ( v) = htlc. transaction_output_index { v } else { return None ; } ;
1518
1523
1519
1524
let mut htlc_spend_txid_opt = None ;
1525
+ let mut htlc_spend_tx_opt = None ;
1520
1526
let mut holder_timeout_spend_pending = None ;
1521
1527
let mut htlc_spend_pending = None ;
1522
1528
let mut holder_delayed_output_pending = None ;
1523
1529
for event in self . onchain_events_awaiting_threshold_conf . iter ( ) {
1524
1530
match event. event {
1525
1531
OnchainEvent :: HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
1526
1532
if commitment_tx_output_idx == Some ( htlc_commitment_tx_output_idx) => {
1527
- debug_assert ! ( htlc_spend_txid_opt. is_none( ) ) ;
1528
- htlc_spend_txid_opt = event. transaction . as_ref ( ) . map ( |tx| tx. txid ( ) ) ;
1533
+ htlc_spend_txid_opt = Some ( & event. txid ) ;
1534
+ debug_assert ! ( htlc_spend_tx_opt. is_none( ) ) ;
1535
+ htlc_spend_tx_opt = event. transaction . as_ref ( ) ;
1529
1536
debug_assert ! ( holder_timeout_spend_pending. is_none( ) ) ;
1530
1537
debug_assert_eq ! ( htlc_value_satoshis. unwrap( ) , htlc. amount_msat / 1000 ) ;
1531
1538
holder_timeout_spend_pending = Some ( event. confirmation_threshold ( ) ) ;
1532
1539
} ,
1533
1540
OnchainEvent :: HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. }
1534
1541
if commitment_tx_output_idx == htlc_commitment_tx_output_idx => {
1535
- debug_assert ! ( htlc_spend_txid_opt. is_none( ) ) ;
1536
- htlc_spend_txid_opt = event. transaction . as_ref ( ) . map ( |tx| tx. txid ( ) ) ;
1542
+ htlc_spend_txid_opt = Some ( & event. txid ) ;
1543
+ debug_assert ! ( htlc_spend_tx_opt. is_none( ) ) ;
1544
+ htlc_spend_tx_opt = event. transaction . as_ref ( ) ;
1537
1545
debug_assert ! ( htlc_spend_pending. is_none( ) ) ;
1538
1546
htlc_spend_pending = Some ( ( event. confirmation_threshold ( ) , preimage. is_some ( ) ) ) ;
1539
1547
} ,
@@ -1548,20 +1556,26 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1548
1556
}
1549
1557
let htlc_resolved = self . htlcs_resolved_on_chain . iter ( )
1550
1558
. find ( |v| if v. commitment_tx_output_idx == Some ( htlc_commitment_tx_output_idx) {
1551
- debug_assert ! ( htlc_spend_txid_opt. is_none( ) ) ;
1552
- htlc_spend_txid_opt = v. resolving_txid ;
1559
+ htlc_spend_txid_opt = v. resolving_txid . as_ref ( ) ;
1560
+ debug_assert ! ( htlc_spend_tx_opt. is_none( ) ) ;
1561
+ htlc_spend_tx_opt = v. resolving_tx . as_ref ( ) ;
1553
1562
true
1554
1563
} else { false } ) ;
1555
1564
debug_assert ! ( holder_timeout_spend_pending. is_some( ) as u8 + htlc_spend_pending. is_some( ) as u8 + htlc_resolved. is_some( ) as u8 <= 1 ) ;
1556
1565
1566
+ let htlc_commitment_outpoint = BitcoinOutPoint :: new ( confirmed_txid. unwrap ( ) , htlc_commitment_tx_output_idx) ;
1557
1567
let htlc_output_to_spend =
1558
- if let Some ( txid) = htlc_spend_txid_opt {
1559
- debug_assert ! (
1560
- self . onchain_tx_handler. channel_transaction_parameters. opt_anchors. is_none( ) ,
1561
- "This code needs updating for anchors" ) ;
1562
- BitcoinOutPoint :: new ( txid, 0 )
1568
+ if let Some ( ref tx) = htlc_spend_tx_opt {
1569
+ // Because HTLCs are signed with SIGHASH_SINGLE|ANYONECANPAY under BIP-0143, we can
1570
+ // locate the correct output by ensuring its adjacent input spends the HTLC output in
1571
+ // the commitment.
1572
+ let htlc_input_idx_opt = tx. input . iter ( ) . enumerate ( )
1573
+ . find ( |( _, input) | input. previous_output == htlc_commitment_outpoint)
1574
+ . map ( |( idx, _) | idx as u32 ) ;
1575
+ debug_assert ! ( htlc_input_idx_opt. is_some( ) ) ;
1576
+ BitcoinOutPoint :: new ( * htlc_spend_txid_opt. unwrap ( ) , htlc_input_idx_opt. unwrap_or ( 0 ) )
1563
1577
} else {
1564
- BitcoinOutPoint :: new ( confirmed_txid . unwrap ( ) , htlc_commitment_tx_output_idx )
1578
+ htlc_commitment_outpoint
1565
1579
} ;
1566
1580
let htlc_output_spend_pending = self . onchain_tx_handler . is_output_spend_pending ( & htlc_output_to_spend) ;
1567
1581
@@ -1585,8 +1599,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1585
1599
} = & event. event {
1586
1600
if event. transaction . as_ref ( ) . map ( |tx| tx. input . iter ( ) . any ( |inp| {
1587
1601
if let Some ( htlc_spend_txid) = htlc_spend_txid_opt {
1588
- Some ( tx. txid ( ) ) == htlc_spend_txid_opt ||
1589
- inp. previous_output . txid == htlc_spend_txid
1602
+ tx. txid ( ) == * htlc_spend_txid || inp. previous_output . txid == * htlc_spend_txid
1590
1603
} else {
1591
1604
Some ( inp. previous_output . txid ) == confirmed_txid &&
1592
1605
inp. previous_output . vout == htlc_commitment_tx_output_idx
@@ -3065,7 +3078,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3065
3078
htlc_value_satoshis,
3066
3079
} ) ) ;
3067
3080
self . htlcs_resolved_on_chain . push ( IrrevocablyResolvedHTLC {
3068
- commitment_tx_output_idx, resolving_txid : Some ( entry. txid ) ,
3081
+ commitment_tx_output_idx,
3082
+ resolving_txid : Some ( entry. txid ) ,
3083
+ resolving_tx : entry. transaction ,
3069
3084
payment_preimage : None ,
3070
3085
} ) ;
3071
3086
} ,
@@ -3077,7 +3092,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3077
3092
} ,
3078
3093
OnchainEvent :: HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. } => {
3079
3094
self . htlcs_resolved_on_chain . push ( IrrevocablyResolvedHTLC {
3080
- commitment_tx_output_idx : Some ( commitment_tx_output_idx) , resolving_txid : Some ( entry. txid ) ,
3095
+ commitment_tx_output_idx : Some ( commitment_tx_output_idx) ,
3096
+ resolving_txid : Some ( entry. txid ) ,
3097
+ resolving_tx : entry. transaction ,
3081
3098
payment_preimage : preimage,
3082
3099
} ) ;
3083
3100
} ,
0 commit comments