]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Function for iterating over Offer TLV records
authorJeffrey Czyz <jkczyz@gmail.com>
Mon, 16 Sep 2024 21:07:54 +0000 (16:07 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Tue, 5 Nov 2024 00:00:22 +0000 (18:00 -0600)
Add a utility function for iterating over Offer TLV records contained in
any valid TLV stream bytes. Using a common function ensures that
experimental TLV records are included once they are supported.

lightning/src/offers/offer.rs
lightning/src/offers/static_invoice.rs

index 675395841cda5dabc316b7bf9377b10ee74422fd..c33d6510fddfcb1497bf01f5624e664793b6ecf6 100644 (file)
@@ -90,7 +90,7 @@ use crate::ln::channelmanager::PaymentId;
 use crate::ln::features::OfferFeatures;
 use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
 use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
-use crate::offers::merkle::{TaggedHash, TlvStream};
+use crate::offers::merkle::{TaggedHash, TlvRecord, TlvStream};
 use crate::offers::nonce::Nonce;
 use crate::offers::parse::{Bech32Encode, Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
 use crate::offers::signer::{Metadata, MetadataMaterial, self};
@@ -128,7 +128,7 @@ impl OfferId {
        }
 
        fn from_valid_invreq_tlv_stream(bytes: &[u8]) -> Self {
-               let tlv_stream = TlvStream::new(bytes).range(OFFER_TYPES);
+               let tlv_stream = Offer::tlv_stream_iter(bytes);
                let tagged_hash = TaggedHash::from_tlv_stream(Self::ID_TAG, tlv_stream);
                Self(tagged_hash.to_bytes())
        }
@@ -687,6 +687,12 @@ impl Offer {
                self.contents.expects_quantity()
        }
 
+       pub(super) fn tlv_stream_iter<'a>(
+               bytes: &'a [u8]
+       ) -> impl core::iter::Iterator<Item = TlvRecord<'a>> {
+               TlvStream::new(bytes).range(OFFER_TYPES)
+       }
+
        #[cfg(async_payments)]
        pub(super) fn verify<T: secp256k1::Signing>(
                &self, nonce: Nonce, key: &ExpandedKey, secp_ctx: &Secp256k1<T>
index 07f2b9d31524eab0d7a31004184e6333f3b69edf..b82a369bad333a40945c03a572297c98904f1d65 100644 (file)
@@ -388,12 +388,10 @@ impl StaticInvoice {
        }
 
        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);
+               let invoice_offer_tlv_stream =
+                       Offer::tlv_stream_iter(&self.bytes).map(|tlv_record| tlv_record.record_bytes);
+               let invreq_offer_tlv_stream =
+                       Offer::tlv_stream_iter(invreq.bytes()).map(|tlv_record| tlv_record.record_bytes);
                invoice_offer_tlv_stream.eq(invreq_offer_tlv_stream)
        }
 }