Skip to content

Commit 7b5e94a

Browse files
committed
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.
1 parent b017b1c commit 7b5e94a

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

lightning/src/offers/offer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ use crate::ln::channelmanager::PaymentId;
9090
use crate::ln::features::OfferFeatures;
9191
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
9292
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
93-
use crate::offers::merkle::{TaggedHash, TlvStream};
93+
use crate::offers::merkle::{TaggedHash, TlvRecord, TlvStream};
9494
use crate::offers::nonce::Nonce;
9595
use crate::offers::parse::{Bech32Encode, Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
9696
use crate::offers::signer::{Metadata, MetadataMaterial, self};
@@ -128,7 +128,7 @@ impl OfferId {
128128
}
129129

130130
fn from_valid_invreq_tlv_stream(bytes: &[u8]) -> Self {
131-
let tlv_stream = TlvStream::new(bytes).range(OFFER_TYPES);
131+
let tlv_stream = Offer::tlv_stream_iter(bytes);
132132
let tagged_hash = TaggedHash::from_tlv_stream(Self::ID_TAG, tlv_stream);
133133
Self(tagged_hash.to_bytes())
134134
}
@@ -687,6 +687,12 @@ impl Offer {
687687
self.contents.expects_quantity()
688688
}
689689

690+
pub(super) fn tlv_stream_iter<'a>(
691+
bytes: &'a [u8]
692+
) -> impl core::iter::Iterator<Item = TlvRecord<'a>> {
693+
TlvStream::new(bytes).range(OFFER_TYPES)
694+
}
695+
690696
#[cfg(async_payments)]
691697
pub(super) fn verify<T: secp256k1::Signing>(
692698
&self, nonce: Nonce, key: &ExpandedKey, secp_ctx: &Secp256k1<T>

lightning/src/offers/static_invoice.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,10 @@ impl StaticInvoice {
367367
}
368368

369369
pub(crate) fn from_same_offer(&self, invreq: &InvoiceRequest) -> bool {
370-
let invoice_offer_tlv_stream = TlvStream::new(&self.bytes)
371-
.range(OFFER_TYPES)
372-
.map(|tlv_record| tlv_record.record_bytes);
373-
let invreq_offer_tlv_stream = TlvStream::new(invreq.bytes())
374-
.range(OFFER_TYPES)
375-
.map(|tlv_record| tlv_record.record_bytes);
370+
let invoice_offer_tlv_stream =
371+
Offer::tlv_stream_iter(&self.bytes).map(|tlv_record| tlv_record.record_bytes);
372+
let invreq_offer_tlv_stream =
373+
Offer::tlv_stream_iter(invreq.bytes()).map(|tlv_record| tlv_record.record_bytes);
376374
invoice_offer_tlv_stream.eq(invreq_offer_tlv_stream)
377375
}
378376
}

0 commit comments

Comments
 (0)