Skip to content

Commit d5f4fba

Browse files
feat: allow self payment
This PR solves issue #2462. If we asked to pay an invoice that we generated ourselves. We generate PaymentSent and PaymentClaimable event and mark the payment as fulfilled in our set of outbound payments. This PR is important because we realized users can easily screw up self payments when they implement it themselves. See here: https://lists.linuxfoundation.org/pipermail/lightning-dev/2023-June/003983.html
1 parent 281a0ae commit d5f4fba

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

lightning/src/ln/outbound_payment.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1414
use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
1515

1616
use crate::sign::{EntropySource, NodeSigner, Recipient};
17-
use crate::events::{self, PaymentFailureReason};
17+
use crate::events::{self, PaymentFailureReason, Event, PaymentPurpose};
1818
use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
1919
use crate::ln::channelmanager::{ChannelDetails, EventCompletionAction, HTLCSource, PaymentId};
2020
use crate::ln::onion_utils::{DecodedOnionFailure, HTLCFailReason};
@@ -902,15 +902,41 @@ impl OutboundPayments {
902902
}
903903
}
904904

905-
let mut route = router.find_route_with_id(
906-
&node_signer.get_node_id(Recipient::Node).unwrap(), &route_params,
905+
let payer = node_signer.get_node_id(Recipient::Node).unwrap();
906+
let mut route;
907+
match router.find_route_with_id(
908+
&payer,&route_params,
907909
Some(&first_hops.iter().collect::<Vec<_>>()), inflight_htlcs(),
908910
payment_hash, payment_id,
909-
).map_err(|_| {
910-
log_error!(logger, "Failed to find route for payment with id {} and hash {}",
911-
payment_id, payment_hash);
912-
RetryableSendFailure::RouteNotFound
913-
})?;
911+
) {
912+
Ok(res) => route = res,
913+
Err(error) => {
914+
if error.err == "Cannot generate a route to ourselves" {
915+
let payment_secret = match recipient_onion.payment_secret {
916+
Some(secret) => secret,
917+
None => PaymentSecret([0; 32])
918+
};
919+
let payment_preimage = PaymentPreimage([0; 32]);
920+
let payment_purpose = PaymentPurpose::InvoicePayment {
921+
payment_preimage: Some(payment_preimage),
922+
payment_secret,
923+
};
924+
let mut pending_outbounds_lock = self.pending_outbound_payments.lock().unwrap();
925+
let payment = pending_outbounds_lock.get_mut(&payment_id).unwrap();
926+
payment.mark_fulfilled();
927+
let mut pending_events_lock = pending_events.lock().unwrap();
928+
pending_events_lock.push_back((Event::PaymentSent { payment_id: Some(payment_id), payment_preimage,
929+
payment_hash, fee_paid_msat: None }, None));
930+
pending_events_lock.push_back((Event::PaymentClaimable { receiver_node_id: Some(payer), payment_hash,
931+
onion_fields: Some(recipient_onion), amount_msat: route_params.final_value_msat, counterparty_skimmed_fee_msat: 0,
932+
purpose: payment_purpose, via_channel_id: None, via_user_channel_id: None, claim_deadline: None }, None));
933+
return Ok(());
934+
}
935+
log_error!(logger, "Failed to find route for payment with id {} and hash {}",
936+
payment_id, payment_hash);
937+
return Err(RetryableSendFailure::RouteNotFound);
938+
}
939+
}
914940

915941
if route.route_params.as_ref() != Some(&route_params) {
916942
debug_assert!(false,

0 commit comments

Comments
 (0)