From fb61bbd49f62ea63189ba5076c87a52efb2080ee Mon Sep 17 00:00:00 2001 From: sneurlax Date: Mon, 7 Sep 2026 20:21:23 -0500 Subject: [PATCH 1/3] Use MWC's own genesis time when estimating a creation height The MWC wallet copied Epic Cash's first-block timestamp (1565370278) under the name mimblewimblecoinFirstBlock. MWC mainnet started on 2019-11-11 (1573462801, the value restore_wallet_view already uses), so the estimate for a freshly created wallet landed about 130k blocks above the actual tip: the initial scan and any later rescan from that height never ran. Wallets restored from a seed were unaffected. Staging already fixed this in 0e56467c6 with 1573462800. Rebased onto it, this only aligns the value with restore_wallet_view. --- lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart b/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart index 721773da6d..cd4095c6fe 100644 --- a/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart +++ b/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart @@ -838,7 +838,7 @@ class MimblewimblecoinWallet extends Bip39Wallet { int _calculateRestoreHeightFrom({required DateTime date}) { final int secondsSinceEpoch = date.millisecondsSinceEpoch ~/ 1000; - const int mimblewimblecoinFirstBlock = 1573462800; + const int mimblewimblecoinFirstBlock = 1573462801; const double overestimateSecondsPerBlock = 61; final int chosenSeconds = secondsSinceEpoch - mimblewimblecoinFirstBlock; final int approximateHeight = chosenSeconds ~/ overestimateSecondsPerBlock; From 0681f18800499b1c3dd243f5776aabbd0f0ec673 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Wed, 11 Nov 2026 15:35:19 -0600 Subject: [PATCH 2/3] Skip MWC fee estimation when there is nothing to spend Opening Send on an empty MWC wallet asks for a fee estimate. libmwc treats amount == available as send all, retries with the amount minus the required fee and aborts the app on the resulting negative amount (seen on the iOS Simulator as a SIGABRT in mwc_rust_get_tx_fees). There is no fee to estimate without a spendable balance or an amount, so return 0 without calling the native library. The real send still reports insufficient funds. --- lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart b/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart index cd4095c6fe..5c0715f161 100644 --- a/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart +++ b/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart @@ -614,9 +614,13 @@ class MimblewimblecoinWallet extends Bip39Wallet { int satoshiAmount, { bool ifErrorEstimateFee = false, }) async { + final int available = info.cachedBalance.spendable.raw.toInt(); + // With nothing to spend, libmwc treats amount == available as send all, + // retries with amount minus the required fee and aborts the app on the + // negative amount. There is no fee to estimate, so skip the call. + if (satoshiAmount <= 0 || available <= 0) return 0; final wallet = await _ensureWalletOpen(); try { - final available = info.cachedBalance.spendable.raw.toInt(); final transactionFees = await libMwc.getTransactionFees( wallet: wallet, amount: satoshiAmount, From adcb53e9a20ea6ba5286dab8d7c2a1fbf5fb32f9 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Fri, 13 Nov 2026 17:08:11 -0600 Subject: [PATCH 3/3] test: cover MWC fee and restore calculations --- .../wallet/impl/mimblewimblecoin_wallet.dart | 16 +++------ .../mimblewimblecoin_wallet_math.dart | 11 +++++++ .../mimblewimblecoin_wallet_math_test.dart | 33 +++++++++++++++++++ 3 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 lib/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart create mode 100644 test/wallets/mimblewimblecoin_wallet_math_test.dart diff --git a/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart b/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart index 5c0715f161..5e18f1e315 100644 --- a/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart +++ b/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart @@ -35,6 +35,7 @@ import '../../crypto_currency/crypto_currency.dart'; import '../../models/tx_data.dart'; import '../intermediate/bip39_wallet.dart'; import '../supporting/mimblewimblecoin_wallet_info_extension.dart'; +import '../supporting/mimblewimblecoin_wallet_math.dart'; import '../supporting/restore_progress.dart'; class MimblewimblecoinWallet extends Bip39Wallet { @@ -618,7 +619,9 @@ class MimblewimblecoinWallet extends Bip39Wallet { // With nothing to spend, libmwc treats amount == available as send all, // retries with amount minus the required fee and aborts the app on the // negative amount. There is no fee to estimate, so skip the call. - if (satoshiAmount <= 0 || available <= 0) return 0; + if (shouldSkipMwcFeeEstimate(amount: satoshiAmount, available: available)) { + return 0; + } final wallet = await _ensureWalletOpen(); try { final transactionFees = await libMwc.getTransactionFees( @@ -841,16 +844,7 @@ class MimblewimblecoinWallet extends Bip39Wallet { } int _calculateRestoreHeightFrom({required DateTime date}) { - final int secondsSinceEpoch = date.millisecondsSinceEpoch ~/ 1000; - const int mimblewimblecoinFirstBlock = 1573462801; - const double overestimateSecondsPerBlock = 61; - final int chosenSeconds = secondsSinceEpoch - mimblewimblecoinFirstBlock; - final int approximateHeight = chosenSeconds ~/ overestimateSecondsPerBlock; - int height = approximateHeight; - if (height < 0) { - height = 0; - } - return height; + return estimateMwcRestoreHeight(date); } // ============== Overrides ================================================== diff --git a/lib/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart b/lib/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart new file mode 100644 index 0000000000..fa9b36fb83 --- /dev/null +++ b/lib/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart @@ -0,0 +1,11 @@ +bool shouldSkipMwcFeeEstimate({required int amount, required int available}) => + amount <= 0 || available <= 0; + +int estimateMwcRestoreHeight(DateTime date) { + final int secondsSinceEpoch = date.millisecondsSinceEpoch ~/ 1000; + const int mimblewimblecoinFirstBlock = 1573462801; + const double overestimateSecondsPerBlock = 61; + final int chosenSeconds = secondsSinceEpoch - mimblewimblecoinFirstBlock; + final int approximateHeight = chosenSeconds ~/ overestimateSecondsPerBlock; + return approximateHeight < 0 ? 0 : approximateHeight; +} diff --git a/test/wallets/mimblewimblecoin_wallet_math_test.dart b/test/wallets/mimblewimblecoin_wallet_math_test.dart new file mode 100644 index 0000000000..c24bfb9371 --- /dev/null +++ b/test/wallets/mimblewimblecoin_wallet_math_test.dart @@ -0,0 +1,33 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:stackwallet/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart'; + +void main() { + test("MWC fee estimation is skipped only for unusable inputs", () { + expect(shouldSkipMwcFeeEstimate(amount: 0, available: 1), isTrue); + expect(shouldSkipMwcFeeEstimate(amount: -1, available: 1), isTrue); + expect(shouldSkipMwcFeeEstimate(amount: 1, available: 0), isTrue); + expect(shouldSkipMwcFeeEstimate(amount: 1, available: -1), isTrue); + expect(shouldSkipMwcFeeEstimate(amount: 1, available: 1), isFalse); + }); + + test("MWC restore height uses its exact first-block timestamp", () { + final genesis = DateTime.fromMillisecondsSinceEpoch( + 1573462801 * 1000, + isUtc: true, + ); + + expect(estimateMwcRestoreHeight(genesis), 0); + expect( + estimateMwcRestoreHeight(genesis.add(const Duration(seconds: 60))), + 0, + ); + expect( + estimateMwcRestoreHeight(genesis.add(const Duration(seconds: 61))), + 1, + ); + expect( + estimateMwcRestoreHeight(genesis.subtract(const Duration(days: 1))), + 0, + ); + }); +}