Include InvoiceRequest fields in PaymentContext
authorJeffrey Czyz <jkczyz@gmail.com>
Fri, 29 Mar 2024 02:31:10 +0000 (21:31 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Thu, 18 Apr 2024 14:15:25 +0000 (09:15 -0500)
When receiving a payment, it's useful to know information about the
InvoiceRequest. Include this data in PaymentContext::Bolt12Offer so
users can display information about an inbound payment (e.g., the payer
note).

lightning/src/blinded_path/payment.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/offers_tests.rs
lightning/src/offers/invoice_request.rs

index e243d478bd91621bdcb811f98420c7de194a40e3..ec441c18c986ed4788948dcf09465e07233878d8 100644 (file)
@@ -12,6 +12,7 @@ use crate::ln::channelmanager::CounterpartyForwardingInfo;
 use crate::ln::features::BlindedHopFeatures;
 use crate::ln::msgs::DecodeError;
 use crate::offers::invoice::BlindedPayInfo;
+use crate::offers::invoice_request::InvoiceRequestFields;
 use crate::offers::offer::OfferId;
 use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, Writeable, Writer};
 
@@ -140,6 +141,12 @@ pub struct Bolt12OfferContext {
        ///
        /// [`Offer`]: crate::offers::offer::Offer
        pub offer_id: OfferId,
+
+       /// Fields from an [`InvoiceRequest`] sent for a [`Bolt12Invoice`].
+       ///
+       /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
+       /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
+       pub invoice_request: InvoiceRequestFields,
 }
 
 /// The context of a payment made for an invoice sent for a BOLT 12 [`Refund`].
@@ -409,6 +416,7 @@ impl Readable for UnknownPaymentContext {
 
 impl_writeable_tlv_based!(Bolt12OfferContext, {
        (0, offer_id, required),
+       (2, invoice_request, required),
 });
 
 impl_writeable_tlv_based!(Bolt12RefundContext, {});
index 63fa82be63e09376aede8225cc5350363543c856..8bf08c1e023715c10f3f03451888388b93c21b2e 100644 (file)
@@ -10367,6 +10367,7 @@ where
 
                                let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
                                        offer_id: invoice_request.offer_id,
+                                       invoice_request: invoice_request.fields(),
                                });
                                let payment_paths = match self.create_blinded_payment_paths(
                                        amount_msats, payment_secret, payment_context
index 4910f38f311bdc0f4d712d686a13fee393a2d254..75a2e290f39e824514fdaa3b6333eb20b319663e 100644 (file)
@@ -46,11 +46,12 @@ use crate::blinded_path::{BlindedPath, IntroductionNode};
 use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
 use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
+use crate::ln::features::InvoiceRequestFeatures;
 use crate::ln::functional_test_utils::*;
 use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
 use crate::offers::invoice::Bolt12Invoice;
 use crate::offers::invoice_error::InvoiceError;
-use crate::offers::invoice_request::InvoiceRequest;
+use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
 use crate::offers::parse::Bolt12SemanticError;
 use crate::onion_message::messenger::PeeledOnion;
 use crate::onion_message::offers::OffersMessage;
@@ -385,7 +386,6 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
                .unwrap()
                .amount_msats(10_000_000)
                .build().unwrap();
-       let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext { offer_id: offer.id() });
        assert_ne!(offer.signing_pubkey(), alice_id);
        assert!(!offer.paths().is_empty());
        for path in offer.paths() {
@@ -408,6 +408,16 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
        alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
 
        let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
+       let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
+               offer_id: offer.id(),
+               invoice_request: InvoiceRequestFields {
+                       payer_id: invoice_request.payer_id(),
+                       amount_msats: None,
+                       features: InvoiceRequestFeatures::empty(),
+                       quantity: None,
+                       payer_note_truncated: None,
+               },
+       });
        assert_eq!(invoice_request.amount_msats(), None);
        assert_ne!(invoice_request.payer_id(), david_id);
        assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(charlie_id));
@@ -537,7 +547,6 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
                .create_offer_builder("coffee".to_string()).unwrap()
                .amount_msats(10_000_000)
                .build().unwrap();
-       let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext { offer_id: offer.id() });
        assert_ne!(offer.signing_pubkey(), alice_id);
        assert!(!offer.paths().is_empty());
        for path in offer.paths() {
@@ -552,6 +561,16 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
        alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
 
        let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
+       let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
+               offer_id: offer.id(),
+               invoice_request: InvoiceRequestFields {
+                       payer_id: invoice_request.payer_id(),
+                       amount_msats: None,
+                       features: InvoiceRequestFeatures::empty(),
+                       quantity: None,
+                       payer_note_truncated: None,
+               },
+       });
        assert_eq!(invoice_request.amount_msats(), None);
        assert_ne!(invoice_request.payer_id(), bob_id);
        assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(bob_id));
