Skip to content

Commit

Permalink
convert wallet_list to return Vec<WalletDescriptor>
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Sep 9, 2023
1 parent dea9062 commit 4ac9052
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
6 changes: 5 additions & 1 deletion cli/src/modules/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions wallet/core/src/runtime/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,7 @@ impl Wallet {
Self::try_with_rpc(Some(rpc), store, network_id)
}

pub fn try_with_rpc(
rpc: Option<Rpc>,
store: Arc<dyn Interface>,
network_id: Option<NetworkId>,
) -> Result<Wallet> {
pub fn try_with_rpc(rpc: Option<Rpc>, store: Arc<dyn Interface>, network_id: Option<NetworkId>) -> Result<Wallet> {
let multiplexer = Multiplexer::<Box<Events>>::new();
let utxo_processor = Arc::new(UtxoProcessor::new(rpc.clone(), network_id, Some(multiplexer.clone())));

Expand Down
14 changes: 13 additions & 1 deletion wallet/core/src/storage/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ impl Drop for AccessContext {
}
}

#[derive(Debug, Clone)]
pub struct WalletDescriptor {
pub title: Option<String>,
pub filename: String,
}

impl WalletDescriptor {
pub fn new(title: Option<String>, filename: String) -> Self {
Self { title, filename }
}
}

pub type StorageStream<T> = Pin<Box<dyn Stream<Item = Result<T>> + Send>>;

#[async_trait]
Expand Down Expand Up @@ -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<Vec<String>>;
async fn wallet_list(&self) -> Result<Vec<WalletDescriptor>>;

/// check if a wallet is currently open
fn is_open(&self) -> bool;
Expand Down
19 changes: 16 additions & 3 deletions wallet/core/src/storage/local/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -228,19 +229,31 @@ impl Interface for LocalStore {
Ok(())
}

async fn wallet_list(&self) -> Result<Vec<String>> {
async fn wallet_list(&self) -> Result<Vec<WalletDescriptor>> {
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| {
let file_name = de.file_name();
file_name.ends_with(".wallet").then(|| file_name.trim_end_matches(".wallet").to_string())
})
.collect::<Vec<_>>();
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 {
Expand Down
2 changes: 1 addition & 1 deletion wallet/core/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 4ac9052

Please sign in to comment.