Files
hash-wallet/cw_core/lib/spl_token_legacy.dart
T
jwinterm dd449f2594 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.
2026-10-24 23:49:29 -04:00

134 lines
3.3 KiB
Dart

import "package:cw_core/cake_hive.dart";
import "package:cw_core/db/sqlite.dart";
import "package:cw_core/hive_type_ids.dart";
import "package:cw_core/spl_token.dart" as spl_new;
import "package:cw_core/utils/print_verbose.dart";
import "package:cw_core/wallet_info.dart";
import "package:cw_core/wallet_type.dart";
import "package:hive/hive.dart";
import "package:sqflite/sqflite.dart";
part "spl_token_legacy.part.dart";
Future<void> performSplTokenHiveMigration() async {
try {
if (!CakeHive.isAdapterRegistered(SPLToken.typeId)) {
CakeHive.registerAdapter(SPLTokenAdapter());
}
final wallets = await WalletInfo.getAll();
await SPLToken.migrateAllToSqlite(wallets);
} catch (e) {
printV("Error performing SPLToken Hive migration: $e, continuing anyway");
}
}
// @HiveType(typeId: SPLToken.typeId)
class SPLToken extends HiveObject {
SPLToken({
required this.name,
required this.symbol,
required this.mintAddress,
required this.decimal,
required this.mint,
this.iconPath,
this.tag = "SOL",
bool enabled = true,
this.isPotentialScam = false,
}) : _enabled = enabled;
// @HiveField(0)
final String name;
// @HiveField(1)
final String symbol;
// @HiveField(2)
final String mintAddress;
// @HiveField(3)
final int decimal;
// @HiveField(4, defaultValue: true)
bool _enabled;
// @HiveField(5)
final String mint;
// @HiveField(6)
final String? iconPath;
// @HiveField(7)
final String? tag;
// @HiveField(8, defaultValue: false)
bool isPotentialScam;
bool get enabled => _enabled;
set enabled(bool value) => _enabled = value;
static const typeId = SPL_TOKEN_TYPE_ID;
static const boxName = "SPLTokens";
static Future<void> migrateAllToSqlite(List<WalletInfo> wallets) async {
final sanitizedToRawNames = <String, Set<String>>{};
for (final wallet in wallets) {
// Hash Bags: solana is removed, so no wallet has tokens to migrate.
continue;
sanitizedToRawNames
.putIfAbsent(wallet.name.replaceAll(" ", "_"), () => <String>{})
.add(wallet.name);
}
for (final entry in sanitizedToRawNames.entries) {
final tokenBoxName = "${entry.key}_$boxName";
try {
if (!await CakeHive.boxExists(tokenBoxName)) {
continue;
}
final box = await CakeHive.openBox<SPLToken>(tokenBoxName);
for (final key in box.keys.toList()) {
final token = box.get(key);
if (token == null) {
continue;
}
for (final rawName in entry.value) {
await token.migrateToSqlite(walletName: rawName);
}
await box.delete(key);
}
await box.deleteFromDisk();
} catch (e) {
printV("Error migrating spl token box $tokenBoxName: $e, continuing anyway");
}
}
}
Future<void> migrateToSqlite({required String walletName}) async {
final row = spl_new.SPLToken(
name: name,
symbol: symbol,
mintAddress: mintAddress,
decimal: decimal,
mint: mint,
enabled: _enabled,
iconPath: iconPath,
tag: tag,
isPotentialScam: isPotentialScam,
walletName: walletName,
).toMap();
row[spl_new.SPLToken.selfIdColumn] = null;
await db!.insert(
spl_new.SPLToken.tableName,
row,
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
}