cake_wallet/lib/core/wallet_loading_service.dart

92 lines
3.7 KiB
Dart
Raw Normal View History

2024-01-24 17:39:13 +00:00
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/core/generate_wallet_password.dart';
import 'package:cake_wallet/core/key_service.dart';
import 'package:cake_wallet/entities/preferences_key.dart';
2024-01-04 17:06:55 +00:00
import 'package:cake_wallet/store/settings_store.dart';
2024-01-24 17:39:13 +00:00
import 'package:cake_wallet/view_model/settings/tor_connection.dart';
2024-01-04 20:08:27 +00:00
import 'package:cake_wallet/view_model/settings/tor_view_model.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_service.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:shared_preferences/shared_preferences.dart';
class WalletLoadingService {
2023-07-12 23:20:11 +00:00
WalletLoadingService(
2024-01-04 17:06:55 +00:00
this.sharedPreferences,
this.keyService,
this.walletServiceFactory,
2024-01-04 20:08:27 +00:00
this.torViewModel,
2024-01-04 17:06:55 +00:00
);
2023-07-12 23:20:11 +00:00
final SharedPreferences sharedPreferences;
final KeyService keyService;
final WalletService Function(WalletType type) walletServiceFactory;
2024-01-04 20:08:27 +00:00
final TorViewModel torViewModel;
2023-07-12 23:20:11 +00:00
2024-01-04 17:06:55 +00:00
Future<void> renameWallet(WalletType type, String name, String newName) async {
2023-07-12 23:20:11 +00:00
final walletService = walletServiceFactory.call(type);
final password = await keyService.getWalletPassword(walletName: name);
// Save the current wallet's password to the new wallet name's key
2024-01-04 17:06:55 +00:00
await keyService.saveWalletPassword(walletName: newName, password: password);
2023-07-12 23:20:11 +00:00
// Delete previous wallet name from keyService to keep only new wallet's name
// otherwise keeps duplicate (old and new names)
await keyService.deleteWalletPassword(walletName: name);
await walletService.rename(name, password, newName);
// set shared preferences flag based on previous wallet name
if (type == WalletType.monero) {
final oldNameKey = PreferencesKey.moneroWalletUpdateV1Key(name);
final isPasswordUpdated = sharedPreferences.getBool(oldNameKey) ?? false;
final newNameKey = PreferencesKey.moneroWalletUpdateV1Key(newName);
await sharedPreferences.setBool(newNameKey, isPasswordUpdated);
}
2023-07-12 23:20:11 +00:00
}
Future<WalletBase> load(WalletType type, String name) async {
final walletService = walletServiceFactory.call(type);
final password = await keyService.getWalletPassword(walletName: name);
final wallet = await walletService.openWallet(name, password);
if (type == WalletType.monero) {
await updateMoneroWalletPassword(wallet);
}
2024-01-24 17:39:13 +00:00
TorConnectionMode mode = TorConnectionMode.deserialize(
raw: sharedPreferences.getInt(PreferencesKey.currentTorConnectionModeKey) ?? 0,
);
2024-01-25 16:04:31 +00:00
if ([WalletType.bitcoin, WalletType.litecoin, WalletType.bitcoinCash].contains(type)) {
2024-01-24 17:39:13 +00:00
bitcoin!.setTorOnly(wallet, mode == TorConnectionMode.torOnly);
}
2024-01-04 20:08:27 +00:00
final status = torViewModel.torConnectionStatus;
if (status == TorConnectionStatus.connected || status == TorConnectionStatus.connecting) {
// connect the node to the tor proxy:
2024-01-25 16:04:31 +00:00
await torViewModel.connectOrDisconnectNodeToProxy(connect: true);
2024-01-04 17:06:55 +00:00
}
2023-07-12 23:20:11 +00:00
return wallet;
}
Future<void> updateMoneroWalletPassword(WalletBase wallet) async {
final key = PreferencesKey.moneroWalletUpdateV1Key(wallet.name);
var isPasswordUpdated = sharedPreferences.getBool(key) ?? false;
if (isPasswordUpdated) {
return;
}
final password = generateWalletPassword();
// Save new generated password with backup key for case where
// wallet will change password, but it will fail to update in secure storage
final bakWalletName = '#__${wallet.name}_bak__#';
2024-01-04 17:06:55 +00:00
await keyService.saveWalletPassword(walletName: bakWalletName, password: password);
2023-07-12 23:20:11 +00:00
await wallet.changePassword(password);
2024-01-04 17:06:55 +00:00
await keyService.saveWalletPassword(walletName: wallet.name, password: password);
2023-07-12 23:20:11 +00:00
isPasswordUpdated = true;
await sharedPreferences.setBool(key, isPasswordUpdated);
}
}