Jelajahi Sumber

refactor: Update SupportedMethods with WsCommand enum and new constructor

thesimplekid 1 bulan lalu
induk
melakukan
d880bb13cc
2 mengubah file dengan 35 tambahan dan 18 penghapusan
  1. 30 13
      crates/cashu/src/nuts/nut17/mod.rs
  2. 5 5
      crates/cdk-mintd/src/main.rs

+ 30 - 13
crates/cashu/src/nuts/nut17/mod.rs

@@ -42,34 +42,51 @@ pub struct SupportedMethods {
     /// Unit
     pub unit: CurrencyUnit,
     /// Command
-    pub commands: Vec<String>,
+    pub commands: Vec<WsCommand>,
 }
 
 impl SupportedMethods {
     /// Create [`SupportedMethods`]
-    pub fn new(method: PaymentMethod, unit: CurrencyUnit) -> Self {
+    pub fn new(method: PaymentMethod, unit: CurrencyUnit, commands: Vec<WsCommand>) -> Self {
         Self {
             method,
             unit,
-            commands: Vec::new(),
+            commands,
         }
     }
-}
 
-impl Default for SupportedMethods {
-    fn default() -> Self {
-        SupportedMethods {
+    /// Create [`SupportedMethods`] for Bolt11 with all supported commands
+    pub fn default_bolt11(unit: CurrencyUnit) -> Self {
+        let commands = vec![
+            WsCommand::Bolt11MintQuote,
+            WsCommand::Bolt11MeltQuote,
+            WsCommand::ProofState,
+        ];
+
+        Self {
             method: PaymentMethod::Bolt11,
-            unit: CurrencyUnit::Sat,
-            commands: vec![
-                "bolt11_mint_quote".to_owned(),
-                "bolt11_melt_quote".to_owned(),
-                "proof_state".to_owned(),
-            ],
+            unit,
+            commands,
         }
     }
 }
 
+/// WebSocket commands supported by the Cashu mint
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
+#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
+#[serde(rename_all = "snake_case")]
+pub enum WsCommand {
+    /// Command to request a Lightning invoice for minting tokens
+    #[serde(rename = "bolt11_mint_quote")]
+    Bolt11MintQuote,
+    /// Command to request a Lightning payment for melting tokens
+    #[serde(rename = "bolt11_melt_quote")]
+    Bolt11MeltQuote,
+    /// Command to check the state of a proof
+    #[serde(rename = "proof_state")]
+    ProofState,
+}
+
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 #[serde(bound = "T: Serialize + DeserializeOwned")]
 #[serde(untagged)]

+ 5 - 5
crates/cdk-mintd/src/main.rs

@@ -209,7 +209,7 @@ async fn main() -> anyhow::Result<()> {
                 mint_builder = mint_builder.set_unit_fee(&CurrencyUnit::Sat, input_fee)?;
             }
 
-            let nut17_supported = SupportedMethods::new(PaymentMethod::Bolt11, CurrencyUnit::Sat);
+            let nut17_supported = SupportedMethods::default_bolt11(CurrencyUnit::Sat);
 
             mint_builder = mint_builder.add_supported_websockets(nut17_supported);
         }
@@ -232,7 +232,7 @@ async fn main() -> anyhow::Result<()> {
                 mint_builder = mint_builder.set_unit_fee(&CurrencyUnit::Sat, input_fee)?;
             }
 
-            let nut17_supported = SupportedMethods::new(PaymentMethod::Bolt11, CurrencyUnit::Sat);
+            let nut17_supported = SupportedMethods::default_bolt11(CurrencyUnit::Sat);
 
             mint_builder = mint_builder.add_supported_websockets(nut17_supported);
         }
@@ -255,7 +255,7 @@ async fn main() -> anyhow::Result<()> {
                 mint_builder = mint_builder.set_unit_fee(&CurrencyUnit::Sat, input_fee)?;
             }
 
-            let nut17_supported = SupportedMethods::new(PaymentMethod::Bolt11, CurrencyUnit::Sat);
+            let nut17_supported = SupportedMethods::default_bolt11(CurrencyUnit::Sat);
 
             mint_builder = mint_builder.add_supported_websockets(nut17_supported);
         }
@@ -284,7 +284,7 @@ async fn main() -> anyhow::Result<()> {
                     mint_builder = mint_builder.set_unit_fee(&unit, input_fee)?;
                 }
 
-                let nut17_supported = SupportedMethods::new(PaymentMethod::Bolt11, unit);
+                let nut17_supported = SupportedMethods::default_bolt11(unit);
 
                 mint_builder = mint_builder.add_supported_websockets(nut17_supported);
             }
@@ -323,7 +323,7 @@ async fn main() -> anyhow::Result<()> {
                     mint_builder = mint_builder.set_unit_fee(&unit, input_fee)?;
                 }
 
-                let nut17_supported = SupportedMethods::new(PaymentMethod::Bolt11, unit);
+                let nut17_supported = SupportedMethods::default_bolt11(unit);
                 mint_builder = mint_builder.add_supported_websockets(nut17_supported);
             }
         }