-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathnutxx.rs
More file actions
249 lines (218 loc) · 7.9 KB
/
Copy pathnutxx.rs
File metadata and controls
249 lines (218 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! NUT-XX: Batch Minting
//!
//! Batch minting support for Cashu
use serde::{Deserialize, Serialize};
use super::nut00::{BlindedMessage, PaymentMethod};
use super::nut23::MintQuoteBolt11Response;
use super::nut25::MintQuoteBolt12Response;
use super::MintQuoteState;
use crate::Amount;
/// Batch Mint Request [NUT-XX]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
pub struct BatchMintRequest {
/// Quote IDs
pub quotes: Vec<String>,
/// Expected amount to mint per quote, in the same order as `quote`
#[serde(skip_serializing_if = "Option::is_none", default)]
pub quote_amounts: Option<Vec<Amount>>,
/// Blinded messages
pub outputs: Vec<BlindedMessage>,
/// Signatures for NUT-20 locked quotes (optional)
#[serde(skip_serializing_if = "Option::is_none")]
pub signatures: Option<Vec<Option<String>>>,
}
impl BatchMintRequest {
/// Total amount of outputs
pub fn total_amount(&self) -> Result<Amount, crate::nuts::nut04::Error> {
Amount::try_sum(self.outputs.iter().map(|msg| msg.amount))
.map_err(|_| crate::nuts::nut04::Error::AmountOverflow)
}
/// Total amount the client expects to mint per quote, if provided
pub fn total_quote_amounts(&self) -> Option<Result<Amount, crate::nuts::nut04::Error>> {
self.quote_amounts.as_ref().map(|amounts| {
Amount::try_sum(amounts.iter().cloned())
.map_err(|_| crate::nuts::nut04::Error::AmountOverflow)
})
}
}
/// Batch Quote Status Request [NUT-XX]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
pub struct BatchQuoteStatusRequest {
/// Quote IDs
pub quotes: Vec<String>,
}
/// Batch minting settings (NUT-XX)
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
pub struct BatchMintSettings {
/// Maximum quotes allowed in a batch request
#[serde(skip_serializing_if = "Option::is_none")]
pub max_batch_size: Option<u16>,
/// Supported payment methods for batch minting
#[serde(default)]
pub methods: Vec<PaymentMethod>,
}
impl Default for BatchMintSettings {
fn default() -> Self {
Self {
max_batch_size: Some(100),
methods: Vec::new(),
}
}
}
impl BatchMintSettings {
/// Returns true when no batch capabilities should be advertised
pub fn is_empty(&self) -> bool {
self.methods.is_empty()
}
}
/// Bolt12 batch status payload extends the standard Bolt12 quote with a derived state.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
#[serde(bound = "Q: Serialize + for<'a> Deserialize<'a>")]
pub struct MintQuoteBolt12BatchStatusResponse<Q> {
/// Underlying Bolt12 quote payload
#[serde(flatten)]
pub quote: MintQuoteBolt12Response<Q>,
/// Derived quote state (UNPAID, PAID, ISSUED)
pub state: MintQuoteState,
}
impl<Q> MintQuoteBolt12BatchStatusResponse<Q> {
fn from_quote(quote: MintQuoteBolt12Response<Q>) -> Self {
let state = derive_quote_state(quote.amount_paid, quote.amount_issued);
Self { quote, state }
}
/// Current quote state
pub fn state(&self) -> MintQuoteState {
self.state
}
}
impl<Q> From<MintQuoteBolt12Response<Q>> for MintQuoteBolt12BatchStatusResponse<Q> {
fn from(value: MintQuoteBolt12Response<Q>) -> Self {
Self::from_quote(value)
}
}
/// Batch quote status entry supporting both Bolt11 and Bolt12 payloads.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
#[serde(untagged)]
pub enum BatchQuoteStatusItem {
/// Bolt11 quote payload
Bolt11(MintQuoteBolt11Response<String>),
/// Bolt12 quote payload
Bolt12(MintQuoteBolt12BatchStatusResponse<String>),
}
impl BatchQuoteStatusItem {
/// Quote state (UNPAID, PAID, ISSUED)
pub fn state(&self) -> MintQuoteState {
match self {
Self::Bolt11(response) => response.state,
Self::Bolt12(response) => response.state(),
}
}
}
impl From<MintQuoteBolt11Response<String>> for BatchQuoteStatusItem {
fn from(value: MintQuoteBolt11Response<String>) -> Self {
Self::Bolt11(value)
}
}
impl From<MintQuoteBolt12Response<String>> for BatchQuoteStatusItem {
fn from(value: MintQuoteBolt12Response<String>) -> Self {
let batch_response = MintQuoteBolt12BatchStatusResponse::from(value);
Self::Bolt12(batch_response)
}
}
impl From<MintQuoteBolt12BatchStatusResponse<String>> for BatchQuoteStatusItem {
fn from(value: MintQuoteBolt12BatchStatusResponse<String>) -> Self {
Self::Bolt12(value)
}
}
/// Batch Quote Status Response [NUT-XX]
/// Returns a Vec that should be serialized as a JSON array
#[derive(Debug, Clone)]
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema), schema(transparent))]
pub struct BatchQuoteStatusResponse(
/// Vector of quote status responses as JSON
pub Vec<BatchQuoteStatusItem>,
);
impl Serialize for BatchQuoteStatusResponse {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for BatchQuoteStatusResponse {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Vec::<BatchQuoteStatusItem>::deserialize(deserializer).map(BatchQuoteStatusResponse)
}
}
fn derive_quote_state(amount_paid: Amount, amount_issued: Amount) -> MintQuoteState {
if amount_paid == Amount::ZERO && amount_issued == Amount::ZERO {
return MintQuoteState::Unpaid;
}
match amount_paid.cmp(&amount_issued) {
std::cmp::Ordering::Less => {
tracing::error!("Bolt12 quote has issued more than paid");
MintQuoteState::Issued
}
std::cmp::Ordering::Equal => MintQuoteState::Issued,
std::cmp::Ordering::Greater => MintQuoteState::Paid,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CurrencyUnit, PublicKey};
use std::str::FromStr;
#[test]
fn batch_quote_status_item_returns_bolt11_state() {
let response = MintQuoteBolt11Response {
quote: "quote-1".to_string(),
request: "bolt11".to_string(),
amount: Some(Amount::from(100u64)),
unit: Some(CurrencyUnit::Sat),
state: MintQuoteState::Paid,
expiry: Some(42),
pubkey: None,
};
let item: BatchQuoteStatusItem = response.clone().into();
assert_eq!(item.state(), MintQuoteState::Paid);
match item {
BatchQuoteStatusItem::Bolt11(inner) => assert_eq!(inner.quote, response.quote),
_ => panic!("Expected bolt11 variant"),
}
}
#[test]
fn batch_quote_status_item_derives_bolt12_state() {
let pubkey = PublicKey::from_str(
"0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
)
.expect("valid pubkey");
let response = MintQuoteBolt12Response {
quote: "quote-2".to_string(),
request: "bolt12".to_string(),
amount: Some(Amount::from(200u64)),
unit: CurrencyUnit::Sat,
expiry: Some(100),
pubkey,
amount_paid: Amount::from(200u64),
amount_issued: Amount::from(50u64),
};
let item: BatchQuoteStatusItem = response.into();
assert_eq!(item.state(), MintQuoteState::Paid);
match item {
BatchQuoteStatusItem::Bolt12(inner) => {
assert_eq!(inner.quote.quote, "quote-2");
assert_eq!(inner.state(), MintQuoteState::Paid);
}
_ => panic!("Expected bolt12 variant"),
}
}
}