Skip to content

Commit 7e44050

Browse files
authored
Merge pull request #87 from Ximik/wallet
List wallets and chain info
2 parents 80daed4 + 98e8c4b commit 7e44050

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

client/src/bin/space-cli.rs

+9-1
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

+11
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ pub fn print_list_unspent(utxos: Vec<WalletOutput>, format: Format) {
150150
}
151151
}
152152

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

client/src/rpc.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ pub struct ServerInfo {
6666

6767
#[derive(Debug, Clone, Serialize, Deserialize)]
6868
pub struct ChainInfo {
69-
blocks: u32,
70-
headers: u32,
69+
pub blocks: u32,
70+
pub headers: u32,
7171
}
7272

7373
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -199,6 +199,9 @@ pub trait Rpc {
199199
#[method(name = "gettxmeta")]
200200
async fn get_tx_meta(&self, txid: Txid) -> Result<Option<TxEntry>, ErrorObjectOwned>;
201201

202+
#[method(name = "listwallets")]
203+
async fn list_wallets(&self) -> Result<Vec<String>, ErrorObjectOwned>;
204+
202205
#[method(name = "walletload")]
203206
async fn wallet_load(&self, name: &str) -> Result<(), ErrorObjectOwned>;
204207

@@ -575,6 +578,21 @@ impl WalletManager {
575578
(network, genesis_hash)
576579
}
577580

581+
pub async fn list_wallets(&self) -> anyhow::Result<Vec<String>> {
582+
let wallets = std::fs::read_dir(&self.data_dir)?
583+
.filter_map(Result::ok)
584+
.filter(|entry| entry.path().is_dir())
585+
.filter_map(|entry| {
586+
entry.path()
587+
.file_name()
588+
.and_then(|name| name.to_str())
589+
.map(String::from)
590+
})
591+
.collect();
592+
593+
Ok(wallets)
594+
}
595+
578596
pub async fn load_wallet(&self, name: &str) -> anyhow::Result<()> {
579597
if self.wallets.read().await.contains_key(name) {
580598
return Ok(());
@@ -820,6 +838,15 @@ impl RpcServer for RpcServerImpl {
820838
Ok(data)
821839
}
822840

841+
async fn list_wallets(&self) -> Result<Vec<String>, ErrorObjectOwned> {
842+
self.wallet_manager
843+
.list_wallets()
844+
.await
845+
.map_err(|error| {
846+
ErrorObjectOwned::owned(-1, error.to_string(), None::<String>)
847+
})
848+
}
849+
823850
async fn wallet_load(&self, name: &str) -> Result<(), ErrorObjectOwned> {
824851
self.wallet_manager
825852
.load_wallet(name)
@@ -1593,4 +1620,4 @@ async fn get_server_info(client: &reqwest::Client, rpc: &BitcoinRpc, tip: ChainA
15931620
},
15941621
progress: calc_progress(start_block, tip.height, info.headers),
15951622
})
1596-
}
1623+
}

0 commit comments

Comments
 (0)