Skip to content

Commit 1eb5baa

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 771aa8e commit 1eb5baa

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
@@ -388,12 +388,10 @@ impl StaticInvoice {
388388
}
389389

390390
pub(crate) fn from_same_offer(&self, invreq: &InvoiceRequest) -> bool {
391-
let invoice_offer_tlv_stream = TlvStream::new(&self.bytes)
392-
.range(OFFER_TYPES)
393-
.map(|tlv_record| tlv_record.record_bytes);
394-
let invreq_offer_tlv_stream = TlvStream::new(invreq.bytes())
395-
.range(OFFER_TYPES)
396-
.map(|tlv_record| tlv_record.record_bytes);
391+
let invoice_offer_tlv_stream =
392+
Offer::tlv_stream_iter(&self.bytes).map(|tlv_record| tlv_record.record_bytes);
393+
let invreq_offer_tlv_stream =
394+
Offer::tlv_stream_iter(invreq.bytes()).map(|tlv_record| tlv_record.record_bytes);
397395
invoice_offer_tlv_stream.eq(invreq_offer_tlv_stream)
398396
}
399397
}

0 commit comments

Comments
 (0)