Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1fa7497

Browse files
committedJan 29, 2024
Add payment claimable path
1 parent 488090a commit 1fa7497

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed
 

‎lightning/src/ln/channelmanager.rs

+31-8
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ struct ClaimablePayment {
674674
purpose: events::PaymentPurpose,
675675
onion_fields: Option<RecipientOnionFields>,
676676
htlcs: Vec<ClaimableHTLC>,
677+
amount_msat: Option<u64>,
677678
}
678679

679680
/// Information about claimable or being-claimed payments
@@ -3504,7 +3505,10 @@ where
35043505
let best_block_height = self.best_block.read().unwrap().height();
35053506
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
35063507
let mut preimage: Option<PaymentPreimage> = None;
3507-
if let Some(payment_secret) = recipient_onion.payment_secret {
3508+
let mut payment_secret = PaymentSecret([0; 32]);
3509+
let mut is_self_pay = false;
3510+
if let Some(secret) = recipient_onion.payment_secret {
3511+
payment_secret = secret;
35083512
if let Payee::Clear{node_id, .. } = route_params.payment_params.payee {
35093513
let is_phantom_payee = match self.node_signer.get_node_id(Recipient::PhantomNode) {
35103514
Ok(phantom_node_id) => node_id == phantom_node_id,
@@ -3513,15 +3517,26 @@ where
35133517
if node_id == self.get_our_node_id() || is_phantom_payee {
35143518
let payment_data = msgs::FinalOnionHopData{ payment_secret, total_msat: route_params.final_value_msat};
35153519
preimage = inbound_payment::verify(payment_hash, &payment_data, self.highest_seen_timestamp.load(Ordering::Acquire) as u64, &self.inbound_payment_key, &self.logger).map_err(|_| RetryableSendFailure::RecipientRejected)?.0;
3516-
// create a pending inbound payment
3520+
is_self_pay = true;
35173521
}
35183522
}
35193523
}
3524+
35203525
self.pending_outbound_payments
3521-
.send_payment(payment_hash, recipient_onion, payment_id, retry_strategy, route_params, preimage,
3526+
.send_payment(payment_hash, recipient_onion.clone(), payment_id, retry_strategy, route_params.clone(), preimage,
35223527
&self.router, self.list_usable_channels(), || self.compute_inflight_htlcs(),
35233528
&self.entropy_source, &self.node_signer, best_block_height, &self.logger,
3524-
&self.pending_events, |args| self.send_payment_along_path(args))
3529+
&self.pending_events, |args| self.send_payment_along_path(args))?;
3530+
3531+
if is_self_pay {
3532+
let mut claimable_payments = self.claimable_payments.lock().unwrap();
3533+
let purpose = events::PaymentPurpose::InvoicePayment { payment_preimage: preimage, payment_secret };
3534+
claimable_payments.claimable_payments.insert(payment_hash, ClaimablePayment{ purpose: purpose.clone(), onion_fields: Some(recipient_onion.clone()), htlcs: vec![], amount_msat: Some(route_params.final_value_msat)});
3535+
let mut pending_events = self.pending_events.lock().unwrap();
3536+
pending_events.push_back((events::Event::PaymentClaimable { receiver_node_id: Some(self.get_our_node_id()), payment_hash, onion_fields: Some(recipient_onion), amount_msat: route_params.final_value_msat, counterparty_skimmed_fee_msat: 0, purpose, via_channel_id: None, via_user_channel_id: None, claim_deadline: None }, None));
3537+
}
3538+
3539+
Ok(())
35253540
}
35263541

35273542
#[cfg(test)]
@@ -4561,7 +4576,7 @@ where
45614576
.or_insert_with(|| {
45624577
committed_to_claimable = true;
45634578
ClaimablePayment {
4564-
purpose: $purpose.clone(), htlcs: Vec::new(), onion_fields: None,
4579+
purpose: $purpose.clone(), htlcs: Vec::new(), onion_fields: None, amount_msat: None,
45654580
}
45664581
});
45674582
if $purpose != claimable_payment.purpose {
@@ -5404,6 +5419,14 @@ where
54045419
let mut claimable_payments = self.claimable_payments.lock().unwrap();
54055420
if let Some(payment) = claimable_payments.claimable_payments.remove(&payment_hash) {
54065421
let mut receiver_node_id = self.our_network_pubkey;
5422+
if let events::PaymentPurpose::InvoicePayment { payment_secret, .. } = payment.purpose {
5423+
if let Ok(_) = self.get_payment_preimage(payment_hash, payment_secret) {
5424+
let mut pending_events_lock = self.pending_events.lock().unwrap();
5425+
pending_events_lock.push_back((Event::PaymentClaimed { receiver_node_id: Some(receiver_node_id), payment_hash,
5426+
amount_msat: payment.amount_msat.unwrap(), purpose: payment.purpose, htlcs: vec![], sender_intended_total_msat: None }, None));
5427+
return;
5428+
}
5429+
}
54075430
for htlc in payment.htlcs.iter() {
54085431
if htlc.prev_hop.phantom_shared_secret.is_some() {
54095432
let phantom_pubkey = self.node_signer.get_node_id(Recipient::PhantomNode)
@@ -10879,14 +10902,14 @@ where
1087910902
purposes.into_iter().zip(onion_fields.into_iter().zip(claimable_htlcs_list.into_iter()))
1088010903
{
1088110904
let existing_payment = claimable_payments.insert(payment_hash, ClaimablePayment {
10882-
purpose, htlcs, onion_fields: onion,
10905+
purpose, htlcs, onion_fields: onion, amount_msat: None,
1088310906
});
1088410907
if existing_payment.is_some() { return Err(DecodeError::InvalidValue); }
1088510908
}
1088610909
} else {
1088710910
for (purpose, (payment_hash, htlcs)) in purposes.into_iter().zip(claimable_htlcs_list.into_iter()) {
1088810911
let existing_payment = claimable_payments.insert(payment_hash, ClaimablePayment {
10889-
purpose, htlcs, onion_fields: None,
10912+
purpose, htlcs, onion_fields: None, amount_msat: None,
1089010913
});
1089110914
if existing_payment.is_some() { return Err(DecodeError::InvalidValue); }
1089210915
}
@@ -10920,7 +10943,7 @@ where
1092010943
events::PaymentPurpose::SpontaneousPayment(*payment_preimage),
1092110944
};
1092210945
claimable_payments.insert(payment_hash, ClaimablePayment {
10923-
purpose, htlcs, onion_fields: None,
10946+
purpose, htlcs, onion_fields: None, amount_msat: None,
1092410947
});
1092510948
}
1092610949
}

‎lightning/src/ln/outbound_payment.rs

+1-1
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, Event, PaymentPurpose};
17+
use crate::events::{self, PaymentFailureReason, Event};
1818
use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
1919
use crate::ln::channelmanager::{ChannelDetails, EventCompletionAction, HTLCSource, PaymentId};
2020
use crate::ln::onion_utils::{DecodedOnionFailure, HTLCFailReason};

0 commit comments

Comments
 (0)
Please sign in to comment.