dev
dart 144 lines 4.8 KB
Raw
1 import 'dart:io';
2
3 import 'package:cw_core/db/sqlite.dart';
4 import 'package:cw_core/unspent_coins_info.dart';
5 import 'package:cw_core/wallet_base.dart';
6 import 'package:cw_core/wallet_info.dart';
7 import 'package:cw_core/wallet_type.dart';
8 import 'package:cw_monero/monero_wallet_service.dart';
9 import 'package:flutter_test/flutter_test.dart';
10 import 'package:hive/hive.dart';
11 import 'package:sqflite/sqflite.dart';
12 import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
13 import 'package:sqflite_common_ffi/sqflite_ffi.dart';
14
15 import 'mock/path_provider.dart';
16 import 'utils/setup_monero_c.dart';
17
18 Future<void> main() async {
19 group("MoneroWalletService Tests", () {
20 late MoneroWalletService walletService;
21 File? moneroCBinary;
22
23 setUpAll(() async {
24 await initDb(pathOverride: './test/data/db');
25 Hive.init('./test/data/db');
26 PathProviderPlatform.instance = MockPathProviderPlatform();
27
28 final Box<UnspentCoinsInfo> unspentCoinsInfoSource =
29 await Hive.openBox('testUnspentCoinsInfo');
30
31 walletService = MoneroWalletService(unspentCoinsInfoSource);
32 moneroCBinary = getMoneroCBinary().copySync(moneroCBinaryName);
33 });
34
35 tearDownAll(() {
36 Directory('./test/data').deleteSync(recursive: true);
37 moneroCBinary!.deleteSync();
38 });
39
40 group("Create wallet", () {
41 test("Create Legacy Wallet", () async {
42 final credentials = _getTestCreateCredentials(
43 name: 'Create Wallet LS', language: 'English', seedType: MoneroSeedType.legacy);
44 final wallet = await walletService.create(credentials);
45
46 expect(wallet.seed.split(" ").length, 25);
47 expect(wallet.restoreHeight, greaterThan(3000000));
48 });
49
50 test("Create Polyseed Wallet", () async {
51 final credentials = _getTestCreateCredentials(
52 name: 'Create Wallet PS', language: 'English', seedType: MoneroSeedType.polyseed);
53 final wallet = await walletService.create(credentials);
54
55 expect(wallet.seed.split(" ").length, 16);
56 expect(wallet.restoreHeight, greaterThan(3000000));
57 });
58
59 test("Create Bip39 Wallet", () async {
60 final credentials = _getTestCreateCredentials(
61 name: 'Create Wallet BS', language: 'English', seedType: MoneroSeedType.bip39);
62 final wallet = await walletService.create(credentials);
63
64 expect(wallet.seed.split(" ").length, 12);
65 expect(wallet.restoreHeight, greaterThan(3000000));
66 });
67 });
68
69 group("Restore wallet", () {
70 test('Legacy Seed', () async {
71 final credentials = _getTestRestoreCredentials(
72 name: 'Test Wallet LS',
73 mnemonic:
74 'ability pockets lordship tomorrow gypsy match neutral uncle avatar betting bicycle junk unzip pyramid lynx mammal edgy empty uneven knowledge juvenile wiring paradise psychic betting',
75 );
76
77 final wallet = await walletService.restoreFromSeed(credentials);
78 expect(wallet.walletAddresses.primaryAddress,
79 '48tLyQXpcwt8w6uKHyb5Zs3vdnoDWAEKFQr1c198o7aX9dBzXP3BTSMVsDiuH3ozDCNqwojb4vNeQZf7xg6URimDLaNtGSN');
80 });
81
82 test('Bip39 Seed', () async {
83 final credentials = _getTestRestoreCredentials(
84 name: 'Test Wallet BS',
85 mnemonic:
86 'color ranch color remove subway public water embrace before begin liberty fault');
87
88 final wallet = await walletService.restoreFromSeed(credentials);
89 expect(wallet.walletAddresses.primaryAddress,
90 '49MggvPosJugF8Zq7WAKbsSchz6vbyL6YiUxM4ryfGQDXphs6wiWiXLFWCSshnLPcceGTWUaKfWWMHQAAKESV3TQJVQsL9a');
91 });
92 });
93 });
94 }
95
96 MoneroRestoreWalletFromSeedCredentials _getTestRestoreCredentials({
97 required String name,
98 required String mnemonic,
99 }) {
100 final credentials = MoneroRestoreWalletFromSeedCredentials(
101 name: name, mnemonic: mnemonic, passphrase: '', password: "test");
102
103 credentials.walletInfo = WalletInfo.external(
104 id: WalletBase.idFor(name, WalletType.monero),
105 name: name,
106 type: WalletType.monero,
107 isRecovery: true,
108 restoreHeight: credentials.height ?? 0,
109 date: DateTime.now(),
110 path: '',
111 dirPath: '',
112 address: '',
113 );
114 return credentials;
115 }
116
117 MoneroNewWalletCredentials _getTestCreateCredentials({
118 required String name,
119 required String language,
120 required MoneroSeedType seedType,
121 String? mnemonic,
122 }) {
123 final credentials = MoneroNewWalletCredentials(
124 name: name,
125 language: language,
126 seedType: seedType,
127 password: "test",
128 mnemonic: mnemonic,
129 passphrase: '',
130 );
131
132 credentials.walletInfo = WalletInfo.external(
133 id: WalletBase.idFor(name, WalletType.monero),
134 name: name,
135 type: WalletType.monero,
136 isRecovery: false,
137 restoreHeight: credentials.height ?? 0,
138 date: DateTime.now(),
139 path: '',
140 dirPath: '',
141 address: '',
142 );
143 return credentials;
144 }