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<String>,
46}
47
48impl SupportedMethods {
49    /// Create [`SupportedMethods`]
50    pub fn new(method: PaymentMethod, unit: CurrencyUnit) -> Self {
51        Self {
52            method,
53            unit,
54            commands: Vec::new(),
55        }
56    }
57}
58
59impl Default for SupportedMethods {
60    fn default() -> Self {
61        SupportedMethods {
62            method: PaymentMethod::Bolt11,
63            unit: CurrencyUnit::Sat,
64            commands: vec![
65                "bolt11_mint_quote".to_owned(),
66                "bolt11_melt_quote".to_owned(),
67                "proof_state".to_owned(),
68            ],
69        }
70    }
71}
72
73#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
74#[serde(bound = "T: Serialize + DeserializeOwned")]
75#[serde(untagged)]
76/// Subscription response
77pub enum NotificationPayload<T> {
78    /// Proof State
79    ProofState(ProofState),
80    /// Melt Quote Bolt11 Response
81    MeltQuoteBolt11Response(MeltQuoteBolt11Response<T>),
82    /// Mint Quote Bolt11 Response
83    MintQuoteBolt11Response(MintQuoteBolt11Response<T>),
84}
85
86impl<T> From<ProofState> for NotificationPayload<T> {
87    fn from(proof_state: ProofState) -> NotificationPayload<T> {
88        NotificationPayload::ProofState(proof_state)
89    }
90}
91
92impl<T> From<MeltQuoteBolt11Response<T>> for NotificationPayload<T> {
93    fn from(melt_quote: MeltQuoteBolt11Response<T>) -> NotificationPayload<T> {
94        NotificationPayload::MeltQuoteBolt11Response(melt_quote)
95    }
96}
97
98impl<T> From<MintQuoteBolt11Response<T>> for NotificationPayload<T> {
99    fn from(mint_quote: MintQuoteBolt11Response<T>) -> NotificationPayload<T> {
100        NotificationPayload::MintQuoteBolt11Response(mint_quote)
101    }
102}
103
104#[cfg(feature = "mint")]
105#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
106/// A parsed notification
107pub enum Notification {
108    /// ProofState id is a Pubkey
109    ProofState(PublicKey),
110    /// MeltQuote id is an Uuid
111    MeltQuoteBolt11(Uuid),
112    /// MintQuote id is an Uuid
113    MintQuoteBolt11(Uuid),
114}
115
116/// Kind
117#[derive(Debug, Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Serialize, Deserialize)]
118#[serde(rename_all = "snake_case")]
119pub enum Kind {
120    /// Bolt 11 Melt Quote
121    Bolt11MeltQuote,
122    /// Bolt 11 Mint Quote
123    Bolt11MintQuote,
124    /// Proof State
125    ProofState,
126}
127
128impl<I> AsRef<I> for Params<I> {
129    fn as_ref(&self) -> &I {
130        &self.id
131    }
132}
133
134/// Parsing error
135#[derive(thiserror::Error, Debug)]
136pub enum Error {
137    #[cfg(feature = "mint")]
138    #[error("Uuid Error: {0}")]
139    /// Uuid Error
140    Uuid(#[from] uuid::Error),
141
142    #[error("PublicKey Error: {0}")]
143    /// PublicKey Error
144    PublicKey(#[from] crate::nuts::nut01::Error),
145}