Skip to content

Commit 0481bc0

Browse files
committed
f: Introduce create_inbound_payment to OffersMessageFlow
1 parent 9d0b054 commit 0481bc0

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

lightning/src/offers/flow.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,29 @@ impl<MR: Deref> OffersMessageFlow<MR>
308308
where
309309
MR::Target: MessageRouter,
310310
{
311+
/// Gets a payment secret and payment hash for use in an invoice given to a third party wishing
312+
/// to pay us.
313+
///
314+
/// See [`ChannelManager::create_inbound_payment`] for more details.
315+
///
316+
/// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
317+
pub fn create_inbound_payment<ES: Deref>(
318+
&self, entropy_source: ES, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32,
319+
min_final_cltv_expiry_delta: Option<u16>,
320+
) -> Result<(PaymentHash, PaymentSecret), ()>
321+
where
322+
ES::Target: EntropySource,
323+
{
324+
inbound_payment::create(
325+
&self.inbound_payment_key,
326+
min_value_msat,
327+
invoice_expiry_delta_secs,
328+
&entropy_source,
329+
self.highest_seen_timestamp.load(Ordering::Acquire) as u64,
330+
min_final_cltv_expiry_delta,
331+
)
332+
}
333+
311334
/// Verifies an [`InvoiceRequest`] using the provided [`OffersContext`] or the invoice request's own metadata.
312335
///
313336
/// - If an [`OffersContext::InvoiceRequest`] with a `nonce` is provided, verification is performed using recipient context data.
@@ -708,8 +731,8 @@ where
708731
///
709732
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
710733
pub fn create_invoice_builder_from_refund<'a, ES: Deref, R: Deref>(
711-
&'a self, router: &R, entropy_source: ES, refund: &'a Refund, payment_hash: PaymentHash,
712-
payment_secret: PaymentSecret, usable_channels: Vec<ChannelDetails>,
734+
&'a self, router: &R, entropy_source: ES, refund: &'a Refund,
735+
usable_channels: Vec<ChannelDetails>,
713736
) -> Result<InvoiceBuilder<'a, DerivedSigningPubkey>, Bolt12SemanticError>
714737
where
715738
ES::Target: EntropySource,
@@ -721,6 +744,10 @@ where
721744
let amount_msats = refund.amount_msats();
722745
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
723746

747+
let (payment_hash, payment_secret) = self
748+
.create_inbound_payment(entropy, Some(amount_msats), relative_expiry, None)
749+
.map_err(|_| Bolt12SemanticError::InvalidAmount)?;
750+
724751
let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
725752
let payment_paths = self
726753
.create_blinded_payment_paths(
@@ -766,19 +793,27 @@ where
766793
/// - We fail to generate a valid signed [`Bolt12Invoice`] for the [`InvoiceRequest`].
767794
pub fn create_response_for_invoice_request<ES: Deref, NS: Deref, R: Deref>(
768795
&self, signer: &NS, router: &R, entropy_source: ES,
769-
invoice_request: VerifiedInvoiceRequest, amount_msats: u64, payment_hash: PaymentHash,
770-
payment_secret: PaymentSecret, usable_channels: Vec<ChannelDetails>,
796+
invoice_request: VerifiedInvoiceRequest, amount_msats: u64,
797+
usable_channels: Vec<ChannelDetails>,
771798
) -> (OffersMessage, Option<MessageContext>)
772799
where
773800
ES::Target: EntropySource,
774801
NS::Target: NodeSigner,
775802
R::Target: Router,
776803
{
777-
let expanded_key = &self.inbound_payment_key;
778804
let entropy = &*entropy_source;
805+
let expanded_key = &self.inbound_payment_key;
779806
let secp_ctx = &self.secp_ctx;
780807

781808
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
809+
let (payment_hash, payment_secret) =
810+
match self.create_inbound_payment(entropy, Some(amount_msats), relative_expiry, None) {
811+
Ok((payment_hash, payment_secret)) => (payment_hash, payment_secret),
812+
Err(()) => {
813+
let error = Bolt12SemanticError::InvalidAmount;
814+
return (OffersMessage::InvoiceError(error.into()), None);
815+
},
816+
};
782817

783818
let context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
784819
offer_id: invoice_request.offer_id,

0 commit comments

Comments
 (0)