mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-11-01 18:07:44 +00:00
3ce4000dcf
* Add initial flow for ethereum * Add initial create Eth wallet flow * Complete Ethereum wallet creation flow * Fix web3dart versioning issue * Add primary receive address extracted from private key * Implement open wallet functionality * Implement restore wallet from seed functionality * Fixate web3dart version as higher versions cause some issues * Add Initial Transaction priorities for eth Add estimated gas price * Rename priority value to tip * Re-order wallet types * Change ethereum node Fix connection issues * Fix estimating gas for priority * Add case for ethereum to fetch it's seeds * Add case for ethereum to request node * Fix Exchange screen initial pairs * Add initial send transaction flow * Add missing configure for ethereum class * Add Eth address initial setup * Fix Private key for Ethereum wallets * Change sign/send transaction flow * - Fix Conflicts with main - Remove unused function from Haven configure.dart * Add build command for ethereum package * Add missing Node list file to pubspec * - Fix balance display - Fix parsing of Ethereum amount - Add more Ethereum Nodes * - Fix extracting Ethereum Private key from seeds - Integrate signing/sending transaction with the send view model * - Update and Fix Conflicts with main * Add Balances for ERC20 tokens * Fix conflicts with main * Add erc20 abi json * Add send erc20 tokens initial function * add missing getHeightByDate in Haven * Allow contacts and wallets from the same tag * Add Shiba Inu icon * Add send ERC-20 tokens initial flow * Add missing import in generated file * Add initial approach for transaction sending for ERC-20 tokens * Refactor signing/sending transactions * Add initial flow for transactions subscription * Refactor signing/sending transactions * Add home settings icon * Fix conflicts with main * Initial flow for home settings * Add logic flow for adding erc20 tokens * Fix initial UI * Finalize UI for Tokens * Integrate UI with Ethereum flow * Add "Enable/Disable" feature for ERC20 tokens * Add initial Erc20 tokens * Add Sorting and Pin Native Token features * Fix price sorting * Sort tokens list as well when Sort criteria changes * - Improve sorting balances flow - Add initial add token from search bar flow * Fix Accounts Popup UI * Fix Pin native token * Fix Enabling/Disabling tokens Fix sorting by fiat once app is opened Improve token availability mechanism * Fix deleting token Fix renaming tokens * Fix issue with search * Add more tokens * - Fix scroll issue - Add ERC20 tokens placeholder image in picker * - Separate and organize default erc20 tokens - Fix scrolling - Add token placeholder images in picker - Sort disabled tokens alphabetically * Change BNB token initial availability * Fix Conflicts with main * Fix Conflicts with main * Add Verse ERC20 token to the initial tokens list * Add rename wallet to Ethereum * Integrate EtherScan API for fetching address transactions Generate Ethereum specific secrets in Ethereum package * Adjust transactions fiat price for ERC20 tokens * Free Up GitHub Actions Ubuntu Runner Disk Space * Free Up GitHub Actions Ubuntu Runner Disk space (trial 2) * Fix Transaction Fee display * Save transaction history * Enhance loading time for erc20 tokens transactions * Minor Fixes and Enhancements * Fix sending erc20 fix block explorer issue * Fix int overflow * Fix transaction amount conversions * Minor: `slow` -> `Slow` * Update build guide * Fix fetching fiat rate taking a lot of time by only fetching enabled tokens only and making the API calls in parallel not sequential * Update transactions on a periodic basis * For fee, use ETH spot price, not ERC-20 spot price * Add Etherscan History privacy option to enable/disable Etherscan API * Show estimated fee amounts in the send screen * fix send fiat fields parsing issue * Fix transactions estimated fee less than actual fee * handle balance sorting when balance is disabled Handle empty transactions list * Fix Delete Ethereum wallet Fix balance < 0.01 * Fix Decimal place for Ethereum amount Fix sending amount issue * Change words count * Remove balance hint and Full balance row from Ethereum wallets * support changing the asset type in send templates * Fix Templates for ERC tokens issues * Fix conflicts in send templates * Disable batch sending in Ethereum * Fix Fee calculation with different priorities * Fix Conflicts with main * Add offline error to ignored exceptions --------- Co-authored-by: Justin Ehrenhofer <justin.ehrenhofer@gmail.com>
571 lines
20 KiB
Dart
571 lines
20 KiB
Dart
import 'dart:io' show File, Platform;
|
|
import 'package:cake_wallet/bitcoin/bitcoin.dart';
|
|
import 'package:cake_wallet/entities/exchange_api_mode.dart';
|
|
import 'package:cw_core/pathForWallet.dart';
|
|
import 'package:cake_wallet/entities/secret_store_key.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:hive/hive.dart';
|
|
import 'package:share_plus/share_plus.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:cake_wallet/entities/preferences_key.dart';
|
|
import 'package:cw_core/wallet_type.dart';
|
|
import 'package:cw_core/node.dart';
|
|
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';
|
|
import 'package:cake_wallet/monero/monero.dart';
|
|
import 'package:cake_wallet/entities/contact.dart';
|
|
import 'package:cake_wallet/entities/fs_migration.dart';
|
|
import 'package:cw_core/wallet_info.dart';
|
|
import 'package:cake_wallet/exchange/trade.dart';
|
|
import 'package:encrypt/encrypt.dart' as encrypt;
|
|
import 'package:collection/collection.dart';
|
|
|
|
const newCakeWalletMoneroUri = 'xmr-node.cakewallet.com:18081';
|
|
const cakeWalletBitcoinElectrumUri = 'electrum.cakewallet.com:50002';
|
|
const cakeWalletLitecoinElectrumUri = 'ltc-electrum.cakewallet.com:50002';
|
|
const havenDefaultNodeUri = 'nodes.havenprotocol.org:443';
|
|
const ethereumDefaultNodeUri = 'ethereum.publicnode.com';
|
|
|
|
Future defaultSettingsMigration(
|
|
{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 {
|
|
if (Platform.isIOS) {
|
|
await ios_migrate_v1(walletInfoSource, tradeSource, contactSource);
|
|
}
|
|
|
|
// check current nodes for nullability regardless of the version
|
|
await checkCurrentNodes(nodes, sharedPreferences);
|
|
|
|
final isNewInstall = sharedPreferences
|
|
.getInt(PreferencesKey.currentDefaultSettingsMigrationVersion) == null;
|
|
|
|
await sharedPreferences.setBool(
|
|
PreferencesKey.isNewInstall, isNewInstall);
|
|
|
|
|
|
final currentVersion = sharedPreferences
|
|
.getInt(PreferencesKey.currentDefaultSettingsMigrationVersion) ??
|
|
0;
|
|
if (currentVersion >= version) {
|
|
return;
|
|
}
|
|
|
|
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(
|
|
PreferencesKey.currentFiatCurrencyKey,
|
|
FiatCurrency.usd.toString());
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentTransactionPriorityKeyLegacy,
|
|
monero!.getDefaultTransactionPriority().raw);
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentBalanceDisplayModeKey,
|
|
BalanceDisplayMode.availableBalance.raw);
|
|
await sharedPreferences.setBool('save_recipient_address', true);
|
|
await resetToDefault(nodes);
|
|
await changeMoneroCurrentNodeToDefault(
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
await changeBitcoinCurrentElectrumServerToDefault(
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
await changeLitecoinCurrentElectrumServerToDefault(
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
await changeHavenCurrentNodeToDefault(
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
|
|
break;
|
|
case 2:
|
|
await replaceNodesMigration(nodes: nodes);
|
|
await replaceDefaultNode(
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
|
|
break;
|
|
case 3:
|
|
await updateNodeTypes(nodes: nodes);
|
|
await addBitcoinElectrumServerList(nodes: nodes);
|
|
|
|
break;
|
|
case 4:
|
|
await changeBitcoinCurrentElectrumServerToDefault(
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
break;
|
|
|
|
case 5:
|
|
await addAddressesForMoneroWallets(walletInfoSource);
|
|
break;
|
|
|
|
case 6:
|
|
await updateDisplayModes(sharedPreferences);
|
|
break;
|
|
|
|
case 9:
|
|
await generateBackupPassword(secureStorage);
|
|
break;
|
|
|
|
case 10:
|
|
await changeTransactionPriorityAndFeeRateKeys(sharedPreferences);
|
|
break;
|
|
|
|
case 11:
|
|
await changeDefaultMoneroNode(nodes, sharedPreferences);
|
|
break;
|
|
|
|
case 12:
|
|
await checkCurrentNodes(nodes, sharedPreferences);
|
|
break;
|
|
|
|
case 13:
|
|
await resetBitcoinElectrumServer(nodes, sharedPreferences);
|
|
break;
|
|
|
|
case 15:
|
|
await addLitecoinElectrumServerList(nodes: nodes);
|
|
await changeLitecoinCurrentElectrumServerToDefault(
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
await checkCurrentNodes(nodes, sharedPreferences);
|
|
break;
|
|
|
|
case 16:
|
|
await addHavenNodeList(nodes: nodes);
|
|
await changeHavenCurrentNodeToDefault(
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
await checkCurrentNodes(nodes, sharedPreferences);
|
|
break;
|
|
|
|
case 17:
|
|
await changeDefaultHavenNode(nodes);
|
|
break;
|
|
|
|
case 18:
|
|
await addOnionNode(nodes);
|
|
break;
|
|
|
|
case 19:
|
|
await validateBitcoinSavedTransactionPriority(sharedPreferences);
|
|
break;
|
|
case 20:
|
|
await migrateExchangeStatus(sharedPreferences);
|
|
break;
|
|
case 21:
|
|
await addEthereumNodeList(nodes: nodes);
|
|
await changeEthereumCurrentNodeToDefault(
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentDefaultSettingsMigrationVersion, version);
|
|
} catch (e) {
|
|
print('Migration error: ${e.toString()}');
|
|
}
|
|
});
|
|
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentDefaultSettingsMigrationVersion, version);
|
|
}
|
|
|
|
Future<void> validateBitcoinSavedTransactionPriority(SharedPreferences sharedPreferences) async {
|
|
if (bitcoin == null) {
|
|
return;
|
|
}
|
|
final int? savedBitcoinPriority =
|
|
sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority);
|
|
if (!bitcoin!.getTransactionPriorities().any((element) => element.raw == savedBitcoinPriority)) {
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.bitcoinTransactionPriority, bitcoin!.getMediumTransactionPriority().serialize());
|
|
}
|
|
}
|
|
|
|
Future<void> addOnionNode(Box<Node> nodes) async {
|
|
final onionNodeUri = "cakexmrl7bonq7ovjka5kuwuyd3f7qnkz6z6s6dmsy3uckwra7bvggyd.onion:18081";
|
|
|
|
// 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));
|
|
}
|
|
}
|
|
|
|
Future<void> replaceNodesMigration({required Box<Node> nodes}) async {
|
|
final replaceNodes = <String, Node>{
|
|
'eu-node.cakewallet.io:18081':
|
|
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)
|
|
};
|
|
|
|
nodes.values.forEach((Node node) async {
|
|
final nodeToReplace = replaceNodes[node.uri];
|
|
|
|
if (nodeToReplace != null) {
|
|
node.uriRaw = nodeToReplace.uriRaw;
|
|
node.login = nodeToReplace.login;
|
|
node.password = nodeToReplace.password;
|
|
await node.save();
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> changeMoneroCurrentNodeToDefault(
|
|
{required SharedPreferences sharedPreferences,
|
|
required Box<Node> nodes}) async {
|
|
final node = getMoneroDefaultNode(nodes: nodes);
|
|
final nodeId = node?.key as int ?? 0; // 0 - England
|
|
|
|
await sharedPreferences.setInt(PreferencesKey.currentNodeIdKey, nodeId);
|
|
}
|
|
|
|
Node? getBitcoinDefaultElectrumServer({required Box<Node> nodes}) {
|
|
return nodes.values.firstWhereOrNull(
|
|
(Node node) => node.uriRaw == cakeWalletBitcoinElectrumUri)
|
|
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.bitcoin);
|
|
}
|
|
|
|
Node? getLitecoinDefaultElectrumServer({required Box<Node> nodes}) {
|
|
return nodes.values.firstWhereOrNull(
|
|
(Node node) => node.uriRaw == cakeWalletLitecoinElectrumUri)
|
|
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.litecoin);
|
|
}
|
|
|
|
Node? getHavenDefaultNode({required Box<Node> nodes}) {
|
|
return nodes.values.firstWhereOrNull(
|
|
(Node node) => node.uriRaw == havenDefaultNodeUri)
|
|
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.haven);
|
|
}
|
|
|
|
Node? getEthereumDefaultNode({required Box<Node> nodes}) {
|
|
return nodes.values.firstWhereOrNull(
|
|
(Node node) => node.uriRaw == ethereumDefaultNodeUri)
|
|
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.ethereum);
|
|
}
|
|
|
|
Node getMoneroDefaultNode({required Box<Node> nodes}) {
|
|
final timeZone = DateTime.now().timeZoneOffset.inHours;
|
|
var nodeUri = '';
|
|
|
|
if (timeZone >= 1) {
|
|
// Eurasia
|
|
nodeUri = 'xmr-node-eu.cakewallet.com:18081';
|
|
} else if (timeZone <= -4) {
|
|
// America
|
|
nodeUri = 'xmr-node-usa-east.cakewallet.com:18081';
|
|
}
|
|
|
|
try {
|
|
return nodes.values
|
|
.firstWhere((Node node) => node.uriRaw == nodeUri);
|
|
} catch(_) {
|
|
return nodes.values.first;
|
|
}
|
|
}
|
|
|
|
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(PreferencesKey.currentBitcoinElectrumSererIdKey, serverId);
|
|
}
|
|
|
|
Future<void> changeLitecoinCurrentElectrumServerToDefault(
|
|
{required SharedPreferences sharedPreferences,
|
|
required Box<Node> nodes}) async {
|
|
final server = getLitecoinDefaultElectrumServer(nodes: nodes);
|
|
final serverId = server?.key as int ?? 0;
|
|
|
|
await sharedPreferences.setInt(PreferencesKey.currentLitecoinElectrumSererIdKey, serverId);
|
|
}
|
|
|
|
Future<void> changeHavenCurrentNodeToDefault(
|
|
{required SharedPreferences sharedPreferences,
|
|
required Box<Node> nodes}) async {
|
|
final node = getHavenDefaultNode(nodes: nodes);
|
|
final nodeId = node?.key as int ?? 0;
|
|
|
|
await sharedPreferences.setInt(PreferencesKey.currentHavenNodeIdKey, nodeId);
|
|
}
|
|
|
|
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(PreferencesKey.currentNodeIdKey);
|
|
final currentNode =
|
|
nodes.values.firstWhereOrNull((Node node) => node.key == currentNodeId);
|
|
final needToReplace =
|
|
currentNode == null ? true : nodesForReplace.contains(currentNode.uriRaw);
|
|
|
|
if (!needToReplace) {
|
|
return;
|
|
}
|
|
|
|
await changeMoneroCurrentNodeToDefault(
|
|
sharedPreferences: sharedPreferences, nodes: nodes);
|
|
}
|
|
|
|
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 loadBitcoinElectrumServerList();
|
|
for (var node in serverList) {
|
|
if (nodes.values.firstWhereOrNull((element) => element.uriRaw == node.uriRaw) == null) {
|
|
await nodes.add(node);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> addLitecoinElectrumServerList({required Box<Node> nodes}) async {
|
|
final serverList = await loadLitecoinElectrumServerList();
|
|
for (var node in serverList) {
|
|
if (nodes.values.firstWhereOrNull((element) => element.uriRaw == node.uriRaw) == null) {
|
|
await nodes.add(node);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> addHavenNodeList({required Box<Node> nodes}) async {
|
|
final nodeList = await loadDefaultHavenNodes();
|
|
for (var node in nodeList) {
|
|
if (nodes.values.firstWhereOrNull((element) => element.uriRaw == node.uriRaw) == null) {
|
|
await nodes.add(node);
|
|
}
|
|
}
|
|
}
|
|
|
|
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());
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> updateDisplayModes(SharedPreferences sharedPreferences) async {
|
|
final currentBalanceDisplayMode =
|
|
sharedPreferences.getInt(PreferencesKey.currentBalanceDisplayModeKey) ?? -1;
|
|
final balanceDisplayMode = currentBalanceDisplayMode < 2 ? 3 : 2;
|
|
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);
|
|
}
|
|
|
|
Future<void> changeTransactionPriorityAndFeeRateKeys(
|
|
SharedPreferences sharedPreferences) async {
|
|
final legacyTransactionPriority = sharedPreferences
|
|
.getInt(PreferencesKey.currentTransactionPriorityKeyLegacy)!;
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.moneroTransactionPriority, legacyTransactionPriority);
|
|
await sharedPreferences.setInt(PreferencesKey.bitcoinTransactionPriority,
|
|
bitcoin!.getMediumTransactionPriority().serialize());
|
|
}
|
|
|
|
Future<void> changeDefaultMoneroNode(
|
|
Box<Node> nodeSource, SharedPreferences sharedPreferences) async {
|
|
const cakeWalletMoneroNodeUriPattern = '.cakewallet.com';
|
|
final currentMoneroNodeId =
|
|
sharedPreferences.getInt(PreferencesKey.currentNodeIdKey);
|
|
final currentMoneroNode =
|
|
nodeSource.values.firstWhere((node) => node.key == currentMoneroNodeId);
|
|
final needToReplaceCurrentMoneroNode =
|
|
currentMoneroNode.uri.toString().contains(cakeWalletMoneroNodeUriPattern);
|
|
|
|
nodeSource.values.forEach((node) async {
|
|
if (node.type == WalletType.monero &&
|
|
node.uri.toString().contains(cakeWalletMoneroNodeUriPattern)) {
|
|
await node.delete();
|
|
}
|
|
});
|
|
|
|
final newCakeWalletNode =
|
|
Node(uri: newCakeWalletMoneroUri, type: WalletType.monero);
|
|
|
|
await nodeSource.add(newCakeWalletNode);
|
|
|
|
if (needToReplaceCurrentMoneroNode) {
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentNodeIdKey, newCakeWalletNode.key as int);
|
|
}
|
|
}
|
|
|
|
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);
|
|
final currentHavenNodeId = sharedPreferences
|
|
.getInt(PreferencesKey.currentHavenNodeIdKey);
|
|
final currentEthereumNodeId = sharedPreferences
|
|
.getInt(PreferencesKey.currentEthereumNodeIdKey);
|
|
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);
|
|
final currentEthereumNodeServer = nodeSource.values.firstWhereOrNull(
|
|
(node) => node.key == currentEthereumNodeId);
|
|
|
|
if (currentMoneroNode == null) {
|
|
final newCakeWalletNode =
|
|
Node(uri: newCakeWalletMoneroUri, type: WalletType.monero);
|
|
await nodeSource.add(newCakeWalletNode);
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentNodeIdKey, newCakeWalletNode.key as int);
|
|
}
|
|
|
|
if (currentBitcoinElectrumServer == null) {
|
|
final cakeWalletElectrum =
|
|
Node(uri: cakeWalletBitcoinElectrumUri, type: WalletType.bitcoin);
|
|
await nodeSource.add(cakeWalletElectrum);
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentBitcoinElectrumSererIdKey,
|
|
cakeWalletElectrum.key as int);
|
|
}
|
|
|
|
if (currentLitecoinElectrumServer == null) {
|
|
final cakeWalletElectrum =
|
|
Node(uri: cakeWalletLitecoinElectrumUri, type: WalletType.litecoin);
|
|
await nodeSource.add(cakeWalletElectrum);
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentLitecoinElectrumSererIdKey,
|
|
cakeWalletElectrum.key as int);
|
|
}
|
|
|
|
if (currentHavenNodeServer == null) {
|
|
final node = Node(uri: havenDefaultNodeUri, type: WalletType.haven);
|
|
await nodeSource.add(node);
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentHavenNodeIdKey, node.key as int);
|
|
}
|
|
|
|
if (currentEthereumNodeServer == null) {
|
|
final node = Node(uri: ethereumDefaultNodeUri, type: WalletType.ethereum);
|
|
await nodeSource.add(node);
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentEthereumNodeIdKey, node.key as int);
|
|
}
|
|
}
|
|
|
|
Future<void> resetBitcoinElectrumServer(
|
|
Box<Node> nodeSource, SharedPreferences sharedPreferences) async {
|
|
final currentElectrumSeverId =
|
|
sharedPreferences.getInt(PreferencesKey.currentBitcoinElectrumSererIdKey);
|
|
final oldElectrumServer = nodeSource.values.firstWhereOrNull(
|
|
(node) => node.uri.toString().contains('electrumx.cakewallet.com'));
|
|
var cakeWalletNode = nodeSource.values.firstWhereOrNull(
|
|
(node) => node.uriRaw.toString() == cakeWalletBitcoinElectrumUri);
|
|
|
|
if (cakeWalletNode == null) {
|
|
cakeWalletNode =
|
|
Node(uri: cakeWalletBitcoinElectrumUri, type: WalletType.bitcoin);
|
|
await nodeSource.add(cakeWalletNode);
|
|
}
|
|
|
|
if (currentElectrumSeverId == oldElectrumServer?.key) {
|
|
await sharedPreferences.setInt(
|
|
PreferencesKey.currentBitcoinElectrumSererIdKey,
|
|
cakeWalletNode.key as int);
|
|
}
|
|
|
|
await oldElectrumServer?.delete();
|
|
}
|
|
|
|
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();
|
|
});
|
|
}
|
|
|
|
Future<void> migrateExchangeStatus(SharedPreferences sharedPreferences) async {
|
|
final isExchangeDisabled = sharedPreferences.getBool(PreferencesKey.disableExchangeKey);
|
|
if (isExchangeDisabled == null) {
|
|
return;
|
|
}
|
|
|
|
await sharedPreferences.setInt(PreferencesKey.exchangeStatusKey, isExchangeDisabled
|
|
? ExchangeApiMode.disabled.raw : ExchangeApiMode.enabled.raw);
|
|
|
|
await sharedPreferences.remove(PreferencesKey.disableExchangeKey);
|
|
}
|
|
|
|
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);
|
|
}
|