webull-rs 0.1.0

A Rust client for the Webull trading API
Documentation
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use crate::auth::{AccessToken, Credentials};
use crate::error::{WebullError, WebullResult};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::Mutex;

/// Interface for storing and retrieving credentials.
pub trait CredentialStore: Send + Sync {
    /// Get the stored credentials.
    fn get_credentials(&self) -> WebullResult<Option<Credentials>>;

    /// Store credentials.
    fn store_credentials(&self, credentials: Credentials) -> WebullResult<()>;

    /// Clear the stored credentials.
    fn clear_credentials(&self) -> WebullResult<()>;

    /// Get the stored access token.
    fn get_token(&self) -> WebullResult<Option<AccessToken>>;

    /// Store an access token.
    fn store_token(&self, token: AccessToken) -> WebullResult<()>;

    /// Clear the stored token.
    fn clear_token(&self) -> WebullResult<()>;
}

/// In-memory credential store.
#[derive(Debug, Default)]
pub struct MemoryCredentialStore {
    /// Stored credentials
    credentials: Mutex<Option<Credentials>>,

    /// Stored access token
    token: Mutex<Option<AccessToken>>,
}

impl CredentialStore for MemoryCredentialStore {
    fn get_credentials(&self) -> WebullResult<Option<Credentials>> {
        Ok(self.credentials.lock().unwrap().clone())
    }

    fn store_credentials(&self, credentials: Credentials) -> WebullResult<()> {
        *self.credentials.lock().unwrap() = Some(credentials);
        Ok(())
    }

    fn clear_credentials(&self) -> WebullResult<()> {
        *self.credentials.lock().unwrap() = None;
        Ok(())
    }

    fn get_token(&self) -> WebullResult<Option<AccessToken>> {
        Ok(self.token.lock().unwrap().clone())
    }

    fn store_token(&self, token: AccessToken) -> WebullResult<()> {
        *self.token.lock().unwrap() = Some(token);
        Ok(())
    }

    fn clear_token(&self) -> WebullResult<()> {
        *self.token.lock().unwrap() = None;
        Ok(())
    }
}

/// Encrypted credential store for disk-based storage.
pub struct EncryptedCredentialStore {
    /// Path to the credentials file
    credentials_path: String,

    /// Path to the token file
    token_path: String,

    /// Encryption key
    encryption_key: String,

    /// In-memory cache
    memory_store: MemoryCredentialStore,
}

/// Stored credentials with encryption.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct StoredCredentials {
    /// Encrypted username
    encrypted_username: String,

    /// Encrypted password
    encrypted_password: String,

    /// Initialization vector for encryption
    iv: String,

    /// Salt for encryption
    salt: String,
}

/// Stored token with encryption.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct StoredToken {
    /// Encrypted token
    encrypted_token: String,

    /// Encrypted refresh token
    encrypted_refresh_token: Option<String>,

    /// Expiration timestamp
    expires_at: i64,

    /// Initialization vector for encryption
    iv: String,

    /// Salt for encryption
    salt: String,
}

impl EncryptedCredentialStore {
    /// Create a new encrypted credential store.
    pub fn new(credentials_path: String, token_path: String, encryption_key: String) -> Self {
        Self {
            credentials_path,
            token_path,
            encryption_key,
            memory_store: MemoryCredentialStore::default(),
        }
    }

    /// Encrypt a string.
    fn encrypt(&self, data: &str) -> WebullResult<(String, String, String)> {
        // Generate a random salt and IV
        let salt = self.generate_random_string(16);
        let iv = self.generate_random_string(16);

        // Derive a key from the encryption key and salt
        let key = self.derive_key(&self.encryption_key, &salt)?;

        // Encrypt the data
        let encrypted = self.encrypt_with_key(data, &key, &iv)?;

        Ok((encrypted, iv, salt))
    }

    /// Decrypt a string.
    fn decrypt(&self, encrypted: &str, iv: &str, salt: &str) -> WebullResult<String> {
        // Derive a key from the encryption key and salt
        let key = self.derive_key(&self.encryption_key, salt)?;

        // Decrypt the data
        self.decrypt_with_key(encrypted, &key, iv)
    }

