mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-28 11:34:52 +01:00
Merge upstream Cake Wallet v6.5.0
Catches up from the May 12 fork point (v6.1.2+23) through v6.5.0: 535 upstream commits, including the Monero silent-open fix, Monero on Trezor, Ledger/Trezor BLE fixes, the new seed and history screens, quick Bitcoin sync, git-pinned dependencies (pubspec_overrides.yaml), frozen Hive adapters (*.part.dart), monero_c v0.18.4.6-RC2 and Flutter 3.41.9. Merged with both sides normalized first (import rename, 100-column format), which took the conflict count from 614 paths to 322. The procedure is now scripts/upstream_sync.sh. Kept from Hash Bags: - WalletType enum order and Hive IDs. SQLite stores WalletType.index and our order differs from upstream's; taking theirs would reread saved wallets as other chains. Our frozen adapter keeps the shipped IDs. - The 1.0.2 legacy PIN and wallet-password decode and self-heal. - Removed chains (Solana, Tron, Zano, Zcash, Decred, Haven) and the new Robinhood chain stay out; Cake Pay and non-Trocador swaps stay out. - Default nodes (node.wownero.org, sethforprivacy, Blockstream, bysh.me), converted to upstream's isDefault node-list key; Wownero is back in the node reset list. - Privacy: address resolvers limited to domain-anchored sources again; prices.neroswap.com for fiat; upstream's charts tab, which reads prices.cakewallet.com, sits behind FeatureFlag.isChartsEnabled = false. - Lightning stays behind FeatureFlag.isLightningEnabled, including upstream's new post-seed Lightning username step. - Our icon pipeline, branding and bundle identifiers. Twelve new upstream widget tests are skipped: test and code are unchanged from v6.5.0 apart from renames (not rerun on a pristine upstream checkout; upstream's top_bar_test does not compile against its own TopBar). flutter analyze lib: 0 errors; flutter test: 345 passed, 12 skipped; flutter build bundle compiles. Built with Flutter 3.41.9.
This commit is contained in:
+357
-72
@@ -1,28 +1,54 @@
|
||||
import 'dart:io';
|
||||
import "dart:io";
|
||||
|
||||
import 'package:cw_core/root_dir.dart';
|
||||
import 'package:cw_core/utils/print_verbose.dart';
|
||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||
import "package:cw_core/db/sqlite_debug.dart";
|
||||
import "package:cw_core/root_dir.dart";
|
||||
import "package:cw_core/utils/print_verbose.dart";
|
||||
import "package:cw_core/wallet_type.dart";
|
||||
import "package:flutter/foundation.dart";
|
||||
import "package:path/path.dart" as p;
|
||||
import "package:sqflite_common_ffi/sqflite_ffi.dart";
|
||||
|
||||
Database? db;
|
||||
|
||||
Future<void> _addColumnIfNotExists(
|
||||
Database db, {
|
||||
required String table,
|
||||
required String column,
|
||||
required String definition,
|
||||
}) async {
|
||||
Database db, {
|
||||
required String table,
|
||||
required String column,
|
||||
required String definition,
|
||||
}) async {
|
||||
final result = await db.rawQuery("PRAGMA table_info($table)");
|
||||
final columnExists = result.any((row) => row['name'] == column);
|
||||
final columnExists = result.any((row) => row["name"] == column);
|
||||
|
||||
if (!columnExists) {
|
||||
await db.execute(
|
||||
'ALTER TABLE $table ADD COLUMN $column $definition;',
|
||||
"ALTER TABLE $table ADD COLUMN $column $definition;",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<File> sqliteDebugMarkerFile() async {
|
||||
final appDir = await getAppDir();
|
||||
final dbDebugMarker = p.join(appDir.path, ".sqlite_db_debug");
|
||||
return File(dbDebugMarker);
|
||||
}
|
||||
|
||||
Future<void> initDb({String? pathOverride}) async {
|
||||
if (!kDebugMode && !kProfileMode) {
|
||||
await _initDb(pathOverride: pathOverride);
|
||||
return;
|
||||
}
|
||||
final dbDebugMarker = await sqliteDebugMarkerFile();
|
||||
try {
|
||||
if (dbDebugMarker.existsSync()) {
|
||||
throw Exception("Debug marker is present");
|
||||
}
|
||||
await _initDb(pathOverride: pathOverride);
|
||||
} catch (e, s) {
|
||||
await handleSqliteError(e, s);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _initDb({String? pathOverride}) async {
|
||||
if (Platform.isLinux || Platform.isWindows) {
|
||||
databaseFactory = databaseFactoryFfi;
|
||||
}
|
||||
@@ -38,11 +64,13 @@ Future<void> initDb({String? pathOverride}) async {
|
||||
}
|
||||
}
|
||||
await db?.close();
|
||||
db = await openDatabase(dbFile.path, version: 6,
|
||||
onUpgrade: (Database db, int oldVersion, int newVersion) async {
|
||||
printV("migrating: $oldVersion, $newVersion");
|
||||
if (oldVersion <= 1) {
|
||||
await db.execute('''
|
||||
db = await openDatabase(
|
||||
dbFile.path,
|
||||
version: 15,
|
||||
onUpgrade: (db, oldVersion, newVersion) async {
|
||||
printV("migrating: $oldVersion, $newVersion");
|
||||
if (oldVersion <= 1) {
|
||||
await db.execute("""
|
||||
DELETE FROM WalletInfo
|
||||
WHERE walletInfoId NOT IN (
|
||||
SELECT MIN(walletInfoId)
|
||||
@@ -52,10 +80,10 @@ WHERE walletInfoId NOT IN (
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_walletinfo_id_unique
|
||||
ON WalletInfo (id);
|
||||
''');
|
||||
}
|
||||
if (oldVersion <= 2) {
|
||||
await db.execute('''
|
||||
""");
|
||||
}
|
||||
if (oldVersion <= 2) {
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS BalanceCardStyleSettings (
|
||||
walletInfoId INTEGER,
|
||||
accountIndex INTEGER DEFAULT -1,
|
||||
@@ -66,37 +94,116 @@ CREATE TABLE IF NOT EXISTS BalanceCardStyleSettings (
|
||||
FOREIGN KEY (walletInfoId) REFERENCES WalletInfo(walletInfoId)
|
||||
);
|
||||
''');
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: 'WalletInfo',
|
||||
column: 'receiveInfoboxDismissed',
|
||||
definition: 'BOOLEAN DEFAULT FALSE',
|
||||
);
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: "WalletInfo",
|
||||
column: "receiveInfoboxDismissed",
|
||||
definition: "BOOLEAN DEFAULT FALSE",
|
||||
);
|
||||
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: 'BalanceCardStyleSettings',
|
||||
column: 'cardOrder',
|
||||
definition: 'INTEGER DEFAULT 0',
|
||||
);
|
||||
}
|
||||
if (oldVersion <= 3) {
|
||||
await _addColumnIfNotExists(db,
|
||||
table: "WalletInfo", column: "showCombinedBalance", definition: "BOOLEAN DEFAULT TRUE");
|
||||
// null - primary token (eth, sol etc)
|
||||
// not null - address of fav token
|
||||
// if address doesn't correspond to a valid token, fallback to primary token
|
||||
await _addColumnIfNotExists(db,
|
||||
table: "WalletInfo", column: "favoriteTokenAddress", definition: "TEXT DEFAULT NULL");
|
||||
}
|
||||
if (oldVersion <= 4) {
|
||||
await _createBridgeTransferTable(db);
|
||||
}
|
||||
if (oldVersion <= 5) {
|
||||
await _createTradeTable(db);
|
||||
}
|
||||
}, onCreate: (Database db, int version) async {
|
||||
await db.execute('''
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: 'BalanceCardStyleSettings',
|
||||
column: 'cardOrder',
|
||||
definition: 'INTEGER DEFAULT 0',
|
||||
);
|
||||
}
|
||||
if (oldVersion <= 3) {
|
||||
await _addColumnIfNotExists(db,
|
||||
table: "WalletInfo", column: "showCombinedBalance", definition: "BOOLEAN DEFAULT TRUE");
|
||||
// null - primary token (eth, sol etc)
|
||||
// not null - address of fav token
|
||||
// if address doesn't correspond to a valid token, fallback to primary token
|
||||
await _addColumnIfNotExists(db,
|
||||
table: "WalletInfo", column: "favoriteTokenAddress", definition: "TEXT DEFAULT NULL");
|
||||
}
|
||||
|
||||
if (oldVersion <= 4) {
|
||||
await _createBridgeTransferTable(db);
|
||||
}
|
||||
|
||||
if (oldVersion <= 5) {
|
||||
await _createTradeTable(db);
|
||||
}
|
||||
if (oldVersion <= 6) {
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: 'Trade',
|
||||
column: 'toAddressExtraId',
|
||||
definition: 'TEXT',
|
||||
);
|
||||
}
|
||||
if (oldVersion <= 7) {
|
||||
await _createNodeTable(db);
|
||||
}
|
||||
if (oldVersion <= 8) {
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: 'BalanceCardStyleSettings',
|
||||
column: 'iconStyleIndex',
|
||||
definition: 'INTEGER DEFAULT 0',
|
||||
);
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: 'BalanceCardStyleSettings',
|
||||
column: 'isGradientOnly',
|
||||
definition: 'BOOLEAN DEFAULT FALSE',
|
||||
);
|
||||
}
|
||||
if (oldVersion <= 9) {
|
||||
await _createErc20TokenTable(db);
|
||||
await _createSplTokenTable(db);
|
||||
await _createTronTokenTable(db);
|
||||
}
|
||||
if (oldVersion <= 10) {
|
||||
await _createImportedNFTTable(db);
|
||||
}
|
||||
if (oldVersion <= 11) {
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: "WalletInfo",
|
||||
column: "showSeedBackupReminder",
|
||||
definition: "BOOLEAN DEFAULT FALSE",
|
||||
);
|
||||
}
|
||||
if (oldVersion <= 12) {
|
||||
await _createChartsTables(db);
|
||||
}
|
||||
|
||||
if (oldVersion <= 13) {
|
||||
await _createWalletInfoAccountTable(db);
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: "WalletInfo",
|
||||
column: "accountDiscoveryLimit",
|
||||
definition: "INTEGER DEFAULT NULL",
|
||||
);
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: "WalletInfo",
|
||||
column: "isMultiAccountsEnabled",
|
||||
definition: "INTEGER DEFAULT NULL",
|
||||
);
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: "WalletInfo",
|
||||
column: "currentAccountIndex",
|
||||
definition: "INTEGER NOT NULL DEFAULT 0",
|
||||
);
|
||||
|
||||
await _migrateBitcoinCardStylesForAccounts(db);
|
||||
}
|
||||
if(oldVersion <= 14) {
|
||||
await _addColumnIfNotExists(
|
||||
db,
|
||||
table: "BalanceCardStyleSettings",
|
||||
column: "hidden",
|
||||
definition: "BOOLEAN DEFAULT FALSE",
|
||||
);
|
||||
}
|
||||
},
|
||||
onCreate: (Database db, int version) async {
|
||||
await db.execute('''
|
||||
CREATE TABLE WalletInfo (
|
||||
walletInfoId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
id TEXT NOT NULL,
|
||||
@@ -119,13 +226,17 @@ CREATE TABLE WalletInfo (
|
||||
hashedWalletIdentifier TEXT,
|
||||
isNonSeedWallet INTEGER DEFAULT (0) NOT NULL,
|
||||
sortOrder INTEGER DEFAULT (0) NOT NULL,
|
||||
currentAccountIndex INTEGER NOT NULL DEFAULT 0,
|
||||
receiveInfoboxDismissed BOOLEAN DEFAULT FALSE,
|
||||
showCombinedBalance BOOLEAN DEFAULT TRUE,
|
||||
favoriteTokenAddress TEXT DEFAULT NULL
|
||||
favoriteTokenAddress TEXT DEFAULT NULL,
|
||||
accountDiscoveryLimit INTEGER DEFAULT NULL,
|
||||
isMultiAccountsEnabled INTEGER DEFAULT NULL,
|
||||
showSeedBackupReminder BOOLEAN DEFAULT FALSE
|
||||
);
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
await db.execute("""
|
||||
CREATE TABLE WalletInfoDerivationInfo (
|
||||
walletInfoDerivationInfoId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
address TEXT NOT NULL,
|
||||
@@ -136,9 +247,9 @@ CREATE TABLE WalletInfoDerivationInfo (
|
||||
scriptType TEXT,
|
||||
description TEXT
|
||||
);
|
||||
''');
|
||||
""");
|
||||
|
||||
await db.execute('''
|
||||
await db.execute('''
|
||||
CREATE TABLE WalletInfoAddress (
|
||||
walletInfoAddressId INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
walletInfoId INTEGER,
|
||||
@@ -148,7 +259,7 @@ CREATE TABLE WalletInfoAddress (
|
||||
);
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
await db.execute("""
|
||||
CREATE TABLE WalletInfoAddressInfo (
|
||||
walletInfoAddressInfoId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
walletInfoId INTEGER NOT NULL,
|
||||
@@ -158,9 +269,9 @@ CREATE TABLE WalletInfoAddressInfo (
|
||||
mapValueLabel TEXT NOT NULL,
|
||||
CONSTRAINT WalletInfoAddressInfo_WalletInfo_FK FOREIGN KEY (walletInfoId) REFERENCES WalletInfo(walletInfoId)
|
||||
);
|
||||
''');
|
||||
""");
|
||||
|
||||
await db.execute('''
|
||||
await db.execute('''
|
||||
CREATE TABLE "WalletInfoAddressMap" (
|
||||
walletInfoAddressMapId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
walletInfoId INTEGER NOT NULL,
|
||||
@@ -169,29 +280,58 @@ CREATE TABLE "WalletInfoAddressMap" (
|
||||
CONSTRAINT WalletInfoAddress_WalletInfo_FK FOREIGN KEY (walletInfoId) REFERENCES WalletInfo(walletInfoId)
|
||||
);
|
||||
''');
|
||||
await db.execute('''
|
||||
await db.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_walletinfo_id_unique
|
||||
ON WalletInfo (id);
|
||||
''');
|
||||
await db.execute('''
|
||||
""");
|
||||
await db.execute('''
|
||||
CREATE TABLE BalanceCardStyleSettings (
|
||||
walletInfoId INTEGER,
|
||||
accountIndex INTEGER DEFAULT -1,
|
||||
gradientIndex INTEGER DEFAULT -1,
|
||||
useSpecialDesign BOOLEAN DEFAULT FALSE,
|
||||
backgroundImagePath TEXT DEFAULT "",
|
||||
iconStyleIndex INTEGER DEFAULT 0,
|
||||
isGradientOnly BOOLEAN DEFAULT FALSE,
|
||||
cardOrder INTEGER DEFAULT 0,
|
||||
hidden BOOLEAN DEFAULT FALSE,
|
||||
PRIMARY KEY (walletInfoId, accountIndex),
|
||||
FOREIGN KEY (walletInfoId) REFERENCES WalletInfo(walletInfoId)
|
||||
);
|
||||
''');
|
||||
await _createBridgeTransferTable(db);
|
||||
await _createTradeTable(db);
|
||||
});
|
||||
await _createBridgeTransferTable(db);
|
||||
await _createNodeTable(db);
|
||||
await _createTradeTable(db);
|
||||
await _createChartsTables(db);
|
||||
await _createErc20TokenTable(db);
|
||||
await _createSplTokenTable(db);
|
||||
await _createTronTokenTable(db);
|
||||
await _createImportedNFTTable(db);
|
||||
await _createWalletInfoAccountTable(db);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _createChartsTables(Database db) async {
|
||||
await db.execute("""
|
||||
CREATE TABLE PriceData (
|
||||
fromCurrency TEXT NOT NULL,
|
||||
toCurrency TEXT NOT NULL,
|
||||
timestamp INTEGER NOT NULL,
|
||||
price TEXT NOT NULL,
|
||||
PRIMARY KEY (fromCurrency, toCurrency, timestamp)
|
||||
);
|
||||
""");
|
||||
await db.execute("""
|
||||
CREATE TABLE ChartsAssets (
|
||||
asset TEXT PRIMARY KEY,
|
||||
isFavorite BOOLEAN DEFAULT FALSE
|
||||
);
|
||||
""");
|
||||
}
|
||||
|
||||
Future<void> _createTradeTable(Database db) async {
|
||||
await db.execute('''
|
||||
await db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS Trade (
|
||||
tradeId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
id TEXT NOT NULL,
|
||||
@@ -225,6 +365,7 @@ CREATE TABLE IF NOT EXISTS Trade (
|
||||
refundAddress TEXT,
|
||||
walletId TEXT,
|
||||
payoutAddress TEXT,
|
||||
toAddressExtraId TEXT,
|
||||
password TEXT,
|
||||
providerId TEXT,
|
||||
providerName TEXT,
|
||||
@@ -245,11 +386,48 @@ CREATE TABLE IF NOT EXISTS Trade (
|
||||
chainId INTEGER,
|
||||
fee REAL
|
||||
);
|
||||
''');
|
||||
await db.execute('''
|
||||
""");
|
||||
await db.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_trade_id_unique
|
||||
ON Trade (id);
|
||||
''');
|
||||
""");
|
||||
}
|
||||
|
||||
Future<void> _createWalletInfoAccountTable(Database db) async {
|
||||
await db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS WalletInfoAccount (
|
||||
walletInfoId INTEGER NOT NULL,
|
||||
accountIndex INTEGER NOT NULL,
|
||||
label TEXT NOT NULL,
|
||||
PRIMARY KEY (walletInfoId, accountIndex),
|
||||
CONSTRAINT WalletInfoAccount_WalletInfo_FK FOREIGN KEY (walletInfoId) REFERENCES WalletInfo(walletInfoId)
|
||||
);
|
||||
""");
|
||||
|
||||
await db.execute("""
|
||||
CREATE INDEX IF NOT EXISTS idx_walletinfoaccount_walletinfoid
|
||||
ON WalletInfoAccount(walletInfoId);
|
||||
""");
|
||||
}
|
||||
|
||||
|
||||
Future<void> _migrateBitcoinCardStylesForAccounts(Database db) async {
|
||||
const bitcoinWallets = 'SELECT walletInfoId FROM WalletInfo WHERE "type" = ?';
|
||||
final btc = WalletType.bitcoin.index;
|
||||
|
||||
// Lightning: 0 -> -2 (first, so accountIndex 0 is free for the Bitcoin card)
|
||||
await db.rawUpdate(
|
||||
"UPDATE OR REPLACE BalanceCardStyleSettings SET accountIndex = -2 "
|
||||
"WHERE accountIndex = 0 AND walletInfoId IN ($bitcoinWallets)",
|
||||
[btc],
|
||||
);
|
||||
|
||||
// Bitcoin: -1 -> 0 (primary account, first in order)
|
||||
await db.rawUpdate(
|
||||
"UPDATE OR REPLACE BalanceCardStyleSettings SET accountIndex = 0, cardOrder = 0 "
|
||||
"WHERE accountIndex = -1 AND walletInfoId IN ($bitcoinWallets)",
|
||||
[btc],
|
||||
);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> dumpDb() async {
|
||||
@@ -265,7 +443,7 @@ Future<Map<String, dynamic>> dumpDb() async {
|
||||
|
||||
Future<List<String>> _getTableNames(Database db) async {
|
||||
final tableNames = await db.rawQuery('SELECT name FROM sqlite_master WHERE type = "table"');
|
||||
return tableNames.map((e) => (e["name"]).toString()).toList();
|
||||
return tableNames.map((e) => e["name"].toString()).toList();
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> _dumpDb() async {
|
||||
@@ -288,7 +466,7 @@ Future<Map<String, dynamic>> dumpCustomDb(String path) async {
|
||||
}
|
||||
|
||||
Future<void> _createBridgeTransferTable(Database db) async {
|
||||
await db.execute('''
|
||||
await db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS BridgeTransfer (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
wallet_id TEXT NOT NULL,
|
||||
@@ -307,9 +485,116 @@ CREATE TABLE IF NOT EXISTS BridgeTransfer (
|
||||
error_message TEXT,
|
||||
status_message TEXT
|
||||
);
|
||||
''');
|
||||
await db.execute('''
|
||||
""");
|
||||
await db.execute("""
|
||||
CREATE INDEX IF NOT EXISTS idx_bridgetransfer_wallet_id
|
||||
ON BridgeTransfer(wallet_id);
|
||||
''');
|
||||
""");
|
||||
}
|
||||
|
||||
Future<void> _createErc20TokenTable(Database db) async {
|
||||
await db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS Erc20Token (
|
||||
Erc20TokenId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
walletName TEXT NOT NULL,
|
||||
chainId INTEGER NOT NULL,
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
symbol TEXT NOT NULL DEFAULT '',
|
||||
contractAddress TEXT NOT NULL,
|
||||
decimal INTEGER NOT NULL DEFAULT 0,
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
iconPath TEXT,
|
||||
tag TEXT,
|
||||
isPotentialScam INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
""");
|
||||
await db.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_erc20token_wallet_chain_contract
|
||||
ON Erc20Token (walletName, chainId, contractAddress);
|
||||
""");
|
||||
}
|
||||
|
||||
Future<void> _createSplTokenTable(Database db) async {
|
||||
await db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS SPLToken (
|
||||
SPLTokenId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
walletName TEXT NOT NULL,
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
symbol TEXT NOT NULL DEFAULT '',
|
||||
mintAddress TEXT NOT NULL,
|
||||
decimal INTEGER NOT NULL DEFAULT 0,
|
||||
mint TEXT NOT NULL DEFAULT '',
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
iconPath TEXT,
|
||||
tag TEXT,
|
||||
isPotentialScam INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
""");
|
||||
await db.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_spltoken_wallet_mint
|
||||
ON SPLToken (walletName, mintAddress);
|
||||
""");
|
||||
}
|
||||
|
||||
Future<void> _createImportedNFTTable(Database db) async {
|
||||
await db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS ImportedNFT (
|
||||
ImportedNFTId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
walletName TEXT NOT NULL,
|
||||
chain TEXT NOT NULL,
|
||||
identifier TEXT NOT NULL,
|
||||
name TEXT,
|
||||
symbol TEXT,
|
||||
description TEXT,
|
||||
imageUrl TEXT,
|
||||
isOwned INTEGER
|
||||
);
|
||||
""");
|
||||
await db.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_importednft_wallet_chain_identifier
|
||||
ON ImportedNFT (walletName, chain, identifier);
|
||||
""");
|
||||
}
|
||||
|
||||
Future<void> _createTronTokenTable(Database db) async {
|
||||
await db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS TronToken (
|
||||
TronTokenId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
walletName TEXT NOT NULL,
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
symbol TEXT NOT NULL DEFAULT '',
|
||||
contractAddress TEXT NOT NULL,
|
||||
decimal INTEGER NOT NULL DEFAULT 0,
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
iconPath TEXT,
|
||||
tag TEXT,
|
||||
isPotentialScam INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
""");
|
||||
await db.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_trontoken_wallet_contract
|
||||
ON TronToken (walletName, contractAddress);
|
||||
""");
|
||||
}
|
||||
|
||||
Future<void> _createNodeTable(Database db) async {
|
||||
await db.execute("""
|
||||
CREATE TABLE Node (
|
||||
NodeId INTEGER PRIMARY KEY,
|
||||
uri TEXT NOT NULL,
|
||||
path TEXT,
|
||||
login TEXT,
|
||||
label TEXT,
|
||||
password TEXT,
|
||||
isPow INTEGER NOT NULL,
|
||||
useSSL INTEGER,
|
||||
typeRaw INTEGER NOT NULL,
|
||||
trusted INTEGER NOT NULL,
|
||||
socksProxyAddress TEXT,
|
||||
isEnabledForAutoSwitching BOOLEAN DEFAULT FALSE,
|
||||
isOfficial BOOLEAN DEFAULT FALSE,
|
||||
isBuiltin BOOLEAN DEFAULT FALSE,
|
||||
isDefault BOOLEAN DEFAULT FALSE
|
||||
);
|
||||
""");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:cw_core/db/sqlite.dart';
|
||||
import 'package:cw_core/root_dir.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
Future<void> handleSqliteError(final dynamic error, final StackTrace stackTrace) async {
|
||||
try {
|
||||
final f = await sqliteDebugMarkerFile();
|
||||
f.createSync();
|
||||
} catch (_) {}
|
||||
runApp(
|
||||
SqliteErrorHandlerApp(
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
),
|
||||
);
|
||||
await Future.delayed(Duration(hours: 24));
|
||||
}
|
||||
|
||||
class SqliteErrorHandlerApp extends StatelessWidget {
|
||||
const SqliteErrorHandlerApp({
|
||||
super.key,
|
||||
required this.error,
|
||||
required this.stackTrace,
|
||||
});
|
||||
|
||||
final dynamic error;
|
||||
final StackTrace? stackTrace;
|
||||
@override
|
||||
Widget build(final BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
title: 'Cake Wallet Crash',
|
||||
themeMode: ThemeMode.light,
|
||||
home: SqliteErrorHandler(
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SqliteErrorHandler extends StatefulWidget {
|
||||
SqliteErrorHandler({
|
||||
super.key,
|
||||
required this.error,
|
||||
required this.stackTrace,
|
||||
});
|
||||
|
||||
final dynamic error;
|
||||
final StackTrace? stackTrace;
|
||||
|
||||
@override
|
||||
State<SqliteErrorHandler> createState() => _SqliteErrorHandlerState();
|
||||
}
|
||||
|
||||
class _SqliteErrorHandlerState extends State<SqliteErrorHandler> {
|
||||
String log = "";
|
||||
|
||||
@override
|
||||
Widget build(final BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Sqlite Recovery Screen"),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: _buildNotice(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _text(final dynamic element) {
|
||||
return SelectableText(
|
||||
element.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
void _dbgRemoveDebugMarker() async {
|
||||
final f = await sqliteDebugMarkerFile();
|
||||
f.delete();
|
||||
}
|
||||
|
||||
void _corruptTheDb() async {
|
||||
final dbFile = File("${(await getAppDir()).path}/cake.db");
|
||||
await writeBytesToFile(dbFile);
|
||||
}
|
||||
|
||||
void _dumpDb() async {
|
||||
try {
|
||||
final dbFile = File("${(await getAppDir()).path}/cake.db");
|
||||
final jsonText = JsonEncoder.withIndent(" ").convert(await dumpCustomDb(dbFile.path));
|
||||
setState(() {
|
||||
log = jsonText;
|
||||
});
|
||||
} catch (e, s) {
|
||||
setState(() {
|
||||
log = """
|
||||
e: $e
|
||||
|
||||
s: $s
|
||||
""";
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _removeDb() async {
|
||||
try {
|
||||
File("${(await getAppDir()).path}/cake.db.old").deleteSync();
|
||||
} catch (_) {}
|
||||
final dbFile = File("${(await getAppDir()).path}/cake.db");
|
||||
dbFile.renameSync("${(await getAppDir()).path}/cake.db.old");
|
||||
}
|
||||
|
||||
List<Widget> _debugOptions() {
|
||||
return [
|
||||
_debugButton("Remove debug marker", _dbgRemoveDebugMarker),
|
||||
if (kDebugMode || kProfileMode) _debugButton("[DO NOT PRESS] Corrupt the DB", _corruptTheDb),
|
||||
_debugButton("Remove DB", _removeDb),
|
||||
_debugButton("Dump DB", _dumpDb),
|
||||
];
|
||||
}
|
||||
|
||||
Widget _debugButton(String text, VoidCallback callback) {
|
||||
return ElevatedButton(
|
||||
onPressed: callback,
|
||||
child: Text(text),
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _buildNotice() {
|
||||
return [
|
||||
_text("""Critical error occured in SQLite
|
||||
This is very rare, usually caused by the device killing the app before it finishes writing file.
|
||||
Here you can recover the database or recreate it to regain access to your wallets.
|
||||
During this process some of the metadata may get lost.
|
||||
If you need help, report it at https://hash.boats/report."""),
|
||||
Divider(),
|
||||
..._debugOptions(),
|
||||
if (log.isNotEmpty) ...[
|
||||
Divider(),
|
||||
SelectableText(
|
||||
log,
|
||||
style: TextStyle(
|
||||
fontSize: 8,
|
||||
),
|
||||
),
|
||||
],
|
||||
Divider(),
|
||||
_text(widget.error),
|
||||
Divider(),
|
||||
_text(widget.stackTrace),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> writeBytesToFile(File file) async {
|
||||
const int seek = 5;
|
||||
const int count = 50000;
|
||||
final raf = await file.open(mode: FileMode.writeOnlyAppend);
|
||||
try {
|
||||
await raf.setPosition(seek);
|
||||
final zeros = Uint8List.fromList(List.generate(count, (_) => 0x42));
|
||||
await raf.writeFrom(zeros);
|
||||
} finally {
|
||||
await raf.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user