Skip to content

Commit

Permalink
f - define try_from_tlv_stream_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
jkczyz committed Nov 9, 2022
1 parent 635d1df commit 49871a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
17 changes: 6 additions & 11 deletions lightning/src/offers/invoice_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,8 @@ impl TryFrom<Vec<u8>> for InvoiceRequest {
type Error = ParseError;

fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
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)
}
}

Expand All @@ -360,7 +353,9 @@ tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, 80..160, {
(89, payer_note: (String, WithoutLength)),
});

type ParsedInvoiceRequest = (Vec<u8>, FullInvoiceRequestTlvStream);
try_from_tlv_stream_bytes!(FullInvoiceRequestTlvStream, ParsedInvoiceRequest);

struct ParsedInvoiceRequest(Vec<u8>, FullInvoiceRequestTlvStream);

type FullInvoiceRequestTlvStream =
(PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream, SignatureTlvStream);
Expand Down Expand Up @@ -388,7 +383,7 @@ impl TryFrom<ParsedInvoiceRequest> for InvoiceRequest {
type Error = ParseError;

fn try_from(invoice_request: ParsedInvoiceRequest) -> Result<Self, Self::Error> {
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 },
Expand Down
19 changes: 7 additions & 12 deletions lightning/src/offers/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -474,15 +474,8 @@ impl TryFrom<Vec<u8>> for Offer {
type Error = ParseError;

fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
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)
}
}

Expand Down Expand Up @@ -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<u8>, OfferTlvStream);
struct ParsedOffer(Vec<u8>, OfferTlvStream);

impl FromStr for Offer {
type Err = ParseError;
Expand All @@ -573,7 +568,7 @@ impl TryFrom<ParsedOffer> for Offer {
type Error = ParseError;

fn try_from(offer: ParsedOffer) -> Result<Self, Self::Error> {
let (bytes, tlv_stream) = offer;
let ParsedOffer(bytes, tlv_stream) = offer;
let contents = OfferContents::try_from(tlv_stream)?;
Ok(Offer { bytes, contents })
}
Expand Down
20 changes: 20 additions & 0 deletions lightning/src/util/ser_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> for $parsed_message {
type Error = $crate::ln::msgs::DecodeError;

fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
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)),* $(,)*}
Expand Down

0 comments on commit 49871a1

Please sign in to comment.