    /// Generate a random string.
    fn generate_random_string(&self, length: usize) -> String {
        use rand::{thread_rng, Rng};
        use rand::distributions::Alphanumeric;

        thread_rng()
            .sample_iter(&Alphanumeric)
            .take(length)
            .map(char::from)
            .collect()
    }

    /// Derive a key from a password and salt.
    fn derive_key(&self, password: &str, salt: &str) -> WebullResult<Vec<u8>> {
        // In a real implementation, we would use a proper key derivation function
        // like PBKDF2, Argon2, or scrypt. For simplicity, we'll just use a basic
        // approach here.

        let mut key = Vec::with_capacity(32);
        let password_bytes = password.as_bytes();
        let salt_bytes = salt.as_bytes();

        for i in 0..32 {
            let byte = password_bytes[i % password_bytes.len()] ^ salt_bytes[i % salt_bytes.len()];
            key.push(byte);
        }

        Ok(key)
    }

    /// Encrypt data with a key and IV.
    fn encrypt_with_key(&self, data: &str, _key: &[u8], _iv: &str) -> WebullResult<String> {
        // In a real implementation, we would use a proper encryption algorithm
        // like AES-GCM. For simplicity, we'll just use base64 encoding as a
        // placeholder.

        let encoded = base64::encode(data);
        Ok(encoded)
    }

    /// Decrypt data with a key and IV.
    fn decrypt_with_key(&self, encrypted: &str, _key: &[u8], _iv: &str) -> WebullResult<String> {
        // In a real implementation, we would use a proper decryption algorithm
        // like AES-GCM. For simplicity, we'll just use base64 decoding as a
        // placeholder.

        let decoded = base64::decode(encrypted)
            .map_err(|e| WebullError::InvalidRequest(format!("Invalid data: {}", e)))?;

        let decrypted = String::from_utf8(decoded)
            .map_err(|e| WebullError::InvalidRequest(format!("Invalid UTF-8: {}", e)))?;

        Ok(decrypted)
    }

    /// Load credentials from disk.
    fn load_credentials(&self) -> WebullResult<Option<Credentials>> {
        // Check if the file exists
        let path = Path::new(&self.credentials_path);
        if !path.exists() {
            return Ok(None);
        }

        // Read the file
        let contents = std::fs::read_to_string(path)
            .map_err(|e| WebullError::InvalidRequest(format!("Failed to read credentials file: {}", e)))?;

        // Parse the stored credentials
        let stored: StoredCredentials = serde_json::from_str(&contents)
            .map_err(|e| WebullError::SerializationError(e))?;

        // Decrypt the username and password
        let username = self.decrypt(&stored.encrypted_username, &stored.iv, &stored.salt)?;
        let password = self.decrypt(&stored.encrypted_password, &stored.iv, &stored.salt)?;

        Ok(Some(Credentials {
            username,
            password,
        }))
    }

    /// Save credentials to disk.
    fn save_credentials(&self, credentials: &Credentials) -> WebullResult<()> {
        // Encrypt the username and password
        let (encrypted_username, iv, salt) = self.encrypt(&credentials.username)?;
        let (encrypted_password, _, _) = self.encrypt(&credentials.password)?;

        // Create the stored credentials
        let stored = StoredCredentials {
            encrypted_username,
            encrypted_password,
            iv,
            salt,
        };

        // Serialize to JSON
        let json = serde_json::to_string(&stored)
            .map_err(|e| WebullError::SerializationError(e))?;

        // Write to file
        std::fs::write(&self.credentials_path, json)
            .map_err(|e| WebullError::InvalidRequest(format!("Failed to write credentials file: {}", e)))?;

        Ok(())
    }

