cake_wallet/lib/entities/fs_migration.dart

444 lines
14 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'dart:convert';
import 'package:cake_wallet/core/secure_storage.dart';
2022-10-12 17:09:57 +00:00
import 'package:collection/collection.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
2020-09-23 18:26:10 +00:00
import 'package:cake_wallet/core/key_service.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/entities/contact.dart';
2021-12-24 12:37:24 +00:00
import 'package:cw_core/crypto_currency.dart';
2020-09-23 18:26:10 +00:00
import 'package:cake_wallet/entities/encrypt.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/entities/ios_legacy_helper.dart' as ios_legacy_helper;
import 'package:cake_wallet/entities/preferences_key.dart';
2020-09-23 18:26:10 +00:00
import 'package:cake_wallet/entities/secret_store_key.dart';
2021-12-24 12:37:24 +00:00
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart';
2020-09-23 18:26:10 +00:00
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/exchange/trade.dart';
2020-09-23 18:26:10 +00:00
import 'package:cake_wallet/.secrets.g.dart' as secrets;
const reservedNames = ["flutter_assets", "wallets", "db"];
2020-04-03 13:21:00 +00:00
Future<void> migrate_android_v1() async {
final appDocDir = await getApplicationDocumentsDirectory();
2020-09-23 18:26:10 +00:00
await android_migrate_hives(appDocDir: appDocDir);
await android_migrate_wallets(appDocDir: appDocDir);
2020-04-03 13:21:00 +00:00
}
Future<void> ios_migrate_v1(
Box<WalletInfo> walletInfoSource, Box<Trade> tradeSource, Box<Contact> contactSource) async {
2020-09-23 18:26:10 +00:00
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('ios_migration_v1_completed') ?? false) {
return;
}
await ios_migrate_user_defaults();
await ios_migrate_pin();
await ios_migrate_wallet_passwords();
await ios_migrate_wallet_info(walletInfoSource);
await ios_migrate_trades_list(tradeSource);
await ios_migrate_address_book(contactSource);
await prefs.setBool('ios_migration_v1_completed', true);
}
Future<void> ios_migrate_user_defaults() async {
//get the new shared preferences instance
2020-09-23 18:26:10 +00:00
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('ios_migration_user_defaults_completed') ?? false) {
return;
}
//translate the node uri
2020-09-23 18:26:10 +00:00
final nodeURI = await ios_legacy_helper.getString('node_uri');
// await prefs.setString('current_node_id', nodeURI);
await prefs.setInt('current_node_id', 0);
//should we provide default btc node key?
2020-09-23 18:26:10 +00:00
final activeCurrency = await ios_legacy_helper.getInt('currency');
2022-10-12 17:09:57 +00:00
if (activeCurrency != null) {
final convertedCurrency = convertFiatLegacy(activeCurrency);
await prefs.setString('current_fiat_currency', convertedCurrency.serialize());
2020-09-23 18:26:10 +00:00
}
//translate fee priority
2020-09-23 18:26:10 +00:00
final activeFeeTier = await ios_legacy_helper.getInt('saved_fee_priority');
2022-10-12 17:09:57 +00:00
if (activeFeeTier != null) {
await prefs.setInt('current_fee_priority', activeFeeTier);
}
//translate current balance mode
final currentBalanceMode = await ios_legacy_helper.getInt('display_balance_mode');
2022-10-12 17:09:57 +00:00
if (currentBalanceMode != null) {
await prefs.setInt('current_balance_display_mode', currentBalanceMode);
}
//translate should save recipient address
final shouldSave = await ios_legacy_helper.getBool('should_save_recipient_address');
2022-10-12 17:09:57 +00:00
if (shouldSave != null) {
await prefs.setBool('save_recipient_address', shouldSave);
}
//translate biometric
final biometricOn = await ios_legacy_helper.getBool('biometric_authentication_on');
2022-10-12 17:09:57 +00:00
if (biometricOn != null) {
await prefs.setBool('allow_biometrical_authentication', biometricOn);
}
//read the current theme as integer, write it back as a bool
2020-09-23 18:26:10 +00:00
final currentTheme = prefs.getInt('current-theme');
bool isDark = false;
if (currentTheme == 1) {
isDark = true;
}
await prefs.setBool('dark_theme', isDark);
2022-03-09 11:43:18 +00:00
//assign the pin length
2020-09-23 18:26:10 +00:00
final pinLength = await ios_legacy_helper.getInt('pin-length');
2022-10-12 17:09:57 +00:00
if (pinLength != null) {
await prefs.setInt(PreferencesKey.currentPinLength, pinLength);
}
//default value for display list key?
2020-09-23 18:26:10 +00:00
final walletName = await ios_legacy_helper.getString('current_wallet_name');
2022-10-12 17:09:57 +00:00
if (walletName != null) {
await prefs.setString('current_wallet_name', walletName);
}
2020-09-23 18:26:10 +00:00
await prefs.setInt('current_wallet_type', serializeToInt(WalletType.monero));
await prefs.setBool('ios_migration_user_defaults_completed', true);
}
2020-09-23 18:26:10 +00:00
Future<void> ios_migrate_pin() async {
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('ios_migration_pin_completed') ?? false) {
return;
}
final flutterSecureStorage = secureStorageShared;
final pinPassword = await flutterSecureStorage.readNoIOptions(key: 'pin_password');
2022-10-12 17:09:57 +00:00
// No pin
if (pinPassword == null) {
await prefs.setBool('ios_migration_pin_completed', true);
return;
}
2020-09-23 18:26:10 +00:00
final key = generateStoreKeyFor(key: SecretStoreKey.pinCodePassword);
final encodedPassword = encodedPinCode(pin: pinPassword);
await flutterSecureStorage.write(key: key, value: encodedPassword);
2020-09-23 18:26:10 +00:00
await prefs.setBool('ios_migration_pin_completed', true);
}
Future<void> ios_migrate_wallet_passwords() async {
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('ios_migration_wallet_passwords_completed') ?? false) {
return;
}
final appDocDir = await getApplicationDocumentsDirectory();
final flutterSecureStorage = secureStorageShared;
2020-09-23 18:26:10 +00:00
final keyService = KeyService(flutterSecureStorage);
final walletsDir = Directory('${appDocDir.path}/wallets');
final moneroWalletsDir = Directory('${walletsDir.path}/monero');
2020-10-12 08:47:20 +00:00
if (!moneroWalletsDir.existsSync() || moneroWalletsDir.listSync().isEmpty) {
await prefs.setBool('ios_migration_wallet_passwords_completed', true);
return;
}
2020-09-23 18:26:10 +00:00
moneroWalletsDir.listSync().forEach((item) async {
try {
if (item is Directory) {
final name = item.path.split('/').last;
final oldKey = 'wallet_monero_' + name + '_password';
final password = await flutterSecureStorage.readNoIOptions(key: oldKey);
await keyService.saveWalletPassword(walletName: name, password: password!);
2020-09-23 18:26:10 +00:00
}
} catch (e) {
printV(e.toString());
2020-09-23 18:26:10 +00:00
}
});
await prefs.setBool('ios_migration_wallet_passwords_completed', true);
}
FiatCurrency convertFiatLegacy(int raw) {
final _map = {
0: 'aud',
1: 'bgn',
2: 'brl',
3: 'cad',
4: 'chf',
5: 'cny',
6: 'czk',
7: 'eur',
8: 'dkk',
9: 'gbp',
10: 'hkd',
11: 'hrk',
12: 'huf',
13: 'idr',
14: 'ils',
15: 'inr',
16: 'isk',
17: 'jpy',
18: 'krw',
19: 'mxn',
20: 'myr',
21: 'nok',
22: 'nzd',
23: 'php',
24: 'pln',
25: 'ron',
26: 'rub',
27: 'sek',
28: 'sgd',
29: 'thb',
30: 'try',
31: 'usd',
32: 'zar',
33: 'vef'
};
2022-10-12 17:09:57 +00:00
final fiatAsString = _map[raw]!;
2020-09-23 18:26:10 +00:00
return FiatCurrency.deserialize(raw: fiatAsString.toUpperCase());
}
2022-10-12 17:09:57 +00:00
Future<void> android_migrate_hives({required Directory appDocDir}) async {
final dbDir = Directory('${appDocDir.path}/db');
2022-10-12 17:09:57 +00:00
final files = <File>[];
appDocDir.listSync().forEach((FileSystemEntity item) {
final ext = item.path.split('.').last;
if (item is File && (ext == "hive" || ext == "lock")) {
files.add(item);
}
});
if (!dbDir.existsSync()) {
dbDir.createSync();
}
files.forEach((File hive) {
final name = hive.path.split('/').last;
hive.copySync('${dbDir.path}/$name');
hive.deleteSync();
});
}
2022-10-12 17:09:57 +00:00
Future<void> android_migrate_wallets({required Directory appDocDir}) async {
final walletsDir = Directory('${appDocDir.path}/wallets');
final moneroWalletsDir = Directory('${walletsDir.path}/monero');
2022-10-12 17:09:57 +00:00
final dirs = <Directory>[];
appDocDir.listSync().forEach((FileSystemEntity item) {
final name = item.path.split('/').last;
if (item is Directory && !reservedNames.contains(name)) {
dirs.add(item);
}
});
if (!moneroWalletsDir.existsSync()) {
await moneroWalletsDir.create(recursive: true);
}
dirs.forEach((Directory dir) {
final name = dir.path.split('/').last;
final newDir = Directory('${moneroWalletsDir.path}/$name');
newDir.createSync();
dir.listSync().forEach((file) {
if (file is File) {
final fileName = file.path.split('/').last;
file.copySync('${newDir.path}/$fileName');
file.deleteSync();
}
});
dir.deleteSync();
});
}
2020-04-03 13:21:00 +00:00
2020-09-23 18:26:10 +00:00
Future<void> ios_migrate_wallet_info(Box<WalletInfo> walletsInfoSource) async {
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('ios_migration_wallet_info_completed') ?? false) {
return;
}
try {
final appDocDir = await getApplicationDocumentsDirectory();
final walletsDir = Directory('${appDocDir.path}/wallets');
final moneroWalletsDir = Directory('${walletsDir.path}/monero');
final infoRecords = moneroWalletsDir
.listSync()
.map((item) {
try {
if (item is Directory) {
final name = item.path.split('/').last;
final configFile = File('${item.path}/$name.json');
if (!configFile.existsSync()) {
return null;
}
final config = json.decode(configFile.readAsStringSync()) as Map<String, dynamic>;
final isRecovery = config['isRecovery'] as bool? ?? false;
2020-09-23 18:26:10 +00:00
final dateAsDouble = config['date'] as double;
final timestamp = dateAsDouble.toInt() * 1000;
final date = DateTime.fromMillisecondsSinceEpoch(timestamp);
final id = walletTypeToString(WalletType.monero).toLowerCase() + '_' + name;
final exist = walletsInfoSource.values.firstWhereOrNull((el) => el.id == id) != null;
2020-09-23 18:26:10 +00:00
if (exist) {
return null;
}
final walletInfo = WalletInfo.external(
id: id,
type: WalletType.monero,
name: name,
isRecovery: isRecovery,
restoreHeight: 0,
date: date,
dirPath: item.path,
2022-10-12 17:09:57 +00:00
path: '${item.path}/$name',
address: '');
2020-09-23 18:26:10 +00:00
return walletInfo;
}
} catch (e) {
printV(e.toString());
2020-09-23 18:26:10 +00:00
return null;
}
})
.where((el) => el != null)
2022-10-12 17:09:57 +00:00
.whereType<WalletInfo>()
2020-09-23 18:26:10 +00:00
.toList();
await walletsInfoSource.addAll(infoRecords);
await prefs.setBool('ios_migration_wallet_info_completed', true);
} catch (e) {
printV(e.toString());
2020-09-23 18:26:10 +00:00
}
}
2020-09-23 18:26:10 +00:00
Future<void> ios_migrate_trades_list(Box<Trade> tradeSource) async {
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('ios_migration_trade_list_completed') ?? false) {
return;
}
try {
final appDocDir = await getApplicationDocumentsDirectory();
final url = '${appDocDir.path}/trades_list.json';
final file = File(url);
if (!file.existsSync()) {
await prefs.setBool('ios_migration_trade_list_completed', true);
return;
}
final content = file.readAsBytesSync();
final flutterSecureStorage = secureStorageShared;
final masterPassword = await flutterSecureStorage.readNoIOptions(key: 'master_password');
2022-10-12 17:09:57 +00:00
final key = masterPassword!.replaceAll('-', '');
final decoded = await ios_legacy_helper.decrypt(content, key: key, salt: secrets.salt);
2020-09-23 18:26:10 +00:00
final decodedJson = json.decode(decoded) as List<dynamic>;
final trades = decodedJson.map((dynamic el) {
final elAsMap = el as Map<String, dynamic>;
final providerAsString = elAsMap['provider'] as String;
final fromAsString = elAsMap['from'] as String;
final toAsString = elAsMap['to'] as String;
final dateAsDouble = elAsMap['date'] as double;
final tradeId = elAsMap['tradeID'] as String;
final to = CryptoCurrency.fromString(toAsString);
final from = CryptoCurrency.fromString(fromAsString);
final timestamp = dateAsDouble.toInt() * 1000;
final date = DateTime.fromMillisecondsSinceEpoch(timestamp);
2022-10-12 17:09:57 +00:00
ExchangeProviderDescription? provider;
2020-09-23 18:26:10 +00:00
switch (providerAsString.toLowerCase()) {
case 'changenow':
provider = ExchangeProviderDescription.changeNow;
break;
case 'xmr.to':
provider = ExchangeProviderDescription.xmrto;
break;
case 'morph':
provider = ExchangeProviderDescription.morphToken;
break;
default:
break;
}
return Trade(
id: tradeId,
provider: provider!,
from: from,
to: to,
createdAt: date,
amount: '',
receiveAmount: '',
);
2020-09-23 18:26:10 +00:00
});
await tradeSource.addAll(trades);
await prefs.setBool('ios_migration_trade_list_completed', true);
} catch (e) {
printV(e.toString());
2020-09-23 18:26:10 +00:00
}
}
2020-09-23 18:26:10 +00:00
Future<void> ios_migrate_address_book(Box<Contact> contactSource) async {
2020-11-09 23:14:47 +00:00
try {
final prefs = await SharedPreferences.getInstance();
2020-10-12 08:47:20 +00:00
2020-11-09 23:14:47 +00:00
if (prefs.getBool('ios_migration_address_book_completed') ?? false) {
return;
}
2020-09-23 18:26:10 +00:00
2020-11-09 23:14:47 +00:00
final appDocDir = await getApplicationDocumentsDirectory();
final addressBookJSON = File('${appDocDir.path}/address_book.json');
2020-09-23 18:26:10 +00:00
2020-11-09 23:14:47 +00:00
if (!addressBookJSON.existsSync()) {
await prefs.setBool('ios_migration_address_book_completed', true);
return;
}
2020-11-09 23:14:47 +00:00
final List<dynamic> addresses =
json.decode(addressBookJSON.readAsStringSync()) as List<dynamic>;
2020-11-09 23:14:47 +00:00
final contacts = addresses.map((dynamic item) {
final _item = item as Map<String, dynamic>;
final type = _item["type"] as String;
final address = _item["address"] as String;
final name = _item["name"] as String;
return Contact(address: address, name: name, type: CryptoCurrency.fromString(type));
2020-11-09 23:14:47 +00:00
});
2020-09-23 18:26:10 +00:00
2020-11-09 23:14:47 +00:00
await contactSource.addAll(contacts);
await prefs.setBool('ios_migration_address_book_completed', true);
} catch (e) {
printV(e.toString());
2020-11-09 23:14:47 +00:00
}
}