From 49871a11531984aaa8695e69a51fa9ed15ae2587 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Fri, 7 Oct 2022 15:08:27 -0500 Subject: [PATCH] f - define try_from_tlv_stream_bytes --- lightning/src/offers/invoice_request.rs | 17 ++++++----------- lightning/src/offers/offer.rs | 19 +++++++------------ lightning/src/util/ser_macros.rs | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/lightning/src/offers/invoice_request.rs b/lightning/src/offers/invoice_request.rs index 4647b92d3bc..2996dce960e 100644 --- a/lightning/src/offers/invoice_request.rs +++ b/lightning/src/offers/invoice_request.rs @@ -339,15 +339,8 @@ impl TryFrom> for InvoiceRequest { type Error = ParseError; fn try_from(bytes: Vec) -> Result { - let mut cursor = io::Cursor::new(bytes); - let tlv_stream: FullInvoiceRequestTlvStream = SeekReadable::read(&mut cursor)?; - - if cursor.position() < cursor.get_ref().len() as u64 { - return Err(ParseError::Decode(DecodeError::InvalidValue)); - } - - let bytes = cursor.into_inner(); - InvoiceRequest::try_from((bytes, tlv_stream)) + let parsed_invoice_request = ParsedInvoiceRequest::try_from(bytes)?; + InvoiceRequest::try_from(parsed_invoice_request) } } @@ -360,7 +353,9 @@ tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, 80..160, { (89, payer_note: (String, WithoutLength)), }); -type ParsedInvoiceRequest = (Vec, FullInvoiceRequestTlvStream); +try_from_tlv_stream_bytes!(FullInvoiceRequestTlvStream, ParsedInvoiceRequest); + +struct ParsedInvoiceRequest(Vec, FullInvoiceRequestTlvStream); type FullInvoiceRequestTlvStream = (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream, SignatureTlvStream); @@ -388,7 +383,7 @@ impl TryFrom for InvoiceRequest { type Error = ParseError; fn try_from(invoice_request: ParsedInvoiceRequest) -> Result { - let (bytes, tlv_stream) = invoice_request; + let ParsedInvoiceRequest(bytes, tlv_stream) = invoice_request; let ( payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream, SignatureTlvStream { signature }, diff --git a/lightning/src/offers/offer.rs b/lightning/src/offers/offer.rs index 988ad86e0f8..518071d31a6 100644 --- a/lightning/src/offers/offer.rs +++ b/lightning/src/offers/offer.rs @@ -75,7 +75,7 @@ use core::str::FromStr; use core::time::Duration; use crate::io; use crate::ln::features::OfferFeatures; -use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT}; +use crate::ln::msgs::MAX_VALUE_MSAT; use crate::offers::invoice_request::InvoiceRequestBuilder; use crate::offers::parse::{Bech32Encode, ParseError, SemanticError}; use crate::onion_message::BlindedPath; @@ -474,15 +474,8 @@ impl TryFrom> for Offer { type Error = ParseError; fn try_from(bytes: Vec) -> Result { - let mut cursor = io::Cursor::new(bytes); - let tlv_stream: OfferTlvStream = SeekReadable::read(&mut cursor)?; - - if cursor.position() < cursor.get_ref().len() as u64 { - return Err(ParseError::Decode(DecodeError::InvalidValue)); - } - - let bytes = cursor.into_inner(); - Offer::try_from((bytes, tlv_stream)) + let parsed_offer = ParsedOffer::try_from(bytes)?; + Offer::try_from(parsed_offer) } } @@ -555,11 +548,13 @@ tlv_stream!(OfferTlvStream, OfferTlvStreamRef, 1..80, { (22, node_id: PublicKey), }); +try_from_tlv_stream_bytes!(OfferTlvStream, ParsedOffer); + impl Bech32Encode for Offer { const BECH32_HRP: &'static str = "lno"; } -type ParsedOffer = (Vec, OfferTlvStream); +struct ParsedOffer(Vec, OfferTlvStream); impl FromStr for Offer { type Err = ParseError; @@ -573,7 +568,7 @@ impl TryFrom for Offer { type Error = ParseError; fn try_from(offer: ParsedOffer) -> Result { - let (bytes, tlv_stream) = offer; + let ParsedOffer(bytes, tlv_stream) = offer; let contents = OfferContents::try_from(tlv_stream)?; Ok(Offer { bytes, contents }) } diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 31978d5e4c7..647ed0f5c42 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -551,6 +551,26 @@ macro_rules! tlv_record_ref_type { ($type:ty) => { &'a $type }; } +macro_rules! try_from_tlv_stream_bytes { + ($tlv_stream:ty, $parsed_message:ident) => { + impl TryFrom> for $parsed_message { + type Error = $crate::ln::msgs::DecodeError; + + fn try_from(bytes: Vec) -> Result { + let mut cursor = $crate::io::Cursor::new(bytes); + let tlv_stream: $tlv_stream = SeekReadable::read(&mut cursor)?; + + if cursor.position() < cursor.get_ref().len() as u64 { + return Err($crate::ln::msgs::DecodeError::InvalidValue); + } + + let bytes = cursor.into_inner(); + Ok(Self(bytes, tlv_stream)) + } + } + }; +} + macro_rules! _impl_writeable_tlv_based_enum_common { ($st: ident, $(($variant_id: expr, $variant_name: ident) => {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}