2023-08-30 15:11:56 +00:00
|
|
|
import 'dart:io' show Directory, File, Platform;
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin.dart';
|
2023-03-01 21:44:15 +00:00
|
|
|
import 'package:cake_wallet/entities/exchange_api_mode.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/pathForWallet.dart';
|
2021-01-15 17:41:30 +00:00
|
|
|
import 'package:cake_wallet/entities/secret_store_key.dart';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
import 'package:hive/hive.dart';
|
2023-08-30 15:11:56 +00:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/preferences_key.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/wallet_type.dart';
|
|
|
|
import 'package:cw_core/node.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/balance_display_mode.dart';
|
|
|
|
import 'package:cake_wallet/entities/fiat_currency.dart';
|
|
|
|
import 'package:cake_wallet/entities/node_list.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cake_wallet/monero/monero.dart';
|
2020-09-23 18:26:10 +00:00
|
|
|
import 'package:cake_wallet/entities/contact.dart';
|
|
|
|
import 'package:cake_wallet/entities/fs_migration.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/wallet_info.dart';
|
2020-09-23 18:26:10 +00:00
|
|
|
import 'package:cake_wallet/exchange/trade.dart';
|
2021-01-15 17:41:30 +00:00
|
|
|
import 'package:encrypt/encrypt.dart' as encrypt;
|
2022-10-12 17:09:57 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2021-02-01 18:12:37 +00:00
|
|
|
const newCakeWalletMoneroUri = 'xmr-node.cakewallet.com:18081';
|
2021-05-07 07:36:38 +00:00
|
|
|
const cakeWalletBitcoinElectrumUri = 'electrum.cakewallet.com:50002';
|
2021-05-11 16:57:12 +00:00
|
|
|
const cakeWalletLitecoinElectrumUri = 'ltc-electrum.cakewallet.com:50002';
|
2022-05-18 16:13:58 +00:00
|
|
|
const havenDefaultNodeUri = 'nodes.havenprotocol.org:443';
|
2023-08-04 17:01:49 +00:00
|
|
|
const ethereumDefaultNodeUri = 'ethereum.publicnode.com';
|
2023-10-02 14:17:35 +00:00
|
|
|
const zanoDefaultNodeUri = 'zano.org';
|
2023-10-15 13:36:05 +00:00
|
|
|
const dummyDefaultNodeUri = 'example.com';
|
2021-02-01 18:12:37 +00:00
|
|
|
|
2023-08-30 15:11:56 +00:00
|
|
|
Future<void> defaultSettingsMigration(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required int version,
|
|
|
|
required SharedPreferences sharedPreferences,
|
|
|
|
required FlutterSecureStorage secureStorage,
|
|
|
|
required Box<Node> nodes,
|
|
|
|
required Box<WalletInfo> walletInfoSource,
|
|
|
|
required Box<Trade> tradeSource,
|
|
|
|
required Box<Contact> contactSource}) async {
|
2020-09-23 18:26:10 +00:00
|
|
|
if (Platform.isIOS) {
|
|
|
|
await ios_migrate_v1(walletInfoSource, tradeSource, contactSource);
|
|
|
|
}
|
|
|
|
|
2023-03-17 19:15:28 +00:00
|
|
|
// check current nodes for nullability regardless of the version
|
|
|
|
await checkCurrentNodes(nodes, sharedPreferences);
|
|
|
|
|
2023-08-30 15:11:56 +00:00
|
|
|
await _validateWalletInfoBoxData(walletInfoSource);
|
|
|
|
|
2023-04-21 15:21:31 +00:00
|
|
|
final isNewInstall = sharedPreferences
|
|
|
|
.getInt(PreferencesKey.currentDefaultSettingsMigrationVersion) == null;
|
|
|
|
|
|
|
|
await sharedPreferences.setBool(
|
|
|
|
PreferencesKey.isNewInstall, isNewInstall);
|
|
|
|
|
|
|
|
|
2021-01-15 17:41:30 +00:00
|
|
|
final currentVersion = sharedPreferences
|
|
|
|
.getInt(PreferencesKey.currentDefaultSettingsMigrationVersion) ??
|
|
|
|
0;
|
2020-01-04 19:31:52 +00:00
|
|
|
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-09-21 11:50:26 +00:00
|
|
|
PreferencesKey.currentFiatCurrencyKey,
|
2020-08-27 16:54:34 +00:00
|
|
|
FiatCurrency.usd.toString());
|
2020-07-06 20:09:03 +00:00
|
|
|
await sharedPreferences.setInt(
|
2021-01-27 13:51:51 +00:00
|
|
|
PreferencesKey.currentTransactionPriorityKeyLegacy,
|
2022-10-12 17:09:57 +00:00
|
|
|
monero!.getDefaultTransactionPriority().raw);
|
2020-01-08 16:38:38 +00:00
|
|
|
await sharedPreferences.setInt(
|
2020-09-21 11:50:26 +00:00
|
|
|
PreferencesKey.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);
|
2021-05-07 07:36:38 +00:00
|
|
|
await changeLitecoinCurrentElectrumServerToDefault(
|
|
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
2022-11-26 14:13:54 +00:00
|
|
|
await changeHavenCurrentNodeToDefault(
|
|
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
2020-01-08 16:38:38 +00:00
|
|
|
|
|
|
|
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;
|
2020-09-23 18:26:10 +00:00
|
|
|
|
2021-01-08 18:10:37 +00:00
|
|
|
case 5:
|
|
|
|
await addAddressesForMoneroWallets(walletInfoSource);
|
|
|
|
break;
|
2021-01-11 17:15:27 +00:00
|
|
|
|
|
|
|
case 6:
|
|
|
|
await updateDisplayModes(sharedPreferences);
|
|
|
|
break;
|
|
|
|
|
2021-01-15 17:41:30 +00:00
|
|
|
case 9:
|
|
|
|
await generateBackupPassword(secureStorage);
|
|
|
|
break;
|
2021-05-07 07:36:38 +00:00
|
|
|
|
2021-01-27 13:51:51 +00:00
|
|
|
case 10:
|
|
|
|
await changeTransactionPriorityAndFeeRateKeys(sharedPreferences);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
await changeDefaultMoneroNode(nodes, sharedPreferences);
|
|
|
|
break;
|
2021-01-15 17:41:30 +00:00
|
|
|
|
2021-02-01 18:12:37 +00:00
|
|
|
case 12:
|
|
|
|
await checkCurrentNodes(nodes, sharedPreferences);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 13:
|
2021-05-07 07:36:38 +00:00
|
|
|
await resetBitcoinElectrumServer(nodes, sharedPreferences);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 15:
|
|
|
|
await addLitecoinElectrumServerList(nodes: nodes);
|
|
|
|
await changeLitecoinCurrentElectrumServerToDefault(
|
|
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
|
|
await checkCurrentNodes(nodes, sharedPreferences);
|
2021-02-01 18:12:37 +00:00
|
|
|
break;
|
|
|
|
|
2022-03-30 15:57:04 +00:00
|
|
|
case 16:
|
|
|
|
await addHavenNodeList(nodes: nodes);
|
|
|
|
await changeHavenCurrentNodeToDefault(
|
|
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
|
|
await checkCurrentNodes(nodes, sharedPreferences);
|
|
|
|
break;
|
|
|
|
|
2022-05-18 16:13:58 +00:00
|
|
|
case 17:
|
|
|
|
await changeDefaultHavenNode(nodes);
|
|
|
|
break;
|
|
|
|
|
2022-11-29 23:37:56 +00:00
|
|
|
case 18:
|
|
|
|
await addOnionNode(nodes);
|
|
|
|
break;
|
|
|
|
|
2022-12-15 00:00:59 +00:00
|
|
|
case 19:
|
|
|
|
await validateBitcoinSavedTransactionPriority(sharedPreferences);
|
|
|
|
break;
|
2023-03-01 21:44:15 +00:00
|
|
|
case 20:
|
|
|
|
await migrateExchangeStatus(sharedPreferences);
|
|
|
|
break;
|
2023-08-04 17:01:49 +00:00
|
|
|
case 21:
|
|
|
|
await addEthereumNodeList(nodes: nodes);
|
|
|
|
await changeEthereumCurrentNodeToDefault(
|
|
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
|
|
break;
|
|
|
|
|
2020-01-08 16:38:38 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
await sharedPreferences.setInt(
|
2022-11-29 23:37:56 +00:00
|
|
|
PreferencesKey.currentDefaultSettingsMigrationVersion, version);
|
2020-01-08 16:38:38 +00:00
|
|
|
} 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(
|
2022-11-29 23:37:56 +00:00
|
|
|
PreferencesKey.currentDefaultSettingsMigrationVersion, version);
|
|
|
|
}
|
|
|
|
|
2023-08-30 15:11:56 +00:00
|
|
|
Future<void> _validateWalletInfoBoxData(Box<WalletInfo> walletInfoSource) async {
|
|
|
|
final root = await getApplicationDocumentsDirectory();
|
|
|
|
|
|
|
|
for (var type in WalletType.values) {
|
|
|
|
if (type == WalletType.none) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String prefix = walletTypeToString(type).toLowerCase();
|
|
|
|
Directory walletsDir = Directory('${root.path}/wallets/$prefix/');
|
|
|
|
|
|
|
|
if (!walletsDir.existsSync()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> walletNames = walletsDir.listSync().map((e) => e.path.split("/").last).toList();
|
|
|
|
|
|
|
|
for (var name in walletNames) {
|
|
|
|
final dir = Directory(await pathForWalletDir(name: name, type: type));
|
|
|
|
|
|
|
|
final walletFiles = dir.listSync();
|
|
|
|
final hasCacheFile = walletFiles.any((element) => element.path.contains("$name/$name"));
|
|
|
|
|
|
|
|
if (!hasCacheFile) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == WalletType.monero || type == WalletType.haven) {
|
|
|
|
final hasKeysFile = walletFiles.any((element) => element.path.contains(".keys"));
|
|
|
|
|
|
|
|
if (!hasKeysFile) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final id = prefix + '_' + name;
|
|
|
|
final exist = walletInfoSource.values.any((el) => el.id == id);
|
|
|
|
|
|
|
|
if (exist) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
final walletInfo = WalletInfo.external(
|
|
|
|
id: id,
|
|
|
|
type: type,
|
|
|
|
name: name,
|
|
|
|
isRecovery: true,
|
|
|
|
restoreHeight: 0,
|
|
|
|
date: DateTime.now(),
|
|
|
|
dirPath: dir.path,
|
|
|
|
path: '${dir.path}/$name',
|
|
|
|
address: '',
|
|
|
|
showIntroCakePayCard: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
walletInfoSource.add(walletInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 00:00:59 +00:00
|
|
|
Future<void> validateBitcoinSavedTransactionPriority(SharedPreferences sharedPreferences) async {
|
2022-12-15 00:29:30 +00:00
|
|
|
if (bitcoin == null) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-15 00:05:00 +00:00
|
|
|
final int? savedBitcoinPriority =
|
|
|
|
sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority);
|
2022-12-15 00:29:30 +00:00
|
|
|
if (!bitcoin!.getTransactionPriorities().any((element) => element.raw == savedBitcoinPriority)) {
|
2022-12-15 00:00:59 +00:00
|
|
|
await sharedPreferences.setInt(
|
2022-12-15 00:29:30 +00:00
|
|
|
PreferencesKey.bitcoinTransactionPriority, bitcoin!.getMediumTransactionPriority().serialize());
|
2022-12-15 00:00:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 23:37:56 +00:00
|
|
|
Future<void> addOnionNode(Box<Node> nodes) async {
|
|
|
|
final onionNodeUri = "cakexmrl7bonq7ovjka5kuwuyd3f7qnkz6z6s6dmsy3uckwra7bvggyd.onion:18081";
|
|
|
|
|
2022-12-01 12:29:03 +00:00
|
|
|
// check if the user has this node before (added it manually)
|
|
|
|
if (nodes.values.firstWhereOrNull((element) => element.uriRaw == onionNodeUri) == null) {
|
|
|
|
await nodes.add(Node(uri: onionNodeUri, type: WalletType.monero));
|
|
|
|
}
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
2020-01-08 16:38:38 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> replaceNodesMigration({required Box<Node> nodes}) async {
|
2020-01-08 16:38:38 +00:00
|
|
|
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) {
|
2021-05-10 16:00:20 +00:00
|
|
|
node.uriRaw = nodeToReplace.uriRaw;
|
2020-01-08 16:38:38 +00:00
|
|
|
node.login = nodeToReplace.login;
|
|
|
|
node.password = nodeToReplace.password;
|
|
|
|
await node.save();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:54:34 +00:00
|
|
|
Future<void> changeMoneroCurrentNodeToDefault(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required SharedPreferences sharedPreferences,
|
|
|
|
required Box<Node> nodes}) async {
|
2020-08-27 16:54:34 +00:00
|
|
|
final node = getMoneroDefaultNode(nodes: nodes);
|
2023-08-30 15:11:56 +00:00
|
|
|
final nodeId = node.key as int? ?? 0; // 0 - England
|
2020-08-27 16:54:34 +00:00
|
|
|
|
2022-11-29 23:37:56 +00:00
|
|
|
await sharedPreferences.setInt(PreferencesKey.currentNodeIdKey, nodeId);
|
2020-08-27 16:54:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Node? getBitcoinDefaultElectrumServer({required Box<Node> nodes}) {
|
|
|
|
return nodes.values.firstWhereOrNull(
|
|
|
|
(Node node) => node.uriRaw == cakeWalletBitcoinElectrumUri)
|
|
|
|
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.bitcoin);
|
2020-08-27 16:54:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Node? getLitecoinDefaultElectrumServer({required Box<Node> nodes}) {
|
|
|
|
return nodes.values.firstWhereOrNull(
|
|
|
|
(Node node) => node.uriRaw == cakeWalletLitecoinElectrumUri)
|
|
|
|
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.litecoin);
|
2021-05-07 07:36:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Node? getHavenDefaultNode({required Box<Node> nodes}) {
|
|
|
|
return nodes.values.firstWhereOrNull(
|
|
|
|
(Node node) => node.uriRaw == havenDefaultNodeUri)
|
|
|
|
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.haven);
|
2022-03-30 15:57:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 17:01:49 +00:00
|
|
|
Node? getEthereumDefaultNode({required Box<Node> nodes}) {
|
|
|
|
return nodes.values.firstWhereOrNull(
|
|
|
|
(Node node) => node.uriRaw == ethereumDefaultNodeUri)
|
|
|
|
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.ethereum);
|
|
|
|
}
|
|
|
|
|
2023-10-15 13:36:05 +00:00
|
|
|
Node? getZanoDefaultNode({required Box<Node> nodes}) {
|
|
|
|
return nodes.values.firstWhereOrNull(
|
|
|
|
(Node node) => node.uriRaw == zanoDefaultNodeUri)
|
|
|
|
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.zano);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node? getDummyDefaultNode({required Box<Node> nodes}) {
|
|
|
|
return nodes.values.firstWhereOrNull(
|
|
|
|
(Node node) => node.uriRaw == dummyDefaultNodeUri)
|
|
|
|
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.dummy);
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
try {
|
|
|
|
return nodes.values
|
|
|
|
.firstWhere((Node node) => node.uriRaw == nodeUri);
|
|
|
|
} catch(_) {
|
|
|
|
return 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(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required SharedPreferences sharedPreferences,
|
|
|
|
required Box<Node> nodes}) async {
|
2020-08-27 16:54:34 +00:00
|
|
|
final server = getBitcoinDefaultElectrumServer(nodes: nodes);
|
2023-08-30 15:11:56 +00:00
|
|
|
final serverId = server?.key as int? ?? 0;
|
2020-08-27 16:54:34 +00:00
|
|
|
|
2022-11-29 23:37:56 +00:00
|
|
|
await sharedPreferences.setInt(PreferencesKey.currentBitcoinElectrumSererIdKey, serverId);
|
2020-01-08 16:38:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
Future<void> changeLitecoinCurrentElectrumServerToDefault(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required SharedPreferences sharedPreferences,
|
|
|
|
required Box<Node> nodes}) async {
|
2021-05-07 07:36:38 +00:00
|
|
|
final server = getLitecoinDefaultElectrumServer(nodes: nodes);
|
2023-08-30 15:11:56 +00:00
|
|
|
final serverId = server?.key as int? ?? 0;
|
2021-05-07 07:36:38 +00:00
|
|
|
|
2022-11-29 23:37:56 +00:00
|
|
|
await sharedPreferences.setInt(PreferencesKey.currentLitecoinElectrumSererIdKey, serverId);
|
2021-05-07 07:36:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 15:57:04 +00:00
|
|
|
Future<void> changeHavenCurrentNodeToDefault(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required SharedPreferences sharedPreferences,
|
|
|
|
required Box<Node> nodes}) async {
|
2022-03-30 15:57:04 +00:00
|
|
|
final node = getHavenDefaultNode(nodes: nodes);
|
2023-08-30 15:11:56 +00:00
|
|
|
final nodeId = node?.key as int? ?? 0;
|
2022-03-30 15:57:04 +00:00
|
|
|
|
|
|
|
await sharedPreferences.setInt(PreferencesKey.currentHavenNodeIdKey, nodeId);
|
|
|
|
}
|
|
|
|
|
2020-01-08 16:38:38 +00:00
|
|
|
Future<void> replaceDefaultNode(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required SharedPreferences sharedPreferences,
|
|
|
|
required Box<Node> nodes}) async {
|
2020-01-08 16:38:38 +00:00
|
|
|
const nodesForReplace = <String>[
|
|
|
|
'xmr-node-uk.cakewallet.com:18081',
|
|
|
|
'eu-node.cakewallet.io:18081',
|
|
|
|
'node.cakewallet.io:18081'
|
|
|
|
];
|
2022-11-29 23:37:56 +00:00
|
|
|
final currentNodeId = sharedPreferences.getInt(PreferencesKey.currentNodeIdKey);
|
2020-01-08 16:38:38 +00:00
|
|
|
final currentNode =
|
2022-10-12 17:09:57 +00:00
|
|
|
nodes.values.firstWhereOrNull((Node node) => node.key == currentNodeId);
|
2020-01-08 16:38:38 +00:00
|
|
|
final needToReplace =
|
2022-10-12 17:09:57 +00:00
|
|
|
currentNode == null ? true : nodesForReplace.contains(currentNode.uriRaw);
|
2020-01-08 16:38:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> updateNodeTypes({required Box<Node> nodes}) async {
|
2020-07-06 20:09:03 +00:00
|
|
|
nodes.values.forEach((node) async {
|
|
|
|
if (node.type == null) {
|
|
|
|
node.type = WalletType.monero;
|
|
|
|
await node.save();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> addBitcoinElectrumServerList({required Box<Node> nodes}) async {
|
2021-05-07 07:36:38 +00:00
|
|
|
final serverList = await loadBitcoinElectrumServerList();
|
2022-11-26 14:13:54 +00:00
|
|
|
for (var node in serverList) {
|
|
|
|
if (nodes.values.firstWhereOrNull((element) => element.uriRaw == node.uriRaw) == null) {
|
|
|
|
await nodes.add(node);
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 07:36:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> addLitecoinElectrumServerList({required Box<Node> nodes}) async {
|
2021-05-07 07:36:38 +00:00
|
|
|
final serverList = await loadLitecoinElectrumServerList();
|
2022-11-26 14:13:54 +00:00
|
|
|
for (var node in serverList) {
|
|
|
|
if (nodes.values.firstWhereOrNull((element) => element.uriRaw == node.uriRaw) == null) {
|
|
|
|
await nodes.add(node);
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 20:09:03 +00:00
|
|
|
}
|
2021-01-08 18:10:37 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> addHavenNodeList({required Box<Node> nodes}) async {
|
2022-03-30 15:57:04 +00:00
|
|
|
final nodeList = await loadDefaultHavenNodes();
|
2022-11-26 14:13:54 +00:00
|
|
|
for (var node in nodeList) {
|
|
|
|
if (nodes.values.firstWhereOrNull((element) => element.uriRaw == node.uriRaw) == null) {
|
|
|
|
await nodes.add(node);
|
|
|
|
}
|
|
|
|
}
|
2022-03-30 15:57:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 18:10:37 +00:00
|
|
|
Future<void> addAddressesForMoneroWallets(
|
|
|
|
Box<WalletInfo> walletInfoSource) async {
|
|
|
|
final moneroWalletsInfo =
|
|
|
|
walletInfoSource.values.where((info) => info.type == WalletType.monero);
|
|
|
|
moneroWalletsInfo.forEach((info) async {
|
|
|
|
try {
|
|
|
|
final walletPath =
|
|
|
|
await pathForWallet(name: info.name, type: WalletType.monero);
|
|
|
|
final addressFilePath = '$walletPath.address.txt';
|
|
|
|
final addressFile = File(addressFilePath);
|
|
|
|
|
|
|
|
if (!addressFile.existsSync()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final addressText = await addressFile.readAsString();
|
|
|
|
info.address = addressText;
|
|
|
|
await info.save();
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-01-11 17:15:27 +00:00
|
|
|
|
|
|
|
Future<void> updateDisplayModes(SharedPreferences sharedPreferences) async {
|
|
|
|
final currentBalanceDisplayMode =
|
2022-10-12 17:09:57 +00:00
|
|
|
sharedPreferences.getInt(PreferencesKey.currentBalanceDisplayModeKey) ?? -1;
|
2021-01-11 17:15:27 +00:00
|
|
|
final balanceDisplayMode = currentBalanceDisplayMode < 2 ? 3 : 2;
|
2021-01-15 17:41:30 +00:00
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentBalanceDisplayModeKey, balanceDisplayMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> generateBackupPassword(FlutterSecureStorage secureStorage) async {
|
|
|
|
final key = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
|
|
|
|
|
|
|
|
if ((await secureStorage.read(key: key))?.isNotEmpty ?? false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final password = encrypt.Key.fromSecureRandom(32).base16;
|
|
|
|
await secureStorage.write(key: key, value: password);
|
2021-01-11 17:15:27 +00:00
|
|
|
}
|
2021-01-27 13:51:51 +00:00
|
|
|
|
|
|
|
Future<void> changeTransactionPriorityAndFeeRateKeys(
|
|
|
|
SharedPreferences sharedPreferences) async {
|
|
|
|
final legacyTransactionPriority = sharedPreferences
|
2022-10-12 17:09:57 +00:00
|
|
|
.getInt(PreferencesKey.currentTransactionPriorityKeyLegacy)!;
|
2021-01-27 13:51:51 +00:00
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.moneroTransactionPriority, legacyTransactionPriority);
|
|
|
|
await sharedPreferences.setInt(PreferencesKey.bitcoinTransactionPriority,
|
2022-10-12 17:09:57 +00:00
|
|
|
bitcoin!.getMediumTransactionPriority().serialize());
|
2021-01-27 13:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> changeDefaultMoneroNode(
|
|
|
|
Box<Node> nodeSource, SharedPreferences sharedPreferences) async {
|
|
|
|
const cakeWalletMoneroNodeUriPattern = '.cakewallet.com';
|
2021-05-07 07:36:38 +00:00
|
|
|
final currentMoneroNodeId =
|
|
|
|
sharedPreferences.getInt(PreferencesKey.currentNodeIdKey);
|
|
|
|
final currentMoneroNode =
|
|
|
|
nodeSource.values.firstWhere((node) => node.key == currentMoneroNodeId);
|
|
|
|
final needToReplaceCurrentMoneroNode =
|
2021-05-10 16:00:20 +00:00
|
|
|
currentMoneroNode.uri.toString().contains(cakeWalletMoneroNodeUriPattern);
|
2021-01-27 13:51:51 +00:00
|
|
|
|
|
|
|
nodeSource.values.forEach((node) async {
|
2021-05-07 07:36:38 +00:00
|
|
|
if (node.type == WalletType.monero &&
|
2021-05-10 16:00:20 +00:00
|
|
|
node.uri.toString().contains(cakeWalletMoneroNodeUriPattern)) {
|
2021-01-27 13:51:51 +00:00
|
|
|
await node.delete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
final newCakeWalletNode =
|
|
|
|
Node(uri: newCakeWalletMoneroUri, type: WalletType.monero);
|
2021-01-27 13:51:51 +00:00
|
|
|
|
|
|
|
await nodeSource.add(newCakeWalletNode);
|
|
|
|
|
|
|
|
if (needToReplaceCurrentMoneroNode) {
|
2021-05-07 07:36:38 +00:00
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentNodeIdKey, newCakeWalletNode.key as int);
|
2021-01-27 13:51:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 18:12:37 +00:00
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
Future<void> checkCurrentNodes(
|
|
|
|
Box<Node> nodeSource, SharedPreferences sharedPreferences) async {
|
|
|
|
final currentMoneroNodeId =
|
|
|
|
sharedPreferences.getInt(PreferencesKey.currentNodeIdKey);
|
|
|
|
final currentBitcoinElectrumSeverId =
|
|
|
|
sharedPreferences.getInt(PreferencesKey.currentBitcoinElectrumSererIdKey);
|
|
|
|
final currentLitecoinElectrumSeverId = sharedPreferences
|
|
|
|
.getInt(PreferencesKey.currentLitecoinElectrumSererIdKey);
|
2022-03-30 15:57:04 +00:00
|
|
|
final currentHavenNodeId = sharedPreferences
|
|
|
|
.getInt(PreferencesKey.currentHavenNodeIdKey);
|
2023-08-04 17:01:49 +00:00
|
|
|
final currentEthereumNodeId = sharedPreferences
|
|
|
|
.getInt(PreferencesKey.currentEthereumNodeIdKey);
|
2023-10-02 14:17:35 +00:00
|
|
|
final currentZanoNodeId = sharedPreferences
|
|
|
|
.getInt(PreferencesKey.currentZanoNodeIdKey);
|
2023-10-15 13:36:05 +00:00
|
|
|
final currentDummyNodeId = sharedPreferences
|
|
|
|
.getInt(PreferencesKey.currentDummyNodeIdKey);
|
2022-10-12 17:09:57 +00:00
|
|
|
final currentMoneroNode = nodeSource.values.firstWhereOrNull(
|
|
|
|
(node) => node.key == currentMoneroNodeId);
|
|
|
|
final currentBitcoinElectrumServer = nodeSource.values.firstWhereOrNull(
|
|
|
|
(node) => node.key == currentBitcoinElectrumSeverId);
|
|
|
|
final currentLitecoinElectrumServer = nodeSource.values.firstWhereOrNull(
|
|
|
|
(node) => node.key == currentLitecoinElectrumSeverId);
|
|
|
|
final currentHavenNodeServer = nodeSource.values.firstWhereOrNull(
|
|
|
|
(node) => node.key == currentHavenNodeId);
|
2023-08-04 17:01:49 +00:00
|
|
|
final currentEthereumNodeServer = nodeSource.values.firstWhereOrNull(
|
|
|
|
(node) => node.key == currentEthereumNodeId);
|
2023-10-02 14:17:35 +00:00
|
|
|
final currentZanoNode = nodeSource.values.firstWhereOrNull((node) => node.key == currentZanoNodeId);
|
2023-10-15 13:36:05 +00:00
|
|
|
final currentDummyNode = nodeSource.values.firstWhereOrNull((node) => node.key == currentDummyNodeId);
|
|
|
|
|
2021-02-01 18:12:37 +00:00
|
|
|
if (currentMoneroNode == null) {
|
2021-05-07 07:36:38 +00:00
|
|
|
final newCakeWalletNode =
|
|
|
|
Node(uri: newCakeWalletMoneroUri, type: WalletType.monero);
|
2021-02-01 18:12:37 +00:00
|
|
|
await nodeSource.add(newCakeWalletNode);
|
2021-05-07 07:36:38 +00:00
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentNodeIdKey, newCakeWalletNode.key as int);
|
2021-02-01 18:12:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
if (currentBitcoinElectrumServer == null) {
|
|
|
|
final cakeWalletElectrum =
|
|
|
|
Node(uri: cakeWalletBitcoinElectrumUri, type: WalletType.bitcoin);
|
2021-02-01 18:12:37 +00:00
|
|
|
await nodeSource.add(cakeWalletElectrum);
|
2021-05-07 07:36:38 +00:00
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentBitcoinElectrumSererIdKey,
|
|
|
|
cakeWalletElectrum.key as int);
|
2021-02-01 18:12:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
if (currentLitecoinElectrumServer == null) {
|
|
|
|
final cakeWalletElectrum =
|
|
|
|
Node(uri: cakeWalletLitecoinElectrumUri, type: WalletType.litecoin);
|
|
|
|
await nodeSource.add(cakeWalletElectrum);
|
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentLitecoinElectrumSererIdKey,
|
|
|
|
cakeWalletElectrum.key as int);
|
|
|
|
}
|
2022-03-30 15:57:04 +00:00
|
|
|
|
|
|
|
if (currentHavenNodeServer == null) {
|
2022-10-19 22:21:16 +00:00
|
|
|
final node = Node(uri: havenDefaultNodeUri, type: WalletType.haven);
|
2022-03-30 15:57:04 +00:00
|
|
|
await nodeSource.add(node);
|
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentHavenNodeIdKey, node.key as int);
|
|
|
|
}
|
2023-08-04 17:01:49 +00:00
|
|
|
|
|
|
|
if (currentEthereumNodeServer == null) {
|
|
|
|
final node = Node(uri: ethereumDefaultNodeUri, type: WalletType.ethereum);
|
|
|
|
await nodeSource.add(node);
|
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentEthereumNodeIdKey, node.key as int);
|
|
|
|
}
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
if (currentZanoNode == null) {
|
|
|
|
final node = Node(uri: zanoDefaultNodeUri, type: WalletType.zano);
|
|
|
|
await nodeSource.add(node);
|
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentZanoNodeIdKey, node.key as int);
|
|
|
|
}
|
2023-10-15 13:36:05 +00:00
|
|
|
|
|
|
|
if (currentDummyNode == null) {
|
|
|
|
final node = Node(uri: dummyDefaultNodeUri, type: WalletType.dummy);
|
|
|
|
await nodeSource.add(node);
|
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentDummyNodeIdKey, node.key as int);
|
|
|
|
}
|
2021-05-07 07:36:38 +00:00
|
|
|
}
|
2021-02-01 18:12:37 +00:00
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
Future<void> resetBitcoinElectrumServer(
|
|
|
|
Box<Node> nodeSource, SharedPreferences sharedPreferences) async {
|
|
|
|
final currentElectrumSeverId =
|
|
|
|
sharedPreferences.getInt(PreferencesKey.currentBitcoinElectrumSererIdKey);
|
2022-10-12 17:09:57 +00:00
|
|
|
final oldElectrumServer = nodeSource.values.firstWhereOrNull(
|
|
|
|
(node) => node.uri.toString().contains('electrumx.cakewallet.com'));
|
|
|
|
var cakeWalletNode = nodeSource.values.firstWhereOrNull(
|
2022-11-26 14:13:54 +00:00
|
|
|
(node) => node.uriRaw.toString() == cakeWalletBitcoinElectrumUri);
|
2021-02-01 18:12:37 +00:00
|
|
|
|
|
|
|
if (cakeWalletNode == null) {
|
2021-05-07 07:36:38 +00:00
|
|
|
cakeWalletNode =
|
|
|
|
Node(uri: cakeWalletBitcoinElectrumUri, type: WalletType.bitcoin);
|
2021-02-01 18:12:37 +00:00
|
|
|
await nodeSource.add(cakeWalletNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentElectrumSeverId == oldElectrumServer?.key) {
|
2021-05-07 07:36:38 +00:00
|
|
|
await sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentBitcoinElectrumSererIdKey,
|
|
|
|
cakeWalletNode.key as int);
|
2021-02-01 18:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await oldElectrumServer?.delete();
|
|
|
|
}
|
2022-05-18 16:13:58 +00:00
|
|
|
|
|
|
|
Future<void> changeDefaultHavenNode(
|
|
|
|
Box<Node> nodeSource) async {
|
|
|
|
const previousHavenDefaultNodeUri = 'vault.havenprotocol.org:443';
|
|
|
|
final havenNodes = nodeSource.values.where(
|
|
|
|
(node) => node.uriRaw == previousHavenDefaultNodeUri);
|
|
|
|
havenNodes.forEach((node) async {
|
|
|
|
node.uriRaw = havenDefaultNodeUri;
|
|
|
|
await node.save();
|
|
|
|
});
|
|
|
|
}
|
2023-03-01 21:44:15 +00:00
|
|
|
|
|
|
|
Future<void> migrateExchangeStatus(SharedPreferences sharedPreferences) async {
|
|
|
|
final isExchangeDisabled = sharedPreferences.getBool(PreferencesKey.disableExchangeKey);
|
|
|
|
if (isExchangeDisabled == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-04 17:01:49 +00:00
|
|
|
await sharedPreferences.setInt(PreferencesKey.exchangeStatusKey, isExchangeDisabled
|
2023-03-01 21:44:15 +00:00
|
|
|
? ExchangeApiMode.disabled.raw : ExchangeApiMode.enabled.raw);
|
2023-08-04 17:01:49 +00:00
|
|
|
|
2023-03-01 21:46:05 +00:00
|
|
|
await sharedPreferences.remove(PreferencesKey.disableExchangeKey);
|
2023-03-01 21:44:15 +00:00
|
|
|
}
|
2023-08-04 17:01:49 +00:00
|
|
|
|
|
|
|
Future<void> addEthereumNodeList({required Box<Node> nodes}) async {
|
|
|
|
final nodeList = await loadDefaultEthereumNodes();
|
|
|
|
for (var node in nodeList) {
|
|
|
|
if (nodes.values.firstWhereOrNull((element) => element.uriRaw == node.uriRaw) == null) {
|
|
|
|
await nodes.add(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> changeEthereumCurrentNodeToDefault(
|
|
|
|
{required SharedPreferences sharedPreferences,
|
|
|
|
required Box<Node> nodes}) async {
|
|
|
|
final node = getEthereumDefaultNode(nodes: nodes);
|
|
|
|
final nodeId = node?.key as int? ?? 0;
|
|
|
|
|
|
|
|
await sharedPreferences.setInt(PreferencesKey.currentEthereumNodeIdKey, nodeId);
|
|
|
|
}
|