-
Notifications
You must be signed in to change notification settings - Fork 95
Add wallets --delete to remove saved wallet config
#312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,11 @@ impl WalletConfig { | |
| .ok_or_else(|| Error::Generic(format!("Wallet {wallet_name} not found in config")))? | ||
| .try_into() | ||
| } | ||
|
|
||
| #[must_use] | ||
| pub fn remove_wallet(&mut self, wallet_name: &str) -> Option<WalletConfigInner> { | ||
| self.wallets.remove(wallet_name) | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<&WalletConfigInner> for WalletOpts { | ||
|
|
@@ -346,4 +351,61 @@ mod tests { | |
| let result: Result<WalletOpts, Error> = (&inner).try_into(); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| fn test_wallet(name: &str) -> WalletConfigInner { | ||
| WalletConfigInner { | ||
| wallet: name.to_string(), | ||
| network: "testnet".to_string(), | ||
| ext_descriptor: EXT_DESCRIPTOR.to_string(), | ||
| int_descriptor: Some(INT_DESCRIPTOR.to_string()), | ||
| #[cfg(any(feature = "sqlite", feature = "redb"))] | ||
| database_type: "sqlite".to_string(), | ||
| #[cfg(any( | ||
| feature = "electrum", | ||
| feature = "esplora", | ||
| feature = "rpc", | ||
| feature = "cbf" | ||
| ))] | ||
| client_type: Some("rpc".to_string()), | ||
| #[cfg(any(feature = "electrum", feature = "esplora", feature = "rpc"))] | ||
| server_url: Some("http://localhost:18443".to_string()), | ||
| #[cfg(feature = "electrum")] | ||
| batch_size: None, | ||
| #[cfg(feature = "esplora")] | ||
| parallel_requests: None, | ||
| #[cfg(feature = "rpc")] | ||
| rpc_user: None, | ||
| #[cfg(feature = "rpc")] | ||
| rpc_password: None, | ||
| #[cfg(feature = "rpc")] | ||
| cookie: None, | ||
| #[cfg(any(feature = "electrum", feature = "esplora"))] | ||
| proxy: None, | ||
| #[cfg(any(feature = "electrum", feature = "esplora"))] | ||
| proxy_auth: None, | ||
| #[cfg(any(feature = "electrum", feature = "esplora"))] | ||
| proxy_retries: None, | ||
| #[cfg(any(feature = "electrum", feature = "esplora"))] | ||
| proxy_timeout: None, | ||
| #[cfg(feature = "cbf")] | ||
| conn_count: None, | ||
| } | ||
|
Comment on lines
+356
to
+392
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Since you are adding a helper anyway, consider turning it into a small builder and reusing it there too - those two only differ in a few values. Fine to leave for a follow-up. |
||
| } | ||
| #[test] | ||
| fn test_remove_wallet_config() { | ||
| let mut config = WalletConfig { | ||
| wallets: HashMap::from([ | ||
| ("alice".to_string(), test_wallet("alice")), | ||
| ("bob".to_string(), test_wallet("bob")), | ||
| ]), | ||
| }; | ||
|
|
||
| let removed = config.remove_wallet("alice"); | ||
| assert!(removed.is_some()); | ||
| assert_eq!(removed.unwrap().wallet, "alice"); | ||
| assert!(!config.wallets.contains_key("alice")); | ||
| assert!(config.wallets.contains_key("bob")); | ||
|
|
||
| assert!(config.remove_wallet("charlie").is_none()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -158,16 +158,27 @@ impl AppCommand<AppContext<Init>> for SaveConfigCommand { | |||||
| } | ||||||
|
|
||||||
| #[derive(Args, Debug, Clone, PartialEq)] | ||||||
| pub struct ListWalletsCommand; | ||||||
| pub struct WalletsCommand { | ||||||
| /// Delete the saved configuration for the given wallet instead of listing. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| #[arg(long = "delete", value_name = "WALLET_NAME")] | ||||||
| pub(crate) delete: Option<String>, | ||||||
| } | ||||||
|
|
||||||
| impl AppCommand<AppContext<Init>> for ListWalletsCommand { | ||||||
| impl AppCommand<AppContext<Init>> for WalletsCommand { | ||||||
| type Output = WalletsListResult; | ||||||
|
|
||||||
| fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> { | ||||||
| let config = match WalletConfig::load(&ctx.datadir)? { | ||||||
| Some(cfg) => cfg, | ||||||
| None => return Err(Error::Generic("No wallets configured yet.".into())), | ||||||
| }; | ||||||
| let mut config = WalletConfig::load(&ctx.datadir)? | ||||||
| .ok_or_else(|| Error::Generic("No wallets configured yet.".into()))?; | ||||||
|
|
||||||
| if let Some(wallet_name) = &self.delete { | ||||||
| if config.remove_wallet(wallet_name).is_none() { | ||||||
| return Err(Error::Generic(format!( | ||||||
| "Wallet '{wallet_name}' not found in config" | ||||||
| ))); | ||||||
| } | ||||||
| config.save(&ctx.datadir)?; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking: deleting the last wallet leaves Probably worth folding into the |
||||||
| } | ||||||
|
|
||||||
| Ok(WalletsListResult(config.wallets)) | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.