X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=sidebyside;f=lightning%2Fsrc%2Foffers%2Foffer.rs;h=704045f760b02867e094026a3a0e87816bd82189;hb=1e26a2bc197207be512d905b05595492242b5f90;hp=e57334f316c02745f2d53127ef3c548b3383af47;hpb=03d0a4b497338b29fe9aa8c065edcfe26442e6e0;p=rust-lightning diff --git a/lightning/src/offers/offer.rs b/lightning/src/offers/offer.rs index e57334f3..704045f7 100644 --- a/lightning/src/offers/offer.rs +++ b/lightning/src/offers/offer.rs @@ -76,9 +76,9 @@ use core::time::Duration; use crate::io; use crate::ln::features::OfferFeatures; use crate::ln::msgs::MAX_VALUE_MSAT; -use crate::offers::parse::{Bech32Encode, ParseError, SemanticError}; +use crate::offers::parse::{Bech32Encode, ParseError, ParsedMessage, SemanticError}; use crate::onion_message::BlindedPath; -use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer}; +use crate::util::ser::{HighZeroBytesDroppedBigSize, WithoutLength, Writeable, Writer}; use crate::util::string::PrintableString; use crate::prelude::*; @@ -185,7 +185,8 @@ impl OfferBuilder { self } - /// Sets the quantity of items for [`Offer::supported_quantity`]. + /// Sets the quantity of items for [`Offer::supported_quantity`]. If not called, defaults to + /// [`Quantity::one`]. /// /// Successive calls to this method will override the previous setting. pub fn supported_quantity(mut self, quantity: Quantity) -> Self { @@ -394,15 +395,6 @@ impl Writeable for OfferContents { } } -impl TryFrom> for Offer { - type Error = ParseError; - - fn try_from(bytes: Vec) -> Result { - let tlv_stream: OfferTlvStream = Readable::read(&mut &bytes[..])?; - Offer::try_from((bytes, tlv_stream)) - } -} - /// The minimum amount required for an item in an [`Offer`], denominated in either bitcoin or /// another currency. #[derive(Clone, Debug, PartialEq)] @@ -434,7 +426,8 @@ pub enum Quantity { } impl Quantity { - fn one() -> Self { + /// The default quantity of one. + pub fn one() -> Self { Quantity::Bounded(NonZeroU64::new(1).unwrap()) } @@ -449,7 +442,7 @@ impl Quantity { } } -tlv_stream!(OfferTlvStream, OfferTlvStreamRef, { +tlv_stream!(OfferTlvStream, OfferTlvStreamRef, 1..80, { (2, chains: (Vec, WithoutLength)), (4, metadata: (Vec, WithoutLength)), (6, currency: CurrencyCode), @@ -467,8 +460,6 @@ impl Bech32Encode for Offer { const BECH32_HRP: &'static str = "lno"; } -type ParsedOffer = (Vec, OfferTlvStream); - impl FromStr for Offer { type Err = ParseError; @@ -477,11 +468,12 @@ impl FromStr for Offer { } } -impl TryFrom for Offer { +impl TryFrom> for Offer { type Error = ParseError; - fn try_from(offer: ParsedOffer) -> Result { - let (bytes, tlv_stream) = offer; + fn try_from(bytes: Vec) -> Result { + let offer = ParsedMessage::::try_from(bytes)?; + let ParsedMessage { bytes, tlv_stream } = offer; let contents = OfferContents::try_from(tlv_stream)?; Ok(Offer { bytes, contents }) } @@ -551,10 +543,10 @@ mod tests { use core::num::NonZeroU64; use core::time::Duration; use crate::ln::features::OfferFeatures; - use crate::ln::msgs::MAX_VALUE_MSAT; + use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT}; use crate::offers::parse::{ParseError, SemanticError}; use crate::onion_message::{BlindedHop, BlindedPath}; - use crate::util::ser::Writeable; + use crate::util::ser::{BigSize, Writeable}; use crate::util::string::PrintableString; fn pubkey(byte: u8) -> PublicKey { @@ -1003,6 +995,22 @@ mod tests { }, } } + + #[test] + fn fails_parsing_offer_with_extra_tlv_records() { + let offer = OfferBuilder::new("foo".into(), pubkey(42)).build().unwrap(); + + let mut encoded_offer = Vec::new(); + offer.write(&mut encoded_offer).unwrap(); + BigSize(80).write(&mut encoded_offer).unwrap(); + BigSize(32).write(&mut encoded_offer).unwrap(); + [42u8; 32].write(&mut encoded_offer).unwrap(); + + match Offer::try_from(encoded_offer) { + Ok(_) => panic!("expected error"), + Err(e) => assert_eq!(e, ParseError::Decode(DecodeError::InvalidValue)), + } + } } #[cfg(test)]