Quellcode durchsuchen

feat: remove custom payment methods

thesimplekid vor 5 Monaten
Ursprung
Commit
f9b9d30a11

+ 1 - 0
bindings/cdk-js/src/nuts/nut00/currency_unit.rs

@@ -18,6 +18,7 @@ impl From<CurrencyUnit> for JsCurrencyUnit {
             CurrencyUnit::Msat => JsCurrencyUnit::Msat,
             CurrencyUnit::Usd => JsCurrencyUnit::Usd,
             CurrencyUnit::Eur => JsCurrencyUnit::Eur,
+            _ => panic!("Unsupported unit"),
         }
     }
 }

+ 14 - 8
crates/cdk-strike/src/lib.rs

@@ -132,6 +132,7 @@ impl MintLightning for Strike {
             CurrencyUnit::Msat => StrikeCurrencyUnit::BTC,
             CurrencyUnit::Usd => StrikeCurrencyUnit::USD,
             CurrencyUnit::Eur => StrikeCurrencyUnit::EUR,
+            _ => return Err(Self::Err::UnsupportedUnit),
         };
 
         let payment_quote_request = PayInvoiceQuoteRequest {
@@ -195,7 +196,7 @@ impl MintLightning for Strike {
 
         let invoice_request = InvoiceRequest {
             correlation_id: Some(request_lookup_id.to_string()),
-            amount: to_strike_unit(amount, &self.unit),
+            amount: to_strike_unit(amount, &self.unit)?,
             description: Some(description),
         };
 
@@ -278,32 +279,37 @@ pub(crate) fn from_strike_amount(
                 bail!("Could not convert to EUR");
             }
         }
+        _ => bail!("Unsupported unit"),
     }
 }
 
-pub(crate) fn to_strike_unit<T>(amount: T, current_unit: &CurrencyUnit) -> StrikeAmount
+pub(crate) fn to_strike_unit<T>(
+    amount: T,
+    current_unit: &CurrencyUnit,
+) -> anyhow::Result<StrikeAmount>
 where
     T: Into<u64>,
 {
     let amount = amount.into();
     match current_unit {
-        CurrencyUnit::Sat => StrikeAmount::from_sats(amount),
-        CurrencyUnit::Msat => StrikeAmount::from_sats(amount / 1000),
+        CurrencyUnit::Sat => Ok(StrikeAmount::from_sats(amount)),
+        CurrencyUnit::Msat => Ok(StrikeAmount::from_sats(amount / 1000)),
         CurrencyUnit::Usd => {
             let dollars = (amount as f64 / 100_f64) * 100.0;
 
-            StrikeAmount {
+            Ok(StrikeAmount {
                 currency: StrikeCurrencyUnit::USD,
                 amount: dollars.round() / 100.0,
-            }
+            })
         }
         CurrencyUnit::Eur => {
             let euro = (amount as f64 / 100_f64) * 100.0;
 
-            StrikeAmount {
+            Ok(StrikeAmount {
                 currency: StrikeCurrencyUnit::EUR,
                 amount: euro.round() / 100.0,
-            }
+            })
         }
+        _ => bail!("Unsupported unit"),
     }
 }

+ 3 - 0
crates/cdk/src/cdk_lightning/mod.rs

@@ -23,6 +23,9 @@ pub enum Error {
     /// Invoice pay pending
     #[error("Invoice pay is pending")]
     InvoicePaymentPending,
+    /// Unsupported unit
+    #[error("Unsupported unit")]
+    UnsupportedUnit,
     /// Lightning Error
     #[error(transparent)]
     Lightning(Box<dyn std::error::Error + Send + Sync>),

+ 14 - 14
crates/cdk/src/nuts/nut00/mod.rs

@@ -8,7 +8,7 @@ use std::hash::{Hash, Hasher};
 use std::str::FromStr;
 use std::string::FromUtf8Error;
 
-use serde::{Deserialize, Deserializer, Serialize};
+use serde::{de, Deserialize, Deserializer, Serialize};
 use thiserror::Error;
 
 use super::nut10;
@@ -41,6 +41,9 @@ pub enum Error {
     /// Unsupported token
     #[error("Unsupported unit")]
     UnsupportedUnit,
+    /// Unsupported token
+    #[error("Unsupported payment method")]
+    UnsupportedPaymentMethod,
     /// Invalid Url
     #[error("Invalid URL")]
     InvalidUrl,
@@ -321,6 +324,7 @@ where
 }
 
 /// Currency Unit
+#[non_exhaustive]
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
 pub enum CurrencyUnit {
     /// Sat
@@ -391,23 +395,20 @@ impl<'de> Deserialize<'de> for CurrencyUnit {
 }
 
 /// Payment Method
-#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
+#[non_exhaustive]
+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
 pub enum PaymentMethod {
     /// Bolt11 payment type
     #[default]
     Bolt11,
-    /// Custom payment type:
-    Custom(String),
 }
 
-impl<S> From<S> for PaymentMethod
-where
-    S: AsRef<str>,
-{
-    fn from(method: S) -> Self {
-        match method.as_ref() {
-            "bolt11" => Self::Bolt11,
-            o => Self::Custom(o.to_string()),
+impl FromStr for PaymentMethod {
+    type Err = Error;
+    fn from_str(value: &str) -> Result<Self, Self::Err> {
+        match value {
+            "bolt11" => Ok(Self::Bolt11),
+            _ => Err(Error::UnsupportedPaymentMethod),
         }
     }
 }
@@ -416,7 +417,6 @@ impl fmt::Display for PaymentMethod {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         match self {
             PaymentMethod::Bolt11 => write!(f, "bolt11"),
-            PaymentMethod::Custom(unit) => write!(f, "{}", unit),
         }
     }
 }
@@ -436,7 +436,7 @@ impl<'de> Deserialize<'de> for PaymentMethod {
         D: Deserializer<'de>,
     {
         let payment_method: String = String::deserialize(deserializer)?;
-        Ok(Self::from(payment_method))
+        Self::from_str(&payment_method).map_err(|_| de::Error::custom("Unsupported payment method"))
     }
 }
 

+ 1 - 1
crates/cdk/src/nuts/nut05.rs

@@ -242,7 +242,7 @@ impl From<MeltQuoteBolt11Response> for MeltBolt11Response {
 }
 
 /// Melt Method Settings
-#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
 pub struct MeltMethodSettings {
     /// Payment Method e.g. bolt11
     pub method: PaymentMethod,