From: Jeffrey Czyz Date: Wed, 21 Sep 2022 18:09:06 +0000 (-0500) Subject: Use SemanticError in OfferBuilder::build X-Git-Tag: v0.0.113~36^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=3a6d7b867eb46d85645997e7a485967eab80aa8c;p=rust-lightning Use SemanticError in OfferBuilder::build --- diff --git a/lightning/src/offers/offer.rs b/lightning/src/offers/offer.rs index d941b6937..ebab6599f 100644 --- a/lightning/src/offers/offer.rs +++ b/lightning/src/offers/offer.rs @@ -48,8 +48,7 @@ //! .issuer("Foo Bar".to_string()) //! .path(create_blinded_path()) //! .path(create_another_blinded_path()) -//! .build() -//! .unwrap(); +//! .build()?; //! //! // Encode as a bech32 string for use in a QR code. //! let encoded_offer = offer.to_string(); @@ -195,14 +194,14 @@ impl OfferBuilder { } /// Builds an [`Offer`] from the builder's settings. - pub fn build(mut self) -> Result { + pub fn build(mut self) -> Result { match self.offer.amount { Some(Amount::Bitcoin { amount_msats }) => { if amount_msats > MAX_VALUE_MSAT { - return Err(()); + return Err(SemanticError::InvalidAmount); } }, - Some(Amount::Currency { .. }) => unreachable!(), + Some(Amount::Currency { .. }) => return Err(SemanticError::UnsupportedCurrency), None => {}, } @@ -553,6 +552,7 @@ mod tests { use core::time::Duration; use crate::ln::features::OfferFeatures; use crate::ln::msgs::MAX_VALUE_MSAT; + use crate::offers::parse::SemanticError; use crate::onion_message::{BlindedHop, BlindedPath}; use crate::util::ser::Writeable; use crate::util::string::PrintableString; @@ -678,6 +678,10 @@ mod tests { assert_eq!(builder.offer.amount, Some(currency_amount.clone())); assert_eq!(tlv_stream.amount, Some(10)); assert_eq!(tlv_stream.currency, Some(b"USD")); + match builder.build() { + Ok(_) => panic!("expected error"), + Err(e) => assert_eq!(e, SemanticError::UnsupportedCurrency), + } let offer = OfferBuilder::new("foo".into(), pubkey(42)) .amount(currency_amount.clone()) @@ -691,7 +695,7 @@ mod tests { let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 }; match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, ()), + Err(e) => assert_eq!(e, SemanticError::InvalidAmount), } } diff --git a/lightning/src/offers/parse.rs b/lightning/src/offers/parse.rs index c9d568a90..032e95b3c 100644 --- a/lightning/src/offers/parse.rs +++ b/lightning/src/offers/parse.rs @@ -98,6 +98,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.