Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details.
## [Unreleased]

- Added support for Multipath (two-paths) descriptors.

- Added deletion of config file through the cli

## [4.0.0]

Expand Down
3 changes: 3 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//! All subcommands are defined in the below enums.

#![allow(clippy::large_enum_variant)]
use crate::handlers::config::DeleteConfigCommand;
#[cfg(feature = "message_signer")]
use crate::handlers::offline::{SignMessageCommand, VerifyMessageCommand};
use crate::handlers::{
Expand Down Expand Up @@ -213,6 +214,8 @@ pub enum CliSubCommand {
pub enum WalletSubCommand {
/// Save wallet configuration to `config.toml`.
Config(SaveConfigCommand),
/// Delete a saved wallet configuration.
DeleteConfig(DeleteConfigCommand),
#[cfg(any(
feature = "electrum",
feature = "esplora",
Expand Down
22 changes: 22 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::str::FromStr;

Expand Down Expand Up @@ -95,6 +96,27 @@ impl WalletConfig {
Ok(())
}

/// Safely delete the configuration file from the wallet's data directory.
///
/// Returns `Ok(true)` if the file was deleted, and `Ok(false)` if the file
/// did not exist (in which case no action is taken).
pub fn delete(datadir: &Path) -> Result<bool, Error> {
let config_path = datadir.join("config.toml");
if !config_path.exists() {
writeln!(
std::io::stderr(),
"Config file {config_path:?} does not exist, nothing to delete"
)
.map_err(|e| Error::Generic(format!("Failed to write warning: {e}")))?;
return Ok(false);
}
fs::remove_file(&config_path).map_err(|e| {
Error::Generic(format!("Failed to delete config file {config_path:?}: {e}"))
})?;
log::debug!("Deleted config file {config_path:?}");
Ok(true)
}

/// Get config for a wallet
pub fn get_wallet_opts(&self, wallet_name: &str) -> Result<WalletOpts, Error> {
self.wallets
Expand Down
34 changes: 34 additions & 0 deletions src/handlers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,40 @@ impl AppCommand<AppContext<Init>> for SaveConfigCommand {
}
}

#[derive(Args, Debug, Clone, PartialEq)]
pub struct DeleteConfigCommand {
/// The name of the wallet whose configuration should be deleted.
#[arg(env = "WALLET_NAME", short = 'w', long = "wallet", required = true)]
pub(crate) wallet: String,
}

impl AppCommand<AppContext<Init>> for DeleteConfigCommand {
type Output = StatusResult;

fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> {
let wallet_name = &self.wallet;

let mut config = WalletConfig::load(&ctx.datadir)?
.ok_or_else(|| Error::Generic("No wallets configured yet.".to_owned()))?;

if config.wallets.remove(wallet_name).is_none() {
return Err(Error::Generic(format!(
"Wallet '{}' not found in config.",
wallet_name
)));
}

WalletConfig::delete(&ctx.datadir)?;

Ok(StatusResult {
message: format!(
"Wallet '{}' configuration deleted successfully.",
wallet_name
),
})
}
}

#[derive(Args, Debug, Clone, PartialEq)]
pub struct ListWalletsCommand;

Expand Down
9 changes: 9 additions & 0 deletions src/handlers/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ pub(crate) async fn respond(
.map_err(|e| e.to_string())?;
Some(())
}
WalletSubCommand::DeleteConfig(_) => {
writeln!(
std::io::stdout(),
"`delete-config` is not available in REPL mode — the wallet for this session \
is already loaded. Exit and run `bdk-cli wallet --wallet <name> delete-config ...`."
)
.map_err(|e| e.to_string())?;
Some(())
}
},

ReplSubCommand::Descriptor(cmd) => {
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> {

config_cmd.execute(&mut ctx)?.write_out(std::io::stdout())?;
}

WalletSubCommand::DeleteConfig(mut delete_cmd) => {
delete_cmd.wallet = wallet_name;

let mut ctx = AppContext::new(cli_opts.network, home_dir);

delete_cmd.execute(&mut ctx)?.write_out(std::io::stdout())?;
}
},

CliSubCommand::Key { subcommand } => {
Expand Down