@@ -132,18 +132,6 @@ struct OutboundHTLCOutput {
132
132
fail_reason : Option < HTLCFailReason > ,
133
133
}
134
134
135
- macro_rules! get_htlc_in_commitment {
136
- ( $htlc: expr, $offered: expr) => {
137
- HTLCOutputInCommitment {
138
- offered: $offered,
139
- amount_msat: $htlc. amount_msat,
140
- cltv_expiry: $htlc. cltv_expiry,
141
- payment_hash: $htlc. payment_hash,
142
- transaction_output_index: 0
143
- }
144
- }
145
- }
146
-
147
135
/// See AwaitingRemoteRevoke ChannelState for more info
148
136
enum HTLCUpdateAwaitingACK {
149
137
AddHTLC {
@@ -775,38 +763,46 @@ impl Channel {
775
763
} ;
776
764
777
765
let mut txouts: Vec < ( TxOut , Option < ( HTLCOutputInCommitment , Option < & HTLCSource > ) > ) > = Vec :: with_capacity ( self . pending_inbound_htlcs . len ( ) + self . pending_outbound_htlcs . len ( ) + 2 ) ;
778
- let mut unincluded_htlc_sources: Vec < ( PaymentHash , & HTLCSource , Option < u32 > ) > = Vec :: new ( ) ;
766
+ let mut unincluded_htlc_sources: Vec < ( HTLCOutputInCommitment , Option < & HTLCSource > ) > = Vec :: new ( ) ;
779
767
780
768
let dust_limit_satoshis = if local { self . our_dust_limit_satoshis } else { self . their_dust_limit_satoshis } ;
781
769
let mut remote_htlc_total_msat = 0 ;
782
770
let mut local_htlc_total_msat = 0 ;
783
771
let mut value_to_self_msat_offset = 0 ;
784
772
773
+ macro_rules! get_htlc_in_commitment {
774
+ ( $htlc: expr, $offered: expr) => {
775
+ HTLCOutputInCommitment {
776
+ offered: $offered,
777
+ amount_msat: $htlc. amount_msat,
778
+ cltv_expiry: $htlc. cltv_expiry,
779
+ payment_hash: $htlc. payment_hash,
780
+ transaction_output_index: None
781
+ }
782
+ }
783
+ }
784
+
785
785
macro_rules! add_htlc_output {
786
786
( $htlc: expr, $outbound: expr, $source: expr) => {
787
787
if $outbound == local { // "offered HTLC output"
788
+ let htlc_in_tx = get_htlc_in_commitment!( $htlc, true ) ;
788
789
if $htlc. amount_msat / 1000 >= dust_limit_satoshis + ( feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000 ) {
789
- let htlc_in_tx = get_htlc_in_commitment!( $htlc, true ) ;
790
790
txouts. push( ( TxOut {
791
791
script_pubkey: chan_utils:: get_htlc_redeemscript( & htlc_in_tx, & keys) . to_v0_p2wsh( ) ,
792
792
value: $htlc. amount_msat / 1000
793
793
} , Some ( ( htlc_in_tx, $source) ) ) ) ;
794
794
} else {
795
- if let Some ( source) = $source {
796
- unincluded_htlc_sources. push( ( $htlc. payment_hash, source, None ) ) ;
797
- }
795
+ unincluded_htlc_sources. push( ( htlc_in_tx, $source) ) ;
798
796
}
799
797
} else {
798
+ let htlc_in_tx = get_htlc_in_commitment!( $htlc, false ) ;
800
799
if $htlc. amount_msat / 1000 >= dust_limit_satoshis + ( feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000 ) {
801
- let htlc_in_tx = get_htlc_in_commitment!( $htlc, false ) ;
802
800
txouts. push( ( TxOut { // "received HTLC output"
803
801
script_pubkey: chan_utils:: get_htlc_redeemscript( & htlc_in_tx, & keys) . to_v0_p2wsh( ) ,
804
802
value: $htlc. amount_msat / 1000
805
803
} , Some ( ( htlc_in_tx, $source) ) ) ) ;
806
804
} else {
807
- if let Some ( source) = $source {
808
- unincluded_htlc_sources. push( ( $htlc. payment_hash, source, None ) ) ;
809
- }
805
+ unincluded_htlc_sources. push( ( htlc_in_tx, $source) ) ;
810
806
}
811
807
}
812
808
}
@@ -924,14 +920,18 @@ impl Channel {
924
920
for ( idx, out) in txouts. drain ( ..) . enumerate ( ) {
925
921
outputs. push ( out. 0 ) ;
926
922
if let Some ( ( mut htlc, source_option) ) = out. 1 {
927
- htlc. transaction_output_index = idx as u32 ;
923
+ htlc. transaction_output_index = Some ( idx as u32 ) ;
928
924
if let Some ( source) = source_option {
929
925
htlc_sources. push ( ( htlc. payment_hash , source, Some ( idx as u32 ) ) ) ;
930
926
}
931
927
htlcs_included. push ( htlc) ;
932
928
}
933
929
}
934
- htlc_sources. append ( & mut unincluded_htlc_sources) ;
930
+ for ( htlc, source_option) in unincluded_htlc_sources. drain ( ..) {
931
+ if let Some ( source) = source_option {
932
+ htlc_sources. push ( ( htlc. payment_hash , source, None ) ) ;
933
+ }
934
+ }
935
935
936
936
( Transaction {
937
937
version : 2 ,
@@ -1720,18 +1720,20 @@ impl Channel {
1720
1720
1721
1721
let mut htlcs_and_sigs = Vec :: with_capacity ( local_commitment_tx. 1 . len ( ) ) ;
1722
1722
for ( idx, htlc) in local_commitment_tx. 1 . drain ( ..) . enumerate ( ) {
1723
- let mut htlc_tx = self . build_htlc_transaction ( & local_commitment_txid, & htlc, true , & local_keys, feerate_per_kw) ;
1724
- let htlc_redeemscript = chan_utils:: get_htlc_redeemscript ( & htlc, & local_keys) ;
1725
- let htlc_sighash = Message :: from_slice ( & bip143:: SighashComponents :: new ( & htlc_tx) . sighash_all ( & htlc_tx. input [ 0 ] , & htlc_redeemscript, htlc. amount_msat / 1000 ) [ ..] ) . unwrap ( ) ;
1726
- secp_check ! ( self . secp_ctx. verify( & htlc_sighash, & msg. htlc_signatures[ idx] , & local_keys. b_htlc_key) , "Invalid HTLC tx signature from peer" ) ;
1727
- let htlc_sig = if htlc. offered {
1728
- let htlc_sig = self . sign_htlc_transaction ( & mut htlc_tx, & msg. htlc_signatures [ idx] , & None , & htlc, & local_keys) ?;
1729
- new_local_commitment_txn. push ( htlc_tx) ;
1730
- htlc_sig
1731
- } else {
1732
- self . create_htlc_tx_signature ( & htlc_tx, & htlc, & local_keys) ?. 1
1733
- } ;
1734
- htlcs_and_sigs. push ( ( htlc, msg. htlc_signatures [ idx] , htlc_sig) ) ;
1723
+ if let Some ( _) = htlc. transaction_output_index {
1724
+ let mut htlc_tx = self . build_htlc_transaction ( & local_commitment_txid, & htlc, true , & local_keys, feerate_per_kw) ;
1725
+ let htlc_redeemscript = chan_utils:: get_htlc_redeemscript ( & htlc, & local_keys) ;
1726
+ let htlc_sighash = Message :: from_slice ( & bip143:: SighashComponents :: new ( & htlc_tx) . sighash_all ( & htlc_tx. input [ 0 ] , & htlc_redeemscript, htlc. amount_msat / 1000 ) [ ..] ) . unwrap ( ) ;
1727
+ secp_check ! ( self . secp_ctx. verify( & htlc_sighash, & msg. htlc_signatures[ idx] , & local_keys. b_htlc_key) , "Invalid HTLC tx signature from peer" ) ;
1728
+ let htlc_sig = if htlc. offered {
1729
+ let htlc_sig = self . sign_htlc_transaction ( & mut htlc_tx, & msg. htlc_signatures [ idx] , & None , & htlc, & local_keys) ?;
1730
+ new_local_commitment_txn. push ( htlc_tx) ;
1731
+ htlc_sig
1732
+ } else {
1733
+ self . create_htlc_tx_signature ( & htlc_tx, & htlc, & local_keys) ?. 1
1734
+ } ;
1735
+ htlcs_and_sigs. push ( ( htlc, msg. htlc_signatures [ idx] , htlc_sig) ) ;
1736
+ }
1735
1737
}
1736
1738
1737
1739
let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number - 1 ) ) ;
@@ -3295,11 +3297,13 @@ impl Channel {
3295
3297
let mut htlc_sigs = Vec :: new ( ) ;
3296
3298
3297
3299
for ref htlc in remote_commitment_tx. 1 . iter ( ) {
3298
- let htlc_tx = self . build_htlc_transaction ( & remote_commitment_txid, htlc, false , & remote_keys, feerate_per_kw) ;
3299
- let htlc_redeemscript = chan_utils:: get_htlc_redeemscript ( & htlc, & remote_keys) ;
3300
- let htlc_sighash = Message :: from_slice ( & bip143:: SighashComponents :: new ( & htlc_tx) . sighash_all ( & htlc_tx. input [ 0 ] , & htlc_redeemscript, htlc. amount_msat / 1000 ) [ ..] ) . unwrap ( ) ;
3301
- let our_htlc_key = secp_check ! ( chan_utils:: derive_private_key( & self . secp_ctx, & remote_keys. per_commitment_point, & self . local_keys. htlc_base_key) , "Derived invalid key, peer is maliciously selecting parameters" ) ;
3302
- htlc_sigs. push ( self . secp_ctx . sign ( & htlc_sighash, & our_htlc_key) ) ;
3300
+ if let Some ( _) = htlc. transaction_output_index {
3301
+ let htlc_tx = self . build_htlc_transaction ( & remote_commitment_txid, htlc, false , & remote_keys, feerate_per_kw) ;
3302
+ let htlc_redeemscript = chan_utils:: get_htlc_redeemscript ( & htlc, & remote_keys) ;
3303
+ let htlc_sighash = Message :: from_slice ( & bip143:: SighashComponents :: new ( & htlc_tx) . sighash_all ( & htlc_tx. input [ 0 ] , & htlc_redeemscript, htlc. amount_msat / 1000 ) [ ..] ) . unwrap ( ) ;
3304
+ let our_htlc_key = secp_check ! ( chan_utils:: derive_private_key( & self . secp_ctx, & remote_keys. per_commitment_point, & self . local_keys. htlc_base_key) , "Derived invalid key, peer is maliciously selecting parameters" ) ;
3305
+ htlc_sigs. push ( self . secp_ctx . sign ( & htlc_sighash, & our_htlc_key) ) ;
3306
+ }
3303
3307
}
3304
3308
3305
3309
Ok ( ( msgs:: CommitmentSigned {
0 commit comments