What changed, and why it matters
This commit adds a cleanup routine for Ethereum transactions that were replaced by another transaction with the same nonce. In Ethereum, if you send a second transaction with the same sequence number (nonce) but a higher fee, the first one can be dropped by the network. Stack Wallet previously kept these dropped transactions visible as 'pending' forever. The new code detects them by checking the blockchain and removes them from the local transaction list. It is a bug-fix/quality improvement rather than an active remote hack vulnerability.
Treat as a routine reliability fix. Reviewers should verify that the nonce comparison (transaction.nonce >= latestConfirmedNonce continue; else consider replaced) correctly handles reorgs and that getTransactionByHash returning a transaction with blockHash == null is a reliable signal of a replaced transaction on the target RPC providers. Consider whether silently deleting records could hide transactions that are merely temporarily unavailable from the node.
Security signals we found
Data-consistency bug fix: stale pending Ethereum transactions could mislead users about balance and transaction state
Relies on external node RPC (getTransactionCount + getTransactionByHash); node errors are logged and cause preservation of pending records
Deletion is scoped to pending transactions with non-null nonce belonging to the wallet
No input from untrusted users is used in the deletion query beyond already-stored transaction records
Evidence from the diff
The patch introduces findReplacedPendingEthereumTransactions() and deleteReplacedPendingTransactions() in EthereumWallet, plus calls from EthTokenWallet. The logic: fetch pending outgoing ETH/token transactions that have a nonce lower than the wallet’s latest confirmed on-chain nonce, then query each txid; if the node returns null (transaction no longer known) or the response has no blockHash, the local pending record is considered replaced/dropped and is deleted from Isar. Unit tests cover happy path, subtype/type/wallet/height filters, nonce lookup failure, hash lookup failure, and the no-candidates short-circuit.
Changed components
lib/wallets/wallet/impl/ethereum_wallet.dartlib/wallets/wallet/impl/sub_wallets/eth_token_wallet.dartEthereum transaction history store (Isar transactionV2s)ETH and ERC-20 token transaction refresh pathsInspect captured patch +338 / −0
diff --git a/lib/wallets/wallet/impl/ethereum_wallet.dart b/lib/wallets/wallet/impl/ethereum_wallet.dart
index 133fbc2..f5444c7 100644
--- a/lib/wallets/wallet/impl/ethereum_wallet.dart
+++ b/lib/wallets/wallet/impl/ethereum_wallet.dart
@@ -29,6 +29,62 @@ import '../../models/tx_data.dart';
import '../intermediate/bip39_wallet.dart';
import '../wallet_mixin_interfaces/private_key_interface.dart';
+@visibleForTesting
+Future<List<TransactionV2>> findReplacedPendingEthereumTransactions({
+ required String walletId,
+ required Iterable<TransactionV2> transactions,
+ required Future<int> Function() getLatestConfirmedNonce,
+ required Future<web3.TransactionInformation?> Function(String txid)
+ getTransactionByHash,
+ void Function(Object error, StackTrace stackTrace)? onNonceLookupError,
+ void Function(TransactionV2 transaction, Object error, StackTrace stackTrace)?
+ onTransactionLookupError,
+}) async {
+ final candidates = transactions
+ .where(
+ (transaction) =>
+ transaction.walletId == walletId &&
+ transaction.height == null &&
+ transaction.blockHash == null &&
+ transaction.nonce != null &&
+ (transaction.type == TransactionType.outgoing ||
+ transaction.type == TransactionType.sentToSelf) &&
+ (transaction.subType == TransactionSubType.none ||
+ transaction.subType == TransactionSubType.ethToken),
+ )
+ .toList(growable: false);
+
+ if (candidates.isEmpty) {
+ return const [];
+ }
+
+ final int latestConfirmedNonce;
+ try {
+ latestConfirmedNonce = await getLatestConfirmedNonce();
+ } catch (e, s) {
+ onNonceLookupError?.call(e, s);
+ return const [];
+ }
+
+ final replacedTransactions = <TransactionV2>[];
+ for (final transaction in candidates) {
+ if (transaction.nonce! >= latestConfirmedNonce) {
+ continue;
+ }
+
+ try {
+ final originalTransaction = await getTransactionByHash(transaction.txid);
+ if (originalTransaction?.blockHash == null) {
+ replacedTransactions.add(transaction);
+ }
+ } catch (e, s) {
+ onTransactionLookupError?.call(transaction, e, s);
+ }
+ }
+
+ return replacedTransactions;
+}
+
// Eth can not use tor with web3dart
@visibleForTesting
@@ -339,6 +395,7 @@ class EthereumWallet extends Bip39Wallet with PrivateKeyInterface {
if (response.value!.isEmpty) {
// no new transactions found
+ await deleteReplacedPendingTransactions();
return;
}
@@ -457,6 +514,55 @@ class EthereumWallet extends Bip39Wallet with PrivateKeyInterface {
txns.add(txn);
}
await mainDB.updateOrPutTransactionV2s(txns);
+ await deleteReplacedPendingTransactions();
+ }
+
+ Future<void> deleteReplacedPendingTransactions() async {
+ final pendingTransactions = await mainDB.isar.transactionV2s
+ .where()
+ .walletIdEqualTo(walletId)
+ .filter()
+ .heightIsNull()
+ .nonceIsNotNull()
+ .findAll();
+
+ web3.Web3Client? client;
+ web3.Web3Client getClient() => client ??= getEthClient();
+ final replacedTransactions = await findReplacedPendingEthereumTransactions(
+ walletId: walletId,
+ transactions: pendingTransactions,
+ getLatestConfirmedNonce: () async => getClient().getTransactionCount(
+ await getMyWeb3Address(),
+ atBlock: const web3.BlockNum.current(),
+ ),
+ getTransactionByHash: (txid) => getClient().getTransactionByHash(txid),
+ onNonceLookupError: (error, stackTrace) {
+ Logging.instance.w(
+ "$runtimeType failed to get the latest confirmed nonce",
+ error: error,
+ stackTrace: stackTrace,
+ );
+ },
+ onTransactionLookupError: (transaction, error, stackTrace) {
+ Logging.instance.w(
+ "$runtimeType failed to look up pending transaction "
+ "${transaction.txid}",
+ error: error,
+ stackTrace: stackTrace,
+ );
+ },
+ );
+ final replacedTransactionIds = replacedTransactions
+ .map((transaction) => transaction.id)
+ .toList(growable: false);
+
+ if (replacedTransactionIds.isEmpty) {
+ return;
+ }
+
+ await mainDB.isar.writeTxn(() async {
+ await mainDB.isar.transactionV2s.deleteAll(replacedTransactionIds);
+ });
}
@override
diff --git a/lib/wallets/wallet/impl/sub_wallets/eth_token_wallet.dart b/lib/wallets/wallet/impl/sub_wallets/eth_token_wallet.dart
index 18cb8f7..1c61f29 100644
--- a/lib/wallets/wallet/impl/sub_wallets/eth_token_wallet.dart
+++ b/lib/wallets/wallet/impl/sub_wallets/eth_token_wallet.dart
@@ -388,6 +388,7 @@ class EthTokenWallet extends Wallet {
// no need to continue if no transactions found
if (response.value!.isEmpty) {
+ await ethWallet.deleteReplacedPendingTransactions();
return;
}
@@ -509,6 +510,7 @@ class EthTokenWallet extends Wallet {
}
}
await mainDB.updateOrPutTransactionV2s(txns);
+ await ethWallet.deleteReplacedPendingTransactions();
} catch (e, s) {
Logging.instance.w(
"$runtimeType wallet failed to update transactions: ",
diff --git a/test/wallets/ethereum_replaced_transaction_test.dart b/test/wallets/ethereum_replaced_transaction_test.dart
new file mode 100644
index 0000000..f2029bc
--- /dev/null
+++ b/test/wallets/ethereum_replaced_transaction_test.dart
@@ -0,0 +1,230 @@
+import 'dart:convert';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart';
+import 'package:stackwallet/models/isar/models/blockchain_data/v2/transaction_v2.dart';
+import 'package:stackwallet/wallets/wallet/impl/ethereum_wallet.dart';
+import 'package:web3dart/web3dart.dart' as web3;
+
+const _walletId = "wallet";
+const _otherWalletId = "otherWallet";
+const _address = "0x1111111111111111111111111111111111111111";
+const _nativeTxid =
+ "0x1111111111111111111111111111111111111111111111111111111111111111";
+const _tokenTxid =
+ "0x2222222222222222222222222222222222222222222222222222222222222222";
+const _minedTxid =
+ "0x3333333333333333333333333333333333333333333333333333333333333333";
+
+void main() {
+ test("finds replaced pending ETH and token transactions", () async {
+ final nativeTransaction = _transaction(
+ id: 1,
+ txid: _nativeTxid,
+ nonce: 6,
+ subType: TransactionSubType.none,
+ );
+ final tokenTransaction = _transaction(
+ id: 2,
+ txid: _tokenTxid,
+ nonce: 7,
+ subType: TransactionSubType.ethToken,
+ type: TransactionType.sentToSelf,
+ );
+ final transactions = <String, web3.TransactionInformation?>{
+ _nativeTxid: null,
+ _tokenTxid: _transactionInformation(_tokenTxid, mined: false),
+ };
+ final lookedUpTxids = <String>[];
+ int transactionCountCalls = 0;
+
+ final replacedTransactions = await findReplacedPendingEthereumTransactions(
+ walletId: _walletId,
+ transactions: [nativeTransaction, tokenTransaction],
+ getLatestConfirmedNonce: () async {
+ transactionCountCalls++;
+ return 8;
+ },
+ getTransactionByHash: (txid) async {
+ lookedUpTxids.add(txid);
+ return transactions[txid];
+ },
+ );
+
+ expect(
+ replacedTransactions.map((transaction) => transaction.id),
+ unorderedEquals([nativeTransaction.id, tokenTransaction.id]),
+ );
+ expect(transactionCountCalls, 1);
+ expect(lookedUpTxids, unorderedEquals([_nativeTxid, _tokenTxid]));
+ });
+
+ test("preserves transactions without replacement proof", () async {
+ final unconsumed = _transaction(id: 1, txid: "unconsumed", nonce: 9);
+ final mined = _transaction(id: 2, txid: _minedTxid, nonce: 8);
+ final incoming = _transaction(
+ id: 3,
+ txid: "incoming",
+ nonce: 7,
+ type: TransactionType.incoming,
+ );
+ final unsupportedSubtype = _transaction(
+ id: 4,
+ txid: "unsupportedSubtype",
+ nonce: 6,
+ subType: TransactionSubType.cashFusion,
+ );
+ final confirmed = _transaction(
+ id: 5,
+ txid: "confirmed",
+ nonce: 5,
+ height: 1,
+ );
+ final otherWallet = _transaction(
+ id: 6,
+ txid: "otherWallet",
+ nonce: 4,
+ walletId: _otherWalletId,
+ );
+ final missingNonce = _transaction(id: 7, txid: "missingNonce", nonce: null);
+ final lookedUpTxids = <String>[];
+
+ final replacedTransactions = await findReplacedPendingEthereumTransactions(
+ walletId: _walletId,
+ transactions: [
+ unconsumed,
+ mined,
+ incoming,
+ unsupportedSubtype,
+ confirmed,
+ otherWallet,
+ missingNonce,
+ ],
+ getLatestConfirmedNonce: () async => 9,
+ getTransactionByHash: (txid) async {
+ lookedUpTxids.add(txid);
+ return _transactionInformation(txid);
+ },
+ );
+
+ expect(replacedTransactions, isEmpty);
+ expect(lookedUpTxids, [_minedTxid]);
+ });
+
+ test("preserves pending transactions when nonce lookup fails", () async {
+ int transactionLookupCalls = 0;
+ Object? lookupError;
+
+ final replacedTransactions = await findReplacedPendingEthereumTransactions(
+ walletId: _walletId,
+ transactions: [_transaction(id: 1, txid: _nativeTxid, nonce: 1)],
+ getLatestConfirmedNonce: () async =>
+ throw StateError("nonce lookup failed"),
+ getTransactionByHash: (txid) async {
+ transactionLookupCalls++;
+ return null;
+ },
+ onNonceLookupError: (error, stackTrace) {
+ lookupError = error;
+ },
+ );
+
+ expect(replacedTransactions, isEmpty);
+ expect(transactionLookupCalls, 0);
+ expect(lookupError, isA<StateError>());
+ });
+
+ test("preserves pending transactions when hash lookup fails", () async {
+ Object? lookupError;
+ TransactionV2? failedTransaction;
+
+ final transaction = _transaction(id: 1, txid: _nativeTxid, nonce: 1);
+ final replacedTransactions = await findReplacedPendingEthereumTransactions(
+ walletId: _walletId,
+ transactions: [transaction],
+ getLatestConfirmedNonce: () async => 2,
+ getTransactionByHash: (txid) async =>
+ throw StateError("hash lookup failed"),
+ onTransactionLookupError: (transaction, error, stackTrace) {
+ failedTransaction = transaction;
+ lookupError = error;
+ },
+ );
+
+ expect(replacedTransactions, isEmpty);
+ expect(failedTransaction, same(transaction));
+ expect(lookupError, isA<StateError>());
+ });
+
+ test("does not query the node without pending transactions", () async {
+ int transactionCountCalls = 0;
+ int transactionLookupCalls = 0;
+
+ final replacedTransactions = await findReplacedPendingEthereumTransactions(
+ walletId: _walletId,
+ transactions: [
+ _transaction(
+ id: 1,
+ txid: "incoming",
+ nonce: 1,
+ type: TransactionType.incoming,
+ ),
+ ],
+ getLatestConfirmedNonce: () async {
+ transactionCountCalls++;
+ return 2;
+ },
+ getTransactionByHash: (txid) async {
+ transactionLookupCalls++;
+ return null;
+ },
+ );
+
+ expect(replacedTransactions, isEmpty);
+ expect(transactionCountCalls, 0);
+ expect(transactionLookupCalls, 0);
+ });
+}
+
+TransactionV2 _transaction({
+ required int id,
+ required String txid,
+ required int? nonce,
+ String walletId = _walletId,
+ int? height,
+ TransactionType type = TransactionType.outgoing,
+ TransactionSubType subType = TransactionSubType.none,
+}) => TransactionV2(
+ walletId: walletId,
+ blockHash: height == null ? null : "blockHash",
+ hash: txid,
+ txid: txid,
+ timestamp: 1,
+ height: height,
+ inputs: const [],
+ outputs: const [],
+ version: -1,
+ type: type,
+ subType: subType,
+ otherData: jsonEncode({TxV2OdKeys.nonce: nonce}),
+)..id = id;
+
+web3.TransactionInformation _transactionInformation(
+ String txid, {
+ bool mined = true,
+}) => web3.TransactionInformation.fromMap({
+ "blockHash": mined ? "blockHash" : null,
+ "blockNumber": mined ? "1" : null,
+ "from": _address,
+ "gas": "21000",
+ "gasPrice": "1",
+ "hash": txid,
+ "input": "0x",
+ "nonce": "1",
+ "to": _address,
+ "transactionIndex": mined ? "0" : null,
+ "value": "1",
+ "v": "27",
+ "r": "0x1",
+ "s": "0x1",
+});
Why this scored 34/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.