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
use cdk_common::ensure_cdk;
use tracing::instrument;
use super::MintQuote;
use crate::amount::SplitTarget;
use crate::dhke::construct_proofs;
use crate::nuts::nut00::ProofsMethods;
use crate::nuts::{
nut12, MintBolt11Request, MintQuoteBolt11Request, MintQuoteBolt11Response, PreMintSecrets,
Proofs, SecretKey, SpendingConditions, State,
};
use crate::types::ProofInfo;
use crate::util::unix_time;
use crate::wallet::MintQuoteState;
use crate::{Amount, Error, Wallet};
impl Wallet {
/// Mint Quote
/// # Synopsis
/// ```rust
/// use std::sync::Arc;
///
/// use cdk::amount::Amount;
/// use cdk_sqlite::wallet::memory;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::wallet::Wallet;
/// use rand::Rng;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let seed = rand::thread_rng().gen::<[u8; 32]>();
/// let mint_url = "https://testnut.cashu.space";
/// let unit = CurrencyUnit::Sat;
///
/// let localstore = memory::empty().await?;
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None)?;
/// let amount = Amount::from(100);
///
/// let quote = wallet.mint_quote(amount, None).await?;
/// Ok(())
/// }
/// ```
#[instrument(skip(self))]
pub async fn mint_quote(
&self,
amount: Amount,
description: Option<String>,
) -> Result<MintQuote, Error> {
let mint_url = self.mint_url.clone();
let unit = self.unit.clone();
// If we have a description, we check that the mint supports it.
if description.is_some() {
let settings = self
.localstore
.get_mint(mint_url.clone())
.await?
.ok_or(Error::IncorrectMint)?
.nuts
.nut04
.get_settings(&unit, &crate::nuts::PaymentMethod::Bolt11)
.ok_or(Error::UnsupportedUnit)?;
ensure_cdk!(settings.description, Error::InvoiceDescriptionUnsupported);
}
let secret_key = SecretKey::generate();
let request = MintQuoteBolt11Request {
amount,
unit: unit.clone(),
description,
pubkey: Some(secret_key.public_key()),
};
let quote_res = self.client.post_mint_quote(request).await?;
let quote = MintQuote {
mint_url,
id: quote_res.quote,
amount,
unit,
request: quote_res.request,
state: quote_res.state,
expiry: quote_res.expiry.unwrap_or(0),
secret_key: Some(secret_key),
};
self.localstore.add_mint_quote(quote.clone()).await?;
Ok(quote)
}
/// Check mint quote status
#[instrument(skip(self, quote_id))]
pub async fn mint_quote_state(
&self,
quote_id: &str,
) -> Result<MintQuoteBolt11Response<String>, Error> {
let response = self.client.get_mint_quote_status(quote_id).await?;
match self.localstore.get_mint_quote(quote_id).await? {
Some(quote) => {
let mut quote = quote;
quote.state = response.state;
self.localstore.add_mint_quote(quote).await?;
}
None => {
tracing::info!("Quote mint {} unknown", quote_id);
}
}
Ok(response)
}
/// Check status of pending mint quotes
#[instrument(skip(self))]
pub async fn check_all_mint_quotes(&self) -> Result<Amount, Error> {
let mint_quotes = self.localstore.get_mint_quotes().await?;
let mut total_amount = Amount::ZERO;
for mint_quote in mint_quotes {
let mint_quote_response = self.mint_quote_state(&mint_quote.id).await?;
if mint_quote_response.state == MintQuoteState::Paid {
// TODO: Need to pass in keys here
let proofs = self
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;
total_amount += proofs.total_amount()?;
} else if mint_quote.expiry.le(&unix_time()) {
self.localstore.remove_mint_quote(&mint_quote.id).await?;
}
}
Ok(total_amount)
}
/// Mint
/// # Synopsis
/// ```rust
/// use std::sync::Arc;
///
/// use anyhow::Result;
/// use cdk::amount::{Amount, SplitTarget};
/// use cdk_sqlite::wallet::memory;
/// use cdk::nuts::nut00::ProofsMethods;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::wallet::Wallet;
/// use rand::Rng;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// let seed = rand::thread_rng().gen::<[u8; 32]>();
/// let mint_url = "https://testnut.cashu.space";
/// let unit = CurrencyUnit::Sat;
///
/// let localstore = memory::empty().await?;
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
/// let amount = Amount::from(100);
///
/// let quote = wallet.mint_quote(amount, None).await?;
/// let quote_id = quote.id;
/// // To be called after quote request is paid
/// let minted_proofs = wallet.mint("e_id, SplitTarget::default(), None).await?;
/// let minted_amount = minted_proofs.total_amount()?;
///
/// Ok(())
/// }
/// ```
#[instrument(skip(self))]
pub async fn mint(
&self,
quote_id: &str,
amount_split_target: SplitTarget,
spending_conditions: Option<SpendingConditions>,
) -> Result<Proofs, Error> {
// Check that mint is in store of mints
if self
.localstore
.get_mint(self.mint_url.clone())
.await?
.is_none()
{
self.get_mint_info().await?;
}
let quote_info = self
.localstore
.get_mint_quote(quote_id)
.await?
.ok_or(Error::UnknownQuote)?;
let unix_time = unix_time();
ensure_cdk!(
quote_info.expiry > unix_time || quote_info.expiry == 0,
Error::ExpiredQuote(quote_info.expiry, unix_time)
);
let active_keyset_id = self.get_active_mint_keyset().await?.id;
let count = self
.localstore
.get_keyset_counter(&active_keyset_id)
.await?;
let count = count.map_or(0, |c| c + 1);
let premint_secrets = match &spending_conditions {
Some(spending_conditions) => PreMintSecrets::with_conditions(
active_keyset_id,
quote_info.amount,
&amount_split_target,
spending_conditions,
)?,
None => PreMintSecrets::from_xpriv(
active_keyset_id,
count,
self.xpriv,
quote_info.amount,
&amount_split_target,
)?,
};
let mut request = MintBolt11Request {
quote: quote_id.to_string(),
outputs: premint_secrets.blinded_messages(),
signature: None,
};
if let Some(secret_key) = quote_info.secret_key {
request.sign(secret_key)?;
}
let mint_res = self.client.post_mint(request).await?;
let keys = self.get_keyset_keys(active_keyset_id).await?;
// Verify the signature DLEQ is valid
{
for (sig, premint) in mint_res.signatures.iter().zip(&premint_secrets.secrets) {
let keys = self.get_keyset_keys(sig.keyset_id).await?;
let key = keys.amount_key(sig.amount).ok_or(Error::AmountKey)?;
match sig.verify_dleq(key, premint.blinded_message.blinded_secret) {
Ok(_) | Err(nut12::Error::MissingDleqProof) => (),
Err(_) => return Err(Error::CouldNotVerifyDleq),
}
}
}
let proofs = construct_proofs(
mint_res.signatures,
premint_secrets.rs(),
premint_secrets.secrets(),
&keys,
)?;
// Remove filled quote from store
self.localstore.remove_mint_quote("e_info.id).await?;
if spending_conditions.is_none() {
tracing::debug!(
"Incrementing keyset {} counter by {}",
active_keyset_id,
proofs.len()
);
// Update counter for keyset
self.localstore
.increment_keyset_counter(&active_keyset_id, proofs.len() as u32)
.await?;
}
let proof_infos = proofs
.iter()
.map(|proof| {
ProofInfo::new(
proof.clone(),
self.mint_url.clone(),
State::Unspent,
quote_info.unit.clone(),
)
})
.collect::<Result<Vec<ProofInfo>, _>>()?;
// Add new proofs to store
self.localstore.update_proofs(proof_infos, vec![]).await?;
Ok(proofs)
}
}