| 1 | import 'package:cake_wallet/core/utilities.dart'; |
| 2 | import 'package:cake_wallet/entities/preferences_key.dart'; |
| 3 | import 'package:cake_wallet/store/settings_store.dart'; |
| 4 | import 'package:cw_core/node.dart'; |
| 5 | import 'package:cw_core/node_list.dart'; |
| 6 | import 'package:cw_core/wallet_type.dart'; |
| 7 | import 'package:shared_preferences/shared_preferences.dart'; |
| 8 | |
| 9 | const Map<WalletType, String> nodePreferenceKeys = { |
| 10 | WalletType.monero: PreferencesKey.currentNodeIdKey, |
| 11 | WalletType.bitcoin: PreferencesKey.currentBitcoinElectrumSererIdKey, |
| 12 | WalletType.litecoin: PreferencesKey.currentLitecoinElectrumSererIdKey, |
| 13 | WalletType.haven: PreferencesKey.currentHavenNodeIdKey, |
| 14 | WalletType.ethereum: PreferencesKey.currentEthereumNodeIdKey, |
| 15 | WalletType.polygon: PreferencesKey.currentPolygonNodeIdKey, |
| 16 | WalletType.base: PreferencesKey.currentBaseNodeIdKey, |
| 17 | WalletType.arbitrum: PreferencesKey.currentArbitrumNodeIdKey, |
| 18 | WalletType.bsc: PreferencesKey.currentBscNodeIdKey, |
| 19 | WalletType.nano: PreferencesKey.currentNanoNodeIdKey, |
| 20 | WalletType.decred: PreferencesKey.currentDecredNodeIdKey, |
| 21 | WalletType.bitcoinCash: PreferencesKey.currentBitcoinCashNodeIdKey, |
| 22 | WalletType.dogecoin: PreferencesKey.currentDogecoinNodeIdKey, |
| 23 | WalletType.solana: PreferencesKey.currentSolanaNodeIdKey, |
| 24 | WalletType.tron: PreferencesKey.currentTronNodeIdKey, |
| 25 | WalletType.wownero: PreferencesKey.currentWowneroNodeIdKey, |
| 26 | WalletType.zano: PreferencesKey.currentZanoNodeIdKey, |
| 27 | WalletType.zcash: PreferencesKey.currentZcashNodeIdKey, |
| 28 | }; |
| 29 | |
| 30 | const Map<WalletType, String> powNodePreferenceKeys = { |
| 31 | WalletType.nano: PreferencesKey.currentNanoPowNodeIdKey |
| 32 | }; |
| 33 | |
| 34 | Future<void> checkNodeForWalletType(SharedPreferences sharedPreferences, |
| 35 | SettingsStore settingsStore, WalletType type, bool isPow) async { |
| 36 | final preferenceKey = isPow ? powNodePreferenceKeys[type] : nodePreferenceKeys[type]; |
| 37 | |
| 38 | if (preferenceKey == null) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | final currentId = await sharedPreferences.getInt(preferenceKey); |
| 43 | |
| 44 | final nodes = |
| 45 | isPow ? await Node.getAllForWalletTypePow(type) : await Node.getAllForWalletType(type); |
| 46 | final currentNode = nodes.firstWhereOrNull((item) => item.id == currentId); |
| 47 | |
| 48 | if (currentNode == null) { |
| 49 | final newNode = (isPow |
| 50 | ? await Node.getDefaultPowForWalletType(type) |
| 51 | : await Node.getDefaultForWalletType(type)) ?? |
| 52 | await getDefaultNodeFromFiles(type, isPow: isPow); |
| 53 | final newId = await newNode.save(); |
| 54 | sharedPreferences.setInt(preferenceKey, newId); |
| 55 | settingsStore.nodes[type] = newNode; |
| 56 | } else { |
| 57 | settingsStore.nodes[type] = currentNode; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | Future<void> checkCurrentNodes( |
| 62 | SharedPreferences sharedPreferences, SettingsStore settingsStore) async { |
| 63 | for (final walletType in nodePreferenceKeys.keys) { |
| 64 | await checkNodeForWalletType(sharedPreferences, settingsStore, walletType, false); |
| 65 | } |
| 66 | |
| 67 | for (final walletType in powNodePreferenceKeys.keys) { |
| 68 | await checkNodeForWalletType(sharedPreferences, settingsStore, walletType, true); |
| 69 | } |
| 70 | } |