-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathnut23.rs
More file actions
314 lines (292 loc) · 9.24 KB
/
Copy pathnut23.rs
File metadata and controls
314 lines (292 loc) · 9.24 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! Bolt11
use std::fmt;
use std::str::FromStr;
use lightning_invoice::Bolt11Invoice;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use super::{BlindSignature, CurrencyUnit, MeltQuoteState, Mpp, PublicKey};
#[cfg(feature = "mint")]
use crate::quote_id::QuoteId;
use crate::util::serde_helpers::deserialize_empty_string_as_none;
use crate::Amount;
/// NUT023 Error
#[derive(Debug, Error)]
pub enum Error {
/// Unknown Quote State
#[error("Unknown Quote State")]
UnknownState,
/// Amount overflow
#[error("Amount overflow")]
AmountOverflow,
/// Invalid Amount
#[error("Invalid Request")]
InvalidAmountRequest,
}
/// Mint quote request [NUT-04]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MintQuoteBolt11Request {
/// Amount
pub amount: Amount,
/// Unit wallet would like to pay with
pub unit: CurrencyUnit,
/// Memo to create the invoice with
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// NUT-19 Pubkey
#[serde(skip_serializing_if = "Option::is_none")]
pub pubkey: Option<PublicKey>,
}
/// Possible states of a quote
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum QuoteState {
/// Quote has not been paid
#[default]
Unpaid,
/// Quote has been paid and wallet can mint
Paid,
/// ecash issued for quote
Issued,
}
impl fmt::Display for QuoteState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Unpaid => write!(f, "UNPAID"),
Self::Paid => write!(f, "PAID"),
Self::Issued => write!(f, "ISSUED"),
}
}
}
impl FromStr for QuoteState {
type Err = Error;
fn from_str(state: &str) -> Result<Self, Self::Err> {
match state {
"PAID" => Ok(Self::Paid),
"UNPAID" => Ok(Self::Unpaid),
"ISSUED" => Ok(Self::Issued),
_ => Err(Error::UnknownState),
}
}
}
/// Mint quote response [NUT-04]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(bound = "Q: Serialize + DeserializeOwned")]
pub struct MintQuoteBolt11Response<Q> {
/// Quote Id
pub quote: Q,
/// Payment request to fulfil
pub request: String,
/// Amount
// REVIEW: This is now required in the spec, we should remove the option once all mints update
pub amount: Option<Amount>,
/// Unit
// REVIEW: This is now required in the spec, we should remove the option once all mints update
pub unit: Option<CurrencyUnit>,
/// Amount that has been paid
#[serde(default)]
pub amount_paid: Amount,
/// Amount that has been issued
#[serde(default)]
pub amount_issued: Amount,
/// Unix timestamp indicating when the quote was last updated
#[serde(default)]
pub updated_at: u64,
/// Quote State
#[serde(default)]
pub state: QuoteState,
/// Unix timestamp until the quote is valid
pub expiry: Option<u64>,
/// NUT-19 Pubkey
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_empty_string_as_none"
)]
pub pubkey: Option<PublicKey>,
}
impl<Q: ToString> MintQuoteBolt11Response<Q> {
/// Convert the MintQuote with a quote type Q to a String
pub fn to_string_id(&self) -> MintQuoteBolt11Response<String> {
MintQuoteBolt11Response {
quote: self.quote.to_string(),
request: self.request.clone(),
state: self.state,
expiry: self.expiry,
pubkey: self.pubkey,
amount: self.amount,
unit: self.unit.clone(),
amount_paid: self.amount_paid,
amount_issued: self.amount_issued,
updated_at: self.updated_at,
}
}
}
#[cfg(feature = "mint")]
impl From<MintQuoteBolt11Response<QuoteId>> for MintQuoteBolt11Response<String> {
fn from(value: MintQuoteBolt11Response<QuoteId>) -> Self {
Self {
quote: value.quote.to_string(),
request: value.request,
state: value.state,
expiry: value.expiry,
pubkey: value.pubkey,
amount: value.amount,
unit: value.unit.clone(),
amount_paid: value.amount_paid,
amount_issued: value.amount_issued,
updated_at: value.updated_at,
}
}
}
/// BOLT11 melt quote request [NUT-23]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct MeltQuoteBolt11Request {
/// Bolt11 invoice to be paid
pub request: Bolt11Invoice,
/// Unit wallet would like to pay with
pub unit: CurrencyUnit,
/// Payment Options
pub options: Option<MeltOptions>,
}
/// Melt Options
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum MeltOptions {
/// Mpp Options
Mpp {
/// MPP
mpp: Mpp,
},
/// Amountless options
Amountless {
/// Amountless
amountless: Amountless,
},
}
impl MeltOptions {
/// Create new [`MeltOptions::Mpp`]
pub fn new_mpp<A>(amount: A) -> Self
where
A: Into<Amount>,
{
Self::Mpp {
mpp: Mpp {
amount: amount.into(),
},
}
}
/// Create new [`MeltOptions::Amountless`]
pub fn new_amountless<A>(amount_msat: A) -> Self
where
A: Into<Amount>,
{
Self::Amountless {
amountless: Amountless {
amount_msat: amount_msat.into(),
},
}
}
/// Payment amount
pub fn amount_msat(&self) -> Amount {
match self {
Self::Mpp { mpp } => mpp.amount,
Self::Amountless { amountless } => amountless.amount_msat,
}
}
}
/// Amountless payment
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Amountless {
/// Amount to pay in msat
pub amount_msat: Amount,
}
impl MeltQuoteBolt11Request {
/// Amount from [`MeltQuoteBolt11Request`]
///
/// Amount can either be defined in the bolt11 invoice,
/// in the request for an amountless bolt11 or in MPP option.
pub fn amount_msat(&self) -> Result<Amount, Error> {
let MeltQuoteBolt11Request {
request, options, ..
} = self;
match options {
None => Ok(request
.amount_milli_satoshis()
.ok_or(Error::InvalidAmountRequest)?
.into()),
Some(MeltOptions::Mpp { mpp }) => Ok(mpp.amount),
Some(MeltOptions::Amountless { amountless }) => {
let amount = amountless.amount_msat;
if let Some(amount_msat) = request.amount_milli_satoshis() {
if amount != amount_msat.into() {
return Err(Error::InvalidAmountRequest);
}
}
Ok(amount)
}
}
}
}
/// Melt quote response [NUT-05]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(bound = "Q: Serialize + DeserializeOwned")]
pub struct MeltQuoteBolt11Response<Q> {
/// Quote Id
pub quote: Q,
/// The amount that needs to be provided
pub amount: Amount,
/// The fee reserve that is required
pub fee_reserve: Amount,
/// Quote State
pub state: MeltQuoteState,
/// Unix timestamp until the quote is valid
pub expiry: u64,
/// Payment preimage
#[serde(skip_serializing_if = "Option::is_none")]
pub payment_preimage: Option<String>,
/// Change
#[serde(skip_serializing_if = "Option::is_none")]
pub change: Option<Vec<BlindSignature>>,
/// Payment request to fulfill
// REVIEW: This is now required in the spec, we should remove the option once all mints update
#[serde(skip_serializing_if = "Option::is_none")]
pub request: Option<String>,
/// Unit
// REVIEW: This is now required in the spec, we should remove the option once all mints update
#[serde(skip_serializing_if = "Option::is_none")]
pub unit: Option<CurrencyUnit>,
}
impl<Q: ToString> MeltQuoteBolt11Response<Q> {
/// Convert a `MeltQuoteBolt11Response` with type Q (generic/unknown) to a
/// `MeltQuoteBolt11Response` with `String`
pub fn to_string_id(self) -> MeltQuoteBolt11Response<String> {
MeltQuoteBolt11Response {
quote: self.quote.to_string(),
amount: self.amount,
fee_reserve: self.fee_reserve,
state: self.state,
expiry: self.expiry,
payment_preimage: self.payment_preimage,
change: self.change,
request: self.request,
unit: self.unit,
}
}
}
#[cfg(feature = "mint")]
impl From<MeltQuoteBolt11Response<QuoteId>> for MeltQuoteBolt11Response<String> {
fn from(value: MeltQuoteBolt11Response<QuoteId>) -> Self {
Self {
quote: value.quote.to_string(),
amount: value.amount,
fee_reserve: value.fee_reserve,
state: value.state,
expiry: value.expiry,
payment_preimage: value.payment_preimage,
change: value.change,
request: value.request,
unit: value.unit,
}
}
}