feat: initial hive -> SQLite (#2502)

* feat: initial hive -> SQLite

Includes migration script and initial work done to
remove dependency on hive generators.

Also contains some refactoring to make sync code
async.

Features new dev screen that lists all SQLite tables

Aspects that need testing:
- backups (from this version, and from previous versions as well)
- wallet loading / wallet list
- almost everything else
- renaming
- deletion
- sorting in wallet list
- wallet groups
- wallet seed/keys
- wallet switching
- wallet creation / restore - all methods
- wallet address page
- hardware wallet connection (and prompts from mall screens)
- contacts
- advanced settings in wallet creation
- derivation paths
- some minor wownero fixes
- base, hardware wallets

* fix: build errors

* fix: Bad state: No element in backup restore

* address comments from review

* fix: HardwareWalletType
This commit is contained in:
cyan
2025-10-31 22:37:15 +01:00
committed by GitHub
parent 718525926e
commit 76876181e5
121 changed files with 2623 additions and 917 deletions
+32 -29
View File
@@ -5,6 +5,7 @@ import 'package:cake_wallet/core/secure_storage.dart';
import 'package:cake_wallet/entities/exchange_api_mode.dart';
import 'package:cake_wallet/entities/fiat_api_mode.dart';
import 'package:cake_wallet/entities/haven_seed_store.dart';
import 'package:cw_core/cake_hive.dart';
import 'package:cw_core/pathForWallet.dart';
import 'package:cake_wallet/entities/secret_store_key.dart';
import 'package:cw_core/root_dir.dart';
@@ -21,6 +22,7 @@ import 'package:cake_wallet/monero/monero.dart';
import 'package:cake_wallet/entities/contact.dart';
import 'package:cake_wallet/entities/fs_migration.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_info_legacy.dart' as wiLegacy;
import 'package:cake_wallet/exchange/trade.dart';
import 'package:encrypt/encrypt.dart' as encrypt;
import 'package:collection/collection.dart';
@@ -55,12 +57,11 @@ Future<void> defaultSettingsMigration(
required SecureStorage secureStorage,
required Box<Node> nodes,
required Box<Node> powNodes,
required Box<WalletInfo> walletInfoSource,
required Box<Trade> tradeSource,
required Box<Contact> contactSource,
required Box<HavenSeedStore> havenSeedStore}) async {
if (Platform.isIOS) {
await ios_migrate_v1(walletInfoSource, tradeSource, contactSource);
await ios_migrate_v1(tradeSource, contactSource);
}
// check current nodes for nullability regardless of the version
@@ -69,7 +70,7 @@ Future<void> defaultSettingsMigration(
final isNewInstall =
sharedPreferences.getInt(PreferencesKey.currentDefaultSettingsMigrationVersion) == null;
await _validateWalletInfoBoxData(walletInfoSource);
await _validateWalletInfoBoxData();
await sharedPreferences.setBool(PreferencesKey.isNewInstall, isNewInstall);
@@ -162,7 +163,7 @@ Future<void> defaultSettingsMigration(
break;
case 5:
await addAddressesForMoneroWallets(walletInfoSource);
await addAddressesForMoneroWallets();
break;
case 6:
@@ -323,7 +324,7 @@ Future<void> defaultSettingsMigration(
await updateNanoNodeList(nodes: nodes);
break;
case 32:
await updateBtcNanoWalletInfos(walletInfoSource);
await updateBtcNanoWalletInfos();
break;
case 33:
await addWalletNodeList(nodes: nodes, type: WalletType.tron);
@@ -361,7 +362,7 @@ Future<void> defaultSettingsMigration(
// await replaceTronDefaultNode(sharedPreferences: sharedPreferences, nodes: nodes);
break;
case 38:
await fixBtcDerivationPaths(walletInfoSource);
await fixBtcDerivationPaths();
break;
case 39:
_fixNodesUseSSLFlag(nodes);
@@ -738,7 +739,7 @@ Future<void> _updateMoneroPriority(SharedPreferences sharedPreferences) async {
}
}
Future<void> _validateWalletInfoBoxData(Box<WalletInfo> walletInfoSource) async {
Future<void> _validateWalletInfoBoxData() async {
try {
final root = await getAppDir();
@@ -780,7 +781,7 @@ Future<void> _validateWalletInfoBoxData(Box<WalletInfo> walletInfoSource) async
}
final id = prefix + '_' + name;
final exist = walletInfoSource.values.any((el) => el.id == id);
final exist = (await WalletInfo.getAll()).any((el) => el.id == id);
if (exist) {
continue;
@@ -799,7 +800,7 @@ Future<void> _validateWalletInfoBoxData(Box<WalletInfo> walletInfoSource) async
showIntroCakePayCard: false,
);
walletInfoSource.add(walletInfo);
await walletInfo.save();
}
}
} catch (_) {}
@@ -967,8 +968,8 @@ Future<void> updateNodeTypes({required Box<Node> nodes}) async {
});
}
Future<void> addAddressesForMoneroWallets(Box<WalletInfo> walletInfoSource) async {
final moneroWalletsInfo = walletInfoSource.values.where((info) => info.type == WalletType.monero);
Future<void> addAddressesForMoneroWallets() async {
final moneroWalletsInfo = (await WalletInfo.getAll()).where((info) => info.type == WalletType.monero);
moneroWalletsInfo.forEach((info) async {
try {
final walletPath = await pathForWallet(name: info.name, type: WalletType.monero);
@@ -1016,32 +1017,34 @@ Future<void> changeTransactionPriorityAndFeeRateKeys(SharedPreferences sharedPre
bitcoin!.getMediumTransactionPriority().serialize());
}
Future<void> fixBtcDerivationPaths(Box<WalletInfo> walletsInfoSource) async {
for (WalletInfo walletInfo in walletsInfoSource.values) {
Future<void> fixBtcDerivationPaths() async {
for (WalletInfo walletInfo in await WalletInfo.getAll()) {
if (walletInfo.type == WalletType.bitcoin ||
walletInfo.type == WalletType.bitcoinCash ||
walletInfo.type == WalletType.litecoin) {
if (walletInfo.derivationInfo?.derivationPath == "m/0'/0") {
walletInfo.derivationInfo!.derivationPath = "m/0'";
final derivationInfo = await walletInfo.getDerivationInfo();
if (derivationInfo?.derivationPath == "m/0'/0") {
derivationInfo!.derivationPath = "m/0'";
await walletInfo.save();
}
}
}
}
Future<void> updateBtcNanoWalletInfos(Box<WalletInfo> walletsInfoSource) async {
for (WalletInfo walletInfo in walletsInfoSource.values) {
if (walletInfo.type == WalletType.nano || walletInfo.type == WalletType.bitcoin) {
walletInfo.derivationInfo = DerivationInfo(
derivationPath: walletInfo.derivationPath,
derivationType: walletInfo.derivationType,
address: walletInfo.address,
transactionsCount: walletInfo.restoreHeight,
);
await walletInfo.save();
}
}
}
Future<void> updateBtcNanoWalletInfos() async {}
// Future<void> updateBtcNanoWalletInfos() async {
// for (WalletInfo walletInfo in await WalletInfo.getAll()) {
// if (walletInfo.type == WalletType.nano || walletInfo.type == WalletType.bitcoin) {
// final derivationInfo = await walletInfo.getDerivationInfo();
// derivationInfo = DerivationInfo(
// derivationPath: derivationInfo?.derivationPath,
// derivationType: derivationInfo?.derivationType,
// address: walletInfo.address,
// transactionsCount: walletInfo.restoreHeight,
// );
// await walletInfo.save();
// }
// }
// }
Future<void> checkCurrentNodes(
Box<Node> nodeSource, Box<Node> powNodeSource, SharedPreferences sharedPreferences) async {
+8 -6
View File
@@ -29,8 +29,7 @@ Future<void> migrate_android_v1() async {
await android_migrate_wallets(appDocDir: appDocDir);
}
Future<void> ios_migrate_v1(
Box<WalletInfo> walletInfoSource, Box<Trade> tradeSource, Box<Contact> contactSource) async {
Future<void> ios_migrate_v1(Box<Trade> tradeSource, Box<Contact> contactSource) async {
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('ios_migration_v1_completed') ?? false) {
@@ -40,7 +39,7 @@ Future<void> ios_migrate_v1(
await ios_migrate_user_defaults();
await ios_migrate_pin();
await ios_migrate_wallet_passwords();
await ios_migrate_wallet_info(walletInfoSource);
await ios_migrate_wallet_info();
await ios_migrate_trades_list(tradeSource);
await ios_migrate_address_book(contactSource);
@@ -278,7 +277,7 @@ Future<void> android_migrate_wallets({required Directory appDocDir}) async {
});
}
Future<void> ios_migrate_wallet_info(Box<WalletInfo> walletsInfoSource) async {
Future<void> ios_migrate_wallet_info() async {
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('ios_migration_wallet_info_completed') ?? false) {
@@ -289,6 +288,7 @@ Future<void> ios_migrate_wallet_info(Box<WalletInfo> walletsInfoSource) async {
final appDocDir = await getApplicationDocumentsDirectory();
final walletsDir = Directory('${appDocDir.path}/wallets');
final moneroWalletsDir = Directory('${walletsDir.path}/monero');
final walletsInfo = await WalletInfo.getAll();
final infoRecords = moneroWalletsDir
.listSync()
.map((item) {
@@ -307,7 +307,7 @@ Future<void> ios_migrate_wallet_info(Box<WalletInfo> walletsInfoSource) async {
final timestamp = dateAsDouble.toInt() * 1000;
final date = DateTime.fromMillisecondsSinceEpoch(timestamp);
final id = walletTypeToString(WalletType.monero).toLowerCase() + '_' + name;
final exist = walletsInfoSource.values.firstWhereOrNull((el) => el.id == id) != null;
final exist = walletsInfo.firstWhereOrNull((el) => el.id == id) != null;
if (exist) {
return null;
@@ -334,7 +334,9 @@ Future<void> ios_migrate_wallet_info(Box<WalletInfo> walletsInfoSource) async {
.where((el) => el != null)
.whereType<WalletInfo>()
.toList();
await walletsInfoSource.addAll(infoRecords);
for (final info in infoRecords) {
await info.save();
}
await prefs.setBool('ios_migration_wallet_info_completed', true);
} catch (e) {
printV(e.toString());
@@ -4,7 +4,7 @@ import 'package:cake_wallet/entities/preferences_key.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:shared_preferences/shared_preferences.dart';
bool requireHardwareWalletConnection() {
Future<bool> requireHardwareWalletConnection() async {
final name = getIt
.get<SharedPreferences>()
.getString(PreferencesKey.currentWalletName);
@@ -21,5 +21,5 @@ bool requireHardwareWalletConnection() {
final type = deserializeFromInt(typeRaw);
final walletLoadingService = getIt.get<WalletLoadingService>();
return walletLoadingService.requireHardwareWalletConnection(type, name);
return await walletLoadingService.requireHardwareWalletConnection(type, name);
}
+6 -7
View File
@@ -8,17 +8,16 @@ import 'package:hive/hive.dart';
import 'package:shared_preferences/shared_preferences.dart';
class WalletManager {
WalletManager(this._walletInfoSource, this._sharedPreferences);
WalletManager(this._sharedPreferences);
final Box<WalletInfo> _walletInfoSource;
final SharedPreferences _sharedPreferences;
final List<WalletGroup> walletGroups = [];
void updateWalletGroups() {
Future<void> updateWalletGroups() async {
walletGroups.clear();
for (final walletInfo in _walletInfoSource.values) {
for (final walletInfo in await WalletInfo.getAll()) {
final groupKey = _resolveGroupKey(walletInfo);
final group = _getOrCreateGroup(groupKey);
group.wallets.add(walletInfo);
@@ -115,7 +114,7 @@ class WalletManager {
// If the openedWallet already has an hash, then there is nothing to do
if (walletInfo.hashedWalletIdentifier != null &&
walletInfo.hashedWalletIdentifier!.isNotEmpty) {
updateWalletGroups(); // Still skeptical of calling this here. Looking for a better spot.
await updateWalletGroups(); // Still skeptical of calling this here. Looking for a better spot.
return;
}
@@ -123,7 +122,7 @@ class WalletManager {
final oldGroupKey = _resolveGroupKey(walletInfo); // parentAddress fallback
// Find all wallets that share this old group key (i.e the old group)
final oldGroupWallets = _walletInfoSource.values.where((w) {
final oldGroupWallets = (await WalletInfo.getAll()).where((w) {
final key = w.hashedWalletIdentifier != null && w.hashedWalletIdentifier!.isNotEmpty
? w.hashedWalletIdentifier
: (w.parentAddress ?? w.address);
@@ -150,7 +149,7 @@ class WalletManager {
}
// Finally, we rebuild the groups so that these wallets are now in the new group
updateWalletGroups();
await updateWalletGroups();
}
/// Copy an old group name to the new group key, then remove the old key.