]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Pass bytes instead of TlvStream to verify
authorJeffrey Czyz <jkczyz@gmail.com>
Mon, 16 Sep 2024 21:31:01 +0000 (16:31 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Tue, 5 Nov 2024 00:00:22 +0000 (18:00 -0600)
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.

lightning/src/offers/invoice.rs

index 48d0ea1ba76e8849430fd1d2aa7ea66e5402c40f..894c45d42f28c29878b47f8d13b89421a5e54fe4 100644 (file)
@@ -878,7 +878,7 @@ impl Bolt12Invoice {
                                (&refund.payer.0, REFUND_IV_BYTES_WITH_METADATA)
                        },
                };
-               self.contents.verify(TlvStream::new(&self.bytes), metadata, key, iv_bytes, secp_ctx)
+               self.contents.verify(&self.bytes, metadata, key, iv_bytes, secp_ctx)
        }
 
        /// Verifies that the invoice was for a request or refund created using the given key by
@@ -892,7 +892,8 @@ impl Bolt12Invoice {
                        InvoiceContents::ForOffer { .. } => INVOICE_REQUEST_IV_BYTES,
                        InvoiceContents::ForRefund { .. } => REFUND_IV_BYTES_WITHOUT_METADATA,
                };
-               self.contents.verify(TlvStream::new(&self.bytes), &metadata, key, iv_bytes, secp_ctx)
+               self.contents
+                       .verify(&self.bytes, &metadata, key, iv_bytes, secp_ctx)
                        .and_then(|extracted_payment_id| (payment_id == extracted_payment_id)
                                .then(|| payment_id)
                                .ok_or(())
@@ -1143,11 +1144,11 @@ impl InvoiceContents {
        }
 
        fn verify<T: secp256k1::Signing>(
-               &self, tlv_stream: TlvStream<'_>, metadata: &Metadata, key: &ExpandedKey,
-               iv_bytes: &[u8; IV_LEN], secp_ctx: &Secp256k1<T>
+               &self, bytes: &[u8], metadata: &Metadata, key: &ExpandedKey, iv_bytes: &[u8; IV_LEN],
+               secp_ctx: &Secp256k1<T>
        ) -> Result<PaymentId, ()> {
-               let offer_records = tlv_stream.clone().range(OFFER_TYPES);
-               let invreq_records = tlv_stream.range(INVOICE_REQUEST_TYPES).filter(|record| {
+               let offer_records = TlvStream::new(bytes).range(OFFER_TYPES);
+               let invreq_records = TlvStream::new(bytes).range(INVOICE_REQUEST_TYPES).filter(|record| {
                        match record.r#type {
                                PAYER_METADATA_TYPE => false, // Should be outside range
                                INVOICE_REQUEST_PAYER_ID_TYPE => !metadata.derives_payer_keys(),