cdk 0.16.0

Core Cashu Development Kit library implementing the Cashu protocol
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! WalletTrait implementation for the CDK Wallet.

use std::collections::HashMap;
use std::str::FromStr;

use async_trait::async_trait;
use cdk_common::amount::{FeeAndAmounts, KeysetFeeAndAmounts, SplitTarget};
use cdk_common::mint_url::MintUrl;
use cdk_common::nuts::nut07::ProofState;
use cdk_common::nuts::nut18::PaymentRequest;
use cdk_common::nuts::{
    AuthProof, CurrencyUnit, Id, KeySetInfo, Keys, MeltOptions, MintInfo, PaymentMethod, Proofs,
    SpendingConditions,
};
use cdk_common::subscription::WalletParams;
use cdk_common::wallet::{
    KeysetFilter, MeltQuote, MintQuote, ReceiveOptions, Restored, SendOptions, Transaction,
    TransactionDirection, TransactionId, Wallet as WalletTrait,
};
use cdk_common::{Amount, PublicKey, SecretKey};
use tracing::instrument;
use uuid::Uuid;

use crate::wallet::subscription::ActiveSubscription;
use crate::Error;

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl WalletTrait for super::Wallet {
    type Error = Error;
    type Amount = Amount;
    type MintUrl = MintUrl;
    type CurrencyUnit = CurrencyUnit;
    type MintInfo = MintInfo;
    type KeySetInfo = KeySetInfo;
    type MintQuote = MintQuote;
    type MeltQuote = MeltQuote;
    type PaymentMethod = PaymentMethod;
    type MeltOptions = MeltOptions;
    type OperationId = Uuid;
    type PreparedSend<'a> = super::send::PreparedSend<'a>;
    type PreparedMelt<'a> = super::melt::PreparedMelt<'a>;
    type Subscription = ActiveSubscription;
    type SubscribeParams = WalletParams;

    fn mint_url(&self) -> MintUrl {
        self.mint_url.clone()
    }

    fn unit(&self) -> CurrencyUnit {
        self.unit.clone()
    }

    #[instrument(skip(self))]
    async fn total_balance(&self) -> Result<Amount, Self::Error> {
        self.total_balance().await
    }

    #[instrument(skip(self))]
    async fn total_pending_balance(&self) -> Result<Amount, Self::Error> {
        self.total_pending_balance().await
    }

    #[instrument(skip(self))]
    async fn total_reserved_balance(&self) -> Result<Amount, Self::Error> {
        self.total_reserved_balance().await
    }

    #[instrument(skip(self))]
    async fn fetch_mint_info(&self) -> Result<Option<MintInfo>, Self::Error> {
        self.fetch_mint_info().await
    }

    #[instrument(skip(self))]
    async fn load_mint_info(&self) -> Result<MintInfo, Self::Error> {
        self.load_mint_info().await
    }

    #[instrument(skip(self))]
    async fn refresh_keysets(&self) -> Result<Vec<KeySetInfo>, Self::Error> {
        self.refresh_keysets().await
    }

    #[instrument(skip(self))]
    async fn get_active_keyset(&self) -> Result<KeySetInfo, Self::Error> {
        self.get_active_keyset().await
    }

    #[instrument(skip(self))]
    async fn load_keyset_keys(&self, keyset_id: Id) -> Result<Keys, Self::Error> {
        self.load_keyset_keys(keyset_id).await
    }

    #[instrument(skip(self))]
    async fn get_mint_keysets(&self, filter: KeysetFilter) -> Result<Vec<KeySetInfo>, Self::Error> {
        self.get_mint_keysets(filter).await
    }

    #[instrument(skip(self))]
    async fn fetch_active_keyset(&self) -> Result<KeySetInfo, Self::Error> {
        self.fetch_active_keyset().await
    }

    #[instrument(skip(self))]
    async fn get_keyset_fees_and_amounts(&self) -> Result<KeysetFeeAndAmounts, Self::Error> {
        self.get_keyset_fees_and_amounts().await
    }

    #[instrument(skip(self))]
    async fn get_keyset_count_fee(
        &self,
        keyset_id: &Id,
        count: u64,
    ) -> Result<Amount, Self::Error> {
        self.get_keyset_count_fee(keyset_id, count).await
    }

    #[instrument(skip(self))]
    async fn get_keyset_fees_and_amounts_by_id(
        &self,
        keyset_id: Id,
    ) -> Result<FeeAndAmounts, Self::Error> {
        self.get_keyset_fees_and_amounts_by_id(keyset_id).await
    }

    #[instrument(skip(self, method))]
    async fn mint_quote(
        &self,
        method: PaymentMethod,
        amount: Option<Amount>,
        description: Option<String>,
        extra: Option<String>,
    ) -> Result<MintQuote, Self::Error> {
        self.mint_quote(method, amount, description, extra).await
    }

    #[instrument(skip(self, request, options, extra))]
    async fn melt_quote(
        &self,
        method: PaymentMethod,
        request: String,
        options: Option<MeltOptions>,
        extra: Option<String>,
    ) -> Result<MeltQuote, Self::Error> {
        self.melt_quote(method, request, options, extra).await
    }

    #[instrument(skip(self))]
    async fn list_transactions(
        &self,
        direction: Option<TransactionDirection>,
    ) -> Result<Vec<Transaction>, Self::Error> {
        self.list_transactions(direction).await
    }

    #[instrument(skip(self))]
    async fn get_transaction(&self, id: TransactionId) -> Result<Option<Transaction>, Self::Error> {
        self.get_transaction(id).await
    }

    #[instrument(skip(self))]
    async fn get_proofs_for_transaction(&self, id: TransactionId) -> Result<Proofs, Self::Error> {
        self.get_proofs_for_transaction(id).await
    }

    #[instrument(skip(self))]
    async fn revert_transaction(&self, id: TransactionId) -> Result<(), Self::Error> {
        self.revert_transaction(id).await
    }

    #[instrument(skip(self))]
    async fn check_all_pending_proofs(&self) -> Result<Amount, Self::Error> {
        self.check_all_pending_proofs().await
    }

    #[instrument(skip(self, proofs))]
    async fn check_proofs_spent(&self, proofs: Proofs) -> Result<Vec<ProofState>, Self::Error> {
        self.check_proofs_spent(proofs).await
    }

    #[instrument(skip(self))]
    async fn get_keyset_fees_by_id(&self, keyset_id: Id) -> Result<u64, Self::Error> {
        self.get_keyset_fees_by_id(keyset_id).await
    }

    #[instrument(skip(self))]
    async fn calculate_fee(&self, proof_count: u64, keyset_id: Id) -> Result<Amount, Self::Error> {
        self.calculate_fee(proof_count, keyset_id).await
    }

    #[instrument(skip(self, encoded_token, options))]
    async fn receive(
        &self,
        encoded_token: &str,
        options: ReceiveOptions,
    ) -> Result<Amount, Self::Error> {
        self.receive(encoded_token, options).await
    }

    #[instrument(skip(self, proofs, options))]
    async fn receive_proofs(
        &self,
        proofs: Proofs,
        options: ReceiveOptions,
        memo: Option<String>,
        token: Option<String>,
    ) -> Result<Amount, Self::Error> {
        self.receive_proofs(proofs, options, memo, token).await
    }

    #[instrument(skip(self, options))]
    async fn prepare_send(
        &self,
        amount: Amount,
        options: SendOptions,
    ) -> Result<super::send::PreparedSend<'_>, Self::Error> {
        self.prepare_send(amount, options).await
    }

    #[instrument(skip(self))]
    async fn get_pending_sends(&self) -> Result<Vec<Uuid>, Self::Error> {
        self.get_pending_sends().await
    }

    #[instrument(skip(self))]
    async fn revoke_send(&self, operation_id: Uuid) -> Result<Amount, Self::Error> {
        self.revoke_send(operation_id).await
    }

    #[instrument(skip(self))]
    async fn check_send_status(&self, operation_id: Uuid) -> Result<bool, Self::Error> {
        self.check_send_status(operation_id).await
    }

    #[instrument(skip(self, spending_conditions))]
    async fn mint(
        &self,
        quote_id: &str,
        split_target: SplitTarget,
        spending_conditions: Option<SpendingConditions>,
    ) -> Result<Proofs, Self::Error> {
        self.mint(quote_id, split_target, spending_conditions).await
    }

    #[instrument(skip(self))]
    async fn check_mint_quote_status(&self, quote_id: &str) -> Result<MintQuote, Self::Error> {
        self.check_mint_quote_status(quote_id).await
    }

    #[instrument(skip(self))]
    async fn fetch_mint_quote(
        &self,
        quote_id: &str,
        payment_method: Option<PaymentMethod>,
    ) -> Result<MintQuote, Self::Error> {
        self.fetch_mint_quote(quote_id, payment_method).await
    }

    #[instrument(skip(self, metadata))]
    async fn prepare_melt(
        &self,
        quote_id: &str,
        metadata: HashMap<String, String>,
    ) -> Result<super::melt::PreparedMelt<'_>, Self::Error> {
        self.prepare_melt(quote_id, metadata).await
    }

    #[instrument(skip(self, proofs, metadata))]
    async fn prepare_melt_proofs(
        &self,
        quote_id: &str,
        proofs: Proofs,
        metadata: HashMap<String, String>,
    ) -> Result<super::melt::PreparedMelt<'_>, Self::Error> {
        self.prepare_melt_proofs(quote_id, proofs, metadata).await
    }

    #[instrument(skip(self, input_proofs, spending_conditions))]
    async fn swap(
        &self,
        amount: Option<Amount>,
        split_target: SplitTarget,
        input_proofs: Proofs,
        spending_conditions: Option<SpendingConditions>,
        include_fees: bool,
        use_p2bk: bool,
    ) -> Result<Option<Proofs>, Self::Error> {
        self.swap(
            amount,
            split_target,
            input_proofs,
            spending_conditions,
            include_fees,
            use_p2bk,
        )
        .await
    }

    #[instrument(skip(self, cat))]
    async fn set_cat(&self, cat: String) -> Result<(), Self::Error> {
        self.set_cat(cat).await
    }

    #[instrument(skip(self, refresh_token))]
    async fn set_refresh_token(&self, refresh_token: String) -> Result<(), Self::Error> {
        self.set_refresh_token(refresh_token).await
    }

    #[instrument(skip(self))]
    async fn refresh_access_token(&self) -> Result<(), Self::Error> {
        self.refresh_access_token().await
    }

    #[instrument(skip(self))]
    async fn mint_blind_auth(&self, amount: Amount) -> Result<Proofs, Self::Error> {
        self.mint_blind_auth(amount).await
    }

    #[instrument(skip(self))]
    async fn get_unspent_auth_proofs(&self) -> Result<Vec<AuthProof>, Self::Error> {
        self.get_unspent_auth_proofs().await
    }

    #[instrument(skip(self))]
    async fn restore(&self) -> Result<Restored, Self::Error> {
        self.restore().await
    }

    #[instrument(skip(self, token_str))]
    async fn verify_token_dleq(&self, token_str: &str) -> Result<(), Self::Error> {
        let token = cdk_common::nuts::nut00::token::Token::from_str(token_str)?;
        self.verify_token_dleq(&token).await
    }

    #[instrument(skip(self, request))]
    async fn pay_request(
        &self,
        request: PaymentRequest,
        custom_amount: Option<Amount>,
    ) -> Result<(), Self::Error> {
        self.pay_request(request, custom_amount).await
    }

    #[instrument(skip(self, method))]
    async fn subscribe_mint_quote_state(
        &self,
        quote_ids: Vec<String>,
        method: PaymentMethod,
    ) -> Result<ActiveSubscription, Self::Error> {
        self.subscribe_mint_quote_state(quote_ids, method).await
    }

    fn set_metadata_cache_ttl(&self, ttl_secs: Option<u64>) {
        let ttl = ttl_secs.map(std::time::Duration::from_secs);
        self.set_metadata_cache_ttl(ttl);
    }

    #[instrument(skip(self, params))]
    async fn subscribe(&self, params: WalletParams) -> Result<ActiveSubscription, Self::Error> {
        self.subscribe(params).await
    }

    #[cfg(all(feature = "bip353", not(target_arch = "wasm32")))]
    #[instrument(skip(self, amount_msat), fields(address = %bip353_address))]
    async fn melt_bip353_quote(
        &self,
        bip353_address: &str,
        amount_msat: Amount,
        network: bitcoin::Network,
    ) -> Result<MeltQuote, Self::Error> {
        self.melt_bip353_quote(bip353_address, amount_msat, network)
            .await
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[instrument(skip(self, amount_msat), fields(address = %lightning_address))]
    async fn melt_lightning_address_quote(
        &self,
        lightning_address: &str,
        amount_msat: Amount,
    ) -> Result<MeltQuote, Self::Error> {
        self.melt_lightning_address_quote(lightning_address, amount_msat)
            .await
    }

    #[cfg(all(feature = "bip353", not(target_arch = "wasm32")))]
    #[instrument(skip(self, amount_msat), fields(address = %address))]
    async fn melt_human_readable_quote(
        &self,
        address: &str,
        amount_msat: Amount,
        network: bitcoin::Network,
    ) -> Result<MeltQuote, Self::Error> {
        self.melt_human_readable_quote(address, amount_msat, network)
            .await
    }

    #[instrument(skip(self))]
    async fn get_proofs_by_states(
        &self,
        states: Vec<cdk_common::nuts::State>,
    ) -> Result<Proofs, Self::Error> {
        self.get_proofs_by_states(states).await
    }
    /// generates and stores public key in database
    async fn generate_public_key(&self) -> Result<PublicKey, Self::Error> {
        return self.generate_public_key().await;
    }

    /// gets public key by it's hex value
    async fn get_public_key(
        &self,
        pubkey: &PublicKey,
    ) -> Result<Option<cdk_common::wallet::P2PKSigningKey>, Self::Error> {
        let pubkey = self.get_public_key(pubkey).await?;
        Ok(pubkey)
    }

    /// gets list of stored public keys in database
    async fn get_public_keys(
        &self,
    ) -> Result<Vec<cdk_common::wallet::P2PKSigningKey>, Self::Error> {
        let pubkeys = self.get_public_keys().await?;
        Ok(pubkeys)
    }

    /// Gets the latest generated P2PK signing key (most recently created)
    async fn get_latest_public_key(
        &self,
    ) -> Result<Option<cdk_common::wallet::P2PKSigningKey>, Self::Error> {
        let pubkey = self.get_latest_public_key().await?;
        Ok(pubkey)
    }

    /// try to get secret key from p2pk signing key in localstore
    async fn get_signing_key(&self, pubkey: &PublicKey) -> Result<Option<SecretKey>, Self::Error> {
        let signing_key = self.get_signing_key(pubkey).await?;
        Ok(signing_key)
    }
}