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, QuoteState};
9use crate::nuts::nut25::{MintQuoteBolt12Request, MintQuoteBolt12Response};
10use crate::nuts::nut30::{MintQuoteOnchainRequest, MintQuoteOnchainResponse};
11use crate::{Amount, CurrencyUnit, PaymentMethod, PublicKey};
12
13/// Unified mint quote request for all payment methods
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub enum MintQuoteRequest {
16    /// Bolt11 (Lightning invoice)
17    Bolt11(MintQuoteBolt11Request),
18    /// Bolt12 (Offers)
19    Bolt12(MintQuoteBolt12Request),
20    /// Onchain
21    Onchain(MintQuoteOnchainRequest),
22    /// Custom payment method
23    Custom {
24        /// Payment method identifier
25        method: PaymentMethod,
26        /// Payment method specific request
27        request: MintQuoteCustomRequest,
28    },
29}
30
31impl From<MintQuoteBolt11Request> for MintQuoteRequest {
32    fn from(request: MintQuoteBolt11Request) -> Self {
33        MintQuoteRequest::Bolt11(request)
34    }
35}
36
37impl From<MintQuoteBolt12Request> for MintQuoteRequest {
38    fn from(request: MintQuoteBolt12Request) -> Self {
39        MintQuoteRequest::Bolt12(request)
40    }
41}
42
43impl From<MintQuoteOnchainRequest> for MintQuoteRequest {
44    fn from(request: MintQuoteOnchainRequest) -> Self {
45        MintQuoteRequest::Onchain(request)
46    }
47}
48
49impl MintQuoteRequest {
50    /// Returns the payment method for this request.
51    pub fn method(&self) -> PaymentMethod {
52        match self {
53            Self::Bolt11(_) => PaymentMethod::Known(KnownMethod::Bolt11),
54            Self::Bolt12(_) => PaymentMethod::Known(KnownMethod::Bolt12),
55            Self::Onchain(_) => PaymentMethod::Known(KnownMethod::Onchain),
56            Self::Custom { method, .. } => method.clone(),
57        }
58    }
59
60    /// Returns the amount for this request when present.
61    pub fn amount(&self) -> Option<Amount> {
62        match self {
63            Self::Bolt11(request) => Some(request.amount),
64            Self::Bolt12(request) => request.amount,
65            Self::Onchain(_) => None,
66            Self::Custom { request, .. } => Some(request.amount),
67        }
68    }
69
70    /// Returns the unit for this request.
71    pub fn unit(&self) -> CurrencyUnit {
72        match self {
73            Self::Bolt11(request) => request.unit.clone(),
74            Self::Bolt12(request) => request.unit.clone(),
75            Self::Onchain(request) => request.unit.clone(),
76            Self::Custom { request, .. } => request.unit.clone(),
77        }
78    }
79
80    /// Returns the payment method for this request.
81    pub fn payment_method(&self) -> PaymentMethod {
82        self.method()
83    }
84
85    /// Returns the pubkey for this request when present.
86    pub fn pubkey(&self) -> Option<PublicKey> {
87        match self {
88            Self::Bolt11(request) => request.pubkey,
89            Self::Bolt12(request) => Some(request.pubkey),
90            Self::Onchain(request) => Some(request.pubkey),
91            Self::Custom { request, .. } => request.pubkey,
92        }
93    }
94}
95
96/// Unified mint quote response for all payment methods
97#[derive(Debug, Clone, Serialize, Deserialize)]
98#[serde(bound = "Q: Serialize + DeserializeOwned")]
99pub enum MintQuoteResponse<Q> {
100    /// Bolt11 (Lightning invoice)
101    Bolt11(MintQuoteBolt11Response<Q>),
102    /// Bolt12 (Offers)
103    Bolt12(MintQuoteBolt12Response<Q>),
104    /// Onchain
105    Onchain(MintQuoteOnchainResponse<Q>),
106    /// Custom payment method
107    Custom {
108        /// Payment method identifier
109        method: PaymentMethod,
110        /// Payment method specific response
111        response: MintQuoteCustomResponse<Q>,
112    },
113}
114
115impl<Q> MintQuoteResponse<Q> {
116    /// Returns the payment method for this response.
117    pub fn method(&self) -> PaymentMethod {
118        match self {
119            Self::Bolt11(_) => PaymentMethod::Known(KnownMethod::Bolt11),
120            Self::Bolt12(_) => PaymentMethod::Known(KnownMethod::Bolt12),
121            Self::Onchain(_) => PaymentMethod::Known(KnownMethod::Onchain),
122            Self::Custom { method, .. } => method.clone(),
123        }
124    }
125
126    /// Returns the quote ID.
127    pub fn quote(&self) -> &Q {
128        match self {
129            Self::Bolt11(r) => &r.quote,
130            Self::Bolt12(r) => &r.quote,
131            Self::Onchain(r) => &r.quote,
132            Self::Custom { response: r, .. } => &r.quote,
133        }
134    }
135
136    /// Returns the payment request string.
137    pub fn request(&self) -> &str {
138        match self {
139            Self::Bolt11(r) => &r.request,
140            Self::Bolt12(r) => &r.request,
141            Self::Onchain(r) => &r.request,
142            Self::Custom { response: r, .. } => &r.request,
143        }
144    }
145
146    /// Returns the quote state derived from the response data.
147    pub fn state(&self) -> Option<QuoteState> {
148        match self {
149            Self::Bolt11(r) => Some(r.state),
150            Self::Bolt12(r) => Some(quote_state_from_amounts(r.amount_paid, r.amount_issued)),
151            Self::Onchain(r) => Some(quote_state_from_amounts(r.amount_paid, r.amount_issued)),
152            Self::Custom { response, .. } => Some(quote_state_from_amounts(
153                response.amount_paid,
154                response.amount_issued,
155            )),
156        }
157    }
158
159    /// Returns the quote expiry timestamp.
160    pub fn expiry(&self) -> Option<u64> {
161        match self {
162            Self::Bolt11(r) => r.expiry,
163            Self::Bolt12(r) => r.expiry,
164            Self::Onchain(r) => r.expiry,
165            Self::Custom { response: r, .. } => r.expiry,
166        }
167    }
168}
169
170pub(crate) fn quote_state_from_amounts(amount_paid: Amount, amount_issued: Amount) -> QuoteState {
171    if amount_paid == Amount::ZERO && amount_issued == Amount::ZERO {
172        return QuoteState::Unpaid;
173    }
174
175    match amount_paid.cmp(&amount_issued) {
176        std::cmp::Ordering::Less | std::cmp::Ordering::Equal => QuoteState::Issued,
177        std::cmp::Ordering::Greater => QuoteState::Paid,
178    }
179}
180
181#[cfg(test)]
182mod tests {
183    use super::*;
184
185    fn custom_response(amount_paid: Amount, amount_issued: Amount) -> MintQuoteResponse<String> {
186        MintQuoteResponse::Custom {
187            method: PaymentMethod::Custom("custom".to_string()),
188            response: MintQuoteCustomResponse {
189                quote: "quote".to_string(),
190                request: "custom-request".to_string(),
191                amount: Some(Amount::from(100)),
192                amount_paid,
193                amount_issued,
194                unit: Some(CurrencyUnit::Sat),
195                expiry: None,
196                pubkey: None,
197                extra: serde_json::Value::Null,
198            },
199        }
200    }
201
202    #[test]
203    fn custom_state_is_derived_from_amount_counters() {
204        assert_eq!(
205            custom_response(Amount::ZERO, Amount::ZERO).state(),
206            Some(QuoteState::Unpaid)
207        );
208        assert_eq!(
209            custom_response(Amount::from(100), Amount::ZERO).state(),
210            Some(QuoteState::Paid)
211        );
212        assert_eq!(
213            custom_response(Amount::from(100), Amount::from(100)).state(),
214            Some(QuoteState::Issued)
215        );
216    }
217
218    #[test]
219    fn bolt12_state_uses_unissued_amount() {
220        let response = MintQuoteResponse::Bolt12(MintQuoteBolt12Response {
221            quote: "quote".to_string(),
222            request: "bolt12-request".to_string(),
223            amount: Some(Amount::from(100)),
224            unit: CurrencyUnit::Sat,
225            expiry: None,
226            pubkey: PublicKey::from_hex(
227                "02a8cda4cf448bfce9a9e46e588c06ea1780fcb94e3bbdf3277f42995d403a8b0c",
228            )
229            .expect("valid public key"),
230            amount_paid: Amount::from(100),
231            amount_issued: Amount::from(40),
232        });
233
234        assert_eq!(response.state(), Some(QuoteState::Paid));
235    }
236}