//! Specific Subscription for the cdk crate use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use uuid::Uuid; use super::PublicKey; use crate::nuts::{ CurrencyUnit, MeltQuoteBolt11Response, MintQuoteBolt11Response, PaymentMethod, ProofState, }; pub mod ws; /// Subscription Parameter according to the standard #[derive(Debug, Clone, Serialize, Eq, PartialEq, Hash, Deserialize)] #[serde(bound = "I: DeserializeOwned + Serialize")] pub struct Params { /// Kind pub kind: Kind, /// Filters pub filters: Vec, /// Subscription Id #[serde(rename = "subId")] pub id: I, } /// Check state Settings #[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct SupportedSettings { /// Supported methods pub supported: Vec, } /// Supported WS Methods #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct SupportedMethods { /// Payment Method pub method: PaymentMethod, /// Unit pub unit: CurrencyUnit, /// Command pub commands: Vec, } impl SupportedMethods { /// Create [`SupportedMethods`] pub fn new(method: PaymentMethod, unit: CurrencyUnit) -> Self { Self { method, unit, commands: Vec::new(), } } } impl Default for SupportedMethods { fn default() -> Self { SupportedMethods { method: PaymentMethod::Bolt11, unit: CurrencyUnit::Sat, commands: vec![ "bolt11_mint_quote".to_owned(), "bolt11_melt_quote".to_owned(), "proof_state".to_owned(), ], } } } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(bound = "T: Serialize + DeserializeOwned")] #[serde(untagged)] /// Subscription response pub enum NotificationPayload { /// Proof State ProofState(ProofState), /// Melt Quote Bolt11 Response MeltQuoteBolt11Response(MeltQuoteBolt11Response), /// Mint Quote Bolt11 Response MintQuoteBolt11Response(MintQuoteBolt11Response), } impl From for NotificationPayload { fn from(proof_state: ProofState) -> NotificationPayload { NotificationPayload::ProofState(proof_state) } } impl From> for NotificationPayload { fn from(melt_quote: MeltQuoteBolt11Response) -> NotificationPayload { NotificationPayload::MeltQuoteBolt11Response(melt_quote) } } impl From> for NotificationPayload { fn from(mint_quote: MintQuoteBolt11Response) -> NotificationPayload { NotificationPayload::MintQuoteBolt11Response(mint_quote) } } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] /// A parsed notification pub enum Notification { /// ProofState id is a Pubkey ProofState(PublicKey), /// MeltQuote id is an Uuid MeltQuoteBolt11(Uuid), /// MintQuote id is an Uuid MintQuoteBolt11(Uuid), } /// Kind #[derive(Debug, Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum Kind { /// Bolt 11 Melt Quote Bolt11MeltQuote, /// Bolt 11 Mint Quote Bolt11MintQuote, /// Proof State ProofState, } impl AsRef for Params { fn as_ref(&self) -> &I { &self.id } } /// Parsing error #[derive(thiserror::Error, Debug)] pub enum Error { #[error("Uuid Error: {0}")] /// Uuid Error Uuid(#[from] uuid::Error), #[error("PublicKey Error: {0}")] /// PublicKey Error PublicKey(#[from] crate::nuts::nut01::Error), }