From 7a0a17038d016077896d02eb2b7a89073f43a92d Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Mon, 2 Dec 2024 11:30:22 +0000 Subject: [PATCH] fix(cdk-cli/restore): create wallet if not in multimit wallet --- crates/cdk-cli/src/main.rs | 8 +++++++- crates/cdk-cli/src/sub_commands/restore.rs | 24 +++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/crates/cdk-cli/src/main.rs b/crates/cdk-cli/src/main.rs index 7ff70cda0..898448705 100644 --- a/crates/cdk-cli/src/main.rs +++ b/crates/cdk-cli/src/main.rs @@ -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) diff --git a/crates/cdk-cli/src/sub_commands/restore.rs b/crates/cdk-cli/src/sub_commands/restore.rs index 0cef42bfd..3b9908368 100644 --- a/crates/cdk-cli/src/sub_commands/restore.rs +++ b/crates/cdk-cli/src/sub_commands/restore.rs @@ -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)] @@ -18,13 +20,25 @@ pub struct RestoreSubCommand { pub async fn restore( multi_mint_wallet: &MultiMintWallet, + seed: &[u8], + localstore: Arc + 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?;