restore.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use std::str::FromStr;
  2. use std::sync::Arc;
  3. use anyhow::Result;
  4. use cdk::cdk_database::{Error, WalletDatabase};
  5. use cdk::mint_url::MintUrl;
  6. use cdk::nuts::CurrencyUnit;
  7. use cdk::wallet::types::WalletKey;
  8. use cdk::wallet::{MultiMintWallet, Wallet};
  9. use clap::Args;
  10. #[derive(Args)]
  11. pub struct RestoreSubCommand {
  12. /// Mint Url
  13. mint_url: MintUrl,
  14. /// Currency unit e.g. sat
  15. #[arg(default_value = "sat")]
  16. unit: String,
  17. }
  18. pub async fn restore(
  19. multi_mint_wallet: &MultiMintWallet,
  20. seed: &[u8],
  21. localstore: Arc<dyn WalletDatabase<Err = Error> + Sync + Send>,
  22. sub_command_args: &RestoreSubCommand,
  23. ) -> Result<()> {
  24. let unit = CurrencyUnit::from_str(&sub_command_args.unit)?;
  25. let mint_url = sub_command_args.mint_url.clone();
  26. let wallet = match multi_mint_wallet
  27. .get_wallet(&WalletKey::new(mint_url.clone(), unit.clone()))
  28. .await
  29. {
  30. Some(wallet) => wallet.clone(),
  31. None => {
  32. let wallet = Wallet::new(&mint_url.to_string(), unit, localstore, seed, None)?;
  33. multi_mint_wallet.add_wallet(wallet.clone()).await;
  34. wallet
  35. }
  36. };
  37. let amount = wallet.restore().await?;
  38. println!("Restored {}", amount);
  39. Ok(())
  40. }