X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Foffers%2Fparse.rs;h=19d7d74ed6111fd6ddc9fef9d7410a8a47b43354;hb=64b9e83dfed1979adecab83f6efcf10f95ecf987;hp=c9d568a9007aa09c6b90740f0c3ad75ed28ecd85;hpb=60d7ffce10a322ed5c6d93d23c6657ebdd87d389;p=rust-lightning diff --git a/lightning/src/offers/parse.rs b/lightning/src/offers/parse.rs index c9d568a9..19d7d74e 100644 --- a/lightning/src/offers/parse.rs +++ b/lightning/src/offers/parse.rs @@ -13,7 +13,9 @@ use bitcoin::bech32; use bitcoin::bech32::{FromBase32, ToBase32}; use core::convert::TryFrom; use core::fmt; +use crate::io; use crate::ln::msgs::DecodeError; +use crate::util::ser::SeekReadable; use crate::prelude::*; @@ -74,6 +76,30 @@ impl<'a> AsRef for Bech32String<'a> { } } +/// A wrapper for reading a message as a TLV stream `T` from a byte sequence, while still +/// maintaining ownership of the bytes for later use. +pub(crate) struct ParsedMessage { + pub bytes: Vec, + pub tlv_stream: T, +} + +impl TryFrom> for ParsedMessage { + type Error = DecodeError; + + fn try_from(bytes: Vec) -> Result { + let mut cursor = io::Cursor::new(bytes); + let tlv_stream: T = SeekReadable::read(&mut cursor)?; + + // Ensure that there are no more TLV records left to parse. + if cursor.position() < cursor.get_ref().len() as u64 { + return Err(DecodeError::InvalidValue); + } + + let bytes = cursor.into_inner(); + Ok(Self { bytes, tlv_stream }) + } +} + /// Error when parsing a bech32 encoded message using [`str::parse`]. #[derive(Debug, PartialEq)] pub enum ParseError { @@ -98,6 +124,8 @@ pub enum SemanticError { MissingAmount, /// The amount exceeded the total bitcoin supply. InvalidAmount, + /// A currency was provided that is not supported. + UnsupportedCurrency, /// A required description was not provided. MissingDescription, /// A signing pubkey was not provided.