Merge pull request #3616 from cake-tech/fix/cw-1633-cakepay-lightning-balance
What changed, and why it matters
This commit adds a balance check before confirming a Lightning (CakePay) payment. Previously, the app may have allowed a user to confirm a payment even if their Lightning wallet did not hold enough funds to cover the amount plus fees. The fix fetches the current balance and throws an error if it is insufficient, preventing the transaction from proceeding.
No immediate action required; this is a defensive fix. Users should update to a version containing this commit. Review whether similar balance checks exist for other payment flows (on-chain, other integrations).
Security signals we found
Insufficient-funds check added before transaction confirmation
Uses ensureSynced: true to reduce race-condition risk with balance state
Throws existing BitcoinTransactionWrongBalanceException on failure
Evidence from the diff
In cw_bitcoin/lib/lightning/lightning_wallet.dart, a new guard was inserted before payment confirmation. It calculates requiredAmount as the payment amount plus the Lightning fee (unless fees are already included), retrieves the wallet balance via sdk.getInfo(ensureSynced: true), and throws BitcoinTransactionWrongBalanceException if balanceSats is less than requiredAmount. This prevents initiating a payment that would fail or overdraw the Lightning balance.
Changed components
cw_bitcoin/lib/lightning/lightning_wallet.dartCakePay Lightning payment flowInspect captured patch +22 / −0
### cw_bitcoin/lib/lightning/lightning_wallet.dart
@@ -1,3 +1,18 @@
+import 'dart:async';
+import 'dart:io';
+import 'dart:typed_data';
+
+import 'package:breez_sdk_spark_flutter/breez_sdk_spark.dart';
+import 'package:cw_bitcoin/bitcoin_transaction_priority.dart';
+import 'package:cw_bitcoin/electrum_transaction_info.dart';
+import "package:cw_bitcoin/exceptions.dart";
+import 'package:cw_bitcoin/lightning/pending_lightning_transaction.dart';
+import 'package:cw_core/amount/money.dart';
+import 'package:cw_core/crypto_currency.dart';
+import 'package:cw_core/currency.dart';
+import 'package:cw_core/transaction_direction.dart';
+import 'package:cw_core/utils/print_verbose.dart';
+import 'package:cw_core/wallet_type.dart';
import "dart:async";
import "dart:io";
import "dart:typed_data";
@@ -233,6 +248,13 @@ class LightningWallet {
final lightningFeeSats = paymentMethod.lightningFeeSats;
final sparkTransferFeeSats = paymentMethod.sparkTransferFeeSats;
+ final requiredAmount =
+ prepareResponse.amount + (feesIncluded ? BigInt.zero : lightningFeeSats);
+ final balance = await sdk.getInfo(request: const GetInfoRequest(ensureSynced: true));
+ if (balance.balanceSats < requiredAmount) {
+ throw BitcoinTransactionWrongBalanceException();
+ }
+
final baseAmount = request.amount ?? amountSats;
final amount = baseAmount != null
? Money(baseAmount, currency)Why this scored 56/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.