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