@@ -2208,82 +2208,90 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2208
2208
-> ( Vec < PackageTemplate > , CommitmentTxCounterpartyOutputInfo ) {
2209
2209
let mut claimable_outpoints = Vec :: new ( ) ;
2210
2210
let mut to_counterparty_output_info: CommitmentTxCounterpartyOutputInfo = None ;
2211
- if let Some ( htlc_outputs) = self . counterparty_claimable_outpoints . get ( & commitment_txid) {
2212
- if let Some ( per_commitment_points) = self . their_cur_per_commitment_points {
2213
- let per_commitment_point_option =
2214
- // If the counterparty commitment tx is the latest valid state, use their latest
2215
- // per-commitment point
2216
- if per_commitment_points. 0 == commitment_number { Some ( & per_commitment_points. 1 ) }
2217
- else if let Some ( point) = per_commitment_points. 2 . as_ref ( ) {
2218
- // If counterparty commitment tx is the state previous to the latest valid state, use
2219
- // their previous per-commitment point (non-atomicity of revocation means it's valid for
2220
- // them to temporarily have two valid commitment txns from our viewpoint)
2221
- if per_commitment_points. 0 == commitment_number + 1 { Some ( point) } else { None }
2222
- } else { None } ;
2223
- if let Some ( per_commitment_point) = per_commitment_point_option {
2224
- if let Some ( transaction) = tx {
2225
- let revokeable_p2wsh_opt =
2226
- if let Ok ( revocation_pubkey) = chan_utils:: derive_public_revocation_key (
2227
- & self . secp_ctx , & per_commitment_point, & self . holder_revocation_basepoint )
2228
- {
2229
- if let Ok ( delayed_key) = chan_utils:: derive_public_key ( & self . secp_ctx ,
2230
- & per_commitment_point,
2231
- & self . counterparty_commitment_params . counterparty_delayed_payment_base_key )
2232
- {
2233
- Some ( chan_utils:: get_revokeable_redeemscript ( & revocation_pubkey,
2234
- self . counterparty_commitment_params . on_counterparty_tx_csv ,
2235
- & delayed_key) . to_v0_p2wsh ( ) )
2236
- } else {
2237
- debug_assert ! ( false , "Failed to derive a delayed payment key for a commitment state we accepted" ) ;
2238
- None
2239
- }
2240
- } else {
2241
- debug_assert ! ( false , "Failed to derive a revocation pubkey key for a commitment state we accepted" ) ;
2242
- None
2243
- } ;
2244
- if let Some ( revokeable_p2wsh) = revokeable_p2wsh_opt {
2245
- for ( idx, outp) in transaction. output . iter ( ) . enumerate ( ) {
2246
- if outp. script_pubkey == revokeable_p2wsh {
2247
- to_counterparty_output_info =
2248
- Some ( ( idx. try_into ( ) . expect ( "Can't have > 2^32 outputs" ) , outp. value ) ) ;
2249
- }
2250
- }
2251
- }
2211
+
2212
+ let htlc_outputs = match self . counterparty_claimable_outpoints . get ( & commitment_txid) {
2213
+ Some ( outputs) => outputs,
2214
+ None => return ( claimable_outpoints, to_counterparty_output_info) ,
2215
+ } ;
2216
+ let per_commitment_points = match self . their_cur_per_commitment_points {
2217
+ Some ( points) => points,
2218
+ None => return ( claimable_outpoints, to_counterparty_output_info) ,
2219
+ } ;
2220
+
2221
+ let per_commitment_point =
2222
+ // If the counterparty commitment tx is the latest valid state, use their latest
2223
+ // per-commitment point
2224
+ if per_commitment_points. 0 == commitment_number { & per_commitment_points. 1 }
2225
+ else if let Some ( point) = per_commitment_points. 2 . as_ref ( ) {
2226
+ // If counterparty commitment tx is the state previous to the latest valid state, use
2227
+ // their previous per-commitment point (non-atomicity of revocation means it's valid for
2228
+ // them to temporarily have two valid commitment txns from our viewpoint)
2229
+ if per_commitment_points. 0 == commitment_number + 1 {
2230
+ point
2231
+ } else { return ( claimable_outpoints, to_counterparty_output_info) ; }
2232
+ } else { return ( claimable_outpoints, to_counterparty_output_info) ; } ;
2233
+
2234
+ if let Some ( transaction) = tx {
2235
+ let revokeable_p2wsh_opt =
2236
+ if let Ok ( revocation_pubkey) = chan_utils:: derive_public_revocation_key (
2237
+ & self . secp_ctx , & per_commitment_point, & self . holder_revocation_basepoint )
2238
+ {
2239
+ if let Ok ( delayed_key) = chan_utils:: derive_public_key ( & self . secp_ctx ,
2240
+ & per_commitment_point,
2241
+ & self . counterparty_commitment_params . counterparty_delayed_payment_base_key )
2242
+ {
2243
+ Some ( chan_utils:: get_revokeable_redeemscript ( & revocation_pubkey,
2244
+ self . counterparty_commitment_params . on_counterparty_tx_csv ,
2245
+ & delayed_key) . to_v0_p2wsh ( ) )
2246
+ } else {
2247
+ debug_assert ! ( false , "Failed to derive a delayed payment key for a commitment state we accepted" ) ;
2248
+ None
2249
+ }
2250
+ } else {
2251
+ debug_assert ! ( false , "Failed to derive a revocation pubkey key for a commitment state we accepted" ) ;
2252
+ None
2253
+ } ;
2254
+ if let Some ( revokeable_p2wsh) = revokeable_p2wsh_opt {
2255
+ for ( idx, outp) in transaction. output . iter ( ) . enumerate ( ) {
2256
+ if outp. script_pubkey == revokeable_p2wsh {
2257
+ to_counterparty_output_info =
2258
+ Some ( ( idx. try_into ( ) . expect ( "Can't have > 2^32 outputs" ) , outp. value ) ) ;
2252
2259
}
2260
+ }
2261
+ }
2262
+ }
2253
2263
2254
- for ( _, & ( ref htlc, _) ) in htlc_outputs. iter ( ) . enumerate ( ) {
2255
- if let Some ( transaction_output_index) = htlc. transaction_output_index {
2256
- if let Some ( transaction) = tx {
2257
- if transaction_output_index as usize >= transaction. output . len ( ) ||
2258
- transaction. output [ transaction_output_index as usize ] . value != htlc. amount_msat / 1000 {
2259
- // per_commitment_data is corrupt or our commitment signing key leaked!
2260
- return ( claimable_outpoints, to_counterparty_output_info) ;
2261
- }
2262
- }
2263
- let preimage = if htlc. offered { if let Some ( p) = self . payment_preimages . get ( & htlc. payment_hash ) { Some ( * p) } else { None } } else { None } ;
2264
- if preimage. is_some ( ) || !htlc. offered {
2265
- let counterparty_htlc_outp = if htlc. offered {
2266
- PackageSolvingData :: CounterpartyOfferedHTLCOutput (
2267
- CounterpartyOfferedHTLCOutput :: build ( * per_commitment_point,
2268
- self . counterparty_commitment_params . counterparty_delayed_payment_base_key ,
2269
- self . counterparty_commitment_params . counterparty_htlc_base_key ,
2270
- preimage. unwrap ( ) , htlc. clone ( ) ) )
2271
- } else {
2272
- PackageSolvingData :: CounterpartyReceivedHTLCOutput (
2273
- CounterpartyReceivedHTLCOutput :: build ( * per_commitment_point,
2274
- self . counterparty_commitment_params . counterparty_delayed_payment_base_key ,
2275
- self . counterparty_commitment_params . counterparty_htlc_base_key ,
2276
- htlc. clone ( ) ) )
2277
- } ;
2278
- let aggregation = if !htlc. offered { false } else { true } ;
2279
- let counterparty_package = PackageTemplate :: build_package ( commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc. cltv_expiry , aggregation, 0 ) ;
2280
- claimable_outpoints. push ( counterparty_package) ;
2281
- }
2264
+ for ( _, & ( ref htlc, _) ) in htlc_outputs. iter ( ) . enumerate ( ) {
2265
+ if let Some ( transaction_output_index) = htlc. transaction_output_index {
2266
+ if let Some ( transaction) = tx {
2267
+ if transaction_output_index as usize >= transaction. output . len ( ) ||
2268
+ transaction. output [ transaction_output_index as usize ] . value != htlc. amount_msat / 1000 {
2269
+ // per_commitment_data is corrupt or our commitment signing key leaked!
2270
+ return ( claimable_outpoints, to_counterparty_output_info) ;
2282
2271
}
2283
- }
2272
+ }
2273
+ let preimage = if htlc. offered { if let Some ( p) = self . payment_preimages . get ( & htlc. payment_hash ) { Some ( * p) } else { None } } else { None } ;
2274
+ if preimage. is_some ( ) || !htlc. offered {
2275
+ let counterparty_htlc_outp = if htlc. offered {
2276
+ PackageSolvingData :: CounterpartyOfferedHTLCOutput (
2277
+ CounterpartyOfferedHTLCOutput :: build ( * per_commitment_point,
2278
+ self . counterparty_commitment_params . counterparty_delayed_payment_base_key ,
2279
+ self . counterparty_commitment_params . counterparty_htlc_base_key ,
2280
+ preimage. unwrap ( ) , htlc. clone ( ) ) )
2281
+ } else {
2282
+ PackageSolvingData :: CounterpartyReceivedHTLCOutput (
2283
+ CounterpartyReceivedHTLCOutput :: build ( * per_commitment_point,
2284
+ self . counterparty_commitment_params . counterparty_delayed_payment_base_key ,
2285
+ self . counterparty_commitment_params . counterparty_htlc_base_key ,
2286
+ htlc. clone ( ) ) )
2287
+ } ;
2288
+ let aggregation = if !htlc. offered { false } else { true } ;
2289
+ let counterparty_package = PackageTemplate :: build_package ( commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc. cltv_expiry , aggregation, 0 ) ;
2290
+ claimable_outpoints. push ( counterparty_package) ;
2284
2291
}
2285
2292
}
2286
2293
}
2294
+
2287
2295
( claimable_outpoints, to_counterparty_output_info)
2288
2296
}
2289
2297
0 commit comments