Skip to main content

cdk_common/
mint_quote.rs

1//! Unified Mint Quote types for mint use-cases.
2
3use serde::de::DeserializeOwned;
4use serde::{Deserialize, Serialize};
5
6use crate::nuts::nut00::KnownMethod;
7use crate::nuts::nut04::{MintQuoteCustomRequest, MintQuoteCustomResponse};
8use crate::nuts::nut23::{MintQuoteBolt11Request, MintQuoteBolt11Response};
9use crate::nuts::nut25::{MintQuoteBolt12Request, MintQuoteBolt12Response};
10use crate::{Amount, CurrencyUnit, PaymentMethod, PublicKey};
11
12/// Unified mint quote request for all payment methods
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub enum MintQuoteRequest {
15    /// Bolt11 (Lightning invoice)
16    Bolt11(MintQuoteBolt11Request),
17    /// Bolt12 (Offers)
18    Bolt12(MintQuoteBolt12Request),
19    /// Custom payment method
20    Custom((PaymentMethod, MintQuoteCustomRequest)),
21}
22
23impl From<MintQuoteBolt11Request> for MintQuoteRequest {
24    fn from(request: MintQuoteBolt11Request) -> Self {
25        MintQuoteRequest::Bolt11(request)
26    }
27}
28
29impl From<MintQuoteBolt12Request> for MintQuoteRequest {
30    fn from(request: MintQuoteBolt12Request) -> Self {
31        MintQuoteRequest::Bolt12(request)
32    }
33}
34
35impl MintQuoteRequest {
36    /// Returns the payment method for this request.
37    pub fn method(&self) -> PaymentMethod {
38        match self {
39            Self::Bolt11(_) => PaymentMethod::Known(KnownMethod::Bolt11),
40            Self::Bolt12(_) => PaymentMethod::Known(KnownMethod::Bolt12),
41            Self::Custom((method, _)) => method.clone(),
42        }
43    }
44
45    /// Returns the amount for this request when present.
46    pub fn amount(&self) -> Option<Amount> {
47        match self {
48            Self::Bolt11(request) => Some(request.amount),
49            Self::Bolt12(request) => request.amount,
50            Self::Custom((_, request)) => Some(request.amount),
51        }
52    }
53
54    /// Returns the unit for this request.
55    pub fn unit(&self) -> CurrencyUnit {
56        match self {
57            Self::Bolt11(request) => request.unit.clone(),
58            Self::Bolt12(request) => request.unit.clone(),
59            Self::Custom((_, request)) => request.unit.clone(),
60        }
61    }
62
63    /// Returns the payment method for this request.
64    pub fn payment_method(&self) -> PaymentMethod {
65        self.method()
66    }
67
68    /// Returns the pubkey for this request when present.
69    pub fn pubkey(&self) -> Option<PublicKey> {
70        match self {
71            Self::Bolt11(request) => request.pubkey,
72            Self::Bolt12(request) => Some(request.pubkey),
73            Self::Custom((_, request)) => request.pubkey,
74        }
75    }
76}
77
78/// Unified mint quote response for all payment methods
79#[derive(Debug, Clone, Serialize, Deserialize)]
80#[serde(bound = "Q: Serialize + DeserializeOwned")]
81pub enum MintQuoteResponse<Q> {
82    /// Bolt11 (Lightning invoice)
83    Bolt11(MintQuoteBolt11Response<Q>),
84    /// Bolt12 (Offers)
85    Bolt12(MintQuoteBolt12Response<Q>),
86    /// Custom payment method
87    Custom((PaymentMethod, MintQuoteCustomResponse<Q>)),
88}
89
90impl<Q> MintQuoteResponse<Q> {
91    /// Returns the payment method for this response.
92    pub fn method(&self) -> PaymentMethod {
93        match self {
94            Self::Bolt11(_) => PaymentMethod::Known(KnownMethod::Bolt11),
95            Self::Bolt12(_) => PaymentMethod::Known(KnownMethod::Bolt12),
96            Self::Custom((method, _)) => method.clone(),
97        }
98    }
99
100    /// Returns the quote ID.
101    pub fn quote(&self) -> &Q {
102        match self {
103            Self::Bolt11(r) => &r.quote,
104            Self::Bolt12(r) => &r.quote,
105            Self::Custom((_, r)) => &r.quote,
106        }
107    }
108
109    /// Returns the payment request string.
110    pub fn request(&self) -> &str {
111        match self {
112            Self::Bolt11(r) => &r.request,
113            Self::Bolt12(r) => &r.request,
114            Self::Custom((_, r)) => &r.request,
115        }
116    }
117
118    /// Returns the quote state.
119    pub fn state(&self) -> crate::nuts::nut23::QuoteState {
120        match self {
121            Self::Bolt11(r) => r.state,
122            Self::Bolt12(r) => {
123                if r.amount_issued > Amount::ZERO {
124                    crate::nuts::nut23::QuoteState::Issued
125                } else if r.amount_paid >= r.amount.unwrap_or(Amount::ZERO) {
126                    crate::nuts::nut23::QuoteState::Paid
127                } else {
128                    crate::nuts::nut23::QuoteState::Unpaid
129                }
130            }
131            Self::Custom((_, r)) => r.state,
132        }
133    }
134
135    /// Returns the quote expiry timestamp.
136    pub fn expiry(&self) -> Option<u64> {
137        match self {
138            Self::Bolt11(r) => r.expiry,
139            Self::Bolt12(r) => r.expiry,
140            Self::Custom((_, r)) => r.expiry,
141        }
142    }
143}