add extra checks to verify that the returned transaction for an RBF, matches the original one
What changed, and why it matters
This commit adds safety checks to a Bitcoin wallet feature called Replace-By-Fee (RBF), which lets users resend a transaction with a higher fee if it's stuck. The wallet now verifies that the transaction data returned by the Electrum server actually matches the transaction the user asked to replace, and that each input's previous transaction also matches. Without these checks, a malicious or buggy server could return the wrong transaction data, potentially causing the wallet to build an RBF replacement that spends different coins than intended or reveals private information.
Review whether getTransactionExpanded is used elsewhere without similar txId validation, and consider centralizing the integrity check inside getTransactionExpanded itself. Also verify that txId() comparisons are robust to endianness or serialization differences in the underlying bitcoin library.
Security signals we found
RBF transaction integrity check added
Server-returned transaction hash mismatch now rejected
Input UTXO transaction hash mismatch now rejected
Potential wrong-transaction-spend or privacy leak mitigated
Patch is partial: only covers RBF path, not all uses of getTransactionExpanded
Evidence from the diff
In cw_bitcoin/lib/electrum_wallet.dart, the commit adds txId equality checks in two RBF-related methods. In canReplaceByFee(), it returns null if bundle.originalTransaction.txId() does not match tx.txHash. In replaceByFee(), it throws RbfException if the returned original transaction’s txId doesn’t match the requested hash, and additionally checks that each input’s referenced prior transaction (bundle.ins[i].txId()) matches input.txId. These are integrity checks against a confused-deputy / wrong-data scenario where getTransactionExpanded returns a mismatched transaction bundle.
Changed components
cw_bitcoin/lib/electrum_wallet.dartElectrumWalletBase.canReplaceByFee()ElectrumWalletBase.replaceByFee()Bitcoin RBF (Replace-By-Fee) flowInspect captured patch +5 / −1
### cw_bitcoin/lib/electrum_wallet.dart
@@ -2454,6 +2454,7 @@ abstract class ElectrumWalletBase
Future<String?> canReplaceByFee(ElectrumTransactionInfo tx) async {
try {
final bundle = await getTransactionExpanded(hash: tx.txHash);
+ if (bundle.originalTransaction.txId() != tx.txHash) return null;
_updateInputsAndOutputs(tx, bundle);
if (bundle.confirmations > 0) return null;
return bundle.originalTransaction.canReplaceByFee ? bundle.originalTransaction.toHex() : null;
@@ -2505,6 +2506,9 @@ abstract class ElectrumWalletBase
Future<PendingBitcoinTransaction> replaceByFee(String hash, int newFee) async {
try {
final bundle = await getTransactionExpanded(hash: hash);
+ if (bundle.originalTransaction.txId() != hash) {
+ throw RbfException("Transaction does not match $hash");
+ }
final utxos = <UtxoWithAddress>[];
final outputs = <BitcoinOutput>[];
@@ -2517,7 +2521,7 @@ abstract class ElectrumWalletBase
for (var i = 0; i < bundle.originalTransaction.inputs.length; i++) {
final input = bundle.originalTransaction.inputs[i];
final inputTransaction = bundle.ins[i];
- if (inputTransaction == null) {
+ if (inputTransaction == null || inputTransaction.txId() != input.txId) {
throw RbfException("Missing input transaction for replace-by-fee");
}
final vout = input.txIndex;Why this scored 61/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.