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