parakesh-common 0.1.1

Common parts for ParaKesh Cashu wallet
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use cdk::amount::SplitTarget;
use cdk::mint_url::MintUrl;
use cdk::nuts::nut00::ProofsMethods;
use cdk::nuts::{CurrencyUnit, MintQuoteState};
use cdk::wallet::multi_mint_wallet::MultiMintWallet;
use cdk::wallet::types::WalletKey;
use cdk::wallet::{SendOptions, Wallet, WalletBuilder};
use cdk::Amount;
use cdk_common::database::WalletDatabase;
use cdk_redb::WalletRedbDatabase;
// use cdk_sqlite::wallet::memory;
// use cdk_sqlite::WalletSqliteDatabase;

use seedstore::{ChildSpecifier, SeedStore, SeedStoreCreator};

use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

const KEY_DERIVATION_PATH: &str = "m/84'/0'/0'/0/0";

/// Parakesh application, based on CDK.
pub struct PKApp {
    /// Stores the seed
    seedstore: SeedStore,
    unit: CurrencyUnit,
    /// CDK ecash store
    store: Arc<WalletRedbDatabase>,
    /// CDK multi-mint wallet
    multi_mint_wallet: MultiMintWallet,
    /// Current mint, to use with operations
    selected_mint: Option<MintUrl>,
}

/// Summary info about the mints
#[derive(Clone, Debug, Default)]
pub enum MintsSummary {
    #[default]
    None,
    Single(String),
    Multiple(usize),
}

/// Ecash wallet struct
#[derive(Clone, Debug, Default)]
pub struct WalletInfo {
    pub is_inititalized: bool,
    pub mint_count: usize,
    pub mints_summary: MintsSummary,
    pub selected_mint_url: String,
}

#[derive(Clone, Debug, Default)]
pub struct BalanceInfo(pub u64);

#[derive(Clone, Debug)]
pub struct MintInfo {
    pub url: String,
    pub balance: u64,
}

/// Used with CDK error
#[derive(Debug, PartialEq, Eq)]
pub struct StringError(String);

impl std::error::Error for StringError {}

impl fmt::Display for StringError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Intermediary result used in `mint_from_ln_start` and `mint_from_ln_wait`.
#[derive(Clone, Debug)]
pub struct MintFromLnIntermediaryResult {
    mint_quote: cdk::wallet::MintQuote,
    /// Set if complete (paid)
    pub paid_result: Option<Result<u64, String>>,
}

impl PKApp {
    /// Create new app instance
    pub async fn new() -> Result<PKApp, String> {
        // TODO should be in config dir
        let secret_seed_file_name = "./parakesh.secret";
        // TODO should be user input
        let seed_encryption_password = "Parakesh+Password1337";
        let seedstore = match SeedStore::new_from_encrypted_file(
            secret_seed_file_name,
            &seed_encryption_password.to_owned(),
            None,
        ) {
            Ok(seedstore) => seedstore,
            Err(_e) => {
                // Could not read seed, generate a new one
                // TODO do this with init, read PW, etc.
                // TODO Should be random!
                let secret_key = [43u8; 16].to_vec();
                match SeedStoreCreator::new_from_data(&secret_key, None, None) {
                    Ok(seedstore) => {
                        let _res = SeedStoreCreator::write_to_file(
                            &seedstore,
                            secret_seed_file_name,
                            seed_encryption_password,
                            None, // allow weak pw
                        )?;
                        println!("Seed written to secret file {}", secret_seed_file_name);
                        // Try to open again
                        match SeedStore::new_from_encrypted_file(
                            secret_seed_file_name,
                            &seed_encryption_password.to_owned(),
                            None,
                        ) {
                            Ok(seedstore) => seedstore,
                            Err(e2) => {
                                return Err(format!(
                                    "Could not read seed from freshly-generated secret file ({})",
                                    e2
                                ));
                            }
                        }
                    }
                    Err(e3) => {
                        return Err(format!("Could not read seed from secret file, and could not save newly-generated seed ({})", e3));
                    }
                }
            }
        };

        let unit = CurrencyUnit::Sat;

        // Initialize the memory store
        // let store = memory::empty().await?;
        // TODO should be in config dir
        let path = std::path::Path::new("./parakesh_data.dedb");
        let store = Arc::new(WalletRedbDatabase::new(&path).unwrap());

        // read the wallets, create Wallet instances
        let mut wallets: Vec<Wallet> = Vec::new();
        let db_mints = store.get_mints().await.map_err(|e| e.to_string())?;
        let seed_privkey = seedstore.get_secret_child_private_key(&ChildSpecifier::Derivation(
            KEY_DERIVATION_PATH.into(),
        ))?;
        for (mint_url, _) in db_mints {
            let builder = WalletBuilder::new()
                .mint_url(mint_url.clone())
                .unit(unit.clone())
                .localstore(store.clone())
                .seed(seed_privkey.as_ref());
            let wallet = builder.build().map_err(|e| e.to_string())?;
            wallets.push(wallet);
        }

        let wallets_len = wallets.len();
        let multi_mint_wallet = MultiMintWallet::new(wallets);

        let mut app = PKApp {
            seedstore,
            unit,
            store,
            multi_mint_wallet,
            selected_mint: None, // set below
        };

        // Select first wallet
        if wallets_len > 0 {
            let _res = app
                .select_mint_by_index(1)
                .await
                .map_err(|e| e.to_string())?;
        }

        Ok(app)
    }

