Added migration for android wallets and hives files.

M committed Apr 3, 2020 at 15:27 UTC d067df8a51cf0b40720970756284cbf578914678
2 files changed +80 -8
lib/src/domain/common/fs_migration.dart new
+70
@@ -0,0 +1,70 @@
1 +import 'dart:io';
2 +import 'package:path_provider/path_provider.dart';
3 +
4 +const reservedNames = ["flutter_assets", "wallets", "db"];
5 +
6 +Future<void> migrate_fs() async {
7 + final appDocDir = await getApplicationDocumentsDirectory();
8 +
9 + await migrate_hives(appDocDir: appDocDir);
10 + await migrate_wallets(appDocDir: appDocDir);
11 +
12 + appDocDir.listSync(recursive: true).forEach((item) => print(item.path));
13 +}
14 +
15 +Future<void> migrate_hives({Directory appDocDir}) async {
16 + final dbDir = Directory('${appDocDir.path}/db');
17 + final files = List<File>();
18 +
19 + appDocDir.listSync().forEach((FileSystemEntity item) {
20 + final ext = item.path.split('.').last;
21 +
22 + if (item is File && (ext == "hive" || ext == "lock")) {
23 + files.add(item);
24 + }
25 + });
26 +
27 + if (!dbDir.existsSync()) {
28 + dbDir.createSync();
29 + }
30 +
31 + files.forEach((File hive) {
32 + final name = hive.path.split('/').last;
33 + hive.copySync('${dbDir.path}/$name');
34 + hive.deleteSync();
35 + });
36 +}
37 +
38 +Future<void> migrate_wallets({Directory appDocDir}) async {
39 + final walletsDir = Directory('${appDocDir.path}/wallets');
40 + final moneroWalletsDir = Directory('${walletsDir.path}/monero');
41 + final dirs = List<Directory>();
42 +
43 + appDocDir.listSync().forEach((FileSystemEntity item) {
44 + final name = item.path.split('/').last;
45 +
46 + if (item is Directory && !reservedNames.contains(name)) {
47 + dirs.add(item);
48 + }
49 + });
50 +
51 + if (!moneroWalletsDir.existsSync()) {
52 + await moneroWalletsDir.create(recursive: true);
53 + }
54 +
55 + dirs.forEach((Directory dir) {
56 + final name = dir.path.split('/').last;
57 + final newDir = Directory('${moneroWalletsDir.path}/$name');
58 + newDir.createSync();
59 +
60 + dir.listSync().forEach((file) {
61 + if (file is File) {
62 + final fileName = file.path.split('/').last;
63 + file.copySync('${newDir.path}/$fileName');
64 + file.deleteSync();
65 + }
66 + });
67 +
68 + dir.deleteSync();
69 + });
70 +}
lib/src/domain/monero/monero_wallets_manager.dart
+10 -8
@@ -11,9 +11,10 @@ import 'package:cake_wallet/src/domain/common/wallet.dart';
11 import 'package:cake_wallet/src/domain/monero/monero_wallet.dart';
12 import 'package:cake_wallet/src/domain/common/wallet_description.dart';
13
14 -Future<String> pathForWallet({String name}) async {
14 +Future<String> pathForWallet(
15 + {@required WalletType type, @required String name}) async {
16 final directory = await getApplicationDocumentsDirectory();
16 - final pathDir = directory.path + '/$name';
17 + final pathDir = directory.path + '/wallets/${walletTypeToString(type).toLowerCase()}' + '/$name';
18 final dir = Directory(pathDir);
19
20 if (!await dir.exists()) {
@@ -34,9 +35,10 @@ class MoneroWalletsManager extends WalletsManager {
35 Future<Wallet> create(String name, String password, String language) async {
36 try {
37 const isRecovery = false;
37 - final path = await pathForWallet(name: name);
38 + final path = await pathForWallet(type: WalletType.monero, name: name);
39
39 - await monero_wallet_manager.createWallet(path: path, password: password, language: language);
40 + await monero_wallet_manager.createWallet(
41 + path: path, password: password, language: language);
42
43 final wallet = await MoneroWallet.createdWallet(
44 walletInfoSource: walletInfoSource,
@@ -56,7 +58,7 @@ class MoneroWalletsManager extends WalletsManager {
58 String name, String password, String seed, int restoreHeight) async {
59 try {
60 const isRecovery = true;
59 - final path = await pathForWallet(name: name);
61 + final path = await pathForWallet(type: WalletType.monero, name: name);
62
63 await monero_wallet_manager.restoreFromSeed(
64 path: path,
@@ -89,7 +91,7 @@ class MoneroWalletsManager extends WalletsManager {
91 String spendKey) async {
92 try {
93 const isRecovery = true;
92 - final path = await pathForWallet(name: name);
94 + final path = await pathForWallet(type: WalletType.monero, name: name);
95
96 await monero_wallet_manager.restoreFromKeys(
97 path: path,
@@ -117,7 +119,7 @@ class MoneroWalletsManager extends WalletsManager {
119 @override
120 Future<Wallet> openWallet(String name, String password) async {
121 try {
120 - final path = await pathForWallet(name: name);
122 + final path = await pathForWallet(type: WalletType.monero, name: name);
123 monero_wallet_manager.openWallet(path: path, password: password);
124 final wallet = await MoneroWallet.load(walletInfoSource, name, type);
125 await wallet.updateInfo();
@@ -132,7 +134,7 @@ class MoneroWalletsManager extends WalletsManager {
134 @override
135 Future<bool> isWalletExit(String name) async {
136 try {
135 - final path = await pathForWallet(name: name);
137 + final path = await pathForWallet(type: WalletType.monero, name: name);
138 return monero_wallet_manager.isWalletExist(path: path);
139 } catch (e) {
140 print('MoneroWalletsManager Error: $e');