What changed, and why it matters
This commit adds Tor privacy support for Ethereum in Stack Wallet. Previously, Ethereum RPC traffic always traveled over the regular internet (clearnet), even when the user turned on Tor, because the underlying library used a plain HTTP client. The patch routes Ethereum traffic through the Tor SOCKS5 proxy when Tor is enabled, and adds a check that warns if the selected node conflicts with the user's Tor setting. It also adds tests to confirm the proxying works.
No immediate user action required. The fix should be included in the next release. Users relying on Tor for Ethereum should verify they are on a build containing this commit and that their selected Ethereum node supports Tor or is configured appropriately.
Security signals we found
Previously Ethereum RPC and ERC-20 token traffic bypassed Tor despite the user's Tor setting, causing potential IP/transaction graph leakage.
Patch enforces SOCKS5 proxying for Ethereum RPC when Tor is enabled.
Adds Tor/clearnet node mismatch guard consistent with other coins.
Adds unit tests verifying proxy routing and direct fallback behavior.
Removes outdated comments incorrectly stating Ethereum cannot use Tor with web3dart.
Evidence from the diff
The change replaces the plain http.Client used by web3dart with an IOClient wrapping a dart:io HttpClient configured via socks5_proxy/socks to use the Tor SOCKS5 proxy when prefs.useTor is true and the app has the Tor feature. It mirrors the existing Solana implementation. A NodeTorMismatchConfigException is thrown when the wallet’s Tor preference and the node’s tor/clearnet flags disagree. torSupport is set to true for Ethereum, and the node connection test is updated to use the same proxied client builder. A unit test with a fake SOCKS5 server verifies that the RPC request is tunneled through the proxy and that direct clearnet connections are used when Tor is off.
Changed components
lib/wallets/wallet/impl/ethereum_wallet.dartlib/utilities/test_eth_node_connection.dartlib/utilities/test_node_connection.dartlib/wallets/crypto_currency/coins/ethereum.darttest/wallets/ethereum_tor_proxy_test.dartInspect captured patch +214 / −12
### lib/utilities/test_eth_node_connection.dart
@@ -1,12 +1,19 @@
-import 'package:http/http.dart';
-import 'package:web3dart/web3dart.dart' as web3;
+import '../services/tor_service.dart';
+import '../wallets/wallet/impl/ethereum_wallet.dart';
+import 'prefs.dart';
-Future<bool> testEthNodeConnection(String host) async {
- final web3.Web3Client client = web3.Web3Client(host, Client());
+Future<bool> testEthNodeConnection(
+ String host, {
+ required Prefs prefs,
+ required TorService torService,
+}) async {
+ final client = EthereumWallet.createWeb3Client(host, prefs, torService);
try {
await client.getBlockNumber();
return true;
} catch (_) {
return false;
+ } finally {
+ await client.dispose();
}
}
### lib/utilities/test_node_connection.dart
@@ -235,7 +235,11 @@ Future<bool> testNodeConnection({
case Ethereum():
try {
- testPassed = await testEthNodeConnection(formData.host!);
+ testPassed = await testEthNodeConnection(
+ formData.host!,
+ prefs: read(prefsChangeNotifierProvider),
+ torService: read(pTorService),
+ );
} catch (_) {
testPassed = false;
}
### lib/wallets/crypto_currency/coins/ethereum.dart
@@ -47,6 +47,9 @@ class Ethereum extends Bip39Currency {
@override
bool get hasTokenSupport => true;
+ @override
+ bool get torSupport => true;
+
@override
NodeModel defaultNode({required bool isPrimary}) => NodeModel(
host: "https://eth2.stackwallet.com",
### lib/wallets/wallet/impl/ethereum_wallet.dart
@@ -1,16 +1,20 @@
import 'dart:async';
import 'dart:convert';
+import 'dart:io';
import 'package:decimal/decimal.dart';
import 'package:ethereum_addresses/ethereum_addresses.dart';
import 'package:flutter/foundation.dart';
-import 'package:http/http.dart';
+import 'package:http/io_client.dart';
import 'package:isar_community/isar.dart';
+import 'package:socks5_proxy/socks.dart';
import 'package:wallet/wallet.dart' as eth_wallet;
import 'package:web3dart/json_rpc.dart' show RPCError;
import 'package:web3dart/web3dart.dart' as web3;
+import '../../../app_config.dart';
import '../../../dto/ethereum/eth_tx_dto.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';
@@ -21,9 +25,12 @@ import '../../../models/paymint/fee_object_model.dart';
import '../../../services/ethereum/ethereum_api.dart';
import '../../../services/event_bus/events/global/updated_in_background_event.dart';
import '../../../services/event_bus/global_event_bus.dart';
+import '../../../services/tor_service.dart';
import '../../../utilities/amount/amount.dart';
import '../../../utilities/eth_commons.dart';
import '../../../utilities/logger.dart';
+import '../../../utilities/prefs.dart';
+import '../../../utilities/tor_plain_net_option_enum.dart';
import '../../crypto_currency/crypto_currency.dart';
import '../../models/tx_data.dart';
import '../intermediate/bip39_wallet.dart';
@@ -85,8 +92,6 @@ Future<List<TransactionV2>> findReplacedPendingEthereumTransactions({
return replacedTransactions;
}
-// Eth can not use tor with web3dart
-
@visibleForTesting
({BigInt maxFeePerGas, BigInt maxPriorityFeePerGas}) resolveEip1559FeeCaps({
required BigInt baseFee,
@@ -144,10 +149,45 @@ class EthereumWallet extends Bip39Wallet with PrivateKeyInterface {
web3.Web3Client getEthClient() {
final node = getCurrentNode();
- // Eth can not use tor with web3dart as Client does not support proxies
- final client = Client();
+ final netOption = TorPlainNetworkOption.fromNodeData(
+ node.torEnabled,
+ node.clearnetEnabled,
+ );
- return web3.Web3Client(node.host, client);
+ 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",
+ );
+ }
+ }
+
+ return createWeb3Client(node.host, prefs, TorService.sharedInstance);
+ }
+
+ // static helper function for building a web3 client, proxied over tor if
+ // enabled
+ static web3.Web3Client createWeb3Client(
+ String url,
+ Prefs prefs,
+ TorService torService,
+ ) {
+ final httpClient = HttpClient();
+
+ if (AppConfig.hasFeature(AppFeature.tor) && prefs.useTor) {
+ final proxyInfo = torService.getProxyInfo();
+ SocksTCPClient.assignToHttpClient(httpClient, [
+ ProxySettings(proxyInfo.host, proxyInfo.port),
+ ]);
+ }
+
+ return web3.Web3Client(url, IOClient(httpClient));
}
Amount estimateEthFee(BigInt feeRate, int gasLimit, int decimals) {
@@ -292,8 +332,8 @@ class EthereumWallet extends Bip39Wallet with PrivateKeyInterface {
@override
Future<bool> pingCheck() async {
- final web3.Web3Client client = getEthClient();
try {
+ final client = getEthClient();
await client.getBlockNumber();
return true;
} catch (_) {
### test/wallets/ethereum_tor_proxy_test.dart
@@ -0,0 +1,148 @@
+import 'dart:convert';
+import 'dart:io';
+import 'dart:typed_data';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:mockito/mockito.dart';
+import 'package:stackwallet/services/tor_service.dart';
+import 'package:stackwallet/utilities/prefs.dart';
+import 'package:stackwallet/wallets/wallet/impl/ethereum_wallet.dart';
+
+class _MockPrefs extends Mock implements Prefs {
+ _MockPrefs(this._useTor);
+
+ final bool _useTor;
+
+ @override
+ bool get useTor => _useTor;
+}
+
+class _FakeTorService extends Fake implements TorService {
+ _FakeTorService(this.proxyInfo);
+
+ final ({InternetAddress host, int port}) proxyInfo;
+ int proxyInfoReads = 0;
+
+ @override
+ ({InternetAddress host, int port}) getProxyInfo() {
+ proxyInfoReads++;
+ return proxyInfo;
+ }
+}
+
+const _rpcBody = '{"jsonrpc":"2.0","id":0,"result":"0x2a"}';
+
+String _rpcHttpResponse() =>
+ 'HTTP/1.1 200 OK\r\n'
+ 'Content-Type: application/json\r\n'
+ 'Content-Length: ${_rpcBody.length}\r\n'
+ 'Connection: close\r\n\r\n'
+ '$_rpcBody';
+
+// Minimal SOCKS5 server that records the CONNECT target and answers the
+// tunnelled HTTP request with a canned JSON-RPC eth_blockNumber result.
+class _FakeSocksServer {
+ late final ServerSocket _server;
+ int? addressType;
+ String? target;
+ int? port;
+ final request = StringBuffer();
+
+ int get listeningPort => _server.port;
+
+ Future<void> start() async {
+ _server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
+ _server.listen((socket) {
+ var stage = 0;
+ socket.listen((Uint8List bytes) {
+ switch (stage) {
+ case 0:
+ socket.add([0x05, 0x00]);
+ stage = 1;
+ case 1:
+ addressType = bytes[3];
+ if (addressType == 0x03) {
+ target = String.fromCharCodes(bytes.sublist(5, 5 + bytes[4]));
+ } else {
+ target = bytes.sublist(4, bytes.length - 2).join('.');
+ }
+ port = (bytes[bytes.length - 2] << 8) | bytes[bytes.length - 1];
+ socket.add([0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0]);
+ stage = 2;
+ case 2:
+ request.write(utf8.decode(bytes, allowMalformed: true));
+ socket.write(_rpcHttpResponse());
+ socket.close();
+ stage = 3;
+ default:
+ request.write(utf8.decode(bytes, allowMalformed: true));
+ }
+ });
+ });
+ }
+
+ Future<void> stop() => _server.close();
+}
+
+void main() {
+ late _FakeSocksServer socks;
+
+ setUp(() async {
+ socks = _FakeSocksServer();
+ await socks.start();
+ });
+
+ tearDown(() => socks.stop());
+
+ test('web3 client sends RPC through the Tor SOCKS5 proxy', () async {
+ final torService = _FakeTorService((
+ host: InternetAddress.loopbackIPv4,
+ port: socks.listeningPort,
+ ));
+ final client = EthereumWallet.createWeb3Client(
+ 'http://eth.example.invalid:8545',
+ _MockPrefs(true),
+ torService,
+ );
+
+ final height = await client.getBlockNumber();
+ await client.dispose();
+
+ expect(height, 42);
+ expect(torService.proxyInfoReads, 1);
+ // Hostname goes to the proxy, so there is no local DNS lookup.
+ expect(socks.addressType, 0x03);
+ expect(socks.target, 'eth.example.invalid');
+ expect(socks.port, 8545);
+ expect(socks.request.toString(), contains('eth_blockNumber'));
+ });
+
+ test('web3 client connects directly when Tor is off', () async {
+ final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
+ server.listen((req) async {
+ await utf8.decoder.bind(req).join();
+ req.response
+ ..headers.contentType = ContentType.json
+ ..write(_rpcBody);
+ await req.response.close();
+ });
+
+ final torService = _FakeTorService((
+ host: InternetAddress.loopbackIPv4,
+ port: socks.listeningPort,
+ ));
+ final client = EthereumWallet.createWeb3Client(
+ 'http://127.0.0.1:${server.port}',
+ _MockPrefs(false),
+ torService,
+ );
+
+ final height = await client.getBlockNumber();
+ await client.dispose();
+ await server.close(force: true);
+
+ expect(height, 42);
+ expect(torService.proxyInfoReads, 0);
+ expect(socks.target, isNull);
+ });
+}Why this scored 38/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.