| 1 | import 'dart:io'; |
| 2 | |
| 3 | import 'package:cake_wallet/bitcoin/bitcoin.dart'; |
| 4 | import 'package:cake_wallet/entities/auto_generate_subaddress_status.dart'; |
| 5 | import 'package:cake_wallet/entities/priority_for_wallet_type.dart'; |
| 6 | import 'package:cake_wallet/store/settings_store.dart'; |
| 7 | import 'package:cake_wallet/utils/package_info.dart'; |
| 8 | import 'package:cake_wallet/view_model/send/send_view_model.dart'; |
| 9 | import 'package:collection/collection.dart'; |
| 10 | import 'package:cw_core/balance.dart'; |
| 11 | import 'package:cw_core/pathForWallet.dart'; |
| 12 | import 'package:cw_core/transaction_history.dart'; |
| 13 | import 'package:cw_core/transaction_info.dart'; |
| 14 | import 'package:cw_core/transaction_priority.dart'; |
| 15 | import 'package:cw_core/wallet_base.dart'; |
| 16 | import 'package:cw_core/wallet_info.dart'; |
| 17 | import 'package:cw_core/wallet_type.dart'; |
| 18 | import 'package:mobx/mobx.dart'; |
| 19 | |
| 20 | part 'other_settings_view_model.g.dart'; |
| 21 | |
| 22 | class OtherSettingsViewModel = OtherSettingsViewModelBase with _$OtherSettingsViewModel; |
| 23 | |
| 24 | abstract class OtherSettingsViewModelBase with Store { |
| 25 | OtherSettingsViewModelBase(this._settingsStore, this._wallet, this.sendViewModel) |
| 26 | : walletType = _wallet.type, |
| 27 | chainId = _wallet.chainId, |
| 28 | currentVersion = '' { |
| 29 | PackageInfo.fromPlatform() |
| 30 | .then((PackageInfo packageInfo) => currentVersion = packageInfo.version); |
| 31 | |
| 32 | final priority = _settingsStore.getPriority(_wallet.type, chainId: _wallet.chainId); |
| 33 | final priorities = priorityForWalletType(_wallet.type); |
| 34 | |
| 35 | if (!priorities.contains(priority) && priorities.isNotEmpty) { |
| 36 | _settingsStore.setPriority(_wallet.type, priorities.first, chainId: _wallet.chainId); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | final WalletType walletType; |
| 41 | final int? chainId; |
| 42 | final WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> _wallet; |
| 43 | |
| 44 | @observable |
| 45 | String currentVersion; |
| 46 | |
| 47 | final SettingsStore _settingsStore; |
| 48 | final SendViewModel sendViewModel; |
| 49 | |
| 50 | bool get hasSignVerify => _wallet.canSignMessages; |
| 51 | |
| 52 | @computed |
| 53 | TransactionPriority get transactionPriority { |
| 54 | final priority = _settingsStore.getPriority(walletType, chainId: chainId); |
| 55 | |
| 56 | if (priority == null) { |
| 57 | throw Exception('Unexpected type ${walletType.toString()}'); |
| 58 | } |
| 59 | |
| 60 | return priority; |
| 61 | } |
| 62 | |
| 63 | @computed |
| 64 | bool get isAutoGenerateSubaddressesEnabled => |
| 65 | _settingsStore.autoGenerateSubaddressStatus != AutoGenerateSubaddressStatus.disabled; |
| 66 | |
| 67 | @action |
| 68 | void setAutoGenerateSubaddresses(bool value) { |
| 69 | _wallet.isEnabledAutoGenerateSubaddress = value; |
| 70 | _settingsStore.autoGenerateSubaddressStatus = |
| 71 | value ? AutoGenerateSubaddressStatus.enabled : AutoGenerateSubaddressStatus.disabled; |
| 72 | } |
| 73 | |
| 74 | bool get isAutoGenerateSubaddressesVisible => [ |
| 75 | WalletType.monero, |
| 76 | WalletType.wownero, |
| 77 | WalletType.bitcoin, |
| 78 | WalletType.litecoin, |
| 79 | WalletType.bitcoinCash, |
| 80 | WalletType.dogecoin, |
| 81 | WalletType.decred |
| 82 | ].contains(_wallet.type); |
| 83 | |
| 84 | @computed |
| 85 | bool get isMoneroWallet => _wallet.type == WalletType.monero; |
| 86 | |
| 87 | @computed |
| 88 | bool get shouldSaveRecipientAddress => _settingsStore.shouldSaveRecipientAddress; |
| 89 | |
| 90 | @computed |
| 91 | bool get changeRepresentativeEnabled => |
| 92 | [WalletType.nano, WalletType.banano].contains(_wallet.type); |
| 93 | |
| 94 | @computed |
| 95 | bool get changeHardwareWalletTypeEnabled => [ |
| 96 | HardwareWalletType.cupcake, |
| 97 | HardwareWalletType.coldcard, |
| 98 | HardwareWalletType.seedsigner, |
| 99 | ].contains(_wallet.hardwareWalletType); |
| 100 | |
| 101 | @computed |
| 102 | bool get displayTransactionPriority => !(changeRepresentativeEnabled || |
| 103 | [ |
| 104 | WalletType.solana, |
| 105 | WalletType.tron, |
| 106 | WalletType.arbitrum, |
| 107 | ].contains(_wallet.type)); |
| 108 | |
| 109 | String getDisplayPriority(dynamic priority) { |
| 110 | final _priority = priority as TransactionPriority; |
| 111 | |
| 112 | if ([ |
| 113 | WalletType.bitcoin, |
| 114 | WalletType.litecoin, |
| 115 | WalletType.bitcoinCash, |
| 116 | WalletType.dogecoin, |
| 117 | ].contains(_wallet.type)) { |
| 118 | final rate = bitcoin!.getFeeRate(_wallet, _priority); |
| 119 | return bitcoin!.bitcoinTransactionPriorityWithLabel(_priority, rate); |
| 120 | } |
| 121 | |
| 122 | return priority.toString(); |
| 123 | } |
| 124 | |
| 125 | String getDisplayBitcoinPriority(dynamic priority, int customValue) { |
| 126 | final _priority = priority as TransactionPriority; |
| 127 | |
| 128 | if ([ |
| 129 | WalletType.bitcoin, |
| 130 | WalletType.litecoin, |
| 131 | WalletType.bitcoinCash, |
| 132 | WalletType.dogecoin, |
| 133 | ].contains(_wallet.type)) { |
| 134 | final rate = bitcoin!.getFeeRate(_wallet, _priority); |
| 135 | return bitcoin!.bitcoinTransactionPriorityWithLabel(_priority, rate, customRate: customValue); |
| 136 | } |
| 137 | |
| 138 | return priority.toString(); |
| 139 | } |
| 140 | |
| 141 | void onDisplayPrioritySelected(TransactionPriority priority) => |
| 142 | _settingsStore.setPriority(walletType, priority, chainId: chainId); |
| 143 | |
| 144 | void onDisplayBitcoinPrioritySelected(TransactionPriority priority, double customValue) { |
| 145 | if (_wallet.type == WalletType.bitcoin) { |
| 146 | _settingsStore.customBitcoinFeeRate = customValue.round(); |
| 147 | } |
| 148 | _settingsStore.setPriority(_wallet.type, priority, chainId: _wallet.chainId); |
| 149 | } |
| 150 | |
| 151 | @action |
| 152 | Future<void> onHardwareWalletTypeChanged(HardwareWalletType hwType) async { |
| 153 | _wallet.walletInfo.hardwareWalletType = hwType; |
| 154 | await _wallet.walletInfo.save(); |
| 155 | } |
| 156 | |
| 157 | @computed |
| 158 | double get customBitcoinFeeRate => _settingsStore.customBitcoinFeeRate.toDouble(); |
| 159 | |
| 160 | @action |
| 161 | void setShouldSaveRecipientAddress(bool value) => |
| 162 | _settingsStore.shouldSaveRecipientAddress = value; |
| 163 | |
| 164 | int? get customPriorityItemIndex { |
| 165 | final priorities = priorityForWalletType(walletType); |
| 166 | final customItem = priorities |
| 167 | .firstWhereOrNull((element) => element == bitcoin!.getBitcoinTransactionPriorityCustom()); |
| 168 | return customItem != null ? priorities.indexOf(customItem) : null; |
| 169 | } |
| 170 | |
| 171 | int? get maxCustomFeeRate { |
| 172 | if (_wallet.type == WalletType.bitcoin) { |
| 173 | return bitcoin!.getMaxCustomFeeRate(_wallet); |
| 174 | } |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | bool get hasPayjoin => _wallet.hasPayjoinSupport; |
| 179 | |
| 180 | bool get hasLightning => _wallet.hasLightningSupport; |
| 181 | |
| 182 | Future<File?> getLightningLog() async { |
| 183 | final path = await pathForWalletDir(name: _wallet.name, type: walletType); |
| 184 | final logFile = File("$path/lightning.log"); |
| 185 | |
| 186 | if (await logFile.exists()) return logFile; |
| 187 | return null; |
| 188 | } |
| 189 | |
| 190 | Future<File?> getPayjoinLog() async { |
| 191 | final path = await pathForWalletDir(name: _wallet.name, type: walletType); |
| 192 | final logFile = File("$path/payjoin.log"); |
| 193 | |
| 194 | if (await logFile.exists()) return logFile; |
| 195 | return null; |
| 196 | } |
| 197 | } |