Skip to content

List wallets and chain info #87

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

Merged
merged 2 commits into from
Apr 29, 2025
Merged
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
10 changes: 9 additions & 1 deletion client/src/bin/space-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use spaces_client::{
format::{
print_error_rpc_response, print_list_bidouts, print_list_spaces_response,
print_list_transactions, print_list_unspent, print_server_info,
print_wallet_balance_response, print_wallet_info, print_wallet_response, Format,
print_list_wallets, print_wallet_balance_response, print_wallet_info, print_wallet_response,
Format,
},
rpc::{
BidParams, ExecuteParams, OpenParams, RegisterParams, RpcClient, RpcWalletRequest,
Expand Down Expand Up @@ -71,6 +72,9 @@ pub struct Args {

#[derive(Subcommand, Debug, Clone)]
enum Commands {
/// List existing wallets
#[command(name = "listwallets")]
ListWallets,
/// Generate a new wallet
#[command(name = "createwallet")]
CreateWallet,
Expand Down Expand Up @@ -573,6 +577,10 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
let response = cli.client.get_spaceout(outpoint).await?;
println!("{}", serde_json::to_string_pretty(&response)?);
}
Commands::ListWallets => {
let result = cli.client.list_wallets().await?;
print_list_wallets(result, cli.format);
}
Commands::CreateWallet => {
cli.client.wallet_create(&cli.wallet).await?;
}
Expand Down
11 changes: 11 additions & 0 deletions client/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ pub fn print_list_unspent(utxos: Vec<WalletOutput>, format: Format) {
}
}

pub fn print_list_wallets(wallets: Vec<String>, format: Format) {
match format {
Format::Text => {
println!("{}", wallets.join("\n"));
}
Format::Json => {
println!("{}", serde_json::to_string_pretty(&wallets).unwrap());
}
}
}

pub fn print_server_info(info: ServerInfo, format: Format) {
match format {
Format::Text => {
Expand Down
33 changes: 30 additions & 3 deletions client/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub struct ServerInfo {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChainInfo {
blocks: u32,
headers: u32,
pub blocks: u32,
pub headers: u32,
}

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

#[method(name = "listwallets")]
async fn list_wallets(&self) -> Result<Vec<String>, ErrorObjectOwned>;

#[method(name = "walletload")]
async fn wallet_load(&self, name: &str) -> Result<(), ErrorObjectOwned>;

Expand Down Expand Up @@ -583,6 +586,21 @@ impl WalletManager {
(network, genesis_hash)
}

pub async fn list_wallets(&self) -> anyhow::Result<Vec<String>> {
let wallets = std::fs::read_dir(&self.data_dir)?
.filter_map(Result::ok)
.filter(|entry| entry.path().is_dir())
.filter_map(|entry| {
entry.path()
.file_name()
.and_then(|name| name.to_str())
.map(String::from)
})
.collect();

Ok(wallets)
}

pub async fn load_wallet(&self, name: &str) -> anyhow::Result<()> {
if self.wallets.read().await.contains_key(name) {
return Ok(());
Expand Down Expand Up @@ -828,6 +846,15 @@ impl RpcServer for RpcServerImpl {
Ok(data)
}

async fn list_wallets(&self) -> Result<Vec<String>, ErrorObjectOwned> {
self.wallet_manager
.list_wallets()
.await
.map_err(|error| {
ErrorObjectOwned::owned(-1, error.to_string(), None::<String>)
})
}

async fn wallet_load(&self, name: &str) -> Result<(), ErrorObjectOwned> {
self.wallet_manager
.load_wallet(name)
Expand Down Expand Up @@ -1597,4 +1624,4 @@ async fn get_server_info(client: &reqwest::Client, rpc: &BitcoinRpc, tip: ChainA
0.0
},
})
}
}
Loading