forked from btcpay-monero/monero-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneroWalletRpcIntegrationTest.cs
More file actions
850 lines (728 loc) · 31.1 KB
/
Copy pathMoneroWalletRpcIntegrationTest.cs
File metadata and controls
850 lines (728 loc) · 31.1 KB
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
using Monero.Common;
using Monero.Daemon;
using Monero.IntegrationTests.Utils;
using Monero.Wallet;
using Monero.Wallet.Common;
using Xunit;
namespace Monero.IntegrationTests;
public class MoneroWalletRpcIntegrationTest
{
private readonly MoneroDaemonRpc _daemon = TestUtils.GetDaemonRpc(); // daemon instance to test
private readonly MoneroWalletRpc _wallet = TestUtils.GetWalletRpc().Result; // wallet instance to test
private async Task<IMoneroWallet> OpenWallet(MoneroWalletConfig? config)
{
// assign defaults
if (config == null)
{
config = new MoneroWalletConfig();
}
if (config.GetPassword() == null)
{
config.SetPassword(TestUtils.WalletPassword);
}
if (config.GetServer() == null)
{
config.SetServer(_daemon.GetRpcConnection());
}
// create a client connected to an internal monero-wallet-rpc process
MoneroWalletRpc moneroWalletRpc = await TestUtils.GetCreateWallet();
// open wallet
await moneroWalletRpc.OpenWallet(config);
await moneroWalletRpc.SetDaemonConnection(await moneroWalletRpc.GetDaemonConnection(), true, null);
if (await moneroWalletRpc.IsConnectedToDaemon())
{
await moneroWalletRpc.StartSyncing((ulong)TestUtils.SyncPeriodInMs);
}
return moneroWalletRpc;
}
private async Task<IMoneroWallet> CreateWallet(MoneroWalletConfig? config)
{
// assign defaults
if (config == null)
{
config = new MoneroWalletConfig();
}
bool random = config.GetSeed() == null && config.GetPrimaryAddress() == null;
if (config.GetPath() == null)
{
config.SetPath(GenUtils.GetGuid());
}
if (config.GetPassword() == null)
{
config.SetPassword(TestUtils.WalletPassword);
}
if (config.GetRestoreHeight() == null && !random)
{
config.SetRestoreHeight(0);
}
if (config.GetServer() == null)
{
config.SetServer(_daemon.GetRpcConnection());
}
// create a client connected to xmr_wallet_2 container
MoneroWalletRpc walletRpc = await TestUtils.GetCreateWallet();
// create wallet
try
{
await walletRpc.CreateWallet(config);
await walletRpc.SetDaemonConnection(await walletRpc.GetDaemonConnection(), true,
null); // set daemon as trusted
if (await walletRpc.IsConnectedToDaemon())
{
await walletRpc.StartSyncing(TestUtils.SyncPeriodInMs);
}
return walletRpc;
}
catch (MoneroError e)
{
if (!e.Message.ToLower().Contains("already exists"))
{
try { await CloseWallet(walletRpc, false); }
catch (Exception e2)
{
throw new Exception("An error occurred while stopping monero wallet rpc process", e2);
}
}
throw;
}
}
private static async Task CloseWallet(IMoneroWallet walletInstance) { await CloseWallet(walletInstance, false); }
private static async Task CloseWallet(IMoneroWallet walletInstance, bool save)
{
MoneroWalletRpc walletRpc = (MoneroWalletRpc)walletInstance;
string walletPath = await walletRpc.GetPath();
if (string.IsNullOrEmpty(walletPath))
{
return;
}
await walletRpc.Close(save);
}
private async Task TestWallet(Func<Task> action, IMoneroWallet moneroWallet)
{
await TestWallet(action, moneroWallet, false);
}
private static async Task TestWallet(Func<Task> action, IMoneroWallet moneroWallet, bool checkSeed)
{
await action();
if (checkSeed)
{
MoneroUtils.ValidateMnemonic(await moneroWallet.GetSeed());
}
}
// Can create a random wallet
[Fact]
public async Task TestCreateWalletRandom()
{
try
{
// create a random wallet
IMoneroWallet moneroWallet = await CreateWallet(new MoneroWalletConfig());
string path = await moneroWallet.GetPath();
await TestWallet(async () =>
{
MoneroUtils.ValidatePrivateViewKey(await moneroWallet.GetPrivateViewKey());
MoneroUtils.ValidatePrivateSpendKey(await moneroWallet.GetPrivateSpendKey());
MoneroUtils.ValidateMnemonic(await moneroWallet.GetSeed());
}, moneroWallet);
// attempt to create a wallet at the same path
try
{
await CreateWallet(new MoneroWalletConfig().SetPath(path));
throw new Exception("Should have thrown error");
}
catch (Exception e)
{
Assert.True("Wallet already exists: " + path == e.Message);
}
// attempt to create wallet with unknown language
try
{
await CreateWallet(new MoneroWalletConfig().SetLanguage("english")); // TODO: support lowercase?
throw new Exception("Should have thrown error");
}
catch (Exception e)
{
Assert.Equal("Unknown language: english", e.Message);
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
// Can create a wallet from a seed.
[Fact]
public async Task TestCreateWalletFromSeed()
{
// save for comparison
string primaryAddress = await _wallet.GetPrimaryAddress();
string privateViewKey = await _wallet.GetPrivateViewKey();
string privateSpendKey = await _wallet.GetPrivateSpendKey();
// recreate the test wallet from seed
IMoneroWallet moneroWallet = await CreateWallet(new MoneroWalletConfig().SetSeed(TestUtils.Seed)
.SetRestoreHeight(TestUtils.FirstReceiveHeight));
string path = await moneroWallet.GetPath();
await TestWallet(async () =>
{
Assert.True(primaryAddress == await moneroWallet.GetPrimaryAddress());
Assert.True(privateViewKey == await moneroWallet.GetPrivateViewKey());
Assert.True(privateSpendKey == await moneroWallet.GetPrivateSpendKey());
Assert.True(TestUtils.Seed == await moneroWallet.GetSeed());
}, moneroWallet);
// attempt to create a wallet with two missing words
try
{
const string invalidMnemonic = "memoir desk algebra inbound innocent unplugs fully okay five inflamed giant factual ritual toyed topic snake unhappy guarded tweezers haunted inundate giant";
await CreateWallet(new MoneroWalletConfig().SetSeed(invalidMnemonic)
.SetRestoreHeight(TestUtils.FirstReceiveHeight));
}
catch (Exception e)
{
Assert.Equal("Invalid mnemonic", e.Message);
}
await AttemptToCreateWalletAtSamePath(path);
}
private async Task AttemptToCreateWalletAtSamePath(string path)
{
try
{
await CreateWallet(new MoneroWalletConfig().SetPath(path));
throw new Exception("Should have thrown error");
}
catch (Exception e)
{
Assert.Contains("already exists", e.Message.ToLower());
}
}
// Can create a wallet from a seed with a seed offset
[Fact]
public async Task TestCreateWalletFromSeedWithOffset()
{
try
{
// create a test wallet with offset
IMoneroWallet moneroWallet = await CreateWallet(new MoneroWalletConfig().SetSeed(TestUtils.Seed)
.SetRestoreHeight(TestUtils.FirstReceiveHeight).SetSeedOffset("my secret offset!"));
await TestWallet(async () =>
{
MoneroUtils.ValidateMnemonic(await moneroWallet.GetSeed());
Assert.True(TestUtils.Seed != await moneroWallet.GetSeed());
Assert.True(TestUtils.Address != await moneroWallet.GetPrimaryAddress());
}, moneroWallet);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
// Can create a wallet from keys
[Fact]
public async Task TestCreateWalletFromKeys()
{
// save for comparison
string primaryAddress = await _wallet.GetPrimaryAddress();
string privateViewKey = await _wallet.GetPrivateViewKey();
string privateSpendKey = await _wallet.GetPrivateSpendKey();
// recreate the test wallet from keys
IMoneroWallet moneroWallet = await CreateWallet(new MoneroWalletConfig().SetPrimaryAddress(primaryAddress)
.SetPrivateViewKey(privateViewKey).SetPrivateSpendKey(privateSpendKey)
.SetRestoreHeight(await _daemon.GetHeight()));
string path = await moneroWallet.GetPath();
Func<Task> action = async () =>
{
Assert.True(primaryAddress == await moneroWallet.GetPrimaryAddress());
Assert.True(privateViewKey == await moneroWallet.GetPrivateViewKey());
Assert.True(privateSpendKey == await moneroWallet.GetPrivateSpendKey());
if (TestUtils.TestsInContainer)
{
Assert.True(await moneroWallet.IsConnectedToDaemon(),
"Wallet created from keys is not connected to authenticated daemon");
}
};
await TestWallet(action, moneroWallet, false);
await AttemptToCreateWalletAtSamePath(path);
}
// Can get the wallet's version
[Fact]
public async Task TestGetVersion()
{
MoneroVersion version = await _wallet.GetVersion();
Assert.NotNull(version.Number);
Assert.True(version.Number > 0);
Assert.NotNull(version.IsRelease);
}
// Can get the wallet's path
[Fact]
public async Task TestGetPath()
{
// create a random wallet
IMoneroWallet moneroWallet = await CreateWallet(new MoneroWalletConfig());
// set a random attribute
string uuid = Guid.NewGuid().ToString();
await moneroWallet.SetAttribute("uuid", uuid);
// record the wallet's path, then save and close
string path = await moneroWallet.GetPath();
await CloseWallet(moneroWallet, true);
// re-open the wallet using its path
moneroWallet = await OpenWallet(new MoneroWalletConfig().SetPath(path));
// test the attribute
Assert.True(uuid == await moneroWallet.GetAttribute("uuid"));
await CloseWallet(moneroWallet);
}
// Can get the seed
[Fact]
public async Task TestGetSeed()
{
string seed = await _wallet.GetSeed();
MoneroUtils.ValidateMnemonic(seed);
Assert.True(TestUtils.Seed == seed);
}
// Can get the language of the seed
[Fact(Skip = "monero-wallet-rpc does not support getting seed language")]
public async Task TestGetSeedLanguage()
{
string language = await _wallet.GetSeedLanguage();
Assert.True(IMoneroWallet.DefaultLanguage == language);
}
// Can get the private view key
[Fact]
public async Task TestGetPrivateViewKey()
{
string privateViewKey = await _wallet.GetPrivateViewKey();
MoneroUtils.ValidatePrivateViewKey(privateViewKey);
}
// Can get the private spent key
[Fact]
public async Task TestGetPrivateSpendKey()
{
string privateSpendKey = await _wallet.GetPrivateSpendKey();
MoneroUtils.ValidatePrivateSpendKey(privateSpendKey);
}
// Can get the public view key
[Fact(Skip = "Enable after monero-project fix (https://github.com/monero-project/monero/pull/9364)")]
public async Task TestGetPublicViewKey()
{
string publicViewKey = await _wallet.GetPublicViewKey();
MoneroUtils.ValidatePrivateSpendKey(publicViewKey);
}
// Can get the public view key
[Fact(Skip = "Enable after monero-project fix (https://github.com/monero-project/monero/pull/9364)")]
public async Task TestGetPublicSpendKey()
{
string publicSpendKey = await _wallet.GetPublicSpendKey();
MoneroUtils.ValidatePrivateSpendKey(publicSpendKey);
}
// Can get the primary address
[Fact]
public async Task TestGetPrimaryAddress()
{
string primaryAddress = await _wallet.GetPrimaryAddress();
Assert.True(await _wallet.GetAddress(0, 0) == primaryAddress);
}
// Can get the address of a subaddress at a specified account and subaddress index
[Fact]
public async Task TestGetSubaddressAddress()
{
Assert.True(await _wallet.GetPrimaryAddress() == (await _wallet.GetSubaddress(0, 0)).GetAddress());
foreach (MoneroAccount account in await _wallet.GetAccounts(true, true, null))
{
foreach (MoneroSubaddress subaddress in account.Subaddresses!)
{
Assert.True(subaddress.GetAddress() ==
await _wallet.GetAddress((uint)account.AccountIndex!, (uint)subaddress.Index!));
}
}
}
// Can get addresses out of range of used accounts and subaddresses
[Fact]
public async Task TestGetSubaddressAddressOutOfRange()
{
List<MoneroAccount> accounts = await _wallet.GetAccounts(true, true, null);
int accountIdx = accounts.Count - 1;
MoneroAccount account = accounts[accountIdx];
List<MoneroSubaddress>? subaddresses = account.Subaddresses;
if (subaddresses == null)
{
throw new MoneroError("Subaddresses is null");
}
int subaddressIdx = subaddresses.Count;
string? address = await _wallet.GetAddress((uint)accountIdx, (uint)subaddressIdx);
Assert.Null(address);
}
// Can get the current height that the wallet is synchronized to
[Fact]
public async Task TestGetHeight()
{
ulong lastHeight = await _wallet.GetHeight();
await _daemon.WaitForNextBlockHeader();
const int maxRetries = 15;
const int delayMs = 1000;
bool blockFetched = false;
ulong currentHeight = lastHeight;
for (int i = 0; i < maxRetries; i++)
{
var syncResult = await _wallet.Sync(null, null);
currentHeight = await _wallet.GetHeight();
if (syncResult.NumBlocksFetched > 0 && currentHeight > lastHeight)
{
blockFetched = true;
break;
}
await GenUtils.WaitForAsync(delayMs, Xunit.TestContext.Current.CancellationToken);
}
Assert.True(blockFetched, $"No blocks fetched after {maxRetries}s. Last height: {lastHeight}, current height: {currentHeight}");
Assert.True(currentHeight > lastHeight, $"Expected currentHeight: {currentHeight} > lastHeight: {lastHeight}");
}
// Can create a new account without a label
[Fact]
public async Task TestCreateAccountWithoutLabel()
{
List<MoneroAccount> accountsBefore = await _wallet.GetAccounts(false, true, null);
MoneroAccount createdAccount = await _wallet.CreateAccount(null);
TestAccount(createdAccount);
Assert.Equal(accountsBefore.Count, (await _wallet.GetAccounts(false, true, null)).Count - 1);
}
// Can get accounts without subaddresses
[Fact]
public async Task TestGetAccountsWithoutSubaddresses()
{
List<MoneroAccount> accounts = await _wallet.GetAccounts(false, true, null);
Assert.NotEmpty(accounts);
foreach (MoneroAccount account in accounts)
{
TestAccount(account);
Assert.Null(account.Subaddresses);
}
}
// Can get accounts with subaddresses
[Fact]
public async Task TestGetAccountsWithSubaddresses()
{
List<MoneroAccount> accounts = await _wallet.GetAccounts(true, false, null);
Assert.NotEmpty(accounts);
foreach (MoneroAccount account in accounts)
{
TestAccount(account);
List<MoneroSubaddress> subaddresses = account.Subaddresses ?? [];
Assert.NotEmpty(subaddresses);
}
}
// Can set account labels
[Fact]
public async Task TestSetAccountLabel()
{
// create an account
if ((await _wallet.GetAccounts(false, true, null)).Count < 2)
{
// create an account
if ((await _wallet.GetAccounts(false, true, null)).Count < 2)
{
await _wallet.CreateAccount(null);
}
}
// set account label
string label = GenUtils.GetGuid();
await _wallet.SetAccountLabel(1, 0, label);
Assert.Equal(label, (await _wallet.GetSubaddress(1, 0)).GetLabel());
}
// Can create an address with and without a label
[Fact]
public async Task TestCreateAddress()
{
// create addresses across accounts
List<MoneroAccount> accounts = await _wallet.GetAccounts(true, false, null);
if (accounts.Count < 2)
{
await _wallet.CreateAccount(null);
}
accounts = await _wallet.GetAccounts(true, false, null);
Assert.True(accounts.Count > 1);
for (uint accountIdx = 0; accountIdx < 2; accountIdx++)
{
// create an address with no label
List<MoneroSubaddress> subaddresses = await _wallet.GetSubaddresses(accountIdx, false, null);
MoneroSubaddress subaddress = (await _wallet.CreateAddress(accountIdx, null))[0];
Assert.Equal("", subaddress.GetLabel());
TestSubaddress(subaddress);
List<MoneroSubaddress> subaddressesNew = await _wallet.GetSubaddresses(accountIdx, false, null);
Assert.Equal(subaddressesNew.Count - 1, subaddresses.Count);
Assert.True(subaddress.Equals(subaddressesNew[subaddressesNew.Count - 1]));
// create an address with label
subaddresses = await _wallet.GetSubaddresses(accountIdx, false, null);
string uuid = GenUtils.GetGuid();
subaddress = (await _wallet.CreateAddress(accountIdx, uuid))[0];
Assert.Equal(uuid, subaddress.GetLabel());
TestSubaddress(subaddress);
subaddressesNew = await _wallet.GetSubaddresses(accountIdx, false, null);
Assert.Equal(subaddresses.Count, subaddressesNew.Count - 1);
Assert.True(subaddress.Equals(subaddressesNew[subaddressesNew.Count - 1]));
}
}
// Can create multiple subaddresses at once
[Fact]
public async Task TestCreateAddressMultiple()
{
const uint count = 3;
List<MoneroSubaddress> subaddressesBeforeCreate = await _wallet.GetSubaddresses(0, false, null);
List<MoneroSubaddress> created = await _wallet.CreateAddress(0, null, count);
Assert.Equal((int)count, created.Count);
foreach (MoneroSubaddress subaddress in created)
{
var address = subaddress.GetAddress();
Assert.Equal(0u, subaddress.AccountIndex);
Assert.NotNull(address);
Assert.NotEmpty(address);
TestSubaddress(subaddress);
}
List<MoneroSubaddress> subaddressesAfterCreate = await _wallet.GetSubaddresses(0, false, null);
Assert.Equal(subaddressesBeforeCreate.Count + count, subaddressesAfterCreate.Count);
}
// Can get subaddresses at a specified account index
[Fact]
public async Task TestGetSubaddresses()
{
List<MoneroAccount> accounts = await _wallet.GetAccounts(true, false, null);
Assert.NotEmpty(accounts);
foreach (MoneroAccount account in accounts)
{
uint? accountIndex = account.AccountIndex;
if (accountIndex == null)
{
throw new MoneroError("Account index is null");
}
List<MoneroSubaddress> subaddresses = await _wallet.GetSubaddresses((uint)accountIndex, false, null);
Assert.NotEmpty(subaddresses);
foreach (MoneroSubaddress subaddress in subaddresses)
{
TestSubaddress(subaddress);
Assert.Equal(account.AccountIndex, subaddress.AccountIndex);
}
}
}
// Can get subaddresses at a specified account and subaddress indices
[Fact]
public async Task TestGetSubaddressesByIndices()
{
List<MoneroAccount> accounts = await _wallet.GetAccounts(false, true, null);
Assert.NotEmpty(accounts);
foreach (MoneroAccount account in accounts)
{
uint? accountIndex = account.AccountIndex;
if (accountIndex == null)
{
throw new MoneroError("Account index is null");
}
// get subaddresses
List<MoneroSubaddress> subaddresses = await _wallet.GetSubaddresses((uint)accountIndex, true, null);
Assert.True(subaddresses.Count > 0);
// remove a subaddress for a query if possible
if (subaddresses.Count > 1)
{
subaddresses.RemoveAt(0);
}
// get subaddress indices
List<uint> subaddressIndices = new();
foreach (MoneroSubaddress subaddress in subaddresses)
{
uint? subaddressIndex = subaddress.Index;
if (subaddressIndex == null)
{
throw new MoneroError("Subaddress index is null");
}
subaddressIndices.Add((uint)subaddressIndex);
}
Assert.True(subaddressIndices.Count > 0);
// fetch subaddresses by indices
List<MoneroSubaddress> fetchedSubaddresses =
await _wallet.GetSubaddresses((uint)accountIndex, true, subaddressIndices);
// original subaddresses (minus one removed if applicable) is equal to fetched subaddresses
int i = 0;
foreach (MoneroSubaddress subaddr in subaddresses)
{
Assert.True(subaddr.Equals(fetchedSubaddresses[i]));
i++;
}
}
}
// Can get a subaddress at a specified account and subaddress index
[Fact]
public async Task TestGetSubaddressByIndex()
{
List<MoneroAccount> accounts = await _wallet.GetAccounts(false, false, null);
Assert.True(accounts.Count > 0);
foreach (MoneroAccount account in accounts)
{
uint? accountIdx = account.AccountIndex;
if (accountIdx == null)
{
throw new MoneroError("Account index is null");
}
List<MoneroSubaddress> subaddresses = await _wallet.GetSubaddresses((uint)accountIdx, false, null);
Assert.True(subaddresses.Count > 0);
foreach (MoneroSubaddress subaddress in subaddresses)
{
TestSubaddress(subaddress);
uint? subaddressIdx = subaddress.Index;
if (subaddressIdx == null) { throw new MoneroError("Subaddress index is null"); }
Assert.True(subaddress.Equals(await _wallet.GetSubaddress((uint)accountIdx, (uint)subaddressIdx)));
Assert.True(subaddress.Equals(
(await _wallet.GetSubaddresses((uint)accountIdx, false, [(uint)subaddressIdx]))
.First())); // test a plural call with a single subaddress number
}
}
}
// Can set subaddress labels
[Fact]
public async Task TestSetSubaddressLabel()
{
// create subaddresses
while ((await _wallet.GetSubaddresses(0, true, null)).Count < 3)
{
await _wallet.CreateAddress(0, null);
}
uint subaddressesCount = (uint)(await _wallet.GetSubaddresses(0, true, null)).Count;
// set subaddress labels
for (uint subaddressIdx = 0; subaddressIdx < subaddressesCount; subaddressIdx++)
{
string label = GenUtils.GetGuid();
await _wallet.SetAccountLabel(0, subaddressIdx, label);
Assert.Equal(label, (await _wallet.GetSubaddress(0, subaddressIdx)).GetLabel());
}
}
// Can sync (without progress)
[Fact]
public async Task TestSyncWithoutProgress()
{
ulong numBlocks = 10;
ulong chainHeight = await _daemon.GetHeight();
Assert.True(chainHeight >= numBlocks);
MoneroSyncResponse response = await _wallet.Sync(chainHeight - numBlocks, null); // sync end of a chain
Assert.True(response.NumBlocksFetched >= 0);
Assert.NotNull(response.ReceivedMoney);
}
// Can get the locked and unlocked balances of the wallet, accounts, and subaddresses
[Fact]
public async Task TestGetAllBalances()
{
// fetch accounts with all info as reference
List<MoneroAccount> accounts = await _wallet.GetAccounts(true, false, null);
// test that balances add up between accounts and wallet
ulong accountsBalance = 0;
ulong accountsUnlockedBalance = 0;
foreach (MoneroAccount account in accounts)
{
accountsBalance += account.Balance;
accountsUnlockedBalance += account.UnlockedBalance;
// test that balances add up between subaddresses and accounts
ulong subaddressesBalance = 0;
ulong subaddressesUnlockedBalance = 0;
foreach (MoneroSubaddress subaddress in account.Subaddresses!)
{
subaddressesBalance += subaddress.GetBalance() ?? 0;
subaddressesUnlockedBalance += subaddress.GetUnlockedBalance() ?? 0;
// test that balances are consistent with getAccounts() call
Assert.Equal((await _wallet.GetBalance(subaddress.AccountIndex, subaddress.Index)).ToString(),
subaddress.GetBalance().ToString());
Assert.Equal(
(await _wallet.GetUnlockedBalance(subaddress.AccountIndex, subaddress.Index)).ToString(),
subaddress.GetUnlockedBalance().ToString());
}
Assert.Equal((await _wallet.GetBalance(account.AccountIndex, null)).ToString(),
subaddressesBalance.ToString());
Assert.Equal((await _wallet.GetUnlockedBalance(account.AccountIndex, null)).ToString(),
subaddressesUnlockedBalance.ToString());
}
TestUtils.TestUnsignedBigInteger(accountsBalance);
TestUtils.TestUnsignedBigInteger(accountsUnlockedBalance);
Assert.Equal((await _wallet.GetBalance(null, null)).ToString(), accountsBalance.ToString());
Assert.Equal((await _wallet.GetUnlockedBalance(null, null)).ToString(), accountsUnlockedBalance.ToString());
}
// Can save and close the wallet in a single call
[Fact]
public async Task TestSaveAndClose()
{
// create a random wallet
const string password = "";
IMoneroWallet moneroWallet = await CreateWallet(new MoneroWalletConfig().SetPassword(password));
string path = await moneroWallet.GetPath();
// set an attribute
string uuid = GenUtils.GetGuid();
await moneroWallet.SetAttribute("id", uuid);
// close the wallet without saving
await CloseWallet(moneroWallet);
// re-open the wallet and ensure the attribute was not saved
moneroWallet = await OpenWallet(new MoneroWalletConfig().SetPath(path).SetPassword(password));
Assert.Null(await moneroWallet.GetAttribute("id"));
// set the attribute and close with saving
await moneroWallet.SetAttribute("id", uuid);
await CloseWallet(moneroWallet, true);
// re-open the wallet and ensure the attribute was saved
moneroWallet = await OpenWallet(new MoneroWalletConfig().SetPath(path).SetPassword(password));
Assert.Equal(uuid, await moneroWallet.GetAttribute("id"));
await CloseWallet(moneroWallet);
}
// Can validate address
[Fact]
public async Task TestValidateAddress()
{
// mainnet primary and subaddress validation
Assert.True(await _wallet.IsValidAddress("42U9v3qs5CjZEePHBZHwuSckQXebuZu299NSmVEmQ41YJZQhKcPyujyMSzpDH4VMMVSBo3U3b54JaNvQLwAjqDhKS3rvM3L", false, true));
Assert.True(await _wallet.IsValidAddress("891TQPrWshJVpnBR4ZMhHiHpLx1PUnMqa3ccV5TJFBbqcJa3DWhjBh2QByCv3Su7WDPTGMHmCKkiVFN2fyGJKwbM1t6G7Ea", true, true));
// testnet primary and subaddress validation
Assert.True(await _wallet.IsValidAddress("9tUBnNCkC3UKGygHCwYvAB1FscpjUuq5e9MYJd2rXuiiTjjfVeSVjnbSG5VTnJgBgy9Y7GTLfxpZNMUwNZjGfdFr1z79eV1", false, false));
Assert.True(await _wallet.IsValidAddress("BgnKzHPJQDcg7xiP7bMN9MfPv9Z8ciT71iEMYnCdgBRBFETWgu9nKTr8fnzyGfU9h9gyNA8SFzYYzHfTS9KhqytSU943Nu1", true, false));
// stagenet primary and subaddress validation
Assert.True(await _wallet.IsValidAddress("5B8s3obCY2ETeQB3GNAGPK2zRGen5UeW1WzegSizVsmf6z5NvM2GLoN6zzk1vHyzGAAfA8pGhuYAeCFZjHAp59jRVQkunGS", false, true));
Assert.True(await _wallet.IsValidAddress("778B5D2JmMh5TJVWFbygJR15dvio5Z5B24hfSrWDzeroM8j8Lqc9sMoFE6324xg2ReaAZqHJkgfGFRugRmYHugHZ4f17Gxo", true, true));
}
#region Test Utils
private static void TestSubaddress(MoneroSubaddress? subaddress)
{
Assert.NotNull(subaddress);
Assert.True(subaddress.AccountIndex >= 0);
Assert.True(subaddress.Index >= 0);
Assert.NotNull(subaddress.GetAddress());
string? label = subaddress.GetLabel();
TestUtils.TestUnsignedBigInteger(subaddress.GetBalance());
TestUtils.TestUnsignedBigInteger(subaddress.GetUnlockedBalance());
Assert.True(subaddress.GetNumUnspentOutputs() >= 0);
Assert.NotNull(subaddress.IsUsed());
if (subaddress.GetBalance() > 0)
{
Assert.True(subaddress.IsUsed());
}
Assert.True(subaddress.GetNumBlocksToUnlock() >= 0);
}
private static void TestAccount(MoneroAccount? account)
{
// test account
Assert.NotNull(account);
uint? accountIndex = account.AccountIndex;
Assert.NotNull(accountIndex);
TestUtils.TestUnsignedBigInteger(account.Balance);
TestUtils.TestUnsignedBigInteger(account.UnlockedBalance);
// if given, test subaddresses and that their balances add up to account balances
if (account.Subaddresses != null)
{
ulong balance = 0;
ulong unlockedBalance = 0;
List<MoneroSubaddress> subaddresses = account.Subaddresses ?? [];
uint i = 0;
foreach (MoneroSubaddress subaddress in subaddresses)
{
TestSubaddress(subaddress);
Assert.Equal(accountIndex, subaddress.AccountIndex);
Assert.Equal(i, subaddress.Index);
balance += subaddress.GetBalance() ?? 0;
unlockedBalance += subaddress.GetUnlockedBalance() ?? 0;
i++;
}
Assert.True(balance.Equals(account.Balance),
"Subaddress balances " + balance + " != account " + accountIndex + " balance " + account.Balance);
Assert.True(unlockedBalance.Equals(account.UnlockedBalance),
"Subaddress unlocked balances " + unlockedBalance + " != account " + accountIndex +
" unlocked balance " + account.UnlockedBalance);
}
}
#endregion
}