2020-07-06 20:09:03 +00:00
|
|
|
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/common/node.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/common/balance_display_mode.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/common/fiat_currency.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/common/node_list.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/common/transaction_priority.dart';
|
|
|
|
|
|
|
|
Future defaultSettingsMigration(
|
|
|
|
{@required int version,
|
|
|
|
@required SharedPreferences sharedPreferences,
|
|
|
|
@required Box<Node> nodes}) async {
|
2020-01-08 12:26:34 +00:00
|
|
|
final currentVersion =
|
2020-01-04 19:31:52 +00:00
|
|
|
sharedPreferences.getInt('current_default_settings_migration_version') ??
|
|
|
|
0;
|
|
|
|
if (currentVersion >= version) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-08 16:38:38 +00:00
|
|
|
final migrationVersionsLength = version - currentVersion;
|
|
|
|
final migrationVersions = List<int>.generate(
|
|
|
|
migrationVersionsLength, (i) => currentVersion + (i + 1));
|
|
|
|
|
|
|
|
await Future.forEach(migrationVersions, (int version) async {
|
|
|
|
try {
|
|
|
|
switch (version) {
|
|
|
|
case 1:
|
|
|
|
await sharedPreferences.setString(
|
2020-08-27 16:54:34 +00:00
|
|
|
SettingsStoreBase.currentFiatCurrencyKey,
|
|
|
|
FiatCurrency.usd.toString());
|
2020-07-06 20:09:03 +00:00
|
|
|
await sharedPreferences.setInt(
|
2020-08-27 16:54:34 +00:00
|
|
|
SettingsStoreBase.currentTransactionPriorityKey,
|
|
|
|
TransactionPriority.standart.raw);
|
2020-01-08 16:38:38 +00:00
|
|
|
await sharedPreferences.setInt(
|
2020-07-06 20:09:03 +00:00
|
|
|
SettingsStoreBase.currentBalanceDisplayModeKey,
|
2020-01-08 16:38:38 +00:00
|
|
|
BalanceDisplayMode.availableBalance.raw);
|
2020-01-20 19:04:26 +00:00
|
|
|
await sharedPreferences.setBool('save_recipient_address', true);
|
2020-01-08 16:38:38 +00:00
|
|
|
await resetToDefault(nodes);
|
2020-08-27 16:54:34 +00:00
|
|
|
await changeMoneroCurrentNodeToDefault(
|
|
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
|
|
await changeBitcoinCurrentElectrumServerToDefault(
|
2020-01-08 16:38:38 +00:00
|
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
await replaceNodesMigration(nodes: nodes);
|
|
|
|
await replaceDefaultNode(
|
|
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
|
|
|
2020-07-06 20:09:03 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
await updateNodeTypes(nodes: nodes);
|
|
|
|
await addBitcoinElectrumServerList(nodes: nodes);
|
2020-08-27 16:54:34 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
await changeBitcoinCurrentElectrumServerToDefault(
|
|
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
2020-01-08 16:38:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
await sharedPreferences.setInt(
|
|
|
|
'current_default_settings_migration_version', version);
|
|
|
|
} catch (e) {
|
|
|
|
print('Migration error: ${e.toString()}');
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
2020-01-08 16:38:38 +00:00
|
|
|
});
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
await sharedPreferences.setInt(
|
2020-01-04 19:31:52 +00:00
|
|
|
'current_default_settings_migration_version', version);
|
|
|
|
}
|
2020-01-08 16:38:38 +00:00
|
|
|
|
|
|
|
Future<void> replaceNodesMigration({@required Box<Node> nodes}) async {
|
|
|
|
final replaceNodes = <String, Node>{
|
|
|
|
'eu-node.cakewallet.io:18081':
|
2020-08-27 16:54:34 +00:00
|
|
|
Node(uri: 'xmr-node-eu.cakewallet.com:18081', type: WalletType.monero),
|
|
|
|
'node.cakewallet.io:18081': Node(
|
|
|
|
uri: 'xmr-node-usa-east.cakewallet.com:18081', type: WalletType.monero),
|
|
|
|
'node.xmr.ru:13666':
|
|
|
|
Node(uri: 'node.monero.net:18081', type: WalletType.monero)
|
2020-01-08 16:38:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
nodes.values.forEach((Node node) async {
|
|
|
|
final nodeToReplace = replaceNodes[node.uri];
|
|
|
|
|
|
|
|
if (nodeToReplace != null) {
|
|
|
|
node.uri = nodeToReplace.uri;
|
|
|
|
node.login = nodeToReplace.login;
|
|
|
|
node.password = nodeToReplace.password;
|
|
|
|
await node.save();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:54:34 +00:00
|
|
|
Future<void> changeMoneroCurrentNodeToDefault(
|
2020-01-08 16:38:38 +00:00
|
|
|
{@required SharedPreferences sharedPreferences,
|
|
|
|
@required Box<Node> nodes}) async {
|
2020-08-27 16:54:34 +00:00
|
|
|
final node = getMoneroDefaultNode(nodes: nodes);
|
|
|
|
final nodeId = node?.key as int ?? 0; // 0 - England
|
|
|
|
|
|
|
|
await sharedPreferences.setInt('current_node_id', nodeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node getBitcoinDefaultElectrumServer({@required Box<Node> nodes}) {
|
|
|
|
final uri = 'bitcoin.electrumx.multicoin.co:50002';
|
|
|
|
|
|
|
|
return nodes.values
|
|
|
|
.firstWhere((Node node) => node.uri == uri, orElse: () => null) ??
|
|
|
|
nodes.values.firstWhere((node) => node.type == WalletType.bitcoin,
|
|
|
|
orElse: () => null);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node getMoneroDefaultNode({@required Box<Node> nodes}) {
|
2020-01-08 16:38:38 +00:00
|
|
|
final timeZone = DateTime.now().timeZoneOffset.inHours;
|
2020-08-27 16:54:34 +00:00
|
|
|
var nodeUri = '';
|
2020-01-08 16:38:38 +00:00
|
|
|
|
2020-07-06 20:09:03 +00:00
|
|
|
if (timeZone >= 1) {
|
|
|
|
// Eurasia
|
2020-01-08 16:38:38 +00:00
|
|
|
nodeUri = 'xmr-node-eu.cakewallet.com:18081';
|
2020-07-06 20:09:03 +00:00
|
|
|
} else if (timeZone <= -4) {
|
|
|
|
// America
|
2020-01-08 16:38:38 +00:00
|
|
|
nodeUri = 'xmr-node-usa-east.cakewallet.com:18081';
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:54:34 +00:00
|
|
|
return nodes.values
|
|
|
|
.firstWhere((Node node) => node.uri == nodeUri, orElse: () => null) ??
|
2020-01-08 16:38:38 +00:00
|
|
|
nodes.values.first;
|
2020-08-27 16:54:34 +00:00
|
|
|
}
|
2020-01-08 16:38:38 +00:00
|
|
|
|
2020-08-27 16:54:34 +00:00
|
|
|
Future<void> changeBitcoinCurrentElectrumServerToDefault(
|
|
|
|
{@required SharedPreferences sharedPreferences,
|
|
|
|
@required Box<Node> nodes}) async {
|
|
|
|
final server = getBitcoinDefaultElectrumServer(nodes: nodes);
|
|
|
|
final serverId = server?.key as int ?? 0;
|
|
|
|
|
|
|
|
await sharedPreferences.setInt('current_node_id_btc', serverId);
|
2020-01-08 16:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> replaceDefaultNode(
|
|
|
|
{@required SharedPreferences sharedPreferences,
|
|
|
|
@required Box<Node> nodes}) async {
|
|
|
|
const nodesForReplace = <String>[
|
|
|
|
'xmr-node-uk.cakewallet.com:18081',
|
|
|
|
'eu-node.cakewallet.io:18081',
|
|
|
|
'node.cakewallet.io:18081'
|
|
|
|
];
|
|
|
|
final currentNodeId = sharedPreferences.getInt('current_node_id');
|
|
|
|
final currentNode =
|
|
|
|
nodes.values.firstWhere((Node node) => node.key == currentNodeId);
|
|
|
|
final needToReplace =
|
|
|
|
currentNode == null ? true : nodesForReplace.contains(currentNode.uri);
|
|
|
|
|
|
|
|
if (!needToReplace) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:54:34 +00:00
|
|
|
await changeMoneroCurrentNodeToDefault(
|
2020-01-08 16:38:38 +00:00
|
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
|
|
}
|
2020-07-06 20:09:03 +00:00
|
|
|
|
|
|
|
Future<void> updateNodeTypes({@required Box<Node> nodes}) async {
|
|
|
|
nodes.values.forEach((node) async {
|
|
|
|
if (node.type == null) {
|
|
|
|
node.type = WalletType.monero;
|
|
|
|
await node.save();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> addBitcoinElectrumServerList({@required Box<Node> nodes}) async {
|
|
|
|
final serverList = await loadElectrumServerList();
|
|
|
|
await nodes.addAll(serverList);
|
|
|
|
}
|