@@ -653,7 +672,6 @@ fn pays_for_offer_without_blinded_paths() {
                .clear_paths()
                .amount_msats(10_000_000)
                .build().unwrap();
-       let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext { offer_id: offer.id() });
        assert_eq!(offer.signing_pubkey(), alice_id);
        assert!(offer.paths().is_empty());
 
@@ -664,6 +682,18 @@ fn pays_for_offer_without_blinded_paths() {
        let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
        alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
 
+       let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
+       let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
+               offer_id: offer.id(),
+               invoice_request: InvoiceRequestFields {
+                       payer_id: invoice_request.payer_id(),
+                       amount_msats: None,
+                       features: InvoiceRequestFeatures::empty(),
+                       quantity: None,
+                       payer_note_truncated: None,
+               },
+       });
+
        let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
        bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
 
index 07a117233fa2eb2a1dd181506008e4e3b976ab4a..9157613fcd977a132e9f7b5751f196af0592269a 100644 (file)
@@ -76,8 +76,8 @@ use crate::offers::offer::{Offer, OfferContents, OfferId, OfferTlvStream, OfferT
 use crate::offers::parse::{Bolt12ParseError, ParsedMessage, Bolt12SemanticError};
 use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
 use crate::offers::signer::{Metadata, MetadataMaterial};
-use crate::util::ser::{HighZeroBytesDroppedBigSize, SeekReadable, WithoutLength, Writeable, Writer};
-use crate::util::string::PrintableString;
+use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, SeekReadable, WithoutLength, Writeable, Writer};
+use crate::util::string::{PrintableString, UntrustedString};
 
 #[cfg(not(c_bindings))]
 use {
@@ -872,6 +872,24 @@ impl VerifiedInvoiceRequest {
        invoice_request_respond_with_derived_signing_pubkey_methods!(self, self.inner, InvoiceBuilder<DerivedSigningPubkey>);
        #[cfg(c_bindings)]
        invoice_request_respond_with_derived_signing_pubkey_methods!(self, self.inner, InvoiceWithDerivedSigningPubkeyBuilder);
+
+       pub(crate) fn fields(&self) -> InvoiceRequestFields {
+               let InvoiceRequestContents {
+                       payer_id,
+                       inner: InvoiceRequestContentsWithoutPayerId {
+                               payer: _, offer: _, chain: _, amount_msats, features, quantity, payer_note
+                       },
+               } = &self.inner.contents;
+
+               InvoiceRequestFields {
+                       payer_id: *payer_id,
+                       amount_msats: *amount_msats,
+                       features: features.clone(),
+                       quantity: *quantity,
+                       payer_note_truncated: payer_note.clone()
+                               .map(|mut s| { s.truncate(PAYER_NOTE_LIMIT); UntrustedString(s) }),
+               }
+       }
 }
 
 impl InvoiceRequestContents {
@@ -1100,9 +1118,68 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
        }
 }
 
