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