Expose the default Quantity::one as pub
[rust-lightning] / lightning / src / offers / offer.rs
index e57334f316c02745f2d53127ef3c548b3383af47..704045f760b02867e094026a3a0e87816bd82189 100644 (file)
@@ -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<Vec<u8>> for Offer {
-       type Error = ParseError;
-
-       fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
-               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<ChainHash>, WithoutLength)),
        (4, metadata: (Vec<u8>, WithoutLength)),
        (6, currency: CurrencyCode),
@@ -467,8 +460,6 @@ impl Bech32Encode for Offer {
        const BECH32_HRP: &'static str = "lno";
 }
 
-type ParsedOffer = (Vec<u8>, OfferTlvStream);
-
 impl FromStr for Offer {
        type Err = ParseError;
 
@@ -477,11 +468,12 @@ impl FromStr for Offer {
        }
 }
 
-impl TryFrom<ParsedOffer> for Offer {
+impl TryFrom<Vec<u8>> for Offer {
        type Error = ParseError;
 
-       fn try_from(offer: ParsedOffer) -> Result<Self, Self::Error> {
-               let (bytes, tlv_stream) = offer;
+       fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
+               let offer = ParsedMessage::<OfferTlvStream>::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)]