+/// Fields sent in an [`InvoiceRequest`] message to include in [`PaymentContext::Bolt12Offer`].
+///
+/// [`PaymentContext::Bolt12Offer`]: crate::blinded_path::payment::PaymentContext::Bolt12Offer
+#[derive(Clone, Debug, Eq, PartialEq)]
+pub struct InvoiceRequestFields {
+       /// A possibly transient pubkey used to sign the invoice request.
+       pub payer_id: PublicKey,
+
+       /// The amount to pay in msats (i.e., the minimum lightning-payable unit for [`chain`]), which
+       /// must be greater than or equal to [`Offer::amount`], converted if necessary.
+       ///
+       /// [`chain`]: InvoiceRequest::chain
+       pub amount_msats: Option<u64>,
+
+       /// Features pertaining to requesting an invoice.
+       pub features: InvoiceRequestFeatures,
+
+       /// The quantity of the offer's item conforming to [`Offer::is_valid_quantity`].
+       pub quantity: Option<u64>,
+
+       /// A payer-provided note which will be seen by the recipient and reflected back in the invoice
+       /// response. Truncated to [`PAYER_NOTE_LIMIT`] characters.
+       pub payer_note_truncated: Option<UntrustedString>,
+}
+
+/// The maximum number of characters included in [`InvoiceRequestFields::payer_note_truncated`].
+pub const PAYER_NOTE_LIMIT: usize = 512;
+
+impl Writeable for InvoiceRequestFields {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               write_tlv_fields!(writer, {
+                       (0, self.payer_id, required),
+                       (2, self.amount_msats.map(|v| HighZeroBytesDroppedBigSize(v)), option),
+                       (4, WithoutLength(&self.features), required),
+                       (6, self.quantity.map(|v| HighZeroBytesDroppedBigSize(v)), option),
+                       (8, self.payer_note_truncated.as_ref().map(|s| WithoutLength(&s.0)), option),
+               });
+               Ok(())
+       }
+}
+
+impl Readable for InvoiceRequestFields {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+               _init_and_read_len_prefixed_tlv_fields!(reader, {
+                       (0, payer_id, required),
+                       (2, amount_msats, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
+                       (4, features, (option, encoding: (InvoiceRequestFeatures, WithoutLength))),
+                       (6, quantity, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
+                       (8, payer_note_truncated, (option, encoding: (String, WithoutLength))),
+               });
+               let features = features.unwrap_or(InvoiceRequestFeatures::empty());
+
+               Ok(InvoiceRequestFields {
+                       payer_id: payer_id.0.unwrap(), amount_msats, features, quantity,
+                       payer_note_truncated: payer_note_truncated.map(|s| UntrustedString(s)),
+               })
+       }
+}
+
 #[cfg(test)]
 mod tests {
-       use super::{InvoiceRequest, InvoiceRequestTlvStreamRef, SIGNATURE_TAG, UnsignedInvoiceRequest};
+       use super::{InvoiceRequest, InvoiceRequestFields, InvoiceRequestTlvStreamRef, PAYER_NOTE_LIMIT, SIGNATURE_TAG, UnsignedInvoiceRequest};
 
        use bitcoin::blockdata::constants::ChainHash;
        use bitcoin::network::constants::Network;
@@ -1129,8 +1206,8 @@ mod tests {
        use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
        use crate::offers::payer::PayerTlvStreamRef;
        use crate::offers::test_utils::*;
-       use crate::util::ser::{BigSize, Writeable};
-       use crate::util::string::PrintableString;
+       use crate::util::ser::{BigSize, Readable, Writeable};
+       use crate::util::string::{PrintableString, UntrustedString};
 
        #[test]
        fn builds_invoice_request_with_defaults() {
@@ -2166,4 +2243,55 @@ mod tests {
                        Err(e) => assert_eq!(e, Bolt12ParseError::Decode(DecodeError::InvalidValue)),
                }
        }
+
+       #[test]
+       fn copies_verified_invoice_request_fields() {
+               let desc = "foo".to_string();
+               let node_id = recipient_pubkey();
+               let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
+               let entropy = FixedEntropy {};
+               let secp_ctx = Secp256k1::new();
+
+               #[cfg(c_bindings)]
+               use crate::offers::offer::OfferWithDerivedMetadataBuilder as OfferBuilder;
+               let offer = OfferBuilder
+                       ::deriving_signing_pubkey(desc, node_id, &expanded_key, &entropy, &secp_ctx)
+                       .chain(Network::Testnet)
+                       .amount_msats(1000)
+                       .supported_quantity(Quantity::Unbounded)
+                       .build().unwrap();
+               assert_eq!(offer.signing_pubkey(), node_id);
+
+               let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
+                       .chain(Network::Testnet).unwrap()
+                       .amount_msats(1001).unwrap()
+                       .quantity(1).unwrap()
+                       .payer_note("0".repeat(PAYER_NOTE_LIMIT * 2))
+                       .build().unwrap()
+                       .sign(payer_sign).unwrap();
+               match invoice_request.verify(&expanded_key, &secp_ctx) {
+                       Ok(invoice_request) => {
+                               let fields = invoice_request.fields();
+                               assert_eq!(invoice_request.offer_id, offer.id());
+                               assert_eq!(
+                                       fields,
+                                       InvoiceRequestFields {
+                                               payer_id: payer_pubkey(),
+                                               amount_msats: Some(1001),
+                                               features: InvoiceRequestFeatures::empty(),
+                                               quantity: Some(1),
+                                               payer_note_truncated: Some(UntrustedString("0".repeat(PAYER_NOTE_LIMIT))),
+                                       }
+                               );
+
+                               let mut buffer = Vec::new();
+                               fields.write(&mut buffer).unwrap();
+
+                               let deserialized_fields: InvoiceRequestFields =
+                                       Readable::read(&mut buffer.as_slice()).unwrap();
+                               assert_eq!(deserialized_fields, fields);
+                       },
+                       Err(_) => panic!("unexpected error"),
+               }
+       }
 }