From f9b9d30a1111bd0d4ea718ed0d57f6c2b733522d Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sat, 14 Sep 2024 13:58:14 +0100 Subject: [PATCH] feat: remove custom payment methods --- .../cdk-js/src/nuts/nut00/currency_unit.rs | 1 + crates/cdk-strike/src/lib.rs | 22 +++++++++------ crates/cdk/src/cdk_lightning/mod.rs | 3 ++ crates/cdk/src/nuts/nut00/mod.rs | 28 +++++++++---------- crates/cdk/src/nuts/nut05.rs | 2 +- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/bindings/cdk-js/src/nuts/nut00/currency_unit.rs b/bindings/cdk-js/src/nuts/nut00/currency_unit.rs index b3839591..02251de3 100644 --- a/bindings/cdk-js/src/nuts/nut00/currency_unit.rs +++ b/bindings/cdk-js/src/nuts/nut00/currency_unit.rs @@ -18,6 +18,7 @@ impl From for JsCurrencyUnit { CurrencyUnit::Msat => JsCurrencyUnit::Msat, CurrencyUnit::Usd => JsCurrencyUnit::Usd, CurrencyUnit::Eur => JsCurrencyUnit::Eur, + _ => panic!("Unsupported unit"), } } } diff --git a/crates/cdk-strike/src/lib.rs b/crates/cdk-strike/src/lib.rs index 620e81e8..98cb9aad 100644 --- a/crates/cdk-strike/src/lib.rs +++ b/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(amount: T, current_unit: &CurrencyUnit) -> StrikeAmount +pub(crate) fn to_strike_unit( + amount: T, + current_unit: &CurrencyUnit, +) -> anyhow::Result where T: Into, { 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"), } } diff --git a/crates/cdk/src/cdk_lightning/mod.rs b/crates/cdk/src/cdk_lightning/mod.rs index d79fe777..3542a168 100644 --- a/crates/cdk/src/cdk_lightning/mod.rs +++ b/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), diff --git a/crates/cdk/src/nuts/nut00/mod.rs b/crates/cdk/src/nuts/nut00/mod.rs index 8b6e85c9..58fbce99 100644 --- a/crates/cdk/src/nuts/nut00/mod.rs +++ b/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 From for PaymentMethod -where - S: AsRef, -{ - 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 { + 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")) } } diff --git a/crates/cdk/src/nuts/nut05.rs b/crates/cdk/src/nuts/nut05.rs index e1b9fac6..dd01d061 100644 --- a/crates/cdk/src/nuts/nut05.rs +++ b/crates/cdk/src/nuts/nut05.rs @@ -242,7 +242,7 @@ impl From 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,