CW-1681-4-Bitcoin-Accounts-code-enhancements-lightning-deposit (#3825)
What changed, and why it matters
This commit expands Cake Wallet's Lightning Bitcoin feature so it works across multiple Bitcoin accounts instead of being locked to the primary account only. It also fixes address selection so the app picks an unused Segwit address that belongs to the currently selected account, rather than potentially returning an address from a different account. These are functional correctness and usability improvements; there is no direct evidence of a security vulnerability being patched.
Treat as a feature/usability fix with secondary security benefit. Reviewers should verify that accountIndex is consistently propagated for all address types and that Lightning invoices/deposits are correctly associated with the active account in transaction history and backup/restore flows. No urgent security patch action is indicated by the diff alone.
Security signals we found
Account isolation logic changed: Lightning and Segwit address selection now account-aware
Previous code could return a Segwit address from a different account than the one currently selected
Removal of hard-coded account index checks suggests prior account-boundary assumptions were too restrictive
Evidence from the diff
The diff removes the ‘primary account only’ restriction for Lightning in Bitcoin wallets. In bitcoin_wallet.dart, the Lightning receive option is now allowed whenever Lightning is supported and initialized, regardless of currentAccountIndex. In electrum_wallet.dart, switching to an extra account automatically falls back from silent payments to Segwit p2wpkh, and Lightning transactions are no longer filtered to account 0. In cw_bitcoin.dart, getUnusedSegwitAddress now filters by the current account index in addition to type and unused status. A test file imports a new Lightning address type. Dashboard view model no longer forces selection of the primary account when Lightning mode is toggled.
Changed components
cw_bitcoin/lib/bitcoin_wallet.dartcw_bitcoin/lib/electrum_wallet.dartcw_bitcoin/test/address_index_test.dartlib/bitcoin/cw_bitcoin.dartlib/view_model/dashboard/dashboard_view_model.dartInspect captured patch +14 / −14
### cw_bitcoin/lib/bitcoin_wallet.dart
@@ -707,8 +707,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
final isExtraAccount = currentAccountIndex > 0;
if (option == BitcoinReceivePageOption.lightning) {
- // Lightning is tied to the primary account only
- return !isExtraAccount && hasLightningSupport && isLightningInitialized;
+ return hasLightningSupport && isLightningInitialized;
}
if (option == BitcoinReceivePageOption.silent_payments) {
### cw_bitcoin/lib/electrum_wallet.dart
@@ -476,7 +476,11 @@ abstract class ElectrumWalletBase
walletAddresses.currentAccountIndex = accountIndex;
final currentPageType = walletAddresses.addressPageType;
- await walletAddresses.setAddressType(currentPageType);
+ await walletAddresses.setAddressType(
+ accountIndex != 0 && currentPageType == SilentPaymentsAddresType.p2sp
+ ? SegwitAddresType.p2wpkh
+ : currentPageType,
+ );
// For extra accounts, we only prepare SegwitAddresType.p2wpkh type.
if (isNewAccount) {
@@ -4158,7 +4162,7 @@ abstract class ElectrumWalletBase
return all.where((tx) {
- if (tx.additionalInfo["isLightning"] == true) return accountIndex == 0;
+ if (tx.additionalInfo["isLightning"] == true) return true;
if (tx.isReceivedSilentPayment) return accountIndex == 0;
// Locally created transactions store the account index explicitly,
### cw_bitcoin/test/address_index_test.dart
@@ -4,6 +4,7 @@ import "package:cw_bitcoin/bitcoin_address_record.dart";
import "package:cw_bitcoin/bitcoin_wallet_addresses.dart";
import "package:cw_bitcoin/electrum_account.dart";
import "package:cw_bitcoin/electrum_wallet_addresses.dart";
+import "package:cw_bitcoin/lightning/lightning_addres_type.dart";
import "package:cw_bitcoin/payjoin/manager.dart";
import "package:cw_bitcoin/utils.dart";
import "package:cw_core/db/sqlite.dart";
### lib/bitcoin/cw_bitcoin.dart
@@ -759,8 +759,11 @@ class CWBitcoin extends Bitcoin {
String? getUnusedSegwitAddress(Object wallet) {
try {
final electrumWallet = wallet as ElectrumWallet;
- final segwitAddress = electrumWallet.walletAddresses.allAddresses
- .firstWhere((element) => !element.isUsed && element.type == SegwitAddresType.p2wpkh);
+ final walletAddresses = electrumWallet.walletAddresses;
+ final segwitAddress = walletAddresses.allAddresses.firstWhere(
+ (element) => !element.isUsed && element.type == SegwitAddresType.p2wpkh
+ && element.accountIndex == walletAddresses.currentAccountIndex,
+ );
return segwitAddress.address;
} catch (_) {
return null;
### lib/view_model/dashboard/dashboard_view_model.dart
@@ -1386,14 +1386,7 @@ abstract class DashboardViewModelBase with Store {
void toggleLightningMode() {
lightningMode = !lightningMode;
- // Lightning belongs to the primary account only
- if (lightningMode) {
- unawaited(_selectPrimaryAccount().then((_) async {
- if (!lightningMode && isMultiAccountsEnabled) {
- await loadCardDesigns();
- }
- }));
- } else {
+ if (!lightningMode) {
unawaited(loadCardDesigns());
}
}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.