What changed, and why it matters
This commit rewrites how Stack Wallet calculates fees for MWEB (a privacy feature in Litecoin). It introduces a new helper module that reconciles the estimated fee against what the transaction actually pays, and loops up to 10 times to fix mismatches. The change appears to be a bug-fix/refactor of fee arithmetic rather than a new feature or an obvious security patch. There is no vendor statement that this fixes a security vulnerability.
Treat as a routine correctness/refactor commit. Reviewers should verify that the new fee arithmetic cannot produce a lower fee than the network minimum for MWEB peg-ins, and that the 10-attempt reconciliation loop always terminates without leaking state between iterations. No immediate security response is indicated by the available evidence.
Security signals we found
Fee-calculation logic changed for privacy-related MWEB transactions
New loop with maxAttempts=10 could, in theory, fail to converge and throw
Minimum fee rate clamped to 1000 sats/kB to prevent underpayment
Peg-in branch now uses rate-based fee plus vSize floor instead of simple sum
No explicit security advisory, CVE, or bug-bounty attribution in commit or references
Evidence from the diff
The patch adds mweb_fee_utils.dart with three functions: effectiveMwebFeeRatePerKB (clamps fee rate to a 1000 sat/kB minimum and converts sats/vByte to sats/kB), requiredMwebFee (computes the needed fee, with a special peg-in branch that uses a rate-based fee with a vSize floor), and processWithReconciledMwebFee (iteratively rebuilds the transaction candidate until the paid fee meets the required fee, capped at 10 attempts). electrumx_interface.dart and mweb_interface.dart are updated to use these helpers, pass feeRatePerKB explicitly, and avoid double-adding the MWEB fee for peg-in transactions. Unit tests cover the new helpers.
Changed components
lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dartlib/wallets/wallet/wallet_mixin_interfaces/mweb_fee_utils.dartlib/wallets/wallet/wallet_mixin_interfaces/mweb_interface.darttest/wallets/mweb_fee_utils_test.dartInspect captured patch +280 / −36
diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart
index eaf21d9..0152ca3 100644
--- a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart
+++ b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart
@@ -35,6 +35,7 @@ import '../impl/peercoin_wallet.dart';
import '../intermediate/bip39_hd_wallet.dart';
import 'cpfp_interface.dart';
import 'electrum_fee_planner.dart';
+import 'mweb_fee_utils.dart';
import 'mweb_interface.dart';
import 'paynym_interface.dart';
import 'rbf_interface.dart';
@@ -2005,32 +2006,46 @@ mixin ElectrumXInterface<T extends ElectrumXCurrencyInterface>
// mweb
if (result.type.isMweb()) {
- final fee = await (this as MwebInterface).mwebFee(txData: result);
-
- TxData mwebData = await coinSelection(
- txData: result.copyWith(
- recipients: txData.subtractFeeFromAmount
- ? txData.recipients
- : result.recipients!.where((e) => !(e.isChange)).toList(),
- ),
- utxos: utxos?.toList(),
- coinControl: coinControl,
- isSendAll: isSendAll,
- isSendAllCoinControlUtxos: isSendAllCoinControlUtxos,
- minimumFeeAmount: fee.raw,
+ final effectiveFeeRatePerKB = effectiveMwebFeeRatePerKB(
+ feeRatePerKB: result.feeRateAmount!,
+ satsPerVByte: result.satsPerVByte,
);
-
- if (mwebData.type == TxType.mwebPegIn) {
- mwebData = await buildTransaction(
- txData: mwebData,
- inputsWithKeys: mwebData.usedUTXOs!,
- );
- }
- final data = await (this as MwebInterface).processMwebTransaction(
- mwebData,
+ // Awaited so failures surface in this function's catch below.
+ return await processWithReconciledMwebFee<TxData, TxData>(
+ initial: result,
+ feeRatePerKB: effectiveFeeRatePerKB,
+ paidFee: (candidate) => candidate.fee!.raw,
+ requiredFee: (candidate, feeRatePerKB) async =>
+ (await (this as MwebInterface).mwebFee(
+ txData: candidate,
+ feeRatePerKB: feeRatePerKB,
+ )).raw,
+ rebuild: (initial, minimumFee) {
+ // Use the original recipients so send-all selects every input.
+ return coinSelection(
+ txData: initial.copyWith(recipients: txData.recipients),
+ utxos: utxos?.toList(),
+ coinControl: coinControl,
+ isSendAll: isSendAll,
+ isSendAllCoinControlUtxos: isSendAllCoinControlUtxos,
+ minimumFeeAmount: minimumFee,
+ );
+ },
+ process: (candidate, feeRatePerKB) async {
+ if (candidate.type == TxType.mwebPegIn) {
+ candidate = await buildTransaction(
+ txData: candidate,
+ inputsWithKeys: candidate.usedUTXOs!,
+ );
+ }
+ final data = await (this as MwebInterface).processMwebTransaction(
+ txData: candidate,
+ feeRatePerKB: feeRatePerKB,
+ );
+ Logging.instance.d("prepare MWEB send: $data");
+ return data;
+ },
);
- Logging.instance.d("prepare MWEB send: $data");
- return data;
}
Logging.instance.d("prepare send: $result");
diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/mweb_fee_utils.dart b/lib/wallets/wallet/wallet_mixin_interfaces/mweb_fee_utils.dart
new file mode 100644
index 0000000..404c26e
--- /dev/null
+++ b/lib/wallets/wallet/wallet_mixin_interfaces/mweb_fee_utils.dart
@@ -0,0 +1,61 @@
+const _kilobyte = 1000;
+final _kilobyteBigInt = BigInt.from(_kilobyte);
+
+BigInt effectiveMwebFeeRatePerKB({
+ required BigInt feeRatePerKB,
+ required int? satsPerVByte,
+}) {
+ final requestedFeeRatePerKB = satsPerVByte == null
+ ? feeRatePerKB
+ : BigInt.from(satsPerVByte) * _kilobyteBigInt;
+
+ return requestedFeeRatePerKB < _kilobyteBigInt
+ ? _kilobyteBigInt
+ : requestedFeeRatePerKB;
+}
+
+BigInt requiredMwebFee({
+ required BigInt currentFee,
+ required BigInt feeIncrease,
+ required bool hasPegin,
+ required int vSize,
+ required BigInt feeRatePerKB,
+}) {
+ if (!hasPegin) {
+ return currentFee + feeIncrease;
+ }
+
+ final vSizeBigInt = BigInt.from(vSize);
+ final rateFee =
+ (feeRatePerKB * vSizeBigInt + _kilobyteBigInt - BigInt.one) ~/
+ _kilobyteBigInt;
+ final baseFee = rateFee > vSizeBigInt ? rateFee : vSizeBigInt;
+ return baseFee + feeIncrease;
+}
+
+Future<R> processWithReconciledMwebFee<T, R>({
+ required T initial,
+ required BigInt feeRatePerKB,
+ required BigInt Function(T candidate) paidFee,
+ required Future<BigInt> Function(T candidate, BigInt feeRatePerKB)
+ requiredFee,
+ required Future<T> Function(T initial, BigInt minimumFee) rebuild,
+ required Future<R> Function(T candidate, BigInt feeRatePerKB) process,
+}) async {
+ const maxAttempts = 10;
+ var attempt = 0;
+ var candidate = initial;
+
+ while (true) {
+ final fee = await requiredFee(candidate, feeRatePerKB);
+ if (fee <= paidFee(candidate)) {
+ return process(candidate, feeRatePerKB);
+ }
+ if (++attempt > maxAttempts) {
+ throw Exception(
+ "MWEB fee calculation failed to converge after $maxAttempts attempts",
+ );
+ }
+ candidate = await rebuild(initial, fee);
+ }
+}
diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/mweb_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/mweb_interface.dart
index 5f9fe71..f0542bd 100644
--- a/lib/wallets/wallet/wallet_mixin_interfaces/mweb_interface.dart
+++ b/lib/wallets/wallet/wallet_mixin_interfaces/mweb_interface.dart
@@ -29,6 +29,7 @@ import '../../isar/models/wallet_info.dart';
import '../../models/tx_data.dart';
import '../intermediate/external_wallet.dart';
import 'electrumx_interface.dart';
+import 'mweb_fee_utils.dart';
mixin MwebInterface<T extends ElectrumXCurrencyInterface>
on ElectrumXInterface<T>
@@ -440,13 +441,11 @@ mixin MwebInterface<T extends ElectrumXCurrencyInterface>
}
}
- Future<TxData> processMwebTransaction(TxData txData) async {
+ Future<TxData> processMwebTransaction({
+ required TxData txData,
+ required BigInt feeRatePerKB,
+ }) async {
final client = await _client;
- final vBytesPerKilobyte = BigInt.from(1000);
- final customSatsPerVByte = txData.satsPerVByte;
- final feeRatePerKB = customSatsPerVByte != null
- ? BigInt.from(customSatsPerVByte) * vBytesPerKilobyte
- : txData.feeRateAmount!;
final response = await client.create(
CreateRequest(
rawTx: txData.raw!.toUint8ListFromHex,
@@ -964,7 +963,10 @@ mixin MwebInterface<T extends ElectrumXCurrencyInterface>
}
}
- Future<Amount> mwebFee({required TxData txData}) async {
+ Future<Amount> mwebFee({
+ required TxData txData,
+ required BigInt feeRatePerKB,
+ }) async {
final outputs = txData.recipients!;
final utxos = txData.usedUTXOs!;
@@ -974,10 +976,6 @@ mixin MwebInterface<T extends ElectrumXCurrencyInterface>
final fee = sumOfUtxosValue - preOutputSum;
final vBytesPerKilobyte = BigInt.from(1000);
- final customSatsPerVByte = txData.satsPerVByte;
- final feeRatePerKB = customSatsPerVByte != null
- ? BigInt.from(customSatsPerVByte) * vBytesPerKilobyte
- : txData.feeRateAmount!;
final client = await _client;
@@ -1024,7 +1022,13 @@ mixin MwebInterface<T extends ElectrumXCurrencyInterface>
}
return Amount(
- rawValue: fee + feeIncrease,
+ rawValue: requiredMwebFee(
+ currentFee: fee,
+ feeIncrease: feeIncrease,
+ hasPegin: expectedPegin > BigInt.zero,
+ vSize: txData.vSize!,
+ feeRatePerKB: feeRatePerKB,
+ ),
fractionDigits: cryptoCurrency.fractionDigits,
);
}
diff --git a/test/wallets/mweb_fee_utils_test.dart b/test/wallets/mweb_fee_utils_test.dart
new file mode 100644
index 0000000..0db45f6
--- /dev/null
+++ b/test/wallets/mweb_fee_utils_test.dart
@@ -0,0 +1,164 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/mweb_fee_utils.dart';
+
+void main() {
+ group('effectiveMwebFeeRatePerKB', () {
+ test('clamps a preset rate below 1000', () {
+ expect(
+ effectiveMwebFeeRatePerKB(
+ feeRatePerKB: BigInt.from(997),
+ satsPerVByte: null,
+ ),
+ BigInt.from(1000),
+ );
+ });
+
+ test('preserves an exact preset rate above 1000', () {
+ expect(
+ effectiveMwebFeeRatePerKB(
+ feeRatePerKB: BigInt.from(1001),
+ satsPerVByte: null,
+ ),
+ BigInt.from(1001),
+ );
+ });
+
+ test('uses custom sats per vByte instead of the preset sentinel', () {
+ expect(
+ effectiveMwebFeeRatePerKB(
+ feeRatePerKB: BigInt.from(-1),
+ satsPerVByte: 2,
+ ),
+ BigInt.from(2000),
+ );
+ });
+
+ test('also clamps a custom rate below the minimum', () {
+ expect(
+ effectiveMwebFeeRatePerKB(
+ feeRatePerKB: BigInt.from(-1),
+ satsPerVByte: 0,
+ ),
+ BigInt.from(1000),
+ );
+ });
+ });
+
+ group('requiredMwebFee', () {
+ test('raises the fee when a second MWEB output is added', () {
+ final firstFee = requiredMwebFee(
+ currentFee: BigInt.from(200),
+ feeIncrease: BigInt.from(1900),
+ hasPegin: false,
+ vSize: 100,
+ feeRatePerKB: BigInt.from(1000),
+ );
+ final secondFee = requiredMwebFee(
+ currentFee: firstFee,
+ feeIncrease: BigInt.from(1800),
+ hasPegin: false,
+ vSize: 150,
+ feeRatePerKB: BigInt.from(1000),
+ );
+ final finalFee = requiredMwebFee(
+ currentFee: secondFee,
+ feeIncrease: BigInt.zero,
+ hasPegin: false,
+ vSize: 150,
+ feeRatePerKB: BigInt.from(1000),
+ );
+
+ expect(
+ [firstFee, secondFee, finalFee],
+ [BigInt.from(2100), BigInt.from(3900), BigInt.from(3900)],
+ );
+ });
+
+ // A rate of 5001 over 250 vBytes keeps the rate based fee (1251) clear of
+ // the vSize floor and rounds up, so both are pinned.
+ test('does not add the MWEB fee again when recalculating a peg-in', () {
+ final initialFee = requiredMwebFee(
+ currentFee: BigInt.from(100),
+ feeIncrease: BigInt.from(2141),
+ hasPegin: true,
+ vSize: 250,
+ feeRatePerKB: BigInt.from(5001),
+ );
+ final recalculatedFee = requiredMwebFee(
+ currentFee: initialFee,
+ feeIncrease: BigInt.from(2141),
+ hasPegin: true,
+ vSize: 250,
+ feeRatePerKB: BigInt.from(5001),
+ );
+
+ expect(initialFee, BigInt.from(3392));
+ expect(recalculatedFee, initialFee);
+ });
+ });
+
+ test(
+ 'reconciles from the original candidate and forwards the fee rate',
+ () async {
+ final requiredFees = [2100, 3900, 3900];
+ final paidFees = [200, 2100, 3900];
+ final minimumFees = <BigInt>[];
+ final calculationRates = <BigInt>[];
+ final feeRatePerKB = BigInt.from(1001);
+ final original = (pass: 0, feeRateAmount: BigInt.from(-1));
+ BigInt? processingRate;
+ var nextCandidate = 1;
+
+ final result = await processWithReconciledMwebFee(
+ initial: original,
+ feeRatePerKB: feeRatePerKB,
+ paidFee: (candidate) => BigInt.from(paidFees[candidate.pass]),
+ requiredFee: (candidate, feeRatePerKB) async {
+ calculationRates.add(feeRatePerKB);
+ return BigInt.from(requiredFees[candidate.pass]);
+ },
+ rebuild: (initial, minimumFee) async {
+ expect(initial, original);
+ minimumFees.add(minimumFee);
+ return (pass: nextCandidate++, feeRateAmount: initial.feeRateAmount);
+ },
+ process: (candidate, feeRatePerKB) async {
+ processingRate = feeRatePerKB;
+ return candidate;
+ },
+ );
+
+ expect(result.pass, 2);
+ expect(result.feeRateAmount, BigInt.from(-1));
+ expect(minimumFees, [BigInt.from(2100), BigInt.from(3900)]);
+ expect(calculationRates, [feeRatePerKB, feeRatePerKB, feeRatePerKB]);
+ expect(processingRate, feeRatePerKB);
+ },
+ );
+
+ test('processes the first candidate without rebuilding it', () async {
+ final feeRatePerKB = BigInt.from(1001);
+ final original = (pass: 0, feeRateAmount: BigInt.from(-1));
+ var requiredFeeCalls = 0;
+ var rebuilds = 0;
+
+ final result = await processWithReconciledMwebFee(
+ initial: original,
+ feeRatePerKB: feeRatePerKB,
+ paidFee: (candidate) => BigInt.from(3900),
+ requiredFee: (candidate, feeRatePerKB) async {
+ requiredFeeCalls += 1;
+ return BigInt.from(2100);
+ },
+ rebuild: (initial, minimumFee) async {
+ rebuilds += 1;
+ return (pass: 1, feeRateAmount: initial.feeRateAmount);
+ },
+ process: (candidate, feeRatePerKB) async => candidate,
+ );
+
+ expect(result, original);
+ expect(requiredFeeCalls, 1);
+ expect(rebuilds, 0);
+ });
+}
Why this scored 32/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.