wallet_adapter/
storage.rs

1use std::{cell::RefCell, collections::HashMap, rc::Rc};
2
3use crate::Wallet;
4
5/// Convenience type for `HashMap<blake3::Hash, Wallet>;`
6pub type StorageSchema = HashMap<blake3::Hash, Wallet>;
7
8/// Convenience type for `Rc<RefCell<StorageSchema>>;`
9pub type StorageType = Rc<RefCell<StorageSchema>>;
10
11/// Storage used by the [crate::WalletAdapter]
12#[derive(Default, PartialEq, Eq, Clone)]
13pub struct WalletStorage(StorageType);
14
15impl WalletStorage {
16    /// Clone the inner field  as `Rc<RefCell<HashMap<blake3::Hash, Wallet>>>`
17    pub fn clone_inner(&self) -> StorageType {
18        Rc::clone(&self.0)
19    }
20
21    /// Get all the wallets from storage
22    pub fn get_wallets(&self) -> Vec<Wallet> {
23        self.0.borrow().values().cloned().collect::<Vec<Wallet>>()
24    }
25
26    /// Get a certain wallet by name from storage
27    pub fn get_wallet(&self, wallet_name: &str) -> Option<Wallet> {
28        let storage_ref = self.0.borrow();
29        storage_ref
30            .get(&blake3::hash(wallet_name.to_lowercase().as_bytes()))
31            .cloned()
32    }
33}
34
35impl core::fmt::Debug for WalletStorage {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(f, "{:?}", &*self.0.borrow())
38    }
39}