From a16cc51c52ab8dd68f96d705c66cd662522203ac 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 758005cd52b..02ada0bedd9 100644 --- a/lightning/src/offers/invoice_request.rs +++ b/lightning/src/offers/invoice_request.rs @@ -340,15 +340,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) } } @@ -361,7 +354,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); @@ -389,7 +384,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 6f0133ffa7c..12d20061548 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 io; use ln::features::OfferFeatures; -use ln::msgs::{DecodeError, MAX_VALUE_MSAT}; +use ln::msgs::MAX_VALUE_MSAT; use offers::invoice_request::InvoiceRequestBuilder; use offers::parse::{Bech32Encode, ParseError, SemanticError}; use onion_message::BlindedPath; @@ -447,15 +447,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) } } @@ -533,11 +526,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; @@ -551,7 +546,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 ef2673e8fec..709cb4008c8 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 = ::ln::msgs::DecodeError; + + fn try_from(bytes: Vec) -> Result { + let mut cursor = io::Cursor::new(bytes); + let tlv_stream: $tlv_stream = SeekReadable::read(&mut cursor)?; + + if cursor.position() < cursor.get_ref().len() as u64 { + return Err(::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)),* $(,)*}