Skip to content

Commit 4f07247

Browse files
committed
f: Introduce create_inbound_payment to OffersMessageFlow
1 parent c23f041 commit 4f07247

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
@@ -309,6 +309,29 @@ impl<MR: Deref> OffersMessageFlow<MR>
309309
where
310310
MR::Target: MessageRouter,
311311
{
312+
/// Gets a payment secret and payment hash for use in an invoice given to a third party wishing
313+
/// to pay us.
314+
///
315+
/// See [`ChannelManager::create_inbound_payment`] for more details.
316+
///
317+
/// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
318+
pub fn create_inbound_payment<ES: Deref>(
319+
&self, entropy_source: ES, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32,
320+
min_final_cltv_expiry_delta: Option<u16>,
321+
) -> Result<(PaymentHash, PaymentSecret), ()>
322+
where
323+
ES::Target: EntropySource,
324+
{
325+
inbound_payment::create(
326+
&self.inbound_payment_key,
327+
min_value_msat,
328+
invoice_expiry_delta_secs,
329+
&entropy_source,
330+
self.highest_seen_timestamp.load(Ordering::Acquire) as u64,
331+
min_final_cltv_expiry_delta,
332+
)
333+
}
334+
312335
/// Verifies an [`InvoiceRequest`] using the provided [`OffersContext`] or the invoice request's own metadata.
313336
///
314337
/// - If an [`OffersContext::InvoiceRequest`] with a `nonce` is provided, verification is performed using recipient context data.
@@ -725,8 +748,8 @@ where
725748
///
726749
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
727750
pub fn create_invoice_builder_from_refund<'a, ES: Deref, R: Deref>(
728-
&'a self, router: &R, entropy_source: ES, refund: &'a Refund, payment_hash: PaymentHash,
729-
payment_secret: PaymentSecret, usable_channels: Vec<ChannelDetails>,
751+
&'a self, router: &R, entropy_source: ES, refund: &'a Refund,
752+
usable_channels: Vec<ChannelDetails>,
730753
) -> Result<InvoiceBuilder<'a, DerivedSigningPubkey>, Bolt12SemanticError>
731754
where
732755
ES::Target: EntropySource,
@@ -742,6 +765,10 @@ where
742765
let amount_msats = refund.amount_msats();
743766
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
744767

768+
let (payment_hash, payment_secret) = self
769+
.create_inbound_payment(entropy, Some(amount_msats), relative_expiry, None)
770+
.map_err(|_| Bolt12SemanticError::InvalidAmount)?;
771+
745772
let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
746773
let payment_paths = self
747774
.create_blinded_payment_paths(
@@ -787,19 +814,27 @@ where
787814
/// - We fail to generate a valid signed [`Bolt12Invoice`] for the [`InvoiceRequest`].
788815
pub fn create_response_for_invoice_request<ES: Deref, NS: Deref, R: Deref>(
789816
&self, signer: &NS, router: &R, entropy_source: ES,
790-
invoice_request: VerifiedInvoiceRequest, amount_msats: u64, payment_hash: PaymentHash,
791-
payment_secret: PaymentSecret, usable_channels: Vec<ChannelDetails>,
817+
invoice_request: VerifiedInvoiceRequest, amount_msats: u64,
818+
usable_channels: Vec<ChannelDetails>,
792819
) -> (OffersMessage, Option<MessageContext>)
793820
where
794821
ES::Target: EntropySource,
795822
NS::Target: NodeSigner,
796823
R::Target: Router,
797824
{
798-
let expanded_key = &self.inbound_payment_key;
799825
let entropy = &*entropy_source;
826+
let expanded_key = &self.inbound_payment_key;
800827
let secp_ctx = &self.secp_ctx;
801828

802829
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
830+
let (payment_hash, payment_secret) =
831+
match self.create_inbound_payment(entropy, Some(amount_msats), relative_expiry, None) {
832+
Ok((payment_hash, payment_secret)) => (payment_hash, payment_secret),
833+
Err(()) => {
834+
let error = Bolt12SemanticError::InvalidAmount;
835+
return (OffersMessage::InvoiceError(error.into()), None);
836+
},
837+
};
803838

804839
let context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
805840
offer_id: invoice_request.offer_id,

0 commit comments

Comments
 (0)