Skip to content

Commit 98e8c4b

Browse files
committed
add listwallets command
1 parent 39f1546 commit 98e8c4b

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

client/src/bin/space-cli.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use spaces_client::{
2525
format::{
2626
print_error_rpc_response, print_list_bidouts, print_list_spaces_response,
2727
print_list_transactions, print_list_unspent, print_server_info,
28-
print_wallet_balance_response, print_wallet_info, print_wallet_response, Format,
28+
print_list_wallets, print_wallet_balance_response, print_wallet_info, print_wallet_response,
29+
Format,
2930
},
3031
rpc::{
3132
BidParams, ExecuteParams, OpenParams, RegisterParams, RpcClient, RpcWalletRequest,
@@ -71,6 +72,9 @@ pub struct Args {
7172

7273
#[derive(Subcommand, Debug, Clone)]
7374
enum Commands {
75+
/// List existing wallets
76+
#[command(name = "listwallets")]
77+
ListWallets,
7478
/// Generate a new wallet
7579
#[command(name = "createwallet")]
7680
CreateWallet,
@@ -573,6 +577,10 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
573577
let response = cli.client.get_spaceout(outpoint).await?;
574578
println!("{}", serde_json::to_string_pretty(&response)?);
575579
}
580+
Commands::ListWallets => {
581+
let result = cli.client.list_wallets().await?;
582+
print_list_wallets(result, cli.format);
583+
}
576584
Commands::CreateWallet => {
577585
cli.client.wallet_create(&cli.wallet).await?;
578586
}

client/src/format.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ pub fn print_list_unspent(utxos: Vec<WalletOutput>, format: Format) {
149149
}
150150
}
151151

152+
pub fn print_list_wallets(wallets: Vec<String>, format: Format) {
153+
match format {
154+
Format::Text => {
155+
println!("{}", wallets.join("\n"));
156+
}
157+
Format::Json => {
158+
println!("{}", serde_json::to_string_pretty(&wallets).unwrap());
159+
}
160+
}
161+
}
162+
152163
pub fn print_server_info(info: ServerInfo, format: Format) {
153164
match format {
154165
Format::Text => {

client/src/rpc.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ pub trait Rpc {
207207
#[method(name = "gettxmeta")]
208208
async fn get_tx_meta(&self, txid: Txid) -> Result<Option<TxEntry>, ErrorObjectOwned>;
209209

210+
#[method(name = "listwallets")]
211+
async fn list_wallets(&self) -> Result<Vec<String>, ErrorObjectOwned>;
212+
210213
#[method(name = "walletload")]
211214
async fn wallet_load(&self, name: &str) -> Result<(), ErrorObjectOwned>;
212215

@@ -583,6 +586,21 @@ impl WalletManager {
583586
(network, genesis_hash)
584587
}
585588

589+
pub async fn list_wallets(&self) -> anyhow::Result<Vec<String>> {
590+
let wallets = std::fs::read_dir(&self.data_dir)?
591+
.filter_map(Result::ok)
592+
.filter(|entry| entry.path().is_dir())
593+
.filter_map(|entry| {
594+
entry.path()
595+
.file_name()
596+
.and_then(|name| name.to_str())
597+
.map(String::from)
598+
})
599+
.collect();
600+
601+
Ok(wallets)
602+
}
603+
586604
pub async fn load_wallet(&self, name: &str) -> anyhow::Result<()> {
587605
if self.wallets.read().await.contains_key(name) {
588606
return Ok(());
@@ -828,6 +846,15 @@ impl RpcServer for RpcServerImpl {
828846
Ok(data)
829847
}
830848

849+
async fn list_wallets(&self) -> Result<Vec<String>, ErrorObjectOwned> {
850+
self.wallet_manager
851+
.list_wallets()
852+
.await
853+
.map_err(|error| {
854+
ErrorObjectOwned::owned(-1, error.to_string(), None::<String>)
855+
})
856+
}
857+
831858
async fn wallet_load(&self, name: &str) -> Result<(), ErrorObjectOwned> {
832859
self.wallet_manager
833860
.load_wallet(name)

0 commit comments

Comments
 (0)