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
+13 -23
View File
@@ -24,7 +24,7 @@ import 'package:cake_wallet/wallet_types.g.dart';
import 'package:cake_backup/backup.dart' as cake_backup;
class $BackupService {
$BackupService(this._secureStorage, this.walletInfoSource, this.transactionDescriptionBox,
$BackupService(this._secureStorage, this.transactionDescriptionBox,
this.keyService, this.sharedPreferences)
: cipher = Cryptography.instance.chacha20Poly1305Aead(),
correctWallets = <WalletInfo>[];
@@ -37,7 +37,6 @@ class $BackupService {
final Cipher cipher;
final SecureStorage _secureStorage;
final SharedPreferences sharedPreferences;
final Box<WalletInfo> walletInfoSource;
final Box<TransactionDescription> transactionDescriptionBox;
final KeyService keyService;
List<WalletInfo> correctWallets;
@@ -110,27 +109,14 @@ class $BackupService {
}
Future<void> verifyWallets() async {
final walletInfoSource = await reloadHiveWalletInfoBox();
correctWallets =
walletInfoSource.values.where((info) => availableWalletTypes.contains(info.type)).toList();
await performHiveMigration(); // for backups made before sqlite migration
correctWallets = (await WalletInfo.getAll()).where((info) => availableWalletTypes.contains(info.type)).toList();
if (correctWallets.isEmpty) {
throw Exception('Correct wallets not detected');
printV('Correct wallets not detected');
}
}
Future<Box<WalletInfo>> reloadHiveWalletInfoBox() async {
final appDir = await getAppDir();
await CakeHive.close();
CakeHive.init(appDir.path);
if (!CakeHive.isAdapterRegistered(WalletInfo.typeId)) {
CakeHive.registerAdapter(WalletInfoAdapter());
}
return await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);
}
Future<void> importTransactionDescriptionDump() async {
final appDir = await getAppDir();
final transactionDescriptionFile = File('${appDir.path}/~_transaction_descriptions_dump');
@@ -191,12 +177,16 @@ class $BackupService {
String currentWalletName = data[PreferencesKey.currentWalletName] as String;
int currentWalletType = data[PreferencesKey.currentWalletType] as int;
final isCorrentCurrentWallet = correctWallets
final isCorrectCurrentWallet = correctWallets
.any((info) => info.name == currentWalletName && info.type.index == currentWalletType);
if (!isCorrentCurrentWallet) {
currentWalletName = correctWallets.first.name;
currentWalletType = serializeToInt(correctWallets.first.type);
try {
if (!isCorrectCurrentWallet) {
currentWalletName = correctWallets.first.name;
currentWalletType = serializeToInt(correctWallets.first.type);
}
} catch (e) {
}
if (DeviceInfo.instance.isDesktop) {
@@ -283,7 +273,7 @@ class $BackupService {
Future<Uint8List> exportKeychainDumpV2(String password,
{String keychainSalt = secrets.backupKeychainSalt}) async {
final key = generateStoreKeyFor(key: SecretStoreKey.pinCodePassword);
final wallets = await Future.wait(walletInfoSource.values.map((walletInfo) async {
final wallets = await Future.wait((await WalletInfo.getAll()).map((walletInfo) async {
try {
return {
'name': walletInfo.name,
+3 -6
View File
@@ -11,6 +11,7 @@ import 'package:crypto/crypto.dart';
import 'package:cw_core/root_dir.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:flutter/foundation.dart';
enum BackupVersion {
@@ -143,7 +144,7 @@ class BackupMetadata {
}
class BackupServiceV3 extends $BackupService {
BackupServiceV3(super.secureStorage, super.walletInfoSource, super.transactionDescriptionBox, super.keyService, super.sharedPreferences);
BackupServiceV3(super.secureStorage, super.transactionDescriptionBox, super.keyService, super.sharedPreferences);
static BackupVersion get currentVersion => BackupVersion.v3;
@@ -317,7 +318,6 @@ class BackupServiceV3 extends $BackupService {
Future<void> verifyHardwareWallets(String password,
{String keychainSalt = secrets.backupKeychainSalt}) async {
final walletInfoSource = await reloadHiveWalletInfoBox();
final appDir = await getAppDir();
final keychainDumpFile = File('${appDir.path}/~_keychain_dump');
final decryptedKeychainDumpFileData = await decryptV2(
@@ -334,10 +334,7 @@ class BackupServiceV3 extends $BackupService {
for (final expectedHardwareWallet in expectedHardwareWallets) {
final info = expectedHardwareWallet as Map<String, dynamic>;
final actualWalletInfo = walletInfoSource.values
.where((e) =>
e.name == info['name'] && e.type.toString() == info['type'])
.firstOrNull;
final actualWalletInfo = await WalletInfo.get(info['name'] as String, WalletType.values.firstWhere((e) => e.toString() == info['type'] as String));
if (actualWalletInfo != null &&
info["hardwareWalletType"] !=
actualWalletInfo.hardwareWalletType?.index) {
+9 -11
View File
@@ -16,8 +16,7 @@ class WalletCreationService {
{required WalletType initialType,
required this.keyService,
required this.sharedPreferences,
required this.settingsStore,
required this.walletInfoSource})
required this.settingsStore})
: type = initialType {
changeWalletType(type: type);
}
@@ -26,7 +25,6 @@ class WalletCreationService {
final SharedPreferences sharedPreferences;
final SettingsStore settingsStore;
final KeyService keyService;
final Box<WalletInfo> walletInfoSource;
WalletService? _service;
static const _isNewMoneroWalletPasswordUpdated = true;
@@ -36,23 +34,23 @@ class WalletCreationService {
_service = getIt.get<WalletService>(param1: type);
}
bool exists(String name) {
Future<bool> exists(String name) async {
final walletName = name.toLowerCase();
return walletInfoSource.values.any((walletInfo) => walletInfo.name.toLowerCase() == walletName);
return (await WalletInfo.getAll()).any((walletInfo) => walletInfo.name.toLowerCase() == walletName);
}
bool typeExists(WalletType type) {
return walletInfoSource.values.any((walletInfo) => walletInfo.type == type);
Future<bool> typeExists(WalletType type) async {
return (await WalletInfo.getAll()).any((walletInfo) => walletInfo.type == type);
}
void checkIfExists(String name) {
if (exists(name)) {
Future<void> checkIfExists(String name) async {
if (await exists(name)) {
throw Exception('Wallet with name ${name} already exists!');
}
}
Future<WalletBase> create(WalletCredentials credentials, {bool? isTestnet}) async {
checkIfExists(credentials.name);
await checkIfExists(credentials.name);
if (credentials.password == null) {
credentials.password = generateWalletPassword();
@@ -84,12 +82,12 @@ class WalletCreationService {
case WalletType.solana:
case WalletType.tron:
case WalletType.dogecoin:
case WalletType.nano:
return true;
case WalletType.monero:
case WalletType.wownero:
case WalletType.none:
case WalletType.haven:
case WalletType.nano:
case WalletType.banano:
case WalletType.zano:
case WalletType.decred:
+4 -5
View File
@@ -74,7 +74,7 @@ class WalletLoadingService {
} catch (error, stack) {
await ExceptionHandler.resetLastPopupDate();
final isLedgerError = await ExceptionHandler.isLedgerError(error);
if (isLedgerError || requireHardwareWalletConnection(type, name)) rethrow;
if (isLedgerError || await requireHardwareWalletConnection(type, name)) rethrow;
await ExceptionHandler.onError(FlutterErrorDetails(exception: error, stack: stack));
@@ -87,9 +87,8 @@ class WalletLoadingService {
}
// try opening another wallet that is not corrupted to give user access to the app
final walletInfoSource = await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);
WalletBase? wallet;
for (var walletInfo in walletInfoSource.values) {
for (var walletInfo in await WalletInfo.getAll()) {
try {
final walletService = walletServiceFactory.call(walletInfo.type);
final walletPassword = await keyService.getWalletPassword(walletName: walletInfo.name);
@@ -195,8 +194,8 @@ class WalletLoadingService {
return "\n\n$type ($name): ${await walletService.getSeeds(name, password, type)}";
}
bool requireHardwareWalletConnection(WalletType type, String name) {
Future<bool> requireHardwareWalletConnection(WalletType type, String name) async {
final walletService = walletServiceFactory.call(type);
return walletService.requireHardwareWalletConnection(name);
return await walletService.requireHardwareWalletConnection(name);
}
}