    pub async fn get_wallet_info(&self) -> Result<WalletInfo, String> {
        let wallets = self.multi_mint_wallet.get_wallets().await;
        let mint_count = wallets.len();
        let selected_mint_url = if let Some(mint) = &self.selected_mint {
            mint.to_string()
        } else {
            "".to_string()
        };
        let mints_summary = match wallets.len() {
            0 => MintsSummary::None,
            1 => MintsSummary::Single(wallets[0].mint_url.to_string()),
            _ => MintsSummary::Multiple(wallets.len()),
        };
        Ok(WalletInfo {
            is_inititalized: true,
            mint_count,
            mints_summary,
            selected_mint_url,
        })
    }

    pub async fn get_balance(&self) -> Result<BalanceInfo, String> {
        let wallet_balances: BTreeMap<MintUrl, Amount> = self
            .multi_mint_wallet
            .get_balances(&CurrencyUnit::Sat)
            .await
            .map_err(|e| e.to_string())?;
        let total_balance: u64 = wallet_balances
            .iter()
            .map(|(_url, a)| {
                let u: u64 = (*a).into();
                u
            })
            .sum();
        Ok(BalanceInfo(total_balance))
    }

    async fn get_mint_wallet(
        &self,
        mint_url: MintUrl,
    ) -> Result<Wallet, Box<dyn std::error::Error>> {
        let wallet_key = WalletKey::new(mint_url.clone(), self.unit.clone());
        match self.multi_mint_wallet.get_wallet(&wallet_key).await {
            Some(wallet) => Ok(wallet.clone()),
            None => {
                return Err(Box::new(StringError(format!(
                    "Mint not found, {}",
                    mint_url.to_string()
                ))))
            }
        }
    }

    // async fn get_mint_wallet_str(
    //     &mut self,
    //     mint_url_str: &str,
    // ) -> Result<Wallet, Box<dyn std::error::Error>> {
    //     let mint_url = MintUrl::from_str(mint_url_str)?;
    //     self.get_mint_wallet(mint_url).await
    // }

    fn get_seed(&self) -> Result<[u8; 32], String> {
        let seed_privkey = self
            .seedstore
            .get_secret_child_private_key(&ChildSpecifier::Derivation(KEY_DERIVATION_PATH.into()))
            .map_err(|err| err.to_string())?;
        Ok(seed_privkey.as_ref().clone())
    }

    pub async fn add_mint(&mut self, mint_url_str: &str) -> Result<(), String> {
        let wallet = Wallet::new(
            &mint_url_str.to_string(),
            self.unit.clone(),
            self.store.clone(),
            &self.get_seed()?,
            None,
        )
        .map_err(|err| err.to_string())?;
        // This is needed to store the mint in the store
        let mint_info = wallet
            .get_mint_info()
            .await
            .map_err(|err| err.to_string())?;
        if let Some(_info) = mint_info {
        } else {
            return Err(format!("Could not obtain mint info for {}", mint_url_str));
        }
        self.multi_mint_wallet.add_wallet(wallet).await;
        let _res = self.select_mint(mint_url_str).await?;
        Ok(())
    }

