-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathTestMoneroWalletFull.ts
1388 lines (1177 loc) · 64.2 KB
/
TestMoneroWalletFull.ts
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
import assert from "assert";
import TestUtils from "./utils/TestUtils";
import TestMoneroWalletCommon from "./TestMoneroWalletCommon";
import StartMining from "./utils/StartMining";
import WalletSyncPrinter from "./utils/WalletSyncPrinter";
import WalletEqualityUtils from "./utils/WalletEqualityUtils";
import {createWalletFull,
openWalletFull,
LibraryUtils,
MoneroWalletConfig,
GenUtils,
MoneroUtils,
MoneroNetworkType,
MoneroTxWallet,
MoneroOutputQuery,
MoneroOutputWallet,
MoneroRpcConnection,
MoneroWallet,
MoneroWalletFull,
MoneroWalletRpc} from "../../index";
/**
* Tests a Monero wallet using WebAssembly to bridge to monero-project's wallet2.
*/
export default class TestMoneroWalletFull extends TestMoneroWalletCommon {
static FULL_TESTS_RUN: boolean;
constructor(testConfig) {
super(testConfig);
}
async beforeAll() {
await super.beforeAll();
}
async beforeEach(currentTest) {
await super.beforeEach(currentTest);
}
async afterAll() {
await super.afterAll();
TestMoneroWalletFull.FULL_TESTS_RUN = true;
}
async afterEach(currentTest) {
await super.afterEach(currentTest);
// print memory usage
console.log("WASM memory usage: " + await LibraryUtils.getWasmMemoryUsed());
//console.log(process.memoryUsage());
// remove non-whitelisted wallets
let whitelist = [TestUtils.WALLET_NAME, "ground_truth", "moved"];
let items = await (await TestUtils.getDefaultFs()).readdir(TestUtils.TEST_WALLETS_DIR, "buffer");
for (let item of items) {
item = item + ""; // get filename as string
let found = false;
for (let whitelisted of whitelist) {
if (item === whitelisted || item === whitelisted + ".keys" || item === whitelisted + ".address.txt") {
found = true;
break;
}
}
if (!found) await (await TestUtils.getDefaultFs()).unlink(TestUtils.TEST_WALLETS_DIR + "/" + item);
}
}
async getTestWallet() {
return await TestUtils.getWalletFull();
}
async getTestDaemon() {
return await TestUtils.getDaemonRpc();
}
async openWallet(config: Partial<MoneroWalletConfig>, startSyncing?: any): Promise<MoneroWalletFull> {
// assign defaults
config = new MoneroWalletConfig(config);
if (config.getPassword() === undefined) config.setPassword(TestUtils.WALLET_PASSWORD);
if (config.getNetworkType() === undefined) config.setNetworkType(TestUtils.NETWORK_TYPE);
if (config.getProxyToWorker() === undefined) config.setProxyToWorker(TestUtils.PROXY_TO_WORKER);
if (config.getServer() === undefined && !config.getConnectionManager()) config.setServer(TestUtils.getDaemonRpcConnection());
if (config.getFs() === undefined) config.setFs(await TestUtils.getDefaultFs());
// open wallet
let wallet = await openWalletFull(config);
if (startSyncing !== false && await wallet.isConnectedToDaemon()) await wallet.startSyncing(TestUtils.SYNC_PERIOD_IN_MS);
return wallet;
}
async createWallet(config?: Partial<MoneroWalletConfig>, startSyncing?): Promise<MoneroWalletFull> {
// assign defaults
config = new MoneroWalletConfig(config);
let random = config.getSeed() === undefined && config.getPrimaryAddress() === undefined;
if (config.getPath() === undefined) config.setPath(TestUtils.TEST_WALLETS_DIR + "/" + GenUtils.getUUID());
if (config.getPassword() === undefined) config.setPassword(TestUtils.WALLET_PASSWORD);
if (config.getNetworkType() === undefined) config.setNetworkType(TestUtils.NETWORK_TYPE);
if (!config.getRestoreHeight() && !random) config.setRestoreHeight(0);
if (!config.getServer() && !config.getConnectionManager()) config.setServer(TestUtils.getDaemonRpcConnection());
if (config.getProxyToWorker() === undefined) config.setProxyToWorker(TestUtils.PROXY_TO_WORKER);
if (config.getFs() === undefined) config.setFs(await TestUtils.getDefaultFs());
// create wallet
let wallet = await createWalletFull(config);
if (!random) assert.equal(await wallet.getRestoreHeight(), config.getRestoreHeight() === undefined ? 0 : config.getRestoreHeight());
if (startSyncing !== false && await wallet.isConnectedToDaemon()) await wallet.startSyncing(TestUtils.SYNC_PERIOD_IN_MS);
return wallet;
}
async closeWallet(wallet, save?) {
await wallet.close(save);
}
async getSeedLanguages(): Promise<string[]> {
return await MoneroWalletFull.getSeedLanguages();
}
// ------------------------------- BEGIN TESTS ------------------------------
runTests() {
let that = this;
let testConfig = this.testConfig;
describe("TEST MONERO WALLET FULL", function() {
// register handlers to run before and after tests
before(async function() { await that.beforeAll(); });
beforeEach(async function() { await that.beforeEach(this.currentTest); });
after(async function() { await that.afterAll(); });
afterEach(async function() { await that.afterEach(this.currentTest); });
// run tests specific to full wallet
that.testWalletFull();
// run common tests
that.runCommonTests();
});
}
protected testWalletFull() {
let that = this;
let testConfig = this.testConfig;
describe("Tests specific to WebAssembly wallet", function() {
if (false && testConfig.testNonRelays)
it("Does not leak memory", async function() {
let restoreHeight = TestUtils.FIRST_RECEIVE_HEIGHT;
//let wallet = await that.createWallet({seed: TestUtils.SEED, restoreHeight: restoreHeight}, false);
for (let i = 0; i < 100; i++) {
console.log(process.memoryUsage());
await testSyncSeed(TestUtils.FIRST_RECEIVE_HEIGHT, undefined, false, true);
}
});
if (testConfig.testNonRelays)
it("Can get the daemon's height", async function() {
assert(await that.wallet.isConnectedToDaemon());
let daemonHeight = await that.wallet.getDaemonHeight();
assert(daemonHeight > 0);
});
if (testConfig.testNonRelays && !testConfig.liteMode)
it("Can open, sync, and close wallets repeatedly", async function() {
let wallets: MoneroWalletFull[] = [];
for (let i = 0; i < 4; i++) {
let wallet = await that.createWallet({seed: TestUtils.SEED, restoreHeight: TestUtils.FIRST_RECEIVE_HEIGHT});
await wallet.startSyncing();
wallets.push(wallet);
}
for (let wallet of wallets) await wallet.close();
});
if (testConfig.testNonRelays)
it("Can get the daemon's max peer height", async function() {
let height = await (that.wallet as MoneroWalletFull).getDaemonMaxPeerHeight();
assert(height > 0);
});
if (testConfig.testNonRelays)
it("Can create a random full wallet", async function() {
// create unconnected random wallet
let wallet = await that.createWallet({networkType: MoneroNetworkType.MAINNET, server: TestUtils.OFFLINE_SERVER_URI});
await MoneroUtils.validateMnemonic(await wallet.getSeed());
await MoneroUtils.validateAddress(await wallet.getPrimaryAddress(), MoneroNetworkType.MAINNET);
assert.equal(await wallet.getNetworkType(), MoneroNetworkType.MAINNET);
assert.deepEqual((await wallet.getDaemonConnection()).getConfig(), new MoneroRpcConnection(TestUtils.OFFLINE_SERVER_URI).getConfig());
assert(!(await wallet.isConnectedToDaemon()));
assert.equal(await wallet.getSeedLanguage(), "English");
assert(!(await wallet.isSynced()));
assert.equal(await wallet.getHeight(), 1); // TODO monero-project: why does height of new unsynced wallet start at 1?
assert(await wallet.getRestoreHeight() >= 0);
// cannot get daemon chain height
try {
await wallet.getDaemonHeight();
} catch (e: any) {
assert.equal(e.message, "Wallet is not connected to daemon");
}
// set daemon connection and check chain height
await wallet.setDaemonConnection(await that.daemon.getRpcConnection());
assert.equal(await wallet.getDaemonHeight(), await that.daemon.getHeight());
// close wallet which releases resources
await wallet.close();
// create random wallet with non defaults
wallet = await that.createWallet({networkType: MoneroNetworkType.TESTNET, language: "Spanish"}, false);
await MoneroUtils.validateMnemonic(await wallet.getSeed());
await MoneroUtils.validateAddress(await wallet.getPrimaryAddress(), MoneroNetworkType.TESTNET);
assert.equal(await wallet.getNetworkType(), await MoneroNetworkType.TESTNET);
assert(await wallet.getDaemonConnection());
assert((await that.daemon.getRpcConnection()) !== (await wallet.getDaemonConnection())); // not same reference
assert.equal((await wallet.getDaemonConnection()).getUri(), (await that.daemon.getRpcConnection()).getUri());
assert.equal((await wallet.getDaemonConnection()).getUsername(), (await that.daemon.getRpcConnection()).getUsername());
assert.equal((await wallet.getDaemonConnection()).getPassword(), (await that.daemon.getRpcConnection()).getPassword());
assert(await wallet.isConnectedToDaemon());
assert.equal(await wallet.getSeedLanguage(), "Spanish");
assert(!(await wallet.isSynced()));
assert.equal(await wallet.getHeight(), 1); // TODO monero-project: why is height of unsynced wallet 1?
if (await that.daemon.isConnected()) assert.equal(await wallet.getRestoreHeight(), await that.daemon.getHeight());
else assert(await wallet.getRestoreHeight() >= 0);
await wallet.close();
});
if (testConfig.testNonRelays)
it("Can create a full wallet from seed", async function() {
// create unconnected wallet with mnemonic
let wallet = await that.createWallet({seed: TestUtils.SEED, server: TestUtils.OFFLINE_SERVER_URI});
assert.equal(await wallet.getSeed(), TestUtils.SEED);
assert.equal(await wallet.getPrimaryAddress(), TestUtils.ADDRESS);
assert.equal(await wallet.getNetworkType(), TestUtils.NETWORK_TYPE);
assert.deepEqual((await wallet.getDaemonConnection()).getConfig(), new MoneroRpcConnection(TestUtils.OFFLINE_SERVER_URI).getConfig());
assert(!(await wallet.isConnectedToDaemon()));
assert.equal(await wallet.getSeedLanguage(), "English");
assert(!(await wallet.isSynced()));
assert.equal(await wallet.getHeight(), 1);
assert.equal(await wallet.getRestoreHeight(), 0);
try { await wallet.startSyncing(); } catch (e: any) { assert.equal(e.message, "Wallet is not connected to daemon"); }
await wallet.close();
// create wallet without restore height
wallet = await that.createWallet({seed: TestUtils.SEED}, false);
assert.equal(await wallet.getSeed(), TestUtils.SEED);
assert.equal(await wallet.getPrimaryAddress(), TestUtils.ADDRESS);
assert.equal(TestUtils.NETWORK_TYPE, await wallet.getNetworkType());
assert(await wallet.getDaemonConnection());
assert(await that.daemon.getRpcConnection() != await wallet.getDaemonConnection());
assert.equal((await wallet.getDaemonConnection()).getUri(), (await that.daemon.getRpcConnection()).getUri());
assert.equal((await wallet.getDaemonConnection()).getUsername(), (await that.daemon.getRpcConnection()).getUsername());
assert.equal((await wallet.getDaemonConnection()).getPassword(), (await that.daemon.getRpcConnection()).getPassword());
assert(await wallet.isConnectedToDaemon());
assert.equal(await wallet.getSeedLanguage(), "English");
assert(!(await wallet.isSynced()));
assert.equal(await wallet.getHeight(), 1); // TODO monero-project: why does height of new unsynced wallet start at 1?
assert.equal(await wallet.getRestoreHeight(), 0);
await wallet.close();
// create wallet with seed, no connection, and restore height
let restoreHeight = 10000;
wallet = await that.createWallet({seed: TestUtils.SEED, restoreHeight: restoreHeight, server: TestUtils.OFFLINE_SERVER_URI});
assert.equal(await wallet.getSeed(), TestUtils.SEED);
assert.equal(await wallet.getPrimaryAddress(), TestUtils.ADDRESS);
assert.equal(await wallet.getNetworkType(), TestUtils.NETWORK_TYPE);
assert.deepEqual((await wallet.getDaemonConnection()).getConfig(), new MoneroRpcConnection(TestUtils.OFFLINE_SERVER_URI).getConfig());
assert(!(await wallet.isConnectedToDaemon()));
assert.equal(await wallet.getSeedLanguage(), "English");
assert.equal(await wallet.getHeight(), 1); // TODO monero-project: why does height of new unsynced wallet start at 1?
assert.equal(await wallet.getRestoreHeight(), restoreHeight);
let path = await wallet.getPath();
await wallet.close(true);
wallet = await that.openWallet({path: path, server: TestUtils.OFFLINE_SERVER_URI});
assert(!(await wallet.isConnectedToDaemon()));
assert(!(await wallet.isSynced()));
assert.equal(await wallet.getHeight(), 1);
assert.equal(await wallet.getRestoreHeight(), restoreHeight);
await wallet.close();
// create wallet with seed, connection, and restore height
wallet = await that.createWallet({seed: TestUtils.SEED, restoreHeight: restoreHeight}, false);
assert.equal(await wallet.getSeed(), TestUtils.SEED);
assert(await wallet.getPrimaryAddress(), TestUtils.ADDRESS);
assert.equal(await wallet.getNetworkType(), TestUtils.NETWORK_TYPE);
assert(await wallet.getDaemonConnection());
assert(await that.daemon.getRpcConnection() != wallet.getDaemonConnection());
assert.equal((await wallet.getDaemonConnection()).getUri(), (await that.daemon.getRpcConnection()).getUri());
assert.equal((await wallet.getDaemonConnection()).getUsername(), (await that.daemon.getRpcConnection()).getUsername());
assert.equal((await wallet.getDaemonConnection()).getPassword(), (await that.daemon.getRpcConnection()).getPassword());
assert(await wallet.isConnectedToDaemon());
assert.equal(await wallet.getSeedLanguage(), "English");
assert(!(await wallet.isSynced()));
assert.equal(await wallet.getHeight(), 1); // TODO monero-project: why does height of new unsynced wallet start at 1?
assert.equal(await wallet.getRestoreHeight(), restoreHeight);
await wallet.close();
});
if (testConfig.testNonRelays)
it("Can create a full wallet from keys", async function() {
// recreate test wallet from keys
let wallet = that.wallet;
let walletKeys = await that.createWallet({server: TestUtils.OFFLINE_SERVER_URI, networkType: await (wallet as MoneroWalletFull).getNetworkType(), primaryAddress: await wallet.getPrimaryAddress(), privateViewKey: await wallet.getPrivateViewKey(), privateSpendKey: await wallet.getPrivateSpendKey(), restoreHeight: TestUtils.FIRST_RECEIVE_HEIGHT});
let err;
try {
assert.equal(await walletKeys.getSeed(), await wallet.getSeed());
assert.equal(await walletKeys.getPrimaryAddress(), await wallet.getPrimaryAddress());
assert.equal(await walletKeys.getPrivateViewKey(), await wallet.getPrivateViewKey());
assert.equal(await walletKeys.getPublicViewKey(), await wallet.getPublicViewKey());
assert.equal(await walletKeys.getPrivateSpendKey(), await wallet.getPrivateSpendKey());
assert.equal(await walletKeys.getPublicSpendKey(), await wallet.getPublicSpendKey());
assert.equal(await walletKeys.getRestoreHeight(), TestUtils.FIRST_RECEIVE_HEIGHT);
assert(!await walletKeys.isConnectedToDaemon());
assert(!(await walletKeys.isSynced()));
} catch (e) {
err = e;
}
await walletKeys.close();
if (err) throw err;
});
if (testConfig.testNonRelays && !GenUtils.isBrowser())
it("Is compatible with monero-wallet-rpc wallet files", async function() {
// create wallet using monero-wallet-rpc
let walletName = GenUtils.getUUID();
let walletRpc = await TestUtils.getWalletRpc();
await walletRpc.createWallet(new MoneroWalletConfig().setPath(walletName).setPassword(TestUtils.WALLET_PASSWORD).setSeed(TestUtils.SEED).setRestoreHeight(TestUtils.FIRST_RECEIVE_HEIGHT));
await walletRpc.sync();
let balance = await walletRpc.getBalance();
let outputsHex = await walletRpc.exportOutputs();
assert(outputsHex.length > 0);
await walletRpc.close(true);
// open as full wallet
let walletFull = await openWalletFull(new MoneroWalletConfig().setPath(TestUtils.WALLET_RPC_LOCAL_WALLET_DIR + "/" + walletName).setPassword(TestUtils.WALLET_PASSWORD).setNetworkType(TestUtils.NETWORK_TYPE).setServer(TestUtils.DAEMON_RPC_CONFIG));
await walletFull.sync();
assert.equal(TestUtils.SEED, await walletFull.getSeed());
assert.equal(TestUtils.ADDRESS, await walletFull.getPrimaryAddress());
assert.equal(balance.toString(), (await walletFull.getBalance()).toString());
assert.equal(outputsHex.length, (await walletFull.exportOutputs()).length);
await walletFull.close(true);
// create full wallet
walletName = GenUtils.getUUID();
let path = TestUtils.WALLET_RPC_LOCAL_WALLET_DIR + "/" + walletName;
walletFull = await createWalletFull(new MoneroWalletConfig().setPath(path).setPassword(TestUtils.WALLET_PASSWORD).setNetworkType(TestUtils.NETWORK_TYPE).setSeed(TestUtils.SEED).setRestoreHeight(TestUtils.FIRST_RECEIVE_HEIGHT).setServer(TestUtils.DAEMON_RPC_CONFIG));
await walletFull.sync();
balance = await walletFull.getBalance();
outputsHex = await walletFull.exportOutputs();
await walletFull.close(true);
// rebuild wallet cache using full wallet
await (await TestUtils.getDefaultFs()).unlink(path);
walletFull = await openWalletFull(new MoneroWalletConfig().setPath(path).setPassword(TestUtils.WALLET_PASSWORD).setNetworkType(TestUtils.NETWORK_TYPE).setServer(TestUtils.DAEMON_RPC_CONFIG));
await walletFull.close(true);
// open wallet using monero-wallet-rpc
await walletRpc.openWallet(new MoneroWalletConfig().setPath(walletName).setPassword(TestUtils.WALLET_PASSWORD));
await walletRpc.sync();
assert.equal(TestUtils.SEED, await walletRpc.getSeed());
assert.equal(TestUtils.ADDRESS, await walletRpc.getPrimaryAddress());
assert.equal(balance.toString(), (await walletRpc.getBalance()).toString());
assert.equal(outputsHex.length, (await walletRpc.exportOutputs()).length);
await walletRpc.close(true);
});
if (!testConfig.liteMode && (testConfig.testNonRelays || testConfig.testRelays))
it("Is compatible with monero-wallet-rpc outputs and offline transaction signing", async function() {
// create view-only wallet in wallet rpc process
let viewOnlyWallet = await TestUtils.startWalletRpcProcess();
await viewOnlyWallet.createWallet({
path: GenUtils.getUUID(),
password: TestUtils.WALLET_PASSWORD,
primaryAddress: await that.wallet.getPrimaryAddress(),
privateViewKey: await that.wallet.getPrivateViewKey(),
restoreHeight: TestUtils.FIRST_RECEIVE_HEIGHT
});
await viewOnlyWallet.sync();
// create offline full wallet
let offlineWallet = await that.createWallet({primaryAddress: await that.wallet.getPrimaryAddress(), privateViewKey: await that.wallet.getPrivateViewKey(), privateSpendKey: await that.wallet.getPrivateSpendKey(), server: TestUtils.OFFLINE_SERVER_URI, restoreHeight: 0});
// test tx signing with wallets
let err;
try { await that.testViewOnlyAndOfflineWallets(viewOnlyWallet, offlineWallet); }
catch (e) { err = e; }
// finally
TestUtils.stopWalletRpcProcess(viewOnlyWallet);
await that.closeWallet(offlineWallet);
if (err) throw err;
});
if (!testConfig.liteMode)
it("Is compatible with monero-wallet-rpc multisig wallets", async function() {
// create participants with monero-wallet-rpc and full wallet
let participants: MoneroWallet[] = [];
participants.push(await (await TestUtils.startWalletRpcProcess()).createWallet(new MoneroWalletConfig().setPath(GenUtils.getUUID()).setPassword(TestUtils.WALLET_PASSWORD)));
participants.push(await (await TestUtils.startWalletRpcProcess()).createWallet(new MoneroWalletConfig().setPath(GenUtils.getUUID()).setPassword(TestUtils.WALLET_PASSWORD)));
participants.push(await that.createWallet(new MoneroWalletConfig()));
// test multisig
let err;
try {
await that.testMultisigParticipants(participants, 3, 3, true);
} catch (e) {
err = e;
}
// stop mining at end of test
try { await that.daemon.stopMining(); }
catch (e) { }
// save and close participants
if (participants[0] instanceof MoneroWalletRpc) await TestUtils.stopWalletRpcProcess(participants[0]);
else participants[0].close(true); // multisig tests might restore wallet from seed
await TestUtils.stopWalletRpcProcess(participants[1]);
await that.closeWallet(participants[2], true);
if (err) throw err;
});
// TODO monero-project: cannot re-sync from lower block height after wallet saved
if (testConfig.testNonRelays && !testConfig.liteMode && false)
it("Can re-sync an existing wallet from scratch", async function() {
let wallet = await that.openWallet({path: TestUtils.WALLET_FULL_PATH, password: TestUtils.WALLET_PASSWORD, networkType: MoneroNetworkType.TESTNET, server: TestUtils.OFFLINE_SERVER_URI}, true); // wallet must already exist
await wallet.setDaemonConnection(TestUtils.getDaemonRpcConnection());
//long startHeight = TestUtils.TEST_RESTORE_HEIGHT;
let startHeight = 0;
let progressTester = new SyncProgressTester(wallet, startHeight, await wallet.getDaemonHeight());
await wallet.setRestoreHeight(1);
let result = await wallet.sync(progressTester, 1);
await progressTester.onDone(await wallet.getDaemonHeight());
// test result after syncing
assert(await wallet.isConnectedToDaemon());
assert(await wallet.isSynced());
assert.equal(result.getNumBlocksFetched(), await wallet.getDaemonHeight() - startHeight);
assert(result.getReceivedMoney());
assert.equal(await wallet.getHeight(), await that.daemon.getHeight());
await wallet.close();
});
if (testConfig.testNonRelays)
it("Can sync a wallet with a randomly generated seed", async function() {
assert(await that.daemon.isConnected(), "Not connected to daemon");
// create test wallet
let restoreHeight = await that.daemon.getHeight();
let wallet = await that.createWallet({}, false);
// test wallet's height before syncing
let walletGt;
let err;
try {
assert.equal((await wallet.getDaemonConnection()).getUri(), (await that.daemon.getRpcConnection()).getUri());
assert.equal((await wallet.getDaemonConnection()).getUsername(), (await that.daemon.getRpcConnection()).getUsername());
assert.equal((await wallet.getDaemonConnection()).getPassword(), (await that.daemon.getRpcConnection()).getPassword());
assert.equal(await wallet.getDaemonHeight(), restoreHeight);
assert(await wallet.isConnectedToDaemon());
assert(!(await wallet.isSynced()));
assert.equal(await wallet.getHeight(), 1);
assert.equal(await wallet.getRestoreHeight(), restoreHeight);
// sync the wallet
let progressTester = new SyncProgressTester(wallet, await wallet.getRestoreHeight(), await wallet.getDaemonHeight());
let result = await wallet.sync(progressTester, undefined);
await progressTester.onDone(await wallet.getDaemonHeight());
// test result after syncing
walletGt = await that.createWallet({seed: await wallet.getSeed(), restoreHeight: restoreHeight});
await walletGt.sync();
assert(await wallet.isConnectedToDaemon());
assert(await wallet.isSynced());
assert.equal(result.getNumBlocksFetched(), 0);
assert(!result.getReceivedMoney());
if (await wallet.getHeight() !== await that.daemon.getHeight()) console.log("WARNING: wallet height " + await wallet.getHeight() + " is not synced with daemon height " + await that.daemon.getHeight()); // TODO: height may not be same after long sync
// sync the wallet with default params
await wallet.sync();
assert(await wallet.isSynced());
assert.equal(await wallet.getHeight(), await that.daemon.getHeight());
// compare wallet to ground truth
await TestMoneroWalletFull.testWalletEqualityOnChain(walletGt, wallet);
} catch (e) {
err = e;
}
// finally
if (walletGt) await walletGt.close();
await wallet.close();
if (err) throw err;
// attempt to sync unconnected wallet
wallet = await that.createWallet({server: TestUtils.OFFLINE_SERVER_URI});
err = undefined;
try {
await wallet.sync();
throw new Error("Should have thrown exception");
} catch (e1: any) {
try {
assert.equal(e1.message, "Wallet is not connected to daemon");
} catch (e2) {
err = e2;
}
}
// finally
await wallet.close();
if (err) throw err;
});
if (false && testConfig.testNonRelays && !testConfig.liteMode) // TODO: re-enable before release
it("Can sync a wallet created from seed from the genesis", async function() {
await testSyncSeed(undefined, undefined, true, false);
});
if (testConfig.testNonRelays)
it("Can sync a wallet created from seed from a restore height", async function() {
await testSyncSeed(undefined, TestUtils.FIRST_RECEIVE_HEIGHT);
});
if (testConfig.testNonRelays && !testConfig.liteMode)
it("Can sync a wallet created from seed from a start height.", async function() {
await testSyncSeed(TestUtils.FIRST_RECEIVE_HEIGHT, undefined, false, true);
});
if (testConfig.testNonRelays && !testConfig.liteMode)
it("Can sync a wallet created from seed from a start height less than the restore height", async function() {
await testSyncSeed(TestUtils.FIRST_RECEIVE_HEIGHT, TestUtils.FIRST_RECEIVE_HEIGHT + 3);
});
if (testConfig.testNonRelays && !testConfig.liteMode)
it("Can sync a wallet created from seed from a start height greater than the restore height", async function() {
await testSyncSeed(TestUtils.FIRST_RECEIVE_HEIGHT + 3, TestUtils.FIRST_RECEIVE_HEIGHT);
});
async function testSyncSeed(startHeight, restoreHeight?, skipGtComparison?, testPostSyncNotifications?) {
assert(await that.daemon.isConnected(), "Not connected to daemon");
if (startHeight !== undefined && restoreHeight != undefined) assert(startHeight <= TestUtils.FIRST_RECEIVE_HEIGHT || restoreHeight <= TestUtils.FIRST_RECEIVE_HEIGHT);
// create wallet from seed
let wallet = await that.createWallet({seed: TestUtils.SEED, restoreHeight: restoreHeight}, false);
// sanitize expected sync bounds
if (restoreHeight === undefined) restoreHeight = 0;
let startHeightExpected = startHeight === undefined ? restoreHeight : startHeight;
if (startHeightExpected === 0) startHeightExpected = 1;
let endHeightExpected = await wallet.getDaemonMaxPeerHeight();
// test wallet and close as final step
let walletGt: MoneroWallet | undefined = undefined;
let err = undefined; // to permit final cleanup like Java's try...catch...finally
try {
// test wallet's height before syncing
assert(await wallet.isConnectedToDaemon());
assert(!(await wallet.isSynced()));
assert.equal(await wallet.getHeight(), 1);
assert.equal(await wallet.getRestoreHeight(), restoreHeight);
// register a wallet listener which tests notifications throughout the sync
let walletSyncTester = new WalletSyncTester(wallet, startHeightExpected, endHeightExpected);
await wallet.addListener(walletSyncTester);
// sync the wallet with a listener which tests sync notifications
let progressTester = new SyncProgressTester(wallet, startHeightExpected, endHeightExpected);
let result = await wallet.sync(progressTester, startHeight);
// test completion of the wallet and sync listeners
await progressTester.onDone(await wallet.getDaemonHeight());
await walletSyncTester.onDone(await wallet.getDaemonHeight());
// test result after syncing
assert(await wallet.isSynced());
assert.equal(result.getNumBlocksFetched(), await wallet.getDaemonHeight() - startHeightExpected);
assert(result.getReceivedMoney());
if (await wallet.getHeight() !== await that.daemon.getHeight()) console.log("WARNING: wallet height " + await wallet.getHeight() + " is not synced with daemon height " + await that.daemon.getHeight()); // TODO: height may not be same after long sync
assert.equal(await wallet.getDaemonHeight(), await that.daemon.getHeight(), "Daemon heights are not equal: " + await wallet.getDaemonHeight() + " vs " + await that.daemon.getHeight());
if (startHeightExpected > TestUtils.FIRST_RECEIVE_HEIGHT) assert((await wallet.getTxs())[0].getHeight() > TestUtils.FIRST_RECEIVE_HEIGHT); // wallet is partially synced so first tx happens after true restore height
else assert.equal((await wallet.getTxs())[0].getHeight(), TestUtils.FIRST_RECEIVE_HEIGHT); // wallet should be fully synced so first tx happens on true restore height
// sync the wallet with default params
result = await wallet.sync();
assert(await wallet.isSynced());
if (await wallet.getHeight() !== await that.daemon.getHeight()) console.log("WARNING: wallet height " + await wallet.getHeight() + " is not synced with daemon height " + await that.daemon.getHeight() + " after re-syncing");
assert.equal(result.getNumBlocksFetched(), 0);
assert(!result.getReceivedMoney());
// compare with ground truth
if (!skipGtComparison) {
walletGt = await TestUtils.createWalletGroundTruth(TestUtils.NETWORK_TYPE, await wallet.getSeed(), startHeight, restoreHeight);
await TestMoneroWalletFull.testWalletEqualityOnChain(walletGt, wallet);
}
// if testing post-sync notifications, wait for a block to be added to the chain
// then test that sync arg listener was not invoked and registered wallet listener was invoked
if (testPostSyncNotifications) {
// start automatic syncing
await wallet.startSyncing(TestUtils.SYNC_PERIOD_IN_MS);
// attempt to start mining to push the network along // TODO: TestUtils.tryStartMining() : reqId, TestUtils.tryStopMining(reqId)
let startedMining = false;
let miningStatus = await that.daemon.getMiningStatus();
if (!miningStatus.getIsActive()) {
try {
await StartMining.startMining();
startedMining = true;
} catch (e) {
// no problem
}
}
try {
// wait for block
console.log("Waiting for next block to test post sync notifications");
await that.daemon.waitForNextBlockHeader();
// ensure wallet has time to detect new block
await new Promise(function(resolve) { setTimeout(resolve, TestUtils.SYNC_PERIOD_IN_MS + 3000); }); // sleep for wallet interval + time to sync
// test that wallet listener's onSyncProgress() and onNewBlock() were invoked after previous completion
assert(walletSyncTester.getOnSyncProgressAfterDone());
assert(walletSyncTester.getOnNewBlockAfterDone());
} catch (e) {
err = e;
}
// finally
if (startedMining) {
await that.daemon.stopMining();
//await wallet.stopMining(); // TODO: support client-side mining?
}
if (err) throw err;
}
} catch (e) {
err = e;
}
// finally
if (walletGt !== undefined) await walletGt.close(true);
await wallet.close();
if (err) throw err;
}
if (testConfig.testNonRelays)
it("Can sync a wallet created from keys", async function() {
// recreate test wallet from keys
let walletKeys = await that.createWallet({primaryAddress: await that.wallet.getPrimaryAddress(), privateViewKey: await that.wallet.getPrivateViewKey(), privateSpendKey: await that.wallet.getPrivateSpendKey(), restoreHeight: TestUtils.FIRST_RECEIVE_HEIGHT}, false);
// create ground truth wallet for comparison
let walletGt = await TestUtils.createWalletGroundTruth(TestUtils.NETWORK_TYPE, TestUtils.SEED, undefined, TestUtils.FIRST_RECEIVE_HEIGHT);
// test wallet and close as final step
let err;
try {
assert.equal(await walletKeys.getSeed(), await walletGt.getSeed());
assert.equal(await walletKeys.getPrimaryAddress(), await walletGt.getPrimaryAddress());
assert.equal(await walletKeys.getPrivateViewKey(), await walletGt.getPrivateViewKey());
assert.equal(await walletKeys.getPublicViewKey(), await walletGt.getPublicViewKey());
assert.equal(await walletKeys.getPrivateSpendKey(), await walletGt.getPrivateSpendKey());
assert.equal(await walletKeys.getPublicSpendKey(), await walletGt.getPublicSpendKey());
assert.equal(await walletKeys.getRestoreHeight(), TestUtils.FIRST_RECEIVE_HEIGHT);
assert(await walletKeys.isConnectedToDaemon());
assert(!(await walletKeys.isSynced()));
// sync the wallet
let progressTester = new SyncProgressTester(walletKeys, TestUtils.FIRST_RECEIVE_HEIGHT, await walletKeys.getDaemonMaxPeerHeight());
let result = await walletKeys.sync(progressTester);
await progressTester.onDone(await walletKeys.getDaemonHeight());
// test result after syncing
assert(await walletKeys.isSynced());
assert.equal(result.getNumBlocksFetched(), await walletKeys.getDaemonHeight() - TestUtils.FIRST_RECEIVE_HEIGHT);
assert(result.getReceivedMoney());
assert.equal(await walletKeys.getHeight(), await that.daemon.getHeight());
assert.equal(await walletKeys.getDaemonHeight(), await that.daemon.getHeight());
assert.equal((await walletKeys.getTxs())[0].getHeight(), TestUtils.FIRST_RECEIVE_HEIGHT); // wallet should be fully synced so first tx happens on true restore height
// compare with ground truth
await TestMoneroWalletFull.testWalletEqualityOnChain(walletGt, walletKeys);
} catch (e) {
err = e;
}
// finally
await walletGt.close(true);
await walletKeys.close();
if (err) throw err;
});
// TODO: test start syncing, notification of syncs happening, stop syncing, no notifications, etc
if (testConfig.testNonRelays)
it("Can start and stop syncing", async function() {
// test unconnected wallet
let err; // used to emulate Java's try...catch...finally
let path = TestMoneroWalletFull.getRandomWalletPath();
let wallet = await that.createWallet({path: path, password: TestUtils.WALLET_PASSWORD, networkType: TestUtils.NETWORK_TYPE, server: TestUtils.OFFLINE_SERVER_URI});
try {
assert.notEqual(await wallet.getSeed(), undefined);
assert.equal(await wallet.getHeight(), 1);
assert.equal(await wallet.getBalance(), 0n);
await wallet.startSyncing();
} catch (e1: any) { // first error is expected
try {
assert.equal(e1.message, "Wallet is not connected to daemon");
} catch (e2) {
err = e2;
}
}
// finally
await wallet.close();
if (err) throw err;
// test connecting wallet
path = TestMoneroWalletFull.getRandomWalletPath();
wallet = await that.createWallet({path: path, password: TestUtils.WALLET_PASSWORD, networkType: TestUtils.NETWORK_TYPE, server: TestUtils.OFFLINE_SERVER_URI});
try {
assert.notEqual(wallet.getSeed(), undefined);
assert(!await wallet.isConnectedToDaemon());
await wallet.setDaemonConnection(await that.daemon.getRpcConnection());
assert.equal(await wallet.getHeight(), 1);
assert(!await wallet.isSynced());
assert.equal(await wallet.getBalance(), 0n);
let chainHeight = await wallet.getDaemonHeight();
await wallet.setRestoreHeight(chainHeight - 3);
await wallet.startSyncing();
assert.equal(await wallet.getRestoreHeight(), chainHeight - 3);
assert.equal((await wallet.getDaemonConnection()).getUri(), (await that.daemon.getRpcConnection()).getUri()); // TODO: replace with config comparison
assert.equal((await wallet.getDaemonConnection()).getUsername(), (await that.daemon.getRpcConnection()).getUsername());
assert.equal((await wallet.getDaemonConnection()).getPassword(), (await that.daemon.getRpcConnection()).getPassword());
await wallet.stopSyncing();
await wallet.sync();
await wallet.stopSyncing();
await wallet.stopSyncing();
} catch (e) {
err = e;
}
// finally
await wallet.close();
if (err) throw err;
// test that sync starts automatically
let restoreHeight = await that.daemon.getHeight() - 100;
path = TestMoneroWalletFull.getRandomWalletPath();
wallet = await that.createWallet({path: path, password: TestUtils.WALLET_PASSWORD, networkType: TestUtils.NETWORK_TYPE, seed: TestUtils.SEED, server: await that.daemon.getRpcConnection(), restoreHeight: restoreHeight}, false);
try {
// start syncing
assert.equal(await wallet.getHeight(), 1);
assert.equal(await wallet.getRestoreHeight(), restoreHeight);
assert(!(await wallet.isSynced()));
assert.equal(await wallet.getBalance(), BigInt(0));
await wallet.startSyncing(TestUtils.SYNC_PERIOD_IN_MS);
// pause for sync to start
await new Promise(function(resolve) { setTimeout(resolve, TestUtils.SYNC_PERIOD_IN_MS + 1000); }); // in ms
// test that wallet has started syncing
assert(await wallet.getHeight() > 1);
// stop syncing
await wallet.stopSyncing();
// TODO monero-project: wallet.cpp m_synchronized only ever set to true, never false
// // wait for block to be added to chain
// await that.daemon.waitForNextBlockHeader();
//
// // wallet is no longer synced
// assert(!(await wallet.isSynced()));
} catch (e) {
err = e;
}
// finally
await wallet.close();
if (err) throw err;
});
if (testConfig.testNonRelays)
it("Does not interfere with other wallet notifications", async function() {
// create 2 wallets with a recent restore height
let height = await that.daemon.getHeight();
let restoreHeight = height - 5;
let wallet1 = await that.createWallet({seed: TestUtils.SEED, restoreHeight: restoreHeight}, false);
let wallet2 = await that.createWallet({seed: TestUtils.SEED, restoreHeight: restoreHeight}, false);
// track notifications of each wallet
let tester1 = new SyncProgressTester(wallet1, restoreHeight, height);
let tester2 = new SyncProgressTester(wallet2, restoreHeight, height);
await wallet1.addListener(tester1);
await wallet2.addListener(tester2);
// sync first wallet and test that 2nd is not notified
await wallet1.sync();
assert(tester1.isNotified());
assert(!tester2.isNotified());
// sync 2nd wallet and test that 1st is not notified
let tester3 = new SyncProgressTester(wallet1, restoreHeight, height);
await wallet1.addListener(tester3);
await wallet2.sync();
assert(tester2.isNotified());
assert(!(tester3.isNotified()));
// close wallets
await wallet1.close();
await wallet2.close();
});
if (testConfig.testNonRelays)
it("Is equal to the RPC wallet.", async function() {
await WalletEqualityUtils.testWalletEqualityOnChain(await TestUtils.getWalletRpc(), that.wallet);
});
if (testConfig.testNonRelays)
it("Is equal to the RPC wallet with a seed offset", async function() {
// use common offset to compare wallet implementations
let seedOffset = "my super secret offset!";
// create rpc wallet with offset
let walletRpc = await TestUtils.getWalletRpc();
await walletRpc.createWallet({path: GenUtils.getUUID(), password: TestUtils.WALLET_PASSWORD, seed: await walletRpc.getSeed(), restoreHeight: TestUtils.FIRST_RECEIVE_HEIGHT, seedOffset: seedOffset});
// create full wallet with offset
let walletFull = await that.createWallet({seed: TestUtils.SEED, restoreHeight: TestUtils.FIRST_RECEIVE_HEIGHT, seedOffset: seedOffset});
// deep compare
let err;
try {
await WalletEqualityUtils.testWalletEqualityOnChain(walletRpc, walletFull);
} catch (e) {
err = e;
}
await walletFull.close();
if (err) throw err;
});
if (testConfig.testNonRelays)
it("Supports multisig sample code", async function() {
await testCreateMultisigWallet(2, 2);
await testCreateMultisigWallet(2, 3);
await testCreateMultisigWallet(2, 4);
});
async function testCreateMultisigWallet(M, N) {
console.log("Creating " + M + "/" + N + " multisig wallet");
// create participating wallets
let wallets: MoneroWallet[] = []
for (let i = 0; i < N; i++) {
wallets.push(await that.createWallet());
}
// prepare and collect multisig hex from each participant
let preparedMultisigHexes: string[] = []
for (let wallet of wallets) preparedMultisigHexes.push(await wallet.prepareMultisig());
// make each wallet multisig and collect results
let madeMultisigHexes: string[] = [];
for (let i = 0; i < wallets.length; i++) {
// collect prepared multisig hexes from wallet's peers
let peerMultisigHexes: string[] = [];
for (let j = 0; j < wallets.length; j++) if (j !== i) peerMultisigHexes.push(preparedMultisigHexes[j]);
// make wallet multisig and collect result hex
let multisigHex = await wallets[i].makeMultisig(peerMultisigHexes, M, TestUtils.WALLET_PASSWORD);
madeMultisigHexes.push(multisigHex);
}
// exchange multisig keys N - M + 1 times
let multisigHexes = madeMultisigHexes;
for (let i = 0; i < N - M + 1; i++) {
// exchange multisig keys among participants and collect results for next round if applicable
let resultMultisigHexes: string[] = [];
for (let wallet of wallets) {
// import the multisig hex of other participants and collect results
let result = await wallet.exchangeMultisigKeys(multisigHexes, TestUtils.WALLET_PASSWORD);
resultMultisigHexes.push(result.getMultisigHex());
}
// use resulting multisig hex for next round of exchange if applicable
multisigHexes = resultMultisigHexes;
}
// wallets are now multisig
for (let wallet of wallets) {
let primaryAddress = await wallet.getAddress(0, 0);
await MoneroUtils.validateAddress(primaryAddress, await (wallet as MoneroWalletFull).getNetworkType());
let info = await wallet.getMultisigInfo();
assert(info.getIsMultisig());
assert(info.getIsReady());
assert.equal(info.getThreshold(), M);
assert.equal(info.getNumParticipants(), N);
await wallet.close(true);
}
}
if (testConfig.testNonRelays)
it("Can be saved", async function() {
// create unique path for new test wallet
let path = TestMoneroWalletFull.getRandomWalletPath();
// wallet does not exist
assert(!(await MoneroWalletFull.walletExists(path, await TestUtils.getDefaultFs())));
// cannot open non-existent wallet
try {
await that.openWallet({path: path, server: ""});
throw new Error("Cannot open non-existent wallet");
} catch (e: any) {
assert.equal(e.message, "Wallet does not exist at path: " + path);
}
// create wallet at the path
let restoreHeight = await that.daemon.getHeight() - 200;
let wallet = await that.createWallet({path: path, password: TestUtils.WALLET_PASSWORD, networkType: TestUtils.NETWORK_TYPE, seed: TestUtils.SEED, restoreHeight: restoreHeight, server: TestUtils.OFFLINE_SERVER_URI});
// test wallet at newly created state
let err;
try {
assert(await MoneroWalletFull.walletExists(path, await TestUtils.getDefaultFs()));
assert.equal(await wallet.getSeed(), TestUtils.SEED);
assert.equal(await wallet.getNetworkType(), TestUtils.NETWORK_TYPE);
assert.deepEqual((await wallet.getDaemonConnection()).getConfig(), new MoneroRpcConnection(TestUtils.OFFLINE_SERVER_URI).getConfig());
assert.equal(await wallet.getRestoreHeight(), restoreHeight);
assert.equal(await wallet.getSeedLanguage(), "English");
assert.equal(await wallet.getHeight(), 1);
assert.equal(await wallet.getRestoreHeight(), restoreHeight);
// set the wallet's connection and sync
await wallet.setDaemonConnection(TestUtils.getDaemonRpcConnection());
await wallet.sync();
if (await wallet.getHeight() !== await wallet.getDaemonHeight()) console.log("WARNING: wallet height " + await wallet.getHeight() + " is not synced with daemon height " + await that.daemon.getHeight()); // TODO: height may not be same after long sync
// close the wallet without saving
await wallet.close();
// re-open the wallet
wallet = await that.openWallet({path: path, server: TestUtils.OFFLINE_SERVER_URI});
// test wallet is at newly created state
assert(await MoneroWalletFull.walletExists(path, await TestUtils.getDefaultFs()));
assert.equal(await wallet.getSeed(), TestUtils.SEED);
assert.equal(await wallet.getNetworkType(), TestUtils.NETWORK_TYPE);
assert.deepEqual((await wallet.getDaemonConnection()).getConfig(), new MoneroRpcConnection(TestUtils.OFFLINE_SERVER_URI).getConfig());
assert(!(await wallet.isConnectedToDaemon()));
assert.equal(await wallet.getSeedLanguage(), "English");
assert(!(await wallet.isSynced()));
assert.equal(await wallet.getHeight(), 1);
assert(await wallet.getRestoreHeight() > 0);
// set the wallet's connection and sync
await wallet.setDaemonConnection(TestUtils.getDaemonRpcConnection());
assert(await wallet.isConnectedToDaemon());
await wallet.setRestoreHeight(restoreHeight);
await wallet.sync();
assert(await wallet.isSynced());
assert.equal(await wallet.getHeight(), await wallet.getDaemonHeight());
let prevHeight = await wallet.getHeight();
// save and close the wallet
await wallet.save();
await wallet.close();
// re-open the wallet
wallet = await that.openWallet({path: path, server: TestUtils.OFFLINE_SERVER_URI});
// test wallet state is saved
assert(!(await wallet.isConnectedToDaemon()));
await wallet.setDaemonConnection(TestUtils.getDaemonRpcConnection()); // TODO monero-project: daemon connection not stored in wallet files so must be explicitly set each time
assert.deepEqual((await wallet.getDaemonConnection()).getConfig(), TestUtils.getDaemonRpcConnection().getConfig());
assert(await wallet.isConnectedToDaemon());
assert.equal(await wallet.getHeight(), prevHeight);
assert(await wallet.getRestoreHeight() > 0);
assert(await MoneroWalletFull.walletExists(path, await TestUtils.getDefaultFs()));
assert.equal(await wallet.getSeed(), TestUtils.SEED);
assert.equal(await wallet.getNetworkType(), TestUtils.NETWORK_TYPE);
assert.equal(await wallet.getSeedLanguage(), "English");
// sync
await wallet.sync();