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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
|
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>;
#[derive(Debug)]
pub struct Account {
pub index: u32,
pub label: String,
pub balance: u64,
pub unlocked_balance: u64,
}
#[derive(Debug)]
pub struct GetAccounts {
pub accounts: Vec<Account>,
}
pub struct Wallet {
pub ptr: NonNull<c_void>,
pub manager: Arc<WalletManager>,
pub is_closed: bool, // New field to track if the wallet is closed
}
pub struct WalletManager {
ptr: NonNull<c_void>,
}
/// Configuration parameters for initializing a wallet.
#[derive(Debug, Clone)]
pub struct WalletConfig {
pub daemon_address: String,
pub upper_transaction_size_limit: u64,
pub daemon_username: String,
pub daemon_password: String,
pub use_ssl: bool,
pub light_wallet: bool,
pub proxy_address: String,
}
impl Default for WalletConfig {
fn default() -> Self {
WalletConfig {
daemon_address: "localhost:18081".to_string(),
upper_transaction_size_limit: 10000, // TODO set sane value.
daemon_username: "".to_string(),
daemon_password: "".to_string(),
use_ssl: false,
light_wallet: false,
proxy_address: "".to_string(),
}
}
}
pub type BlockHeight = u64;
#[derive(Debug)]
pub struct Refreshed;
/// Represents a destination address and the amount to send.
#[derive(Debug, Clone)]
pub struct Destination {
/// The recipient's address.
pub address: String,
/// The amount to send to the recipient (in atomic units).
pub amount: u64,
}
/// Represents the result of a transfer operation.
#[derive(Debug)]
pub struct Transfer {
/// The transaction ID of the transfer.
pub txid: String,
/// The transaction key, if requested.
pub tx_key: Option<String>,
/// The total amount sent in the transfer.
pub amount: u64,
/// The fee associated with the transfer.
pub fee: u64,
}
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("test_wallet");
/// 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))
}
}
}
pub fn throw_if_error(&self, wallet_ptr: *mut c_void) -> WalletResult<()> {
if wallet_ptr.is_null() {
return Err(WalletError::NullPointer);
}
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("test_wallet", "password", "English", NetworkType::Mainnet);
/// assert!(wallet.is_ok());
///
/// // Cleanup: remove the wallet file and its corresponding keys file, if they exist.
/// if Path::new("test_wallet").exists() {
/// fs::remove_file("test_wallet").expect("Failed to delete test wallet");
/// }
/// if Path::new("test_wallet.keys").exists() {
/// fs::remove_file("test_wallet.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(),
);
self.throw_if_error(wallet_ptr)?;
if wallet_ptr.is_null() {
return Err(WalletError::NullPointer);
}
Ok(Wallet {
ptr: NonNull::new(wallet_ptr).unwrap(),
manager: Arc::clone(self),
is_closed: false,
})
}
}
/// Generates a wallet from provided keys.
///
/// # Arguments
///
/// * `filename` - The filename for the new wallet.
/// * `address` - The public address associated with the wallet.
/// * `spendkey` - The private spend key.
/// * `viewkey` - The private view key.
/// * `restore_height` - The blockchain height from which to start scanning.
/// * `password` - The password to secure the wallet.
/// * `language` - The language for the wallet's mnemonic seed.
/// * `network_type` - The network type (`Mainnet`, `Testnet`, or `Stagenet`).
/// * `autosave_current` - Whether to autosave the current wallet state.
/// * `kdf_rounds` - Number of KDF (Key Derivation Function) rounds. Typically set to 1.
///
/// # Returns
///
/// * `WalletResult<Wallet>` - Returns a `Wallet` instance on success, or a `WalletError` on failure.
///
/// # Example
///
/// ```rust
/// use monero_c_rust::{WalletManager, NetworkType};
///
/// let manager = WalletManager::new().unwrap();
/// let result = manager.generate_from_keys(
/// "new_wallet".to_string(),
/// "45wsWad9EwZgF3VpxQumrUCRaEtdyyh6NG8sVD3YRVVJbK1jkpJ3zq8WHLijVzodQ22LxwkdWx7fS2a6JzaRGzkNU8K2Dhi".to_string(), // Replace with a valid address
/// "29adefc8f67515b4b4bf48031780ab9d071d24f8a674b879ce7f245c37523807".to_string(),
/// "3bc0b202cde92fe5719c3cc0a16aa94f88a5d19f8c515d4e35fae361f6f2120e".to_string(),
/// 0,
/// "password".to_string(),
/// "English".to_string(),
/// NetworkType::Mainnet,
/// true,
/// 1, // Default KDF rounds
/// );
/// assert!(result.is_ok(), "Failed to generate wallet from keys: {:?}", result.err());
/// ```
pub fn generate_from_keys(
self: &Arc<Self>,
filename: String,
address: String,
spendkey: String,
viewkey: String,
restore_height: u64,
password: String,
language: String,
network_type: NetworkType,
kdf_rounds: u64,
) -> WalletResult<Wallet> {
let c_filename = CString::new(filename)
.map_err(|_| WalletError::FfiError("Invalid filename".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()))?;
let c_address = CString::new(address)
.map_err(|_| WalletError::FfiError("Invalid address".to_string()))?;
let c_spendkey = CString::new(spendkey)
.map_err(|_| WalletError::FfiError("Invalid spendkey".to_string()))?;
let c_viewkey = CString::new(viewkey)
.map_err(|_| WalletError::FfiError("Invalid viewkey".to_string()))?;
unsafe {
let wallet_ptr = bindings::MONERO_WalletManager_createWalletFromKeys(
self.ptr.as_ptr(),
c_filename.as_ptr(),
c_password.as_ptr(),
c_language.as_ptr(),
network_type.to_c_int(),
restore_height,
c_address.as_ptr(),
c_viewkey.as_ptr(),
c_spendkey.as_ptr(),
kdf_rounds,
);
if wallet_ptr.is_null() {
return Err(WalletError::NullPointer);
}
self.throw_if_error(wallet_ptr)?;
Ok(Wallet {
ptr: NonNull::new(wallet_ptr).unwrap(),
manager: Arc::clone(self),
is_closed: false,
})
}
}
/// 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("test_wallet");
/// 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(),
);
self.throw_if_error(wallet_ptr)?;
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),
is_closed: false,
};
Ok(wallet)
}
}
}
/// Retrieves the current blockchain height.
///
/// This method communicates with the connected daemon to obtain the latest
/// blockchain height. It returns a `BlockHeight` on success or a `WalletError` on failure.
///
/// # Example
///
/// ```rust
/// use monero_c_rust::{WalletManager, NetworkType};
///
/// let manager = WalletManager::new().unwrap();
/// let height = manager.get_height().unwrap();
/// println!("Current blockchain height: {}", height);
/// ```
pub fn get_height(&self) -> WalletResult<BlockHeight> {
unsafe {
let height = bindings::MONERO_WalletManager_blockchainHeight(self.ptr.as_ptr());
// Assuming the FFI call does not set an error, directly return the height.
// If error handling is required, additional checks should be implemented here.
Ok(height)
}
}
}
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("test_wallet");
/// 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());
self.throw_if_error()?;
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("test_wallet");
/// 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);
self.throw_if_error()?;
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("test_wallet");
/// 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());
self.throw_if_error()?;
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)
}
}
/// Checks for any errors by inspecting the wallet status and throws an error if found.
///
/// # Returns
/// - `Ok(())` if no error is found.
/// - `Err(WalletError)` if an error is encountered.
pub fn throw_if_error(&self) -> WalletResult<()> {
let status_result = self.manager.get_status(self.ptr.as_ptr());
if status_result.is_err() {
return status_result; // Return the error if the status is not OK.
}
Ok(())
}
/// 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("test_wallet");
/// 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);
self.throw_if_error()?;
let unlocked_balance = bindings::MONERO_Wallet_unlockedBalance(self.ptr.as_ptr(), account_index);
self.throw_if_error()?;
Ok(GetBalance { balance, unlocked_balance })
}
}
/// Creates a new subaddress account with the given label.
///
/// # Arguments
///
/// * `label` - A string representing the label for the new subaddress account.
///
/// # Returns
///
/// * `WalletResult<()>` - `Ok(())` if the account was successfully created, or a `WalletError` if an error occurred.
///
/// # Example
///
/// ```
/// use monero_c_rust::{WalletManager, NetworkType};
/// use tempfile::TempDir;
/// use std::fs;
///
/// // Set up the test environment.
/// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
/// let wallet_path = temp_dir.path().join("test_wallet");
/// let wallet_str = wallet_path.to_str().unwrap();
///
/// // Initialize the wallet manager and create a wallet.
/// 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();
///
/// // Create a new account with a label.
/// let result = wallet.create_account("New Account");
/// assert!(result.is_ok(), "Failed to create account: {:?}", result.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 create_account(&self, label: &str) -> WalletResult<()> {
let c_label = CString::new(label).map_err(|_| WalletError::FfiError("Invalid label".to_string()))?;
unsafe {
bindings::MONERO_Wallet_addSubaddressAccount(self.ptr.as_ptr(), c_label.as_ptr());
self.throw_if_error()
}
}
/// Retrieves all accounts associated with the wallet.
///
/// # 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("test_wallet");
/// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
/// let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).expect("Failed to create wallet");
///
/// // Initially, there should be one account (the primary account).
/// let initial_accounts = wallet.get_accounts().expect("Failed to retrieve accounts");
/// assert_eq!(initial_accounts.accounts.len(), 1, "Initial account count mismatch");
/// assert_eq!(initial_accounts.accounts[0].label, "Primary account", "Expected primary account label");
///
/// // Create additional accounts.
/// wallet.create_account("Account 1").expect("Failed to create account 1");
/// wallet.create_account("Account 2").expect("Failed to create account 2");
///
/// // Retrieve all accounts again; we should have three now.
/// let all_accounts = wallet.get_accounts().expect("Failed to retrieve all accounts");
/// assert_eq!(all_accounts.accounts.len(), 3, "Expected 3 accounts after creation");
///
/// // Verify the labels of the accounts.
/// assert_eq!(all_accounts.accounts[0].label, "Primary account", "First account should be the primary account");
/// assert_eq!(all_accounts.accounts[1].label, "Account 1", "Second account should be 'Account 1'");
/// assert_eq!(all_accounts.accounts[2].label, "Account 2", "Third account should be 'Account 2'");
///
/// // 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_accounts(&self) -> WalletResult<GetAccounts> {
unsafe {
let accounts_size = bindings::MONERO_Wallet_numSubaddressAccounts(self.ptr.as_ptr());
self.throw_if_error()?;
let mut accounts = Vec::new();
for i in 0..accounts_size as u32 {
let label_ptr = bindings::MONERO_Wallet_getSubaddressLabel(self.ptr.as_ptr(), i, 0);
let label = if label_ptr.is_null() {
"Unnamed".to_string()
} else {
CStr::from_ptr(label_ptr).to_string_lossy().into_owned()
};
let balance = bindings::MONERO_Wallet_balance(self.ptr.as_ptr(), i);
let unlocked_balance = bindings::MONERO_Wallet_unlockedBalance(self.ptr.as_ptr(), i);
accounts.push(Account {
index: i,
label,
balance,
unlocked_balance,
});
}
Ok(GetAccounts { accounts })
}
}
/// Closes the wallet, releasing any resources associated with it.
///
/// After calling this method, the `Wallet` instance should no longer be used.
///
/// # Returns
///
/// * `WalletResult<()>` - Returns `Ok(())` if the wallet was successfully closed,
/// or a `WalletError` if an error occurred during closing.
///
/// # Example
///
/// ```rust
/// use monero_c_rust::{WalletManager, NetworkType, WalletResult};
/// use tempfile::TempDir;
/// use std::fs;
///
/// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
/// let wallet_path = temp_dir.path().join("test_wallet");
/// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
/// let mut wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).unwrap();
///
/// // Use the wallet for operations...
///
/// // Now close the wallet
/// let close_result = wallet.close_wallet();
/// assert!(close_result.is_ok(), "Failed to close wallet: {:?}", close_result.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 close_wallet(&mut self) -> WalletResult<()> {
if self.is_closed {
return Ok(());
}
unsafe {
let result = bindings::MONERO_WalletManager_closeWallet(
self.manager.ptr.as_ptr(),
self.ptr.as_ptr(),
false, // Don't save the wallet by default.
);
if result {
self.is_closed = true;
Ok(())
} else {
Err(WalletError::FfiError("Failed to close wallet".to_string()))
}
}
}
/// Initializes the wallet with the provided daemon settings.
///
/// This method must be called after creating or opening a wallet to synchronize it
/// with the daemon and prepare it for operations like refreshing.
///
/// # Arguments
///
/// * `config` - An `WalletConfig` struct containing daemon settings.
///
/// # Example
///
/// ```rust
/// use monero_c_rust::{WalletManager, NetworkType, WalletConfig};
/// use tempfile::TempDir;
///
/// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
/// let wallet_path = temp_dir.path().join("test_wallet");
/// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
/// let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
/// .expect("Failed to create wallet");
///
/// let config = WalletConfig {
/// daemon_address: "http://localhost:18081".to_string(),
/// upper_transaction_size_limit: 10000,
/// daemon_username: "user".to_string(),
/// daemon_password: "pass".to_string(),
/// use_ssl: false,
/// light_wallet: false,
/// proxy_address: "".to_string(),
/// };
///
/// let init_result = wallet.init(config);
/// assert!(init_result.is_ok(), "Failed to initialize wallet: {:?}", init_result.err());
/// ```
pub fn init(&self, config: WalletConfig) -> WalletResult<()> {
let c_daemon_address = CString::new(config.daemon_address)
.map_err(|_| WalletError::FfiError("Invalid daemon address".to_string()))?;
let c_daemon_username = CString::new(config.daemon_username)
.map_err(|_| WalletError::FfiError("Invalid daemon username".to_string()))?;
let c_daemon_password = CString::new(config.daemon_password)
.map_err(|_| WalletError::FfiError("Invalid daemon password".to_string()))?;
let c_proxy_address = CString::new(config.proxy_address)
.map_err(|_| WalletError::FfiError("Invalid proxy address".to_string()))?;
unsafe {
let result = bindings::MONERO_Wallet_init(
self.ptr.as_ptr(),
c_daemon_address.as_ptr(),
config.upper_transaction_size_limit,
c_daemon_username.as_ptr(),
c_daemon_password.as_ptr(),
config.use_ssl,
config.light_wallet,
c_proxy_address.as_ptr(),
);
if result {
Ok(())
} else {
// Retrieve the last error from the wallet
Err(self.get_last_error())
}
}
}
/// Refreshes the wallet's state by synchronizing it with the blockchain.
///
/// This method communicates with the connected daemon to update the wallet's
/// balance, transaction history, and other relevant data. It ensures that the
/// wallet remains up-to-date with the latest blockchain state.
///
/// # Example
///
/// ```rust
/// use monero_c_rust::{WalletManager, NetworkType, WalletConfig};
/// use std::fs;
/// use tempfile::TempDir;
///
/// fn main() {
/// // Create a temporary directory for testing purposes.
/// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
/// let wallet_path = temp_dir.path().join("test_wallet");
/// let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
///
/// // Initialize the WalletManager.
/// let manager = WalletManager::new().expect("Failed to create WalletManager");
///
/// // Create a new wallet.
/// let wallet = manager
/// .create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
/// .expect("Failed to create wallet");
///
/// // Define the wallet initialization configuration.
/// let config = WalletConfig {
/// daemon_address: "http://localhost:18081".to_string(),
/// upper_transaction_size_limit: 10000,
/// daemon_username: "user".to_string(),
/// daemon_password: "pass".to_string(),
/// use_ssl: false,
/// light_wallet: false,
/// proxy_address: "".to_string(),
/// };
///
/// // Initialize the wallet with the specified configuration.
/// let init_result = wallet.init(config);
/// assert!(init_result.is_ok(), "Failed to initialize wallet: {:?}", init_result.err());
///
/// // Perform a refresh operation after initialization.
/// let refresh_result = wallet.refresh();
/// assert!(refresh_result.is_ok(), "Failed to refresh wallet: {:?}", refresh_result.err());
///
/// // Optionally, you can verify the refresh by checking the blockchain height or other metrics.
/// // For example:
/// let height = manager.get_height().expect("Failed to get blockchain height");
/// println!("Current blockchain height: {}", height);
///
/// // 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 refresh(&self) -> WalletResult<Refreshed> {
unsafe {
let result = bindings::MONERO_Wallet_refresh(self.ptr.as_ptr());
if result {
Ok(Refreshed)
} else {
// Retrieve the last error from the wallet
Err(self.get_last_error())
}
}
}
/// Initiates a transfer from the wallet to the specified destinations.
///
/// # Arguments
///
/// * `account_index` - The index of the account to send funds from.
/// * `destinations` - A vector of `Destination` specifying where to send funds and how much.
/// * `get_tx_key` - A boolean indicating whether to retrieve the transaction key.
///
/// # Returns
///
/// * `WalletResult<Transfer>` - On success, returns a `Transfer` struct containing transaction details.
/// On failure, returns a `WalletError`.
pub fn transfer(&self, account_index: u32, destinations: Vec<Destination>, get_tx_key: bool, sweep_all: bool) -> WalletResult<Transfer> {
// Define separators
let separator = ";";
let separator_c = CString::new(separator).map_err(|_| WalletError::FfiError("Invalid separator".to_string()))?;
// Concatenate destination addresses and amounts.
let addresses: Vec<String> = destinations.iter().map(|d| d.address.clone()).collect();
let address_list = addresses.join(separator);
let c_address_list = CString::new(address_list).map_err(|_| WalletError::FfiError("Invalid address list".to_string()))?;
let amounts: Vec<String> = destinations.iter().map(|d| d.amount.to_string()).collect();
let amount_list = amounts.join(separator);
let c_amount_list = CString::new(amount_list).map_err(|_| WalletError::FfiError("Invalid amount list".to_string()))?;
// TODO: Payment IDs.
let payment_id = CString::new("").map_err(|_| WalletError::FfiError("Invalid payment_id".to_string()))?;
let mixin_count = 16;
// Pending transaction priority - default to 0 (Default)
let pending_tx_priority = bindings::Priority_Default;
// Subaddress account
let subaddr_account = account_index;
// TODO: Preferred inputs.
let c_preferred_inputs = CString::new("").map_err(|_| WalletError::FfiError("Invalid preferred inputs".to_string()))?;
// Separator for preferred inputs
let preferred_inputs_separator = CString::new("").map_err(|_| WalletError::FfiError("Invalid preferred inputs separator".to_string()))?;
unsafe {
// Create the transaction with multiple destinations.
let tx_ptr = bindings::MONERO_Wallet_createTransactionMultDest(
self.ptr.as_ptr(),
c_address_list.as_ptr(),
separator_c.as_ptr(),
payment_id.as_ptr(),
sweep_all,
c_amount_list.as_ptr(),
separator_c.as_ptr(),
mixin_count,
pending_tx_priority,
subaddr_account,
c_preferred_inputs.as_ptr(),
preferred_inputs_separator.as_ptr(),
);
// Check for errors.
let ptr_as_mut_c_void = self.manager.ptr.as_ptr() as *mut c_void;
self.manager.throw_if_error(ptr_as_mut_c_void)?;
if tx_ptr.is_null() {
return Err(WalletError::NullPointer);
}
// Get the transaction ID.
let txid_ptr = bindings::MONERO_PendingTransaction_txid(tx_ptr, separator_c.as_ptr());
if txid_ptr.is_null() {
return Err(WalletError::FfiError("Failed to get transaction ID".to_string()));
}
let txid = CStr::from_ptr(txid_ptr).to_string_lossy().into_owned();
// Get the fee.
let fee = bindings::MONERO_PendingTransaction_fee(tx_ptr);
// Optionally get the transaction key.
let tx_key = if get_tx_key {
let c_txid = CString::new(txid.clone()).map_err(|_| WalletError::FfiError("Invalid txid".to_string()))?;
let tx_key_ptr = bindings::MONERO_Wallet_getTxKey(self.ptr.as_ptr(), c_txid.as_ptr());
if tx_key_ptr.is_null() {
None
} else {
Some(CStr::from_ptr(tx_key_ptr).to_string_lossy().into_owned())
}
} else {
None
};
// Submit the transaction.
//
// TODO: Make submission optional.
let tx_ptr_as_i8 = tx_ptr as *const i8;
let submit_result = bindings::MONERO_Wallet_submitTransaction(
self.ptr.as_ptr(),
tx_ptr_as_i8,
);
if !submit_result {
return Err(WalletError::FfiError("Failed to submit transaction".to_string()));
}
Ok(Transfer {
txid,
tx_key,
amount: destinations.iter().map(|d| d.amount).sum(),
fee,
})
}
}
// TODO docs.
pub fn sweep_all(&self, account_index: u32, destination: Destination, get_tx_key: bool) -> WalletResult<Transfer> {
// Convert the destination address to a CString.
let c_address = CString::new(destination.address.clone()).map_err(|_| WalletError::FfiError("Invalid address".to_string()))?;
// Placeholder values for fields not needed in sweep_all.
let empty_separator = CString::new("").map_err(|_| WalletError::FfiError("Invalid separator".to_string()))?;
let payment_id = CString::new("").map_err(|_| WalletError::FfiError("Invalid payment_id".to_string()))?;
let mixin_count = 16;
let pending_tx_priority = bindings::Priority_Default;
let c_preferred_inputs = CString::new("").map_err(|_| WalletError::FfiError("Invalid preferred inputs".to_string()))?;
let preferred_inputs_separator = CString::new("").map_err(|_| WalletError::FfiError("Invalid preferred inputs separator".to_string()))?;
unsafe {
// Create the sweep transaction.
let tx_ptr = bindings::MONERO_Wallet_createTransactionMultDest(
self.ptr.as_ptr(),
c_address.as_ptr(),
empty_separator.as_ptr(),
payment_id.as_ptr(),
true, // Sweep all funds.
empty_separator.as_ptr(),
empty_separator.as_ptr(),
mixin_count,
pending_tx_priority,
account_index,
c_preferred_inputs.as_ptr(),
preferred_inputs_separator.as_ptr(),
);
// Check for errors.
let ptr_as_mut_c_void = self.manager.ptr.as_ptr() as *mut c_void;
self.manager.throw_if_error(ptr_as_mut_c_void)?;
if tx_ptr.is_null() {
return Err(WalletError::NullPointer);
}
// Get the transaction ID.
let txid_ptr = bindings::MONERO_PendingTransaction_txid(tx_ptr, empty_separator.as_ptr());
if txid_ptr.is_null() {
return Err(WalletError::FfiError("Failed to get transaction ID".to_string()));
}
let txid = CStr::from_ptr(txid_ptr).to_string_lossy().into_owned();
// Get the fee.
let fee = bindings::MONERO_PendingTransaction_fee(tx_ptr);
// Optionally get the transaction key.
let tx_key = if get_tx_key {
let c_txid = CString::new(txid.clone()).map_err(|_| WalletError::FfiError("Invalid txid".to_string()))?;
let tx_key_ptr = bindings::MONERO_Wallet_getTxKey(self.ptr.as_ptr(), c_txid.as_ptr());
if tx_key_ptr.is_null() {
None
} else {
Some(CStr::from_ptr(tx_key_ptr).to_string_lossy().into_owned())
}
} else {
None
};
// Submit the transaction.
//
// TODO: Make submission optional.
let tx_ptr_as_i8 = tx_ptr as *const i8;
let submit_result = bindings::MONERO_Wallet_submitTransaction(
self.ptr.as_ptr(),
tx_ptr_as_i8,
);
if !submit_result {
return Err(WalletError::FfiError("Failed to submit sweep transaction".to_string()));
}
Ok(Transfer {
txid,
tx_key,
amount: 0, // Since it's sweeping all, amount is not predefined.
fee,
})
}
}
}
#[derive(Debug)]
pub struct GetBalance {
pub balance: u64,
pub unlocked_balance: u64,
}
impl Drop for Wallet {
fn drop(&mut self) {
if !self.is_closed {
let _ = self.close_wallet();
}
}
}
#[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 = &["test_wallet", "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("test_wallet");
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("test_wallet");
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("test_wallet");
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("test_wallet");
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("test_wallet");
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_generate_from_keys_unit() {
println!("Running unit test: test_generate_from_keys_unit");
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("generated_wallet_unit");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
// Test parameters.
//
// TODO add functions to get spend and view keys.
let address = "45wsWad9EwZgF3VpxQumrUCRaEtdyyh6NG8sVD3YRVVJbK1jkpJ3zq8WHLijVzodQ22LxwkdWx7fS2a6JzaRGzkNU8K2Dhi";
let spendkey = "29adefc8f67515b4b4bf48031780ab9d071d24f8a674b879ce7f245c37523807";
let viewkey = "3bc0b202cde92fe5719c3cc0a16aa94f88a5d19f8c515d4e35fae361f6f2120e";
let restore_height = 0;
let password = "password";
let language = "English";
let network_type = NetworkType::Mainnet;
let kdf_rounds = 1;
let result = manager.generate_from_keys(
wallet_str.to_string(),
address.to_string(),
spendkey.to_string(),
viewkey.to_string(),
restore_height,
password.to_string(),
language.to_string(),
network_type,
kdf_rounds,
);
assert!(result.is_ok(), "Failed to generate wallet from keys: {:?}", result.err());
// Clean up wallet files.
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("test_wallet");
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("test_wallet");
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("test_wallet");
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("test_wallet");
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");
}
#[test]
fn test_create_account() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
// Create a wallet.
let wallet = manager
.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
// Create a new account.
let result = wallet.create_account("Test Account");
assert!(result.is_ok(), "Failed to create account: {:?}", result.err());
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_get_accounts() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("test_wallet");
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");
// Add two accounts for testing
wallet.create_account("Test Account 1").expect("Failed to create account 1");
wallet.create_account("Test Account 2").expect("Failed to create account 2");
// Retrieve all accounts
let accounts = wallet.get_accounts().expect("Failed to retrieve accounts");
assert_eq!(accounts.accounts.len(), 3); // Including the primary account
// Check account names
assert_eq!(accounts.accounts[0].label, "Primary account");
assert_eq!(accounts.accounts[1].label, "Test Account 1");
assert_eq!(accounts.accounts[2].label, "Test Account 2");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_close_wallet() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
// Create a wallet.
let mut wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
// Close the wallet.
let close_result = wallet.close_wallet();
assert!(close_result.is_ok(), "Failed to close wallet: {:?}", close_result.err());
// Attempt to close the wallet again.
let close_again_result = wallet.close_wallet();
assert!(close_again_result.is_ok(), "Failed to close wallet a second time: {:?}", close_again_result.err());
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_height_success() {
let manager = WalletManager::new().unwrap();
let height = manager.get_height().unwrap();
// assert!(height > 0, "Blockchain height should be greater than 0");
// The test should not assume network connectivity/any syncing progress, so:
assert!(height == 0, "Blockchain height should be equal to 0");
}
}
#[test]
fn test_init_success() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
// Create a wallet.
let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
// Define initialization configuration.
let config = WalletConfig {
daemon_address: "http://localhost:18081".to_string(),
upper_transaction_size_limit: 10000,
daemon_username: "user".to_string(),
daemon_password: "pass".to_string(),
use_ssl: false,
light_wallet: false,
proxy_address: "".to_string(),
};
// Initialize the wallet.
let init_result = wallet.init(config);
assert!(init_result.is_ok(), "Failed to initialize wallet: {:?}", init_result.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");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_refresh_success() {
println!("Running test_refresh_success");
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
// Construct the full path for the wallet within temp_dir.
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
// Create the wallet.
let wallet = manager
.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
println!("Wallet created successfully.");
// Define initialization configuration.
let config = WalletConfig {
daemon_address: "http://localhost:18081".to_string(),
upper_transaction_size_limit: 10000,
daemon_username: "user".to_string(),
daemon_password: "pass".to_string(),
use_ssl: false,
light_wallet: false,
proxy_address: "".to_string(),
};
// Perform the initialization.
println!("Initializing the wallet...");
let init_result = wallet.init(config);
assert!(init_result.is_ok(), "Failed to initialize wallet: {:?}", init_result.err());
// Perform a refresh operation after initialization.
println!("Refreshing the wallet...");
let refresh_result = wallet.refresh();
assert!(refresh_result.is_ok(), "Failed to refresh wallet: {:?}", refresh_result.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");
teardown(&temp_dir).expect("Failed to clean up after test");
}
|