    pub fn selected_mint(&self) -> String {
        match &self.selected_mint {
            Some(mint) => mint.to_string(),
            None => "(none)".to_string(),
        }
    }

    pub async fn select_mint(&mut self, mint_url_str: &str) -> Result<String, String> {
        let mint_url = MintUrl::from_str(mint_url_str).map_err(|e| e.to_string())?;
        let _wallet = self
            .get_mint_wallet(mint_url.clone())
            .await
            .map_err(|e| e.to_string())?;
        self.selected_mint = Some(mint_url);
        Ok(mint_url_str.to_owned())
    }

    pub async fn select_mint_by_index(
        &mut self,
        mint_index_1_based: usize,
    ) -> Result<usize, String> {
        let wallets = &self.multi_mint_wallet.get_wallets().await;
        if mint_index_1_based == 0 {
            return Err(format!(
                "Invalid mint index {}, the first is 1",
                mint_index_1_based
            ));
        }
        let max_index = wallets.len();
        if mint_index_1_based > max_index {
            return Err(format!(
                "Invalid mint index {}, maximum is {}",
                mint_index_1_based, max_index
            ));
        }
        let mint_url = &wallets[mint_index_1_based - 1].mint_url;
        self.selected_mint = Some(mint_url.clone());
        Ok(mint_index_1_based)
    }

    pub async fn get_mints_info(&self) -> Result<Vec<MintInfo>, String> {
        let wallets = &self.multi_mint_wallet.get_wallets().await;
        let mut info = Vec::new();
        for wallet in wallets.iter() {
            let balance: u64 = wallet.total_balance().await.unwrap_or_default().into();
            info.push(MintInfo {
                url: wallet.mint_url.to_string(),
                balance,
            });
        }
        Ok(info)
    }

    pub async fn receive_ecash(&mut self, token: &str) -> Result<u64, String> {
        if let Some(sel_mint) = &self.selected_mint {
            let wallet = self
                .get_mint_wallet(sel_mint.clone())
                .await
                .map_err(|e| e.to_string())?;

            // Receive the token
            let received = wallet
                .receive(token, SplitTarget::default(), &[], &[])
                .await
                .map_err(|e| e.to_string())?;
            Ok(received.into())
        } else {
            Err("No selected mint!".to_owned())
        }
    }

    pub async fn send_ecash(&mut self, amount_sats: u64) -> Result<(u64, String), String> {
        if let Some(sel_mint) = &self.selected_mint {
            let wallet = self
                .get_mint_wallet(sel_mint.clone())
                .await
                .map_err(|e| e.to_string())?;
            // Send the token
            let prepared_send = wallet
                .prepare_send(Amount::from(amount_sats), SendOptions::default())
                .await
                .map_err(|e| e.to_string())?;
            let token = wallet
                .send(prepared_send, None)
                .await
                .map_err(|e| e.to_string())?;

            Ok((amount_sats, token.to_v3_string()))
        } else {
            Err("No selected mint!".to_string())
        }
    }

    /// Run `mint_from_ln_start` and `mint_from_ln_wait` in sequence.
    /// Return the invoice in a callback.
    /// - `callback`: This callback is called with the invoice to be paid.
    pub async fn mint_from_ln<F: FnOnce(&str)>(
        &mut self,
        amount_sats: u64,
        callback: F,
    ) -> Result<u64, String> {
        let (invoice, intermediary_res) = self.mint_from_ln_start(amount_sats).await?;
        (callback)(invoice.as_str());
        self.mint_from_ln_wait(intermediary_res).await
    }

    /// Receive Lightning: perform Mint from Lightning invoice.
    /// First part, generate the invoice to be paid, and return it, alongside an
    /// intermediary result with which `mint_from_ln_wait` should be invoked.
    pub async fn mint_from_ln_start(
        &mut self,
        amount_sats: u64,
    ) -> Result<(String, MintFromLnIntermediaryResult), String> {
        if let Some(sel_mint) = &self.selected_mint {
            let wallet = self
                .get_mint_wallet(sel_mint.clone())
                .await
                .map_err(|e| e.to_string())?;

            // Request a mint quote from the wallet
            let mint_quote = wallet
                .mint_quote(Amount::from(amount_sats), None)
                .await
                .map_err(|e| e.to_string())?;

            // println!("Pay request: {}", quote.request);
            let invoice_to_be_paid = mint_quote.request.clone();
            Ok((
                invoice_to_be_paid,
                MintFromLnIntermediaryResult {
                    mint_quote,
                    paid_result: None,
                },
            ))
        } else {
            Err("No selected mint!".to_string())
        }
    }

