@@ -2646,12 +2646,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2646
2646
}
2647
2647
2648
2648
/// Attempts to claim a counterparty HTLC-Success/HTLC-Timeout's outputs using the revocation key
2649
- fn check_spend_counterparty_htlc < L : Deref > ( & mut self , tx : & Transaction , commitment_number : u64 , height : u32 , logger : & L ) -> ( Vec < PackageTemplate > , Option < TransactionOutputs > ) where L :: Target : Logger {
2650
- let htlc_txid = tx. txid ( ) ;
2651
- if tx. input . len ( ) != 1 || tx. output . len ( ) != 1 || tx. input [ 0 ] . witness . len ( ) != 5 {
2652
- return ( Vec :: new ( ) , None )
2653
- }
2654
-
2649
+ fn check_spend_counterparty_htlc < L : Deref > (
2650
+ & mut self , tx : & Transaction , commitment_number : u64 , commitment_txid : & Txid , height : u32 , logger : & L
2651
+ ) -> ( Vec < PackageTemplate > , Option < TransactionOutputs > ) where L :: Target : Logger {
2655
2652
macro_rules! ignore_error {
2656
2653
( $thing : expr ) => {
2657
2654
match $thing {
@@ -2665,12 +2662,32 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2665
2662
let per_commitment_key = ignore_error ! ( SecretKey :: from_slice( & secret) ) ;
2666
2663
let per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & per_commitment_key) ;
2667
2664
2668
- log_error ! ( logger, "Got broadcast of revoked counterparty HTLC transaction, spending {}:{}" , htlc_txid, 0 ) ;
2669
- let revk_outp = RevokedOutput :: build ( per_commitment_point, self . counterparty_commitment_params . counterparty_delayed_payment_base_key , self . counterparty_commitment_params . counterparty_htlc_base_key , per_commitment_key, tx. output [ 0 ] . value , self . counterparty_commitment_params . on_counterparty_tx_csv ) ;
2670
- let justice_package = PackageTemplate :: build_package ( htlc_txid, 0 , PackageSolvingData :: RevokedOutput ( revk_outp) , height + self . counterparty_commitment_params . on_counterparty_tx_csv as u32 , true , height) ;
2671
- let claimable_outpoints = vec ! ( justice_package) ;
2672
- let outputs = vec ! [ ( 0 , tx. output[ 0 ] . clone( ) ) ] ;
2673
- ( claimable_outpoints, Some ( ( htlc_txid, outputs) ) )
2665
+ let htlc_txid = tx. txid ( ) ;
2666
+ let mut claimable_outpoints = vec ! [ ] ;
2667
+ let mut outputs_to_watch = None ;
2668
+ for ( idx, input) in tx. input . iter ( ) . enumerate ( ) {
2669
+ // HTLC transactions always spend an output on the commitment transaction with a witness
2670
+ // of 5 elements. The HTLC input will always have a corresponding output at the same
2671
+ // index within the transaction.
2672
+ if input. previous_output . txid == * commitment_txid && input. witness . len ( ) == 5 && tx. output . get ( idx) . is_some ( ) {
2673
+ log_error ! ( logger, "Got broadcast of revoked counterparty HTLC transaction, spending {}:{}" , htlc_txid, idx) ;
2674
+ let revk_outp = RevokedOutput :: build (
2675
+ per_commitment_point, self . counterparty_commitment_params . counterparty_delayed_payment_base_key ,
2676
+ self . counterparty_commitment_params . counterparty_htlc_base_key , per_commitment_key,
2677
+ tx. output [ 0 ] . value , self . counterparty_commitment_params . on_counterparty_tx_csv
2678
+ ) ;
2679
+ let justice_package = PackageTemplate :: build_package (
2680
+ htlc_txid, idx as u32 , PackageSolvingData :: RevokedOutput ( revk_outp) ,
2681
+ height + self . counterparty_commitment_params . on_counterparty_tx_csv as u32 , true , height
2682
+ ) ;
2683
+ claimable_outpoints. push ( justice_package) ;
2684
+ if outputs_to_watch. is_none ( ) {
2685
+ outputs_to_watch = Some ( ( htlc_txid, vec ! [ ] ) ) ;
2686
+ }
2687
+ outputs_to_watch. as_mut ( ) . unwrap ( ) . 1 . push ( ( idx as u32 , tx. output [ idx] . clone ( ) ) ) ;
2688
+ }
2689
+ }
2690
+ ( claimable_outpoints, outputs_to_watch)
2674
2691
}
2675
2692
2676
2693
// Returns (1) `PackageTemplate`s that can be given to the OnchainTxHandler, so that the handler can
@@ -2908,9 +2925,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2908
2925
for tx in & txn_matched {
2909
2926
if tx. input . len ( ) == 1 {
2910
2927
// Assuming our keys were not leaked (in which case we're screwed no matter what),
2911
- // commitment transactions and HTLC transactions will all only ever have one input,
2912
- // which is an easy way to filter out any potential non-matching txn for lazy
2913
- // filters.
2928
+ // commitment transactions and HTLC transactions will all only ever have one input
2929
+ // (except for HTLC transactions for channels with anchor outputs), which is an easy
2930
+ // way to filter out any potential non-matching txn for lazy filters.
2914
2931
let prevout = & tx. input [ 0 ] . previous_output ;
2915
2932
if prevout. txid == self . funding_info . 0 . txid && prevout. vout == self . funding_info . 0 . index as u32 {
2916
2933
let mut balance_spendable_csv = None ;
@@ -2951,7 +2968,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2951
2968
} ) ;
2952
2969
} else {
2953
2970
if let Some ( & commitment_number) = self . counterparty_commitment_txn_on_chain . get ( & prevout. txid ) {
2954
- let ( mut new_outpoints, new_outputs_option) = self . check_spend_counterparty_htlc ( & tx, commitment_number, height, & logger) ;
2971
+ let ( mut new_outpoints, new_outputs_option) = self . check_spend_counterparty_htlc (
2972
+ & tx, commitment_number, & prevout. txid , height, & logger
2973
+ ) ;
2955
2974
claimable_outpoints. append ( & mut new_outpoints) ;
2956
2975
if let Some ( new_outputs) = new_outputs_option {
2957
2976
watch_outputs. push ( new_outputs) ;
0 commit comments