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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
|
use std::ffi::{CStr, CString};
use std::os::raw::{c_int, c_void};
use std::ptr::NonNull;
use std::sync::Arc;
pub mod bindings;
pub use bindings::WalletStatus_Ok;
pub use bindings::WalletStatus_Error;
pub use bindings::WalletStatus_Critical;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NetworkType {
Mainnet = bindings::NetworkType_MAINNET as isize,
Testnet = bindings::NetworkType_TESTNET as isize,
Stagenet = bindings::NetworkType_STAGENET as isize,
}
impl NetworkType {
pub fn from_c_int(value: c_int) -> Option<Self> {
match value {
bindings::NetworkType_MAINNET => Some(NetworkType::Mainnet),
bindings::NetworkType_TESTNET => Some(NetworkType::Testnet),
bindings::NetworkType_STAGENET => Some(NetworkType::Stagenet),
_ => None,
}
}
pub fn to_c_int(self) -> c_int {
self as c_int
}
}
#[derive(Debug)]
pub enum WalletError {
NullPointer,
FfiError(String),
WalletErrorCode(c_int, String),
}
pub type WalletResult<T> = Result<T, WalletError>;
pub struct WalletManager {
ptr: NonNull<c_void>,
}
impl WalletManager {
/// Creates a new `WalletManager` using the statically linked `MONERO_WalletManagerFactory_getWalletManager`.
///
/// # Example
///
/// ```
/// use monero_c_rust::WalletManager;
/// let manager = WalletManager::new();
/// assert!(manager.is_ok());
/// ```
pub fn new() -> WalletResult<Arc<Self>> {
unsafe {
let ptr = bindings::MONERO_WalletManagerFactory_getWalletManager();
let ptr = NonNull::new(ptr).ok_or(WalletError::NullPointer)?;
Ok(Arc::new(WalletManager { ptr }))
}
}
/// Check the status of a wallet to ensure it's in a valid state.
///
/// # Example
///
/// ```rust
/// use monero_c_rust::{WalletManager, NetworkType};
/// use tempfile::TempDir;
///
/// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
/// let wallet_path = temp_dir.path().join("wallet_name");
/// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
/// let wallet_result = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet);
/// assert!(wallet_result.is_ok(), "Failed to create wallet: {:?}", wallet_result.err());
/// let wallet = wallet_result.unwrap();
///
/// // Check the status of the wallet, expecting OK
/// let status_result = manager.get_status(wallet.ptr.as_ptr());
/// assert!(status_result.is_ok(), "Failed to get status: {:?}", status_result.err());
/// assert_eq!(status_result.unwrap(), (), "Expected status to be OK");
///
/// // Clean up wallet files.
/// std::fs::remove_file(wallet_str).expect("Failed to delete test wallet");
/// std::fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys");
/// ```
pub fn get_status(&self, wallet_ptr: *mut c_void) -> WalletResult<()> {
if wallet_ptr.is_null() {
return Err(WalletError::NullPointer); // Ensure NullPointer is returned for null wallet
}
unsafe {
let status = bindings::MONERO_Wallet_status(wallet_ptr);
if status == bindings::WalletStatus_Ok {
Ok(())
} else {
let error_ptr = bindings::MONERO_Wallet_errorString(wallet_ptr);
let error_msg = if error_ptr.is_null() {
"Unknown error".to_string()
} else {
CStr::from_ptr(error_ptr).to_string_lossy().into_owned()
};
Err(WalletError::WalletErrorCode(status, error_msg))
}
}
}
/// Creates a new wallet.
///
/// # Example
///
/// ```
/// use monero_c_rust::{WalletManager, NetworkType};
/// use std::fs;
/// use std::path::Path;
///
/// let manager = WalletManager::new().unwrap();
/// let wallet = manager.create_wallet("wallet_name", "password", "English", NetworkType::Mainnet);
/// assert!(wallet.is_ok());
///
/// // Cleanup: remove the wallet file and its corresponding keys file, if they exist.
/// if Path::new("wallet_name").exists() {
/// fs::remove_file("wallet_name").expect("Failed to delete test wallet");
/// }
/// if Path::new("wallet_name.keys").exists() {
/// fs::remove_file("wallet_name.keys").expect("Failed to delete test wallet keys");
/// }
/// ```
pub fn create_wallet(
self: &Arc<Self>,
path: &str,
password: &str,
language: &str,
network_type: NetworkType,
) -> WalletResult<Wallet> {
let c_path = CString::new(path).map_err(|_| WalletError::FfiError("Invalid path".to_string()))?;
let c_password = CString::new(password).map_err(|_| WalletError::FfiError("Invalid password".to_string()))?;
let c_language = CString::new(language).map_err(|_| WalletError::FfiError("Invalid language".to_string()))?;
unsafe {
let wallet_ptr = bindings::MONERO_WalletManager_createWallet(
self.ptr.as_ptr(),
c_path.as_ptr(),
c_password.as_ptr(),
c_language.as_ptr(),
network_type.to_c_int(),
);
if wallet_ptr.is_null() {
return Err(WalletError::NullPointer);
}
Ok(Wallet { ptr: NonNull::new(wallet_ptr).unwrap(), manager: Arc::clone(self) })
}
}
/// Opens an existing wallet with the provided path, password, and network type.
///
/// # Example
///
/// ```rust
/// use monero_c_rust::{WalletManager, NetworkType};
/// use tempfile::TempDir;
/// use std::fs;
///
/// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
/// let wallet_path = temp_dir.path().join("wallet_name");
/// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
///
/// // First, create a wallet to open later.
/// let wallet_result = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet);
/// assert!(wallet_result.is_ok(), "Failed to create wallet: {:?}", wallet_result.err());
/// let wallet = wallet_result.unwrap();
///
/// // Close the wallet by dropping it.
/// drop(wallet);
///
/// // Now try to open the existing wallet.
/// let open_result = manager.open_wallet(wallet_str, "password", NetworkType::Mainnet);
/// assert!(open_result.is_ok(), "Failed to open wallet: {:?}", open_result.err());
/// let opened_wallet = open_result.unwrap();
///
/// // Clean up wallet files.
/// fs::remove_file(wallet_str).expect("Failed to delete test wallet");
/// fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys");
/// ```
pub fn open_wallet(
self: &Arc<Self>,
path: &str,
password: &str,
network_type: NetworkType,
) -> WalletResult<Wallet> {
let c_path = CString::new(path).map_err(|_| WalletError::FfiError("Invalid path".to_string()))?;
let c_password = CString::new(password).map_err(|_| WalletError::FfiError("Invalid password".to_string()))?;
unsafe {
let wallet_ptr = bindings::MONERO_WalletManager_openWallet(
self.ptr.as_ptr(),
c_path.as_ptr(),
c_password.as_ptr(),
network_type.to_c_int(),
);
if wallet_ptr.is_null() {
Err(self.get_status(wallet_ptr).unwrap_err())
} else {
// Ensuring that we properly close the wallet when it's no longer needed
let wallet = Wallet { ptr: NonNull::new(wallet_ptr).unwrap(), manager: Arc::clone(self) };
Ok(wallet)
}
}
}
}
pub struct Wallet {
pub ptr: NonNull<c_void>,
manager: Arc<WalletManager>,
}
impl Wallet {
/// Retrieves the wallet's seed with an optional offset.
///
/// # Example
///
/// ```
/// use monero_c_rust::{WalletManager, NetworkType};
/// use tempfile::TempDir;
/// use std::fs;
///
/// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
/// let wallet_path = temp_dir.path().join("wallet_name");
/// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
/// let wallet_result = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet);
/// assert!(wallet_result.is_ok(), "Failed to create wallet: {:?}", wallet_result.err());
/// let wallet = wallet_result.unwrap();
///
/// // Get seed with no offset
/// let seed = wallet.get_seed(None);
/// assert!(seed.is_ok(), "Failed to get seed: {:?}", seed.err());
/// let seed = seed.unwrap();
/// assert!(!seed.is_empty(), "Seed should not be empty");
///
/// // Get seed with an offset
/// let seed_with_offset = wallet.get_seed(Some("offset"));
/// assert!(seed_with_offset.is_ok(), "Failed to get seed with offset: {:?}", seed_with_offset.err());
/// let seed_with_offset = seed_with_offset.unwrap();
/// assert!(!seed_with_offset.is_empty(), "Seed with offset should not be empty");
///
/// // Clean up wallet files.
/// fs::remove_file(wallet_str).expect("Failed to delete test wallet");
/// fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys");
/// ```
pub fn get_seed(&self, seed_offset: Option<&str>) -> WalletResult<String> {
let c_seed_offset = CString::new(seed_offset.unwrap_or(""))
.map_err(|_| WalletError::FfiError("Invalid seed_offset".to_string()))?;
unsafe {
let seed_ptr = bindings::MONERO_Wallet_seed(self.ptr.as_ptr(), c_seed_offset.as_ptr());
if seed_ptr.is_null() {
return Err(self.get_last_error());
}
let seed = CStr::from_ptr(seed_ptr).to_string_lossy().into_owned();
if seed.is_empty() {
return Err(WalletError::FfiError("Received empty seed".to_string()));
}
Ok(seed)
}
}
/// Retrieves the wallet's address for the given account and address index.
///
/// # Example
///
/// ```
/// use monero_c_rust::{WalletManager, NetworkType};
/// use tempfile::TempDir;
/// use std::fs;
///
/// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
/// let wallet_path = temp_dir.path().join("wallet_name");
/// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
/// let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).unwrap();
/// let address = wallet.get_address(0, 0);
/// assert!(address.is_ok(), "Failed to get address: {:?}", address.err());
///
/// // Clean up wallet files.
/// fs::remove_file(wallet_str).expect("Failed to delete test wallet");
/// fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys");
/// ```
pub fn get_address(&self, account_index: u64, address_index: u64) -> WalletResult<String> {
unsafe {
let address_ptr = bindings::MONERO_Wallet_address(self.ptr.as_ptr(), account_index, address_index);
if address_ptr.is_null() {
Err(self.get_last_error())
} else {
let address = CStr::from_ptr(address_ptr)
.to_string_lossy()
.into_owned();
Ok(address)
}
}
}
/// Checks if the wallet is deterministic.
///
/// # Example
///
/// ```
/// use monero_c_rust::{WalletManager, NetworkType};
/// use tempfile::TempDir;
/// use std::fs;
///
/// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
/// let wallet_path = temp_dir.path().join("wallet_name");
/// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
/// let wallet_result = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet);
/// assert!(wallet_result.is_ok(), "Failed to create wallet: {:?}", wallet_result.err());
/// let wallet = wallet_result.unwrap();
/// let is_deterministic = wallet.is_deterministic();
/// assert!(is_deterministic.is_ok(), "Failed to check if wallet is deterministic: {:?}", is_deterministic.err());
/// assert!(is_deterministic.unwrap(), "Wallet should be deterministic");
///
/// // Clean up wallet files.
/// fs::remove_file(wallet_str).expect("Failed to delete test wallet");
/// fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys");
/// ```
pub fn is_deterministic(&self) -> WalletResult<bool> {
unsafe {
let result = bindings::MONERO_Wallet_isDeterministic(self.ptr.as_ptr());
Ok(result)
}
}
/// Retrieves the last error from the wallet.
///
/// # Example
///
/// ```
/// use monero_c_rust::{WalletManager, NetworkType, WalletError};
/// let manager = WalletManager::new().unwrap();
/// // Intentionally pass an invalid wallet to force an error.
/// let invalid_wallet = manager.create_wallet("", "", "", NetworkType::Mainnet);
/// if let Err(err) = invalid_wallet {
/// if let WalletError::WalletErrorCode(_, error_msg) = err {
/// // Check that an error message was produced
/// assert!(!error_msg.is_empty(), "Error message should not be empty");
/// }
/// }
/// ```
pub fn get_last_error(&self) -> WalletError {
unsafe {
let error_ptr = bindings::MONERO_Wallet_errorString(self.ptr.as_ptr());
let status = bindings::MONERO_Wallet_status(self.ptr.as_ptr());
let error_msg = if error_ptr.is_null() {
"Unknown error".to_string()
} else {
CStr::from_ptr(error_ptr)
.to_string_lossy()
.into_owned()
};
WalletError::WalletErrorCode(status, error_msg)
}
}
/// Retrieves the balance and unlocked balance for the given account index.
///
/// # Example
///
/// ```
/// use monero_c_rust::{WalletManager, NetworkType, WalletResult};
/// use tempfile::TempDir;
///
/// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
/// let wallet_path = temp_dir.path().join("wallet_name");
/// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
/// let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).unwrap();
///
/// let balance = wallet.get_balance(0);
/// assert!(balance.is_ok(), "Failed to get balance: {:?}", balance.err());
///
/// // Clean up wallet files.
/// std::fs::remove_file(wallet_str).expect("Failed to delete test wallet");
/// std::fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys");
/// ```
pub fn get_balance(&self, account_index: u32) -> WalletResult<GetBalance> {
unsafe {
let balance = bindings::MONERO_Wallet_balance(self.ptr.as_ptr(), account_index);
let unlocked_balance = bindings::MONERO_Wallet_unlockedBalance(self.ptr.as_ptr(), account_index);
Ok(GetBalance { balance, unlocked_balance })
}
}
}
#[derive(Debug)]
pub struct GetBalance {
pub balance: u64,
pub unlocked_balance: u64,
}
impl Drop for Wallet {
fn drop(&mut self) {
unsafe {
bindings::MONERO_WalletManager_closeWallet(
self.manager.ptr.as_ptr(),
self.ptr.as_ptr(),
false, // Don't save the wallet by default.
);
}
}
}
#[cfg(test)]
use tempfile::TempDir;
#[cfg(test)]
use std::fs;
#[cfg(test)]
fn check_and_delete_existing_wallets(temp_dir: &TempDir) -> std::io::Result<()> {
let test_wallet_names = &["wallet_name", "mainnet_wallet", "testnet_wallet", "stagenet_wallet"];
for name in test_wallet_names {
let wallet_file = temp_dir.path().join(name);
let keys_file = temp_dir.path().join(format!("{}.keys", name));
if wallet_file.exists() {
fs::remove_file(&wallet_file)?;
}
if keys_file.exists() {
fs::remove_file(&keys_file)?;
}
}
Ok(())
}
#[cfg(test)]
fn setup() -> WalletResult<(Arc<WalletManager>, TempDir)> {
let temp_dir = tempfile::tempdir().expect("Failed to create temporary directory");
check_and_delete_existing_wallets(&temp_dir).expect("Failed to clean up existing wallets");
let manager = WalletManager::new()?;
Ok((manager, temp_dir))
}
#[cfg(test)]
fn teardown(temp_dir: &TempDir) -> std::io::Result<()> {
check_and_delete_existing_wallets(temp_dir)
}
#[test]
fn test_wallet_manager_creation() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("wallet_name");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet_result = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet);
assert!(wallet_result.is_ok(), "WalletManager creation failed");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_wallet_creation() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("wallet_name");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet);
assert!(wallet.is_ok(), "Failed to create wallet");
let wallet = wallet.unwrap();
assert!(wallet.is_deterministic().is_ok(), "Wallet creation seems to have failed");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_get_seed() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("wallet_name");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
// Create a new wallet.
let wallet = manager
.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
// Test getting seed with no offset (None).
let result = wallet.get_seed(None);
assert!(result.is_ok(), "Failed to get seed without offset: {:?}", result.err());
assert!(!result.unwrap().is_empty(), "Seed without offset is empty");
// Test getting seed with a specific offset (Some("offset")).
let result_with_offset = wallet.get_seed(Some("offset"));
assert!(result_with_offset.is_ok(), "Failed to get seed with offset: {:?}", result_with_offset.err());
assert!(!result_with_offset.unwrap().is_empty(), "Seed with offset is empty");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_get_address() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("wallet_name");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).expect("Failed to create wallet");
let result = wallet.get_address(0, 0);
assert!(result.is_ok(), "Failed to get address: {:?}", result.err());
assert!(!result.unwrap().is_empty(), "Address is empty");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_is_deterministic() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("wallet_name");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).expect("Failed to create wallet");
let result = wallet.is_deterministic();
assert!(result.is_ok(), "Failed to check if wallet is deterministic: {:?}", result.err());
assert!(result.unwrap(), "Wallet should be deterministic");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_wallet_creation_with_different_networks() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallets = vec![
("mainnet_wallet", NetworkType::Mainnet),
("testnet_wallet", NetworkType::Testnet),
("stagenet_wallet", NetworkType::Stagenet),
];
for (name, net_type) in wallets {
let wallet_path = temp_dir.path().join(name);
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager.create_wallet(wallet_str, "password", "English", net_type);
assert!(wallet.is_ok(), "Failed to create wallet: {}", name);
}
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_multiple_address_generation() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("wallet_name");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).expect("Failed to create wallet");
for i in 0..5 {
let result = wallet.get_address(0, i);
assert!(result.is_ok(), "Failed to get address {}: {:?}", i, result.err());
assert!(!result.unwrap().is_empty(), "Address {} is empty", i);
}
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_wallet_error_display() {
// Test WalletError::FfiError variant.
let error = WalletError::FfiError("Test error".to_string());
match error {
WalletError::FfiError(msg) => assert_eq!(msg, "Test error"),
_ => panic!("Expected FfiError variant"),
}
// Test WalletError::NullPointer variant.
let error = WalletError::NullPointer;
match error {
WalletError::NullPointer => assert!(true),
_ => panic!("Expected NullPointer variant"),
}
// Test WalletError::WalletErrorCode variant.
let error = WalletError::WalletErrorCode(2, "Sample wallet error".to_string());
match error {
WalletError::WalletErrorCode(code, msg) => {
assert_eq!(code, 2);
assert_eq!(msg, "Sample wallet error");
},
_ => panic!("Expected WalletErrorCode variant"),
}
}
#[test]
fn test_wallet_status() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("wallet_name");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
// Create a wallet to use for status checking
let wallet = manager
.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
// Check the status of the wallet, expecting it to be OK
let status_result = manager.get_status(wallet.ptr.as_ptr());
assert!(status_result.is_ok(), "Failed to get status: {:?}", status_result.err());
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_open_wallet() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("wallet_name");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
// Create a wallet to be opened later
let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
// Drop the wallet so it can be opened later
drop(wallet);
// Try to open the wallet
let open_result = manager.open_wallet(wallet_str, "password", NetworkType::Mainnet);
assert!(open_result.is_ok(), "Failed to open wallet: {:?}", open_result.err());
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_get_balance() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("wallet_name");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).unwrap();
let balance_result = wallet.get_balance(0);
assert!(balance_result.is_ok(), "Failed to get balance: {:?}", balance_result.err());
let _balance = balance_result.unwrap();
// assert!(_balance.balance >= 0, "Balance should be non-negative");
// assert!(_balance.unlocked_balance >= 0, "Unlocked balance should be non-negative");
// These assertions are meaningless with the constraints of the type.
teardown(&temp_dir).expect("Failed to clean up after test");
}
|