Check payment secret before succeeding payment
authorValentine Wallace <vwallace@protonmail.com>
Wed, 14 Apr 2021 18:58:27 +0000 (14:58 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Mon, 3 May 2021 22:31:02 +0000 (18:31 -0400)
src/main.rs

index 700112920c6cb860f6e52e5632fe01a92d351d95..927284aaf7dadb8cf1046772bbbdf8a70808e84b 100644 (file)
@@ -149,34 +149,43 @@ async fn handle_ldk_events(
                                                .funding_transaction_generated(&temporary_channel_id, final_tx)
                                                .unwrap();
                                }
                                                .funding_transaction_generated(&temporary_channel_id, final_tx)
                                                .unwrap();
                                }
-                               Event::PaymentReceived { payment_hash, .. } => {
+                               Event::PaymentReceived { payment_hash, payment_secret, amt } => {
                                        let mut payments = inbound_payments.lock().unwrap();
                                        if let Some(payment) = payments.get_mut(&payment_hash) {
                                        let mut payments = inbound_payments.lock().unwrap();
                                        if let Some(payment) = payments.get_mut(&payment_hash) {
-                                               assert!(loop_channel_manager.claim_funds(
-                                                       payment.preimage.unwrap().clone(),
-                                                       &payment.secret,
-                                                       payment.amt_msat.0.unwrap(),
-                                               ));
-                                               println!(
-                                                       "\nEVENT: received payment from payment_hash {} of {} millisatoshis",
-                                                       hex_utils::hex_str(&payment_hash.0),
-                                                       payment.amt_msat
-                                               );
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
-                                               payment.status = HTLCStatus::Succeeded;
+                                               if payment.secret == payment_secret {
+                                                       assert!(loop_channel_manager.claim_funds(
+                                                               payment.preimage.unwrap().clone(),
+                                                               &payment.secret,
+                                                               payment.amt_msat.0.unwrap(),
+                                                       ));
+                                                       println!(
+                                                               "\nEVENT: received payment from payment hash {} of {} millisatoshis",
+                                                               hex_utils::hex_str(&payment_hash.0),
+                                                               payment.amt_msat
+                                                     );
+                                                       print!("> ");
+                                                       io::stdout().flush().unwrap();
+                                                       payment.status = HTLCStatus::Succeeded;
+                                               } else {
+                                                       loop_channel_manager
+                                                               .fail_htlc_backwards(&payment_hash, &payment.secret);
+                                                       println!("\nERROR: we received a payment from payment hash {} but the payment secret didn't match", hex_utils::hex_str(&payment_hash.0));
+                                                       print!("> ");
+                                                       io::stdout().flush().unwrap();
+                                                       payment.status = HTLCStatus::Failed;
+                                               }
                                        } else {
                                        } else {
-                                               println!("\nERROR: we received a payment but didn't know the preimage");
+                                               loop_channel_manager.fail_htlc_backwards(&payment_hash, &payment_secret);
+                                               println!("\nERROR: we received a payment for payment hash {} but didn't know the preimage", hex_utils::hex_str(&payment_hash.0));
                                                print!("> ");
                                                io::stdout().flush().unwrap();
                                                print!("> ");
                                                io::stdout().flush().unwrap();
-                                               loop_channel_manager.fail_htlc_backwards(&payment_hash, &None);
                                                payments.insert(
                                                        payment_hash,
                                                        PaymentInfo {
                                                                preimage: None,
                                                payments.insert(
                                                        payment_hash,
                                                        PaymentInfo {
                                                                preimage: None,
-                                                               secret: None,
+                                                               secret: payment_secret,
                                                                status: HTLCStatus::Failed,
                                                                status: HTLCStatus::Failed,
-                                                               amt_msat: MillisatAmount(None),
+                                                               amt_msat: MillisatAmount(Some(amt)),
                                                        },
                                                );
                                        }
                                                        },
                                                );
                                        }