Skip to content

Commit

Permalink
Merge pull request #3408 from valentinewallace/2024-11-async-receive-…
Browse files Browse the repository at this point in the history
…offer-utils

Add static invoice creation utils to `ChannelManager`
  • Loading branch information
TheBlueMatt authored Jan 17, 2025
2 parents 3fbf97d + d3a7efa commit aa2c6fe
Show file tree
Hide file tree
Showing 15 changed files with 829 additions and 195 deletions.
2 changes: 1 addition & 1 deletion fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Router for FuzzRouter {

fn create_blinded_payment_paths<T: secp256k1::Signing + secp256k1::Verification>(
&self, _recipient: PublicKey, _first_hops: Vec<ChannelDetails>, _tlvs: ReceiveTlvs,
_amount_msats: u64, _secp_ctx: &Secp256k1<T>,
_amount_msats: Option<u64>, _secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPaymentPath>, ()> {
unreachable!()
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl Router for FuzzRouter {

fn create_blinded_payment_paths<T: secp256k1::Signing + secp256k1::Verification>(
&self, _recipient: PublicKey, _first_hops: Vec<ChannelDetails>, _tlvs: ReceiveTlvs,
_amount_msats: u64, _secp_ctx: &Secp256k1<T>,
_amount_msats: Option<u64>, _secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPaymentPath>, ()> {
unreachable!()
}
Expand Down
22 changes: 22 additions & 0 deletions lightning/src/blinded_path/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,24 @@ pub enum AsyncPaymentsContext {
/// containing the expected [`PaymentId`].
hmac: Hmac<Sha256>,
},
/// Context contained within the [`BlindedMessagePath`]s we put in static invoices, provided back
/// to us in corresponding [`HeldHtlcAvailable`] messages.
///
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
InboundPayment {
/// A nonce used for authenticating that a [`HeldHtlcAvailable`] message is valid for a
/// preceding static invoice.
///
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
nonce: Nonce,
/// Authentication code for the [`HeldHtlcAvailable`] message.
///
/// Prevents nodes from creating their own blinded path to us, sending a [`HeldHtlcAvailable`]
/// message and trivially getting notified whenever we come online.
///
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
hmac: Hmac<Sha256>,
},
}

impl_writeable_tlv_based_enum!(MessageContext,
Expand Down Expand Up @@ -433,6 +451,10 @@ impl_writeable_tlv_based_enum!(AsyncPaymentsContext,
(2, nonce, required),
(4, hmac, required),
},
(1, InboundPayment) => {
(0, nonce, required),
(2, hmac, required),
},
);

/// Contains a simple nonce for use in a blinded path's context.
Expand Down
22 changes: 22 additions & 0 deletions lightning/src/blinded_path/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ pub enum PaymentContext {
/// [`Offer`]: crate::offers::offer::Offer
Bolt12Offer(Bolt12OfferContext),

/// The payment was made for a static invoice requested from a BOLT 12 [`Offer`].
///
/// [`Offer`]: crate::offers::offer::Offer
AsyncBolt12Offer(AsyncBolt12OfferContext),

/// The payment was made for an invoice sent for a BOLT 12 [`Refund`].
///
/// [`Refund`]: crate::offers::refund::Refund
Expand Down Expand Up @@ -378,6 +383,18 @@ pub struct Bolt12OfferContext {
pub invoice_request: InvoiceRequestFields,
}

/// The context of a payment made for a static invoice requested from a BOLT 12 [`Offer`].
///
/// [`Offer`]: crate::offers::offer::Offer
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AsyncBolt12OfferContext {
/// The [`Nonce`] used to verify that an inbound [`InvoiceRequest`] corresponds to this static
/// invoice's offer.
///
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
pub offer_nonce: Nonce,
}

/// The context of a payment made for an invoice sent for a BOLT 12 [`Refund`].
///
/// [`Refund`]: crate::offers::refund::Refund
Expand Down Expand Up @@ -627,6 +644,7 @@ impl_writeable_tlv_based_enum_legacy!(PaymentContext,
// 0 for Unknown removed in version 0.1.
(1, Bolt12Offer),
(2, Bolt12Refund),
(3, AsyncBolt12Offer),
);

impl<'a> Writeable for PaymentContextRef<'a> {
Expand All @@ -651,6 +669,10 @@ impl_writeable_tlv_based!(Bolt12OfferContext, {
(2, invoice_request, required),
});

impl_writeable_tlv_based!(AsyncBolt12OfferContext, {
(0, offer_nonce, required),
});

impl_writeable_tlv_based!(Bolt12RefundContext, {});

#[cfg(test)]
Expand Down
22 changes: 14 additions & 8 deletions lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,27 +181,32 @@ impl PaymentPurpose {
pub(crate) fn from_parts(
payment_preimage: Option<PaymentPreimage>, payment_secret: PaymentSecret,
payment_context: Option<PaymentContext>,
) -> Self {
) -> Result<Self, ()> {
match payment_context {
None => {
PaymentPurpose::Bolt11InvoicePayment {
Ok(PaymentPurpose::Bolt11InvoicePayment {
payment_preimage,
payment_secret,
}
})
},
Some(PaymentContext::Bolt12Offer(context)) => {
PaymentPurpose::Bolt12OfferPayment {
Ok(PaymentPurpose::Bolt12OfferPayment {
payment_preimage,
payment_secret,
payment_context: context,
}
})
},
Some(PaymentContext::Bolt12Refund(context)) => {
PaymentPurpose::Bolt12RefundPayment {
Ok(PaymentPurpose::Bolt12RefundPayment {
payment_preimage,
payment_secret,
payment_context: context,
}
})
},
Some(PaymentContext::AsyncBolt12Offer(_context)) => {
// This code will change to return Self::Bolt12OfferPayment when we add support for async
// receive.
Err(())
},
}
}
Expand Down Expand Up @@ -1865,7 +1870,8 @@ impl MaybeReadable for Event {
(13, payment_id, option),
});
let purpose = match payment_secret {
Some(secret) => PaymentPurpose::from_parts(payment_preimage, secret, payment_context),
Some(secret) => PaymentPurpose::from_parts(payment_preimage, secret, payment_context)
.map_err(|()| msgs::DecodeError::InvalidValue)?,
None if payment_preimage.is_some() => PaymentPurpose::SpontaneousPayment(payment_preimage.unwrap()),
None => return Err(msgs::DecodeError::InvalidValue),
};
Expand Down
Loading

0 comments on commit aa2c6fe

Please sign in to comment.