Skip to content

Commit

Permalink
fix(cdk-cli/restore): create wallet if not in multimit wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Dec 2, 2024
1 parent 39e8fc2 commit 7a0a170
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
8 changes: 7 additions & 1 deletion crates/cdk-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,13 @@ async fn main() -> Result<()> {
sub_commands::burn::burn(&multi_mint_wallet, sub_command_args).await
}
Commands::Restore(sub_command_args) => {
sub_commands::restore::restore(&multi_mint_wallet, sub_command_args).await
sub_commands::restore::restore(
&multi_mint_wallet,
&mnemonic.to_seed_normalized(""),
localstore,
sub_command_args,
)
.await
}
Commands::UpdateMintUrl(sub_command_args) => {
sub_commands::update_mint_url::update_mint_url(&multi_mint_wallet, sub_command_args)
Expand Down
24 changes: 19 additions & 5 deletions crates/cdk-cli/src/sub_commands/restore.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::str::FromStr;
use std::sync::Arc;

use anyhow::{anyhow, Result};
use anyhow::Result;
use cdk::cdk_database::{Error, WalletDatabase};
use cdk::mint_url::MintUrl;
use cdk::nuts::CurrencyUnit;
use cdk::wallet::multi_mint_wallet::WalletKey;
use cdk::wallet::MultiMintWallet;
use cdk::wallet::{MultiMintWallet, Wallet};
use clap::Args;

#[derive(Args)]
Expand All @@ -18,13 +20,25 @@ pub struct RestoreSubCommand {

pub async fn restore(
multi_mint_wallet: &MultiMintWallet,
seed: &[u8],
localstore: Arc<dyn WalletDatabase<Err = Error> + Sync + Send>,
sub_command_args: &RestoreSubCommand,
) -> Result<()> {
let unit = CurrencyUnit::from_str(&sub_command_args.unit)?;
let wallet = multi_mint_wallet
.get_wallet(&WalletKey::new(sub_command_args.mint_url.clone(), unit))
let mint_url = sub_command_args.mint_url.clone();

let wallet = match multi_mint_wallet
.get_wallet(&WalletKey::new(mint_url.clone(), unit.clone()))
.await
.ok_or(anyhow!("Unknown mint url"))?;
{
Some(wallet) => wallet.clone(),
None => {
let wallet = Wallet::new(&mint_url.to_string(), unit, localstore, seed, None)?;

multi_mint_wallet.add_wallet(wallet.clone()).await;
wallet
}
};

let amount = wallet.restore().await?;

Expand Down

0 comments on commit 7a0a170

Please sign in to comment.