ferrox_wallet/
simple_wallet_manager.rs

1use std::{
2    collections::HashMap,
3    future::Future,
4    pin::Pin,
5    sync::{Arc, Mutex},
6};
7
8use solana_sdk::signature::Keypair;
9
10use crate::{Wallet, WalletManager};
11
12#[derive(Clone)]
13pub struct SimpleWalletManager {
14    wallets: Arc<Mutex<HashMap<String, Wallet>>>,
15}
16
17impl SimpleWalletManager {
18    pub fn new() -> Self {
19        Self {
20            wallets: Arc::new(Mutex::new(HashMap::new())),
21        }
22    }
23}
24
25impl WalletManager for SimpleWalletManager {
26    fn get_wallet(
27        &self,
28        user_id: &str,
29    ) -> Pin<Box<dyn Future<Output = Result<Wallet, String>> + Send + Sync>> {
30        let wallet = self.wallets.lock().unwrap().get(user_id).cloned();
31        if let Some(wallet) = wallet {
32            return Box::pin(async move { Ok(wallet.clone()) });
33        } else {
34            // For test purposes, we return 1 hardcoded wallet
35            let private_key = [
36                103, 17, 11, 163, 113, 182, 255, 6, 9, 212, 145, 104, 9, 54, 192, 214, 170, 91, 36,
37                255, 10, 225, 26, 73, 183, 136, 250, 134, 171, 24, 250, 184, 9, 247, 185, 29, 89,
38                143, 75, 110, 195, 235, 251, 190, 182, 47, 42, 83, 2, 95, 187, 132, 253, 38, 244,
39                162, 168, 81, 252, 6, 133, 28, 79, 228,
40            ];
41            return Box::pin(async move {
42                Ok(Wallet::Solana(Arc::new(
43                    Keypair::from_bytes(&private_key).unwrap(),
44                )))
45            });
46        }
47    }
48
49    fn get_wallets(
50        &self,
51        _user_id: &str,
52    ) -> Pin<Box<dyn Future<Output = Result<Vec<Wallet>, String>> + Send + Sync>> {
53        let wallets = self.wallets.lock().unwrap().values().cloned().collect();
54        return Box::pin(async move { Ok(wallets) });
55    }
56
57    fn create_wallet(
58        &self,
59        user_id: &str,
60    ) -> Pin<Box<dyn Future<Output = Result<Wallet, String>> + Send + Sync>> {
61        let keypair = Keypair::new();
62        let wallet = Wallet::Solana(Arc::new(keypair));
63        self.wallets
64            .lock()
65            .unwrap()
66            .insert(user_id.to_string(), wallet.clone());
67        return Box::pin(async move { Ok(wallet) });
68    }
69}