    /// Check for the status once
    /// Returns an intermediary result, or the amount if paid
    pub async fn mint_from_ln_check(
        &mut self,
        intermediary_result: MintFromLnIntermediaryResult,
    ) -> Result<MintFromLnIntermediaryResult, String> {
        println!("Polling for mint result...");
        if intermediary_result.paid_result.is_some() {
            return Ok(intermediary_result);
        }
        if let Some(sel_mint) = &self.selected_mint {
            let wallet = self
                .get_mint_wallet(sel_mint.clone())
                .await
                .map_err(|e| e.to_string())?;

            let quote_id = &intermediary_result.mint_quote.id;
            let status = wallet
                .mint_quote_state(quote_id)
                .await
                .map_err(|e| e.to_string())?;
            if status.state == MintQuoteState::Paid || status.state == MintQuoteState::Issued {
                // Mint the received amount
                let proofs = wallet
                    .mint(quote_id, SplitTarget::default(), None)
                    .await
                    .map_err(|e| e.to_string())?;
                let receive_amount = proofs.total_amount().map_err(|e| e.to_string())?;
                let mut res2 = intermediary_result;
                res2.paid_result = Some(Ok(receive_amount.into()));
                return Ok(res2);
            }
            // not paid yet
            println!("Quote state: {}", status.state);

            let res2 = intermediary_result;
            Ok(res2)
        } else {
            Err("No selected mint!".to_string())
        }
    }

    /// Second part of `mint_from_ln_start`, should be invoked with the intermediary result.
    /// Polls for result, waits until a result is available (invoice had been paid), or timeout.
    /// Returns the amount received.
    /// Warning: Returns in a long time (waits until user action)
    pub async fn mint_from_ln_wait(
        &mut self,
        intermediary_result: MintFromLnIntermediaryResult,
    ) -> Result<u64, String> {
        // Check the quote state in a loop with a timeout
        let mut int_res = intermediary_result;
        loop {
            let res2 = self.mint_from_ln_check(int_res).await?;
            if let Some(res) = res2.paid_result {
                return res;
            }
            // not paid, wait some more
            int_res = res2;
            // sleep(to_wait).await;
            tokio::time::sleep(Duration::from_secs(2)).await;
        }
    }

    pub async fn melt_to_ln(&mut self, ln_invoice: &str) -> Result<u64, String> {
        if let Some(sel_mint) = &self.selected_mint {
            let wallet = self
                .get_mint_wallet(sel_mint.clone())
                .await
                .map_err(|e| e.to_string())?;

            println!("About to melt_quote...");
            // Request a melt quote from the wallet
            let quote = wallet
                .melt_quote(ln_invoice.to_string(), None)
                .await
                .map_err(|e| e.to_string())?;
            println!("Melt quote: {} {} {:?}", quote.amount, quote.state, quote,);

            /*
            // Check the quote state in a loop with a timeout
            let timeout = Duration::from_secs(60); // Set a timeout duration
            let start = std::time::Instant::now();

            loop {
                let status = wallet.melt_quote_status(&quote.id).await?;
                println!("status {:?}", status);

                if status.state == MeltQuoteState::Paid {
                    break;
                }
                if start.elapsed() >= timeout {
                    return Err("Timeout while waiting for mint quote to be paid".into());
                }

                println!("Quote state: {}", status.state);
                sleep(Duration::from_millis(1500)).await;
            }
            */

            // Melt the sent amount
            let melted = wallet.melt(&quote.id).await.map_err(|e| e.to_string())?;
            Ok(melted.amount.into())
        } else {
            Err("No selected mint!".to_owned())
        }
    }
}

impl MintFromLnIntermediaryResult {
    pub fn id(&self) -> String {
        self.mint_quote.id.clone()
    }
}