]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Support checking that a static invoice matches an outbound invreq.
authorValentine Wallace <vwallace@protonmail.com>
Thu, 13 Jun 2024 20:16:47 +0000 (16:16 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Fri, 13 Sep 2024 14:40:03 +0000 (10:40 -0400)
Useful for ensuring that an inbound static invoice matches one of our outbound
invreqs, otherwise it is an unexpected invoice and should be ignored and not
paid.

lightning/src/offers/invoice_request.rs
lightning/src/offers/merkle.rs
lightning/src/offers/static_invoice.rs

index 32d05249cfad048b7117ff576c4e7c6eef5ab40f..dc2fd4bf1df2aa466d306f7e14600b3a4eb0a412 100644 (file)
@@ -840,6 +840,11 @@ impl InvoiceRequest {
        invoice_request_accessors!(self, self.contents);
        invoice_request_respond_with_explicit_signing_pubkey_methods!(self, self, InvoiceBuilder<ExplicitSigningPubkey>);
        invoice_request_verify_method!(self, Self);
+
+       #[cfg(async_payments)]
+       pub(super) fn bytes(&self) -> &Vec<u8> {
+               &self.bytes
+       }
 }
 
 #[cfg(c_bindings)]
index 90bfc859e500d918b103af356f74c2b1a9f9be8a..e2fed2e800b367a76943c819b26af5ebf6a67290 100644 (file)
@@ -249,6 +249,7 @@ impl<'a> TlvStream<'a> {
 }
 
 /// A slice into a [`TlvStream`] for a record.
+#[derive(Eq, PartialEq)]
 pub(super) struct TlvRecord<'a> {
        pub(super) r#type: u64,
        type_bytes: &'a [u8],
index 33706f928d8993aa88760c1b0f43ad99bb5d8b50..4910c57c5af1f80422d6a04e74f806d79e789f44 100644 (file)
@@ -20,12 +20,13 @@ use crate::offers::invoice::{
        InvoiceTlvStream, InvoiceTlvStreamRef,
 };
 use crate::offers::invoice_macros::{invoice_accessors_common, invoice_builder_methods_common};
+use crate::offers::invoice_request::InvoiceRequest;
 use crate::offers::merkle::{
-       self, SignError, SignFn, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash,
+       self, SignError, SignFn, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, TlvStream,
 };
 use crate::offers::nonce::Nonce;
 use crate::offers::offer::{
-       Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef, Quantity,
+       Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef, Quantity, OFFER_TYPES,
 };
 use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
 use crate::util::ser::{CursorReadable, Iterable, WithoutLength, Writeable, Writer};
@@ -312,6 +313,16 @@ impl StaticInvoice {
        pub fn signature(&self) -> Signature {
                self.signature
        }
+
+       pub(crate) fn from_same_offer(&self, invreq: &InvoiceRequest) -> bool {
+               let invoice_offer_tlv_stream = TlvStream::new(&self.bytes)
+                       .range(OFFER_TYPES)
+                       .map(|tlv_record| tlv_record.record_bytes);
+               let invreq_offer_tlv_stream = TlvStream::new(invreq.bytes())
+                       .range(OFFER_TYPES)
+                       .map(|tlv_record| tlv_record.record_bytes);
+               invoice_offer_tlv_stream.eq(invreq_offer_tlv_stream)
+       }
 }
 
 impl InvoiceContents {