cashu/nuts/nut17/
mod.rs

1//! Specific Subscription for the cdk crate
2use serde::de::DeserializeOwned;
3use serde::{Deserialize, Serialize};
4#[cfg(feature = "mint")]
5use uuid::Uuid;
6
7#[cfg(feature = "mint")]
8use super::PublicKey;
9use crate::nuts::{
10    CurrencyUnit, MeltQuoteBolt11Response, MintQuoteBolt11Response, PaymentMethod, ProofState,
11};
12
13pub mod ws;
14
15/// Subscription Parameter according to the standard
16#[derive(Debug, Clone, Serialize, Eq, PartialEq, Hash, Deserialize)]
17#[serde(bound = "I: DeserializeOwned + Serialize")]
18pub struct Params<I> {
19    /// Kind
20    pub kind: Kind,
21    /// Filters
22    pub filters: Vec<String>,
23    /// Subscription Id
24    #[serde(rename = "subId")]
25    pub id: I,
26}
27
28/// Check state Settings
29#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
30#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
31pub struct SupportedSettings {
32    /// Supported methods
33    pub supported: Vec<SupportedMethods>,
34}
35
36/// Supported WS Methods
37#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
38#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
39pub struct SupportedMethods {
40    /// Payment Method
41    pub method: PaymentMethod,
42    /// Unit
43    pub unit: CurrencyUnit,
44    /// Command
45    pub commands: Vec<WsCommand>,
46}
47
48impl SupportedMethods {
49    /// Create [`SupportedMethods`]
50    pub fn new(method: PaymentMethod, unit: CurrencyUnit, commands: Vec<WsCommand>) -> Self {
51        Self {
52            method,
53            unit,
54            commands,
55        }
56    }
57
58    /// Create [`SupportedMethods`] for Bolt11 with all supported commands
59    pub fn default_bolt11(unit: CurrencyUnit) -> Self {
60        let commands = vec![
61            WsCommand::Bolt11MintQuote,
62            WsCommand::Bolt11MeltQuote,
63            WsCommand::ProofState,
64        ];
65
66        Self {
67            method: PaymentMethod::Bolt11,
68            unit,
69            commands,
70        }
71    }
72}
73
74/// WebSocket commands supported by the Cashu mint
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
76#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
77#[serde(rename_all = "snake_case")]
78pub enum WsCommand {
79    /// Command to request a Lightning invoice for minting tokens
80    #[serde(rename = "bolt11_mint_quote")]
81    Bolt11MintQuote,
82    /// Command to request a Lightning payment for melting tokens
83    #[serde(rename = "bolt11_melt_quote")]
84    Bolt11MeltQuote,
85    /// Command to check the state of a proof
86    #[serde(rename = "proof_state")]
87    ProofState,
88}
89
90#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
91#[serde(bound = "T: Serialize + DeserializeOwned")]
92#[serde(untagged)]
93/// Subscription response
94pub enum NotificationPayload<T> {
95    /// Proof State
96    ProofState(ProofState),
97    /// Melt Quote Bolt11 Response
98    MeltQuoteBolt11Response(MeltQuoteBolt11Response<T>),
99    /// Mint Quote Bolt11 Response
100    MintQuoteBolt11Response(MintQuoteBolt11Response<T>),
101}
102
103impl<T> From<ProofState> for NotificationPayload<T> {
104    fn from(proof_state: ProofState) -> NotificationPayload<T> {
105        NotificationPayload::ProofState(proof_state)
106    }
107}
108
109impl<T> From<MeltQuoteBolt11Response<T>> for NotificationPayload<T> {
110    fn from(melt_quote: MeltQuoteBolt11Response<T>) -> NotificationPayload<T> {
111        NotificationPayload::MeltQuoteBolt11Response(melt_quote)
112    }
113}
114
115impl<T> From<MintQuoteBolt11Response<T>> for NotificationPayload<T> {
116    fn from(mint_quote: MintQuoteBolt11Response<T>) -> NotificationPayload<T> {
117        NotificationPayload::MintQuoteBolt11Response(mint_quote)
118    }
119}
120
121#[cfg(feature = "mint")]
122#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
123/// A parsed notification
124pub enum Notification {
125    /// ProofState id is a Pubkey
126    ProofState(PublicKey),
127    /// MeltQuote id is an Uuid
128    MeltQuoteBolt11(Uuid),
129    /// MintQuote id is an Uuid
130    MintQuoteBolt11(Uuid),
131}
132
133/// Kind
134#[derive(Debug, Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Serialize, Deserialize)]
135#[serde(rename_all = "snake_case")]
136pub enum Kind {
137    /// Bolt 11 Melt Quote
138    Bolt11MeltQuote,
139    /// Bolt 11 Mint Quote
140    Bolt11MintQuote,
141    /// Proof State
142    ProofState,
143}
144
145impl<I> AsRef<I> for Params<I> {
146    fn as_ref(&self) -> &I {
147        &self.id
148    }
149}
150
151/// Parsing error
152#[derive(thiserror::Error, Debug)]
153pub enum Error {
154    #[cfg(feature = "mint")]
155    #[error("Uuid Error: {0}")]
156    /// Uuid Error
157    Uuid(#[from] uuid::Error),
158
159    #[error("PublicKey Error: {0}")]
160    /// PublicKey Error
161    PublicKey(#[from] crate::nuts::nut01::Error),
162}