cdk_common/
subscription.rs1#[cfg(feature = "mint")]
3use std::str::FromStr;
4
5use cashu::nut17::{self};
6#[cfg(feature = "mint")]
7use cashu::nut17::{Error, Kind, Notification};
8#[cfg(feature = "mint")]
9use cashu::{NotificationPayload, PublicKey};
10#[cfg(feature = "mint")]
11use serde::{Deserialize, Serialize};
12#[cfg(feature = "mint")]
13use uuid::Uuid;
14
15#[cfg(feature = "mint")]
16use crate::pub_sub::index::{Index, Indexable, SubscriptionGlobalId};
17use crate::pub_sub::SubId;
18
19pub type Params = nut17::Params<SubId>;
23
24#[cfg(feature = "mint")]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct IndexableParams(Params);
28
29#[cfg(feature = "mint")]
30impl From<Params> for IndexableParams {
31 fn from(params: Params) -> Self {
32 Self(params)
33 }
34}
35
36#[cfg(feature = "mint")]
37impl TryFrom<IndexableParams> for Vec<Index<Notification>> {
38 type Error = Error;
39 fn try_from(params: IndexableParams) -> Result<Self, Self::Error> {
40 let sub_id: SubscriptionGlobalId = Default::default();
41 let params = params.0;
42 params
43 .filters
44 .into_iter()
45 .map(|filter| {
46 let idx = match params.kind {
47 Kind::Bolt11MeltQuote => {
48 Notification::MeltQuoteBolt11(Uuid::from_str(&filter)?)
49 }
50 Kind::Bolt11MintQuote => {
51 Notification::MintQuoteBolt11(Uuid::from_str(&filter)?)
52 }
53 Kind::ProofState => Notification::ProofState(PublicKey::from_str(&filter)?),
54 Kind::Bolt12MintQuote => {
55 Notification::MintQuoteBolt12(Uuid::from_str(&filter)?)
56 }
57 };
58
59 Ok(Index::from((idx, params.id.clone(), sub_id)))
60 })
61 .collect::<Result<_, _>>()
62 }
63}
64
65#[cfg(feature = "mint")]
66impl AsRef<SubId> for IndexableParams {
67 fn as_ref(&self) -> &SubId {
68 &self.0.id
69 }
70}
71
72#[cfg(feature = "mint")]
73impl Indexable for NotificationPayload<Uuid> {
74 type Type = Notification;
75
76 fn to_indexes(&self) -> Vec<Index<Self::Type>> {
77 match self {
78 NotificationPayload::ProofState(proof_state) => {
79 vec![Index::from(Notification::ProofState(proof_state.y))]
80 }
81 NotificationPayload::MeltQuoteBolt11Response(melt_quote) => {
82 vec![Index::from(Notification::MeltQuoteBolt11(melt_quote.quote))]
83 }
84 NotificationPayload::MintQuoteBolt11Response(mint_quote) => {
85 vec![Index::from(Notification::MintQuoteBolt11(mint_quote.quote))]
86 }
87 NotificationPayload::MintQuoteBolt12Response(mint_quote) => {
88 vec![Index::from(Notification::MintQuoteBolt12(mint_quote.quote))]
89 }
90 }
91 }
92}