1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::websocket::{
    get_client, get_client_tls, perform_handshake, Client, ClientSSLConfig, NodeType,
};
use std::collections::HashMap;
use std::io::Error;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use tokio::sync::Mutex;

pub struct WalletClient {
    pub client: Arc<Mutex<Client>>,
}
impl WalletClient {
    pub async fn new(
        host: &str,
        port: u16,
        network_id: &str,
        additional_headers: &Option<HashMap<String, String>>,
        run: Arc<AtomicBool>,
    ) -> Result<Self, Error> {
        let (client, mut stream) = get_client(host, port, additional_headers).await?;
        tokio::spawn(async move { stream.run(run).await });
        let client = Arc::new(Mutex::new(client));
        let _ = perform_handshake(client.clone(), network_id, port, NodeType::Wallet).await;
        Ok(WalletClient { client })
    }
    pub async fn new_ssl(
        host: &str,
        port: u16,
        ssl_info: ClientSSLConfig<'_>,
        network_id: &str,
        additional_headers: &Option<HashMap<String, String>>,
        run: Arc<AtomicBool>,
    ) -> Result<Self, Error> {
        let (client, mut stream) = get_client_tls(host, port, ssl_info, additional_headers).await?;
        tokio::spawn(async move { stream.run(run).await });
        let client = Arc::new(Mutex::new(client));
        let _ = perform_handshake(client.clone(), network_id, port, NodeType::Wallet).await;
        Ok(WalletClient { client })
    }
}