From: Jeffrey Czyz Date: Mon, 16 Sep 2024 21:07:54 +0000 (-0500) Subject: Function for iterating over Offer TLV records X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=1eb5baa00c081a0afe1b87f6c3d58bbb080ceba6;p=rust-lightning Function for iterating over Offer TLV records 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. --- diff --git a/lightning/src/offers/offer.rs b/lightning/src/offers/offer.rs index 675395841..c33d6510f 100644 --- a/lightning/src/offers/offer.rs +++ b/lightning/src/offers/offer.rs @@ -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> { + TlvStream::new(bytes).range(OFFER_TYPES) + } + #[cfg(async_payments)] pub(super) fn verify( &self, nonce: Nonce, key: &ExpandedKey, secp_ctx: &Secp256k1 diff --git a/lightning/src/offers/static_invoice.rs b/lightning/src/offers/static_invoice.rs index 07f2b9d31..b82a369ba 100644 --- a/lightning/src/offers/static_invoice.rs +++ b/lightning/src/offers/static_invoice.rs @@ -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) } }