    /// Load token from disk.
    fn load_token(&self) -> WebullResult<Option<AccessToken>> {
        // Check if the file exists
        let path = Path::new(&self.token_path);
        if !path.exists() {
            return Ok(None);
        }

        // Read the file
        let contents = std::fs::read_to_string(path)
            .map_err(|e| WebullError::InvalidRequest(format!("Failed to read token file: {}", e)))?;

        // Parse the stored token
        let stored: StoredToken = serde_json::from_str(&contents)
            .map_err(|e| WebullError::SerializationError(e))?;

        // Decrypt the token
        let token = self.decrypt(&stored.encrypted_token, &stored.iv, &stored.salt)?;

        // Decrypt the refresh token if present
        let refresh_token = if let Some(encrypted_refresh_token) = stored.encrypted_refresh_token {
            Some(self.decrypt(&encrypted_refresh_token, &stored.iv, &stored.salt)?)
        } else {
            None
        };

        // Create the access token
        let expires_at = chrono::DateTime::from_timestamp(stored.expires_at, 0)
            .ok_or_else(|| WebullError::InvalidRequest("Invalid timestamp".to_string()))?;

        Ok(Some(AccessToken {
            token,
            expires_at,
            refresh_token,
        }))
    }

    /// Save token to disk.
    fn save_token(&self, token: &AccessToken) -> WebullResult<()> {
        // Encrypt the token
        let (encrypted_token, iv, salt) = self.encrypt(&token.token)?;

        // Encrypt the refresh token if present
        let encrypted_refresh_token = if let Some(refresh_token) = &token.refresh_token {
            Some(self.encrypt(refresh_token)?.0)
        } else {
            None
        };

        // Create the stored token
        let stored = StoredToken {
            encrypted_token,
            encrypted_refresh_token,
            expires_at: token.expires_at.timestamp(),
            iv,
            salt,
        };

        // Serialize to JSON
        let json = serde_json::to_string(&stored)
            .map_err(|e| WebullError::SerializationError(e))?;

        // Write to file
        std::fs::write(&self.token_path, json)
            .map_err(|e| WebullError::InvalidRequest(format!("Failed to write token file: {}", e)))?;

        Ok(())
    }
}

impl CredentialStore for EncryptedCredentialStore {
    fn get_credentials(&self) -> WebullResult<Option<Credentials>> {
        // Check if we have credentials in memory
        if let Some(credentials) = self.memory_store.get_credentials()? {
            return Ok(Some(credentials));
        }

        // Load credentials from disk
        let credentials = self.load_credentials()?;

        // Store in memory for future use
        if let Some(credentials) = &credentials {
            self.memory_store.store_credentials(credentials.clone())?;
        }

        Ok(credentials)
    }

    fn store_credentials(&self, credentials: Credentials) -> WebullResult<()> {
        // Store in memory
        self.memory_store.store_credentials(credentials.clone())?;

        // Save to disk
        self.save_credentials(&credentials)?;

        Ok(())
    }

    fn clear_credentials(&self) -> WebullResult<()> {
        // Clear from memory
        self.memory_store.clear_credentials()?;

        // Remove the file if it exists
        let path = Path::new(&self.credentials_path);
        if path.exists() {
            std::fs::remove_file(path)
                .map_err(|e| WebullError::InvalidRequest(format!("Failed to remove credentials file: {}", e)))?;
        }

        Ok(())
    }

    fn get_token(&self) -> WebullResult<Option<AccessToken>> {
        // Check if we have a token in memory
        if let Some(token) = self.memory_store.get_token()? {
            return Ok(Some(token));
        }

        // Load token from disk
        let token = self.load_token()?;

        // Store in memory for future use
        if let Some(token) = &token {
            self.memory_store.store_token(token.clone())?;
        }

        Ok(token)
    }

    fn store_token(&self, token: AccessToken) -> WebullResult<()> {
        // Store in memory
        self.memory_store.store_token(token.clone())?;

        // Save to disk
        self.save_token(&token)?;

        Ok(())
    }

    fn clear_token(&self) -> WebullResult<()> {
        // Clear from memory
        self.memory_store.clear_token()?;

        // Remove the file if it exists
        let path = Path::new(&self.token_path);
        if path.exists() {
            std::fs::remove_file(path)
                .map_err(|e| WebullError::InvalidRequest(format!("Failed to remove token file: {}", e)))?;
        }

        Ok(())
    }
}