Skip to content

Commit 1f35a6b

Browse files
committed
Pass bytes instead of TlvStream to verify
Passing bytes directly to InvoiceContents::verify improves readability as then a TlvStream for each TLV record range can be created from the bytes instead of needing to clone the TlvStream upfront. In an upcoming commit, the experimental TLV record range will utilize this.
1 parent b2c9e20 commit 1f35a6b

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

lightning/src/offers/invoice.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ impl Bolt12Invoice {
868868
(&refund.payer.0, REFUND_IV_BYTES_WITH_METADATA)
869869
},
870870
};
871-
self.contents.verify(TlvStream::new(&self.bytes), metadata, key, iv_bytes, secp_ctx)
871+
self.contents.verify(&self.bytes, metadata, key, iv_bytes, secp_ctx)
872872
}
873873

874874
/// Verifies that the invoice was for a request or refund created using the given key by
@@ -882,7 +882,8 @@ impl Bolt12Invoice {
882882
InvoiceContents::ForOffer { .. } => INVOICE_REQUEST_IV_BYTES,
883883
InvoiceContents::ForRefund { .. } => REFUND_IV_BYTES_WITHOUT_METADATA,
884884
};
885-
self.contents.verify(TlvStream::new(&self.bytes), &metadata, key, iv_bytes, secp_ctx)
885+
self.contents
886+
.verify(&self.bytes, &metadata, key, iv_bytes, secp_ctx)
886887
.and_then(|extracted_payment_id| (payment_id == extracted_payment_id)
887888
.then(|| payment_id)
888889
.ok_or(())
@@ -1133,11 +1134,11 @@ impl InvoiceContents {
11331134
}
11341135

11351136
fn verify<T: secp256k1::Signing>(
1136-
&self, tlv_stream: TlvStream<'_>, metadata: &Metadata, key: &ExpandedKey,
1137-
iv_bytes: &[u8; IV_LEN], secp_ctx: &Secp256k1<T>
1137+
&self, bytes: &[u8], metadata: &Metadata, key: &ExpandedKey, iv_bytes: &[u8; IV_LEN],
1138+
secp_ctx: &Secp256k1<T>
11381139
) -> Result<PaymentId, ()> {
1139-
let offer_records = tlv_stream.clone().range(OFFER_TYPES);
1140-
let invreq_records = tlv_stream.range(INVOICE_REQUEST_TYPES).filter(|record| {
1140+
let offer_records = TlvStream::new(bytes).range(OFFER_TYPES);
1141+
let invreq_records = TlvStream::new(bytes).range(INVOICE_REQUEST_TYPES).filter(|record| {
11411142
match record.r#type {
11421143
PAYER_METADATA_TYPE => false, // Should be outside range
11431144
INVOICE_REQUEST_PAYER_ID_TYPE => !metadata.derives_payer_keys(),

0 commit comments

Comments
 (0)