From b548386097b5c7d06b01bafa1436937547f1d2ed Mon Sep 17 00:00:00 2001 From: julian Date: Tue, 26 Nov 2024 10:45:08 -0600 Subject: [PATCH] hack a check for node tor settings before making calls into tezos as well --- lib/wallets/wallet/impl/tezos_wallet.dart | 41 +++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/wallets/wallet/impl/tezos_wallet.dart b/lib/wallets/wallet/impl/tezos_wallet.dart index a1728c10a..ab1448319 100644 --- a/lib/wallets/wallet/impl/tezos_wallet.dart +++ b/lib/wallets/wallet/impl/tezos_wallet.dart @@ -4,6 +4,7 @@ import 'package:isar/isar.dart'; import 'package:tezart/tezart.dart' as tezart; import 'package:tuple/tuple.dart'; +import '../../../exceptions/wallet/node_tor_mismatch_config_exception.dart'; import '../../../models/balance.dart'; import '../../../models/isar/models/blockchain_data/address.dart'; import '../../../models/isar/models/blockchain_data/transaction.dart'; @@ -14,6 +15,7 @@ import '../../../services/tor_service.dart'; import '../../../utilities/amount/amount.dart'; import '../../../utilities/extensions/impl/string.dart'; import '../../../utilities/logger.dart'; +import '../../../utilities/tor_plain_net_option_enum.dart'; import '../../api/tezos/tezos_account.dart'; import '../../api/tezos/tezos_api.dart'; import '../../api/tezos/tezos_rpc_api.dart'; @@ -42,6 +44,7 @@ class TezosWallet extends Bip39Wallet { String passphrase = "", }) async { try { + _hackedCheckTorNodePrefs(); for (final path in Tezos.possibleDerivationPaths) { final ks = await _getKeyStore(path: path.value); @@ -99,6 +102,7 @@ class TezosWallet extends Bip39Wallet { // Amount? customRevealFee, }) async { try { + _hackedCheckTorNodePrefs(); final sourceKeyStore = await _getKeyStore(); final server = (_xtzNode ?? getCurrentNode()).host; // if (kDebugMode) { @@ -178,6 +182,7 @@ class TezosWallet extends Bip39Wallet { @override Future prepareSend({required TxData txData}) async { try { + _hackedCheckTorNodePrefs(); if (txData.recipients == null || txData.recipients!.length != 1) { throw Exception("$runtimeType prepareSend requires 1 recipient"); } @@ -288,6 +293,7 @@ class TezosWallet extends Bip39Wallet { @override Future confirmSend({required TxData txData}) async { + _hackedCheckTorNodePrefs(); await txData.tezosOperationsList!.inject(); await txData.tezosOperationsList!.monitor(); return txData.copyWith( @@ -301,6 +307,7 @@ class TezosWallet extends Bip39Wallet { TezosAccount account, String recipientAddress, ) async { + _hackedCheckTorNodePrefs(); try { final opList = await _buildSendTransaction( amount: Amount( @@ -356,6 +363,7 @@ class TezosWallet extends Bip39Wallet { int feeRate, { String recipientAddress = "tz1MXvDCyXSqBqXPNDcsdmVZKfoxL9FTHmp2", }) async { + _hackedCheckTorNodePrefs(); if (info.cachedBalance.spendable.raw == BigInt.zero) { return Amount( rawValue: BigInt.zero, @@ -402,6 +410,7 @@ class TezosWallet extends Bip39Wallet { @override Future pingCheck() async { + _hackedCheckTorNodePrefs(); final currentNode = getCurrentNode(); return await TezosRpcAPI.testNetworkConnection( nodeInfo: ( @@ -413,6 +422,7 @@ class TezosWallet extends Bip39Wallet { @override Future recover({required bool isRescan}) async { + _hackedCheckTorNodePrefs(); await refreshMutex.protect(() async { if (isRescan) { await mainDB.deleteWalletBlockchainData(walletId); @@ -463,6 +473,7 @@ class TezosWallet extends Bip39Wallet { @override Future updateBalance() async { try { + _hackedCheckTorNodePrefs(); final currentNode = _xtzNode ?? getCurrentNode(); final balance = await TezosRpcAPI.getBalance( nodeInfo: (host: currentNode.host, port: currentNode.port), @@ -498,6 +509,7 @@ class TezosWallet extends Bip39Wallet { @override Future updateChainHeight() async { try { + _hackedCheckTorNodePrefs(); final currentNode = _xtzNode ?? getCurrentNode(); final height = await TezosRpcAPI.getChainHeight( nodeInfo: ( @@ -530,14 +542,15 @@ class TezosWallet extends Bip39Wallet { @override NodeModel getCurrentNode() { - return _xtzNode ?? + return _xtzNode ??= NodeService(secureStorageInterface: secureStorageInterface) - .getPrimaryNodeFor(currency: info.coin) ?? - info.coin.defaultNode; + .getPrimaryNodeFor(currency: info.coin) ?? + info.coin.defaultNode; } @override Future updateTransactions() async { + _hackedCheckTorNodePrefs(); // TODO: optimize updateTransactions and use V2 final myAddress = (await getCurrentReceivingAddress())!; @@ -615,4 +628,26 @@ class TezosWallet extends Bip39Wallet { // do nothing. Not used in tezos return false; } + + void _hackedCheckTorNodePrefs() { + final node = nodeService.getPrimaryNodeFor(currency: cryptoCurrency)!; + final netOption = TorPlainNetworkOption.fromNodeData( + node.torEnabled, + node.clearnetEnabled, + ); + + if (prefs.useTor) { + if (netOption == TorPlainNetworkOption.clear) { + throw NodeTorMismatchConfigException( + message: "TOR enabled but node set to clearnet only", + ); + } + } else { + if (netOption == TorPlainNetworkOption.tor) { + throw NodeTorMismatchConfigException( + message: "TOR off but node set to TOR only", + ); + } + } + } }