cashu/nuts/
nut25.rs

1//! Bolt12
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4
5use super::{CurrencyUnit, MeltOptions, PublicKey};
6#[cfg(feature = "mint")]
7use crate::quote_id::QuoteId;
8use crate::Amount;
9
10/// NUT18 Error
11#[derive(Debug, Error)]
12pub enum Error {
13    /// Unknown Quote State
14    #[error("Unknown quote state")]
15    UnknownState,
16    /// Amount overflow
17    #[error("Amount Overflow")]
18    AmountOverflow,
19    /// Publickey not defined
20    #[error("Publickey not defined")]
21    PublickeyUndefined,
22}
23
24/// Mint quote request [NUT-24]
25#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
26#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
27pub struct MintQuoteBolt12Request {
28    /// Amount
29    pub amount: Option<Amount>,
30    /// Unit wallet would like to pay with
31    pub unit: CurrencyUnit,
32    /// Memo to create the invoice with
33    pub description: Option<String>,
34    /// Pubkey
35    pub pubkey: PublicKey,
36}
37
38/// Mint quote response [NUT-24]
39#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
40#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
41#[serde(bound = "Q: Serialize + for<'a> Deserialize<'a>")]
42pub struct MintQuoteBolt12Response<Q> {
43    /// Quote Id
44    pub quote: Q,
45    /// Payment request to fulfil
46    pub request: String,
47    /// Amount
48    pub amount: Option<Amount>,
49    /// Unit wallet would like to pay with
50    pub unit: CurrencyUnit,
51    /// Unix timestamp until the quote is valid
52    pub expiry: Option<u64>,
53    /// Pubkey
54    pub pubkey: PublicKey,
55    /// Amount that has been paid
56    pub amount_paid: Amount,
57    /// Amount that has been issued
58    pub amount_issued: Amount,
59}
60
61#[cfg(feature = "mint")]
62impl<Q: ToString> MintQuoteBolt12Response<Q> {
63    /// Convert the MintQuote with a quote type Q to a String
64    pub fn to_string_id(&self) -> MintQuoteBolt12Response<String> {
65        MintQuoteBolt12Response {
66            quote: self.quote.to_string(),
67            request: self.request.clone(),
68            amount: self.amount,
69            unit: self.unit.clone(),
70            expiry: self.expiry,
71            pubkey: self.pubkey,
72            amount_paid: self.amount_paid,
73            amount_issued: self.amount_issued,
74        }
75    }
76}
77
78#[cfg(feature = "mint")]
79impl From<MintQuoteBolt12Response<QuoteId>> for MintQuoteBolt12Response<String> {
80    fn from(value: MintQuoteBolt12Response<QuoteId>) -> Self {
81        Self {
82            quote: value.quote.to_string(),
83            request: value.request,
84            expiry: value.expiry,
85            amount_paid: value.amount_paid,
86            amount_issued: value.amount_issued,
87            pubkey: value.pubkey,
88            amount: value.amount,
89            unit: value.unit,
90        }
91    }
92}
93
94/// Melt quote request [NUT-18]
95#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
96#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
97pub struct MeltQuoteBolt12Request {
98    /// Bolt12 invoice to be paid
99    pub request: String,
100    /// Unit wallet would like to pay with
101    pub unit: CurrencyUnit,
102    /// Payment Options
103    pub options: Option<MeltOptions>,
104}