diff --git a/cli/src/modules/wallet.rs b/cli/src/modules/wallet.rs index 39b39320f..ab76e9704 100644 --- a/cli/src/modules/wallet.rs +++ b/cli/src/modules/wallet.rs @@ -21,7 +21,11 @@ impl Wallet { } else { tprintln!(ctx, "Wallets:"); for wallet in wallets { - tprintln!(ctx, " {}", wallet); + if let Some(title) = wallet.title { + tprintln!(ctx, " {}: {}", wallet.filename, title); + } else { + tprintln!(ctx, " {}", wallet.filename); + } } } } diff --git a/wallet/core/src/runtime/wallet.rs b/wallet/core/src/runtime/wallet.rs index d84305710..9e78d1c75 100644 --- a/wallet/core/src/runtime/wallet.rs +++ b/wallet/core/src/runtime/wallet.rs @@ -133,11 +133,7 @@ impl Wallet { Self::try_with_rpc(Some(rpc), store, network_id) } - pub fn try_with_rpc( - rpc: Option, - store: Arc, - network_id: Option, - ) -> Result { + pub fn try_with_rpc(rpc: Option, store: Arc, network_id: Option) -> Result { let multiplexer = Multiplexer::>::new(); let utxo_processor = Arc::new(UtxoProcessor::new(rpc.clone(), network_id, Some(multiplexer.clone()))); diff --git a/wallet/core/src/storage/interface.rs b/wallet/core/src/storage/interface.rs index 817799006..e9be71619 100644 --- a/wallet/core/src/storage/interface.rs +++ b/wallet/core/src/storage/interface.rs @@ -46,6 +46,18 @@ impl Drop for AccessContext { } } +#[derive(Debug, Clone)] +pub struct WalletDescriptor { + pub title: Option, + pub filename: String, +} + +impl WalletDescriptor { + pub fn new(title: Option, filename: String) -> Self { + Self { title, filename } + } +} + pub type StorageStream = Pin> + Send>>; #[async_trait] @@ -124,7 +136,7 @@ impl OpenArgs { #[async_trait] pub trait Interface: Send + Sync + AnySync { /// enumerate all wallets available in the storage - async fn wallet_list(&self) -> Result>; + async fn wallet_list(&self) -> Result>; /// check if a wallet is currently open fn is_open(&self) -> bool; diff --git a/wallet/core/src/storage/local/interface.rs b/wallet/core/src/storage/local/interface.rs index e1b35de48..329ffa791 100644 --- a/wallet/core/src/storage/local/interface.rs +++ b/wallet/core/src/storage/local/interface.rs @@ -4,6 +4,7 @@ use crate::storage::interface::AddressBookStore; use crate::storage::interface::CreateArgs; use crate::storage::interface::OpenArgs; use crate::storage::interface::StorageStream; +use crate::storage::interface::WalletDescriptor; use crate::storage::local::cache::*; use crate::storage::local::streams::*; use crate::storage::local::transaction::*; @@ -228,11 +229,11 @@ impl Interface for LocalStore { Ok(()) } - async fn wallet_list(&self) -> Result> { + async fn wallet_list(&self) -> Result> { let location = self.location.lock().unwrap().clone().unwrap(); let folder = fs::resolve_path(&location.folder)?; - let files = fs::readdir(folder, false).await?; + let files = fs::readdir(folder.clone(), false).await?; let wallets = files .iter() .filter_map(|de| { @@ -240,7 +241,19 @@ impl Interface for LocalStore { file_name.ends_with(".wallet").then(|| file_name.trim_end_matches(".wallet").to_string()) }) .collect::>(); - Ok(wallets) + + let mut descriptors = vec![]; + for filename in wallets.into_iter() { + let path = folder.join(format!("{}.wallet", filename)); + let title = fs::read_to_string(&path) + .await + .ok() + .and_then(|json| serde_json::Value::from_str(json.as_str()).ok()) + .and_then(|data| data.get("name").and_then(|v| v.as_str()).map(|v| v.to_string())); + descriptors.push(WalletDescriptor { title, filename }); + } + + Ok(descriptors) } fn is_open(&self) -> bool { diff --git a/wallet/core/src/storage/mod.rs b/wallet/core/src/storage/mod.rs index b42e8e701..57d1ff2a4 100644 --- a/wallet/core/src/storage/mod.rs +++ b/wallet/core/src/storage/mod.rs @@ -17,7 +17,7 @@ pub use address::AddressBookEntry; pub use binding::Binding; pub use hint::Hint; pub use id::IdT; -pub use interface::{AccessContextT, AccountStore, Interface, PrvKeyDataStore, TransactionRecordStore}; +pub use interface::{AccessContextT, AccountStore, Interface, PrvKeyDataStore, TransactionRecordStore, WalletDescriptor}; pub use keydata::{KeyCaps, PrvKeyData, PrvKeyDataId, PrvKeyDataInfo, PrvKeyDataMap, PrvKeyDataPayload}; pub use metadata::Metadata; pub use transaction::{TransactionMetadata, TransactionRecord, TransactionType};