fix old migration issues (#3761)
What changed, and why it matters
This commit fixes a bug in Cake Wallet where the app could misidentify how a wallet's keys are derived. Specifically, when reopening a wallet, the app sometimes recreated the wallet's metadata row without its saved derivation type, then let the mnemonic phrase override the type that the wallet file had explicitly recorded. That could cause an Electrum-stretched wallet (restored from a BIP39 phrase) to be reopened as a BIP39 wallet and persisted that way, potentially deriving different addresses than before and making existing funds appear missing. The fix gives precedence to the type recorded in the wallet file, then to what the mnemonic proves, then to a safe fallback. It also ensures an empty derivation path is treated as missing rather than as the master key itself.
Treat this as a correctness and reliability fix with possible security-adjacent impact (fund accessibility). Users who created or restored Bitcoin/Litecoin wallets between the affected migration versions (noted v5.6.0–v5.6.4) and observed missing balances or wrong addresses should verify their wallet derivation type/path. The project should consider whether any wallets were persisted with the wrong type before this patch and whether a migration/repair path is needed. No immediate remote-exploitation vector is evident.
Security signals we found
Incorrect derivation-type assignment could switch a wallet to a different address chain, causing apparent or real loss of access to funds.
Wallet metadata recreation on every launch (`_validateWalletInfoBoxData`) stripped the recorded derivation type, creating a window for the mnemonic to override the file's type.
Empty-string derivation path was previously persisted, which would derive addresses from the master key instead of a standard account path.
Fix explicitly prioritizes wallet-file recorded state over mnemonic inference, reducing trust in user-supplied/recoverable secrets for type selection.
No explicit CVE, advisory, or vendor security disclosure is present in the commit or supplied references.
Evidence from the diff
The patch centralizes unknown-derivation resolution in a new resolveUnknownDerivation() helper. Previously, BitcoinWalletBase and LitecoinWalletBase set defaults unconditionally and, when derivationType was unknown, chose electrum if the snapshot matched electrum, otherwise bip39/segwit. The new logic: (1) trust snapshotType from the wallet file if it is electrum or bip39; (2) otherwise infer from mnemonic validation (Electrum version prefix vs BIP39 checksum); (3) otherwise fall back to electrum if the snapshot path is electrum_path, else bip39. It preserves any saved derivation path (row first, then file), and only supplies a default path when none is saved. wallet_info_legacy.dart now stores a null path instead of '', because '' would derive from the master key rather than trigger the default-path logic. Tests cover precedence and defaults for Bitcoin and Litecoin.
Changed components
cw_bitcoin/lib/bitcoin_wallet.dartcw_bitcoin/lib/litecoin_wallet.dartcw_bitcoin/lib/unknown_derivation.dart (new)cw_bitcoin/lib/electrum_derivations.dartcw_core/lib/wallet_info_legacy.dartcw_bitcoin/test/unknown_derivation_test.dart (new)Inspect captured patch +246 / −17
### cw_bitcoin/lib/bitcoin_wallet.dart
@@ -16,6 +16,7 @@ import 'package:cw_bitcoin/electrum_transaction_info.dart';
import 'package:cw_bitcoin/electrum_wallet.dart';
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
import 'package:cw_bitcoin/electrum_wallet_snapshot.dart';
+import 'package:cw_bitcoin/unknown_derivation.dart';
import 'package:cw_bitcoin/locktime.dart';
import 'package:cw_bitcoin/hardware/bitcoin_hardware_wallet_service.dart';
import 'package:cw_bitcoin/lightning/lightning_wallet.dart';
@@ -281,16 +282,17 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
final derivationInfo = await walletInfo.getDerivationInfo();
// set the default if not present:
- derivationInfo.derivationPath ??= snp?.derivationPath ?? electrum_path;
derivationInfo.derivationType ??= snp?.derivationType ?? DerivationType.electrum;
if (derivationInfo.derivationType == DerivationType.unknown) {
- if (snp?.derivationPath == electrum_path || snp?.derivationType == DerivationType.electrum) {
- derivationInfo.derivationPath = electrum_path;
- derivationInfo.derivationType = DerivationType.electrum;
- } else {
- derivationInfo.derivationPath = segwit_path;
- derivationInfo.derivationType = DerivationType.bip39;
- }
+ resolveUnknownDerivation(
+ derivationInfo,
+ walletType: walletInfo.type,
+ mnemonic: keysData.mnemonic,
+ snapshotPath: snp?.derivationPath,
+ snapshotType: snp?.derivationType,
+ );
+ } else {
+ derivationInfo.derivationPath ??= snp?.derivationPath ?? electrum_path;
}
await derivationInfo.save();
### cw_bitcoin/lib/electrum_derivations.dart
@@ -119,3 +119,4 @@ Map<DerivationType, List<DerivationInfo>> electrum_derivations = {
String electrum_path = electrum_derivations[DerivationType.electrum]!.first.derivationPath!;
String segwit_path = "m/84'/0'/0'";
+String litecoin_segwit_path = "m/84'/2'/0'";
### cw_bitcoin/lib/litecoin_wallet.dart
@@ -30,6 +30,7 @@ import 'package:cw_bitcoin/electrum_derivations.dart';
import 'package:cw_bitcoin/electrum_transaction_info.dart';
import 'package:cw_bitcoin/electrum_wallet.dart';
import 'package:cw_bitcoin/electrum_wallet_snapshot.dart';
+import 'package:cw_bitcoin/unknown_derivation.dart';
import 'package:cw_bitcoin/hardware/bitcoin_hardware_wallet_service.dart';
import 'package:cw_bitcoin/litecoin_wallet_addresses.dart';
import 'package:cw_bitcoin/output_ordering.dart';
@@ -279,16 +280,17 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
final derivationInfo = await walletInfo.getDerivationInfo();
// set the default if not present:
- derivationInfo.derivationPath ??= snp?.derivationPath ?? electrum_path;
derivationInfo.derivationType ??= snp?.derivationType ?? DerivationType.electrum;
if (derivationInfo.derivationType == DerivationType.unknown) {
- if (snp?.derivationPath == electrum_path || snp?.derivationType == DerivationType.electrum) {
- derivationInfo.derivationPath = electrum_path;
- derivationInfo.derivationType = DerivationType.electrum;
- } else {
- derivationInfo.derivationPath = segwit_path;
- derivationInfo.derivationType = DerivationType.bip39;
- }
+ resolveUnknownDerivation(
+ derivationInfo,
+ walletType: walletInfo.type,
+ mnemonic: keysData.mnemonic,
+ snapshotPath: snp?.derivationPath,
+ snapshotType: snp?.derivationType,
+ );
+ } else {
+ derivationInfo.derivationPath ??= snp?.derivationPath ?? electrum_path;
}
Uint8List? seedBytes = null;
### cw_bitcoin/lib/unknown_derivation.dart
@@ -0,0 +1,61 @@
+import 'package:bip39/bip39.dart' as bip39;
+import 'package:collection/collection.dart';
+import 'package:cw_bitcoin/bitcoin_mnemonic.dart' as electrum_mnemonic;
+import 'package:cw_bitcoin/electrum_derivations.dart';
+import 'package:cw_core/wallet_info.dart';
+import 'package:cw_core/wallet_type.dart';
+
+/// Settles the derivation of a wallet whose saved type is [DerivationType.unknown], as left by the
+/// Hive → SQLite migration or by a wallet info row recreated without its derivation.
+///
+/// The seed type is the one the wallet file recorded ([snapshotType]); failing that, the one the
+/// mnemonic proves; failing that, the fallback the open code always had. A path that was saved is
+/// kept, so the wallet keeps deriving the addresses it had. Only a wallet with no saved path gets
+/// its type's default, which for Litecoin is Litecoin's own coin type.
+void resolveUnknownDerivation(
+ DerivationInfo derivationInfo, {
+ required WalletType walletType,
+ String? mnemonic,
+ String? snapshotPath,
+ DerivationType? snapshotType,
+}) {
+ final type = _recordedTypeOf(snapshotType) ??
+ _seedTypeOf(mnemonic) ??
+ (snapshotPath == electrum_path ? DerivationType.electrum : DerivationType.bip39);
+ final savedPath = [derivationInfo.derivationPath, snapshotPath]
+ .firstWhereOrNull((path) => path?.isNotEmpty ?? false);
+
+ derivationInfo.derivationType = type;
+ derivationInfo.derivationPath = savedPath ?? _defaultPath(type, walletType);
+}
+
+/// The type the wallet wrote into its own file at its last save, which is what it has been
+/// deriving with. The file loader reads a file that predates derivation tracking as electrum, and
+/// every such wallet was Electrum-stretched. Null when there is no file, or while the file still
+/// says `unknown` (v5.6.0–v5.6.4 wrote that for migrated rows).
+DerivationType? _recordedTypeOf(DerivationType? snapshotType) =>
+ snapshotType == DerivationType.electrum || snapshotType == DerivationType.bip39
+ ? snapshotType
+ : null;
+
+/// The seed type the mnemonic proves: an Electrum version prefix, or a valid BIP39 checksum. Null
+/// when it's both, neither, or there is no mnemonic (hardware and watch-only wallets).
+DerivationType? _seedTypeOf(String? mnemonic) {
+ if (mnemonic == null || mnemonic.isEmpty) {
+ return null;
+ }
+
+ final isElectrum = electrum_mnemonic.validateMnemonic(mnemonic);
+ final isBip39 = bip39.validateMnemonic(mnemonic);
+ if (isElectrum == isBip39) {
+ return null;
+ }
+ return isElectrum ? DerivationType.electrum : DerivationType.bip39;
+}
+
+String _defaultPath(DerivationType type, WalletType walletType) {
+ if (type == DerivationType.electrum) {
+ return electrum_path;
+ }
+ return walletType == WalletType.litecoin ? litecoin_segwit_path : segwit_path;
+}
### cw_bitcoin/test/unknown_derivation_test.dart
@@ -0,0 +1,161 @@
+import 'package:bip39/bip39.dart' as bip39;
+import 'package:cw_bitcoin/bitcoin_mnemonic.dart' as electrum_mnemonic;
+import 'package:cw_bitcoin/electrum_derivations.dart';
+import 'package:cw_bitcoin/unknown_derivation.dart';
+import 'package:cw_core/wallet_info.dart';
+import 'package:cw_core/wallet_type.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+/// Electrum segwit version prefix, invalid BIP39 checksum: what every pre-2024 Cake wallet has.
+const _electrumSeed = 'flag maid surge feel hundred awkward burger logic '
+ 'maple whale monkey road ribbon practice inside provide '
+ 'insane letter sorry choice zebra purse copy mention';
+
+/// Valid BIP39 checksum, no Electrum version prefix.
+const _bip39Seed = 'abandon abandon abandon abandon abandon abandon '
+ 'abandon abandon abandon abandon abandon about';
+
+/// Valid BIP39 checksum whose version hash also carries a prefix the Electrum validator accepts:
+/// the mnemonic proves nothing.
+const _ambiguousSeed = 'immune boat rice detect proof link disagree help '
+ 'crouch multiply poet glove fabric fee begin lesson '
+ 'hospital forum web beyond army blossom never brother';
+
+DerivationInfo _unknown({String? path}) =>
+ DerivationInfo(derivationType: DerivationType.unknown, derivationPath: path);
+
+void main() {
+ test('fixtures are what they claim', () {
+ expect(electrum_mnemonic.validateMnemonic(_electrumSeed), isTrue);
+ expect(bip39.validateMnemonic(_electrumSeed), isFalse);
+ expect(electrum_mnemonic.validateMnemonic(_bip39Seed), isFalse);
+ expect(bip39.validateMnemonic(_bip39Seed), isTrue);
+ expect(electrum_mnemonic.validateMnemonic(_ambiguousSeed), isTrue);
+ expect(bip39.validateMnemonic(_ambiguousSeed), isTrue);
+ });
+
+ group('type', () {
+ test("the wallet file's recorded type wins over the mnemonic", () {
+ // A BIP39 phrase restored with Electrum stretching, whose row was recreated without its
+ // derivation: the file still says what the wallet has been deriving with.
+ final electrum = _unknown();
+ resolveUnknownDerivation(electrum,
+ walletType: WalletType.bitcoin,
+ mnemonic: _bip39Seed,
+ snapshotType: DerivationType.electrum,
+ snapshotPath: electrum_path);
+ expect(electrum.derivationType, DerivationType.electrum);
+ expect(electrum.derivationPath, electrum_path);
+
+ final litecoin = _unknown();
+ resolveUnknownDerivation(litecoin,
+ walletType: WalletType.litecoin,
+ mnemonic: _bip39Seed,
+ snapshotType: DerivationType.bip39,
+ snapshotPath: "m/84'/2'/0'");
+ expect(litecoin.derivationType, DerivationType.bip39);
+ expect(litecoin.derivationPath, "m/84'/2'/0'");
+ });
+
+ test('a file that predates derivation tracking loads as electrum, which such wallets used', () {
+ for (final seed in [_electrumSeed, _ambiguousSeed]) {
+ final wallet = _unknown();
+ resolveUnknownDerivation(wallet,
+ walletType: WalletType.bitcoin,
+ mnemonic: seed,
+ // ElectrumWalletSnapshot.load defaults absent fields to these.
+ snapshotType: DerivationType.electrum,
+ snapshotPath: electrum_path);
+ expect(wallet.derivationType, DerivationType.electrum, reason: seed);
+ expect(wallet.derivationPath, electrum_path, reason: seed);
+ }
+ });
+
+ test('the mnemonic decides when the file still says unknown or there is no file', () {
+ // v5.6.0–v5.6.4 saved migrated rows as (unknown, '').
+ final dormant = _unknown(path: '');
+ resolveUnknownDerivation(dormant,
+ walletType: WalletType.bitcoin,
+ mnemonic: _electrumSeed,
+ snapshotType: DerivationType.unknown,
+ snapshotPath: '');
+ expect(dormant.derivationType, DerivationType.electrum);
+ expect(dormant.derivationPath, electrum_path);
+
+ final noFileElectrum = _unknown();
+ resolveUnknownDerivation(noFileElectrum,
+ walletType: WalletType.bitcoin, mnemonic: _electrumSeed);
+ expect(noFileElectrum.derivationType, DerivationType.electrum);
+ expect(noFileElectrum.derivationPath, electrum_path);
+
+ final noFileBip39 = _unknown();
+ resolveUnknownDerivation(noFileBip39, walletType: WalletType.bitcoin, mnemonic: _bip39Seed);
+ expect(noFileBip39.derivationType, DerivationType.bip39);
+ expect(noFileBip39.derivationPath, segwit_path);
+ });
+
+ test('without a usable mnemonic the old fallback applies', () {
+ // Hardware and watch-only wallets have no mnemonic; an ambiguous one proves nothing.
+ final electrumPath = _unknown();
+ resolveUnknownDerivation(electrumPath,
+ walletType: WalletType.bitcoin,
+ snapshotType: DerivationType.unknown,
+ snapshotPath: electrum_path);
+ expect(electrumPath.derivationType, DerivationType.electrum);
+ expect(electrumPath.derivationPath, electrum_path);
+
+ final ambiguousNoFile = _unknown();
+ resolveUnknownDerivation(ambiguousNoFile,
+ walletType: WalletType.bitcoin, mnemonic: _ambiguousSeed);
+ expect(ambiguousNoFile.derivationType, DerivationType.bip39);
+ expect(ambiguousNoFile.derivationPath, segwit_path);
+
+ final nothing = _unknown();
+ resolveUnknownDerivation(nothing, walletType: WalletType.bitcoin);
+ expect(nothing.derivationType, DerivationType.bip39);
+ expect(nothing.derivationPath, segwit_path);
+ });
+ });
+
+ group('path', () {
+ test("a saved path is kept, the row's before the wallet file's", () {
+ final fromRow = _unknown(path: "m/44'/0'/0'");
+ resolveUnknownDerivation(fromRow,
+ walletType: WalletType.bitcoin,
+ mnemonic: _bip39Seed,
+ snapshotType: DerivationType.bip39,
+ snapshotPath: segwit_path);
+ expect(fromRow.derivationPath, "m/44'/0'/0'");
+
+ final fromFile = _unknown(path: '');
+ resolveUnknownDerivation(fromFile,
+ walletType: WalletType.litecoin,
+ mnemonic: _bip39Seed,
+ snapshotType: DerivationType.bip39,
+ snapshotPath: "m/84'/2'/0'");
+ expect(fromFile.derivationPath, "m/84'/2'/0'");
+ });
+
+ test("with no path anywhere the type's default applies, Litecoin on its own coin type", () {
+ final bitcoin = _unknown(path: '');
+ resolveUnknownDerivation(bitcoin,
+ walletType: WalletType.bitcoin,
+ mnemonic: _bip39Seed,
+ snapshotType: DerivationType.unknown,
+ snapshotPath: '');
+ expect(bitcoin.derivationPath, segwit_path);
+
+ final litecoin = _unknown();
+ resolveUnknownDerivation(litecoin,
+ walletType: WalletType.litecoin,
+ mnemonic: _bip39Seed,
+ snapshotType: DerivationType.unknown,
+ snapshotPath: '');
+ expect(litecoin.derivationPath, litecoin_segwit_path);
+
+ final electrum = _unknown();
+ resolveUnknownDerivation(electrum, walletType: WalletType.litecoin, mnemonic: _electrumSeed);
+ expect(electrum.derivationPath, electrum_path);
+ });
+ });
+}
### cw_core/lib/wallet_info_legacy.dart
@@ -248,7 +248,9 @@ class WalletInfo extends HiveObject {
id: 0,
derivationType:
derivationInfo?.derivationType ?? derivationType ?? newWi.DerivationType.unknown,
- derivationPath: derivationInfo?.derivationPath ?? derivationPath ?? '',
+ // No path stays null, not '': wallet open fills a missing path with the wallet type's
+ // default, while '' would derive from the master key itself.
+ derivationPath: derivationInfo?.derivationPath ?? derivationPath,
);
final derivationInfoId = await di.save();
final walletInfo = newWi.WalletInfo(Why this scored 59/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.