Skip to content

Commit a400c4c

Browse files
Check payment secret before succeeding payment
1 parent 9a7fdba commit a400c4c

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/main.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,34 +149,43 @@ async fn handle_ldk_events(
149149
.funding_transaction_generated(&temporary_channel_id, final_tx)
150150
.unwrap();
151151
}
152-
Event::PaymentReceived { payment_hash, .. } => {
152+
Event::PaymentReceived { payment_hash, payment_secret, amt } => {
153153
let mut payments = inbound_payments.lock().unwrap();
154154
if let Some(payment) = payments.get_mut(&payment_hash) {
155-
assert!(loop_channel_manager.claim_funds(
156-
payment.preimage.unwrap().clone(),
157-
&payment.secret,
158-
payment.amt_msat.0.unwrap(),
159-
));
160-
println!(
161-
"\nEVENT: received payment from payment_hash {} of {} millisatoshis",
162-
hex_utils::hex_str(&payment_hash.0),
163-
payment.amt_msat
164-
);
165-
print!("> ");
166-
io::stdout().flush().unwrap();
167-
payment.status = HTLCStatus::Succeeded;
155+
if payment.secret == payment_secret {
156+
assert!(loop_channel_manager.claim_funds(
157+
payment.preimage.unwrap().clone(),
158+
&payment.secret,
159+
payment.amt_msat.0.unwrap(),
160+
));
161+
println!(
162+
"\nEVENT: received payment from payment hash {} of {} millisatoshis",
163+
hex_utils::hex_str(&payment_hash.0),
164+
payment.amt_msat
165+
);
166+
print!("> ");
167+
io::stdout().flush().unwrap();
168+
payment.status = HTLCStatus::Succeeded;
169+
} else {
170+
loop_channel_manager
171+
.fail_htlc_backwards(&payment_hash, &payment.secret);
172+
println!("\nERROR: we received a payment from payment hash {} but the payment secret didn't match", hex_utils::hex_str(&payment_hash.0));
173+
print!("> ");
174+
io::stdout().flush().unwrap();
175+
payment.status = HTLCStatus::Failed;
176+
}
168177
} else {
169-
println!("\nERROR: we received a payment but didn't know the preimage");
178+
loop_channel_manager.fail_htlc_backwards(&payment_hash, &payment_secret);
179+
println!("\nERROR: we received a payment for payment hash {} but didn't know the preimage", hex_utils::hex_str(&payment_hash.0));
170180
print!("> ");
171181
io::stdout().flush().unwrap();
172-
loop_channel_manager.fail_htlc_backwards(&payment_hash, &None);
173182
payments.insert(
174183
payment_hash,
175184
PaymentInfo {
176185
preimage: None,
177-
secret: None,
186+
secret: payment_secret,
178187
status: HTLCStatus::Failed,
179-
amt_msat: MillisatAmount(None),
188+
amt_msat: MillisatAmount(Some(amt)),
180189
},
181190
);
182191
}

0 commit comments

Comments
 (0)