This commit is contained in:
Matthew Fosse 2024-09-17 12:35:25 -07:00
parent 35c4f3e2f1
commit 4f9d602ee4
5 changed files with 120 additions and 127 deletions

View file

@ -306,10 +306,8 @@ abstract class ElectrumWalletBase
bool? doSingleScan, bool? doSingleScan,
bool? usingSupportedNode, bool? usingSupportedNode,
}) async { }) async {
// final chainTip = chainTipParam ?? await getUpdatedChainTip(); final chainTip = chainTipParam ?? await getUpdatedChainTip();
final chainTip = 861586;
print("chainTip: $chainTip");
if (chainTip == height) { if (chainTip == height) {
syncStatus = SyncedSyncStatus(); syncStatus = SyncedSyncStatus();
return; return;
@ -408,11 +406,6 @@ abstract class ElectrumWalletBase
nodeSupportsSilentPayments = false; nodeSupportsSilentPayments = false;
} }
if (message.syncStatus is SyncingSyncStatus) {
print("sp sync: ${(message.syncStatus as SyncingSyncStatus).blocksLeft} blocks left");
}
syncStatus = message.syncStatus; syncStatus = message.syncStatus;
await walletInfo.updateRestoreHeight(message.height); await walletInfo.updateRestoreHeight(message.height);
} }
@ -540,6 +533,15 @@ abstract class ElectrumWalletBase
} }
Future<bool> getNodeSupportsSilentPayments() async { Future<bool> getNodeSupportsSilentPayments() async {
int secondsWaited = 0;
while (!electrumClient.isConnected) {
await Future.delayed(const Duration(seconds: 1));
secondsWaited++;
if (secondsWaited > 5) {
return false;
}
}
// As of today (august 2024), only ElectrumRS supports silent payments // As of today (august 2024), only ElectrumRS supports silent payments
if (!(await getNodeIsElectrs())) { if (!(await getNodeIsElectrs())) {
return false; return false;
@ -582,8 +584,16 @@ abstract class ElectrumWalletBase
electrumClient.onConnectionStatusChange = _onConnectionStatusChange; electrumClient.onConnectionStatusChange = _onConnectionStatusChange;
await electrumClient.connectToUri(node.uri, useSSL: node.useSSL); await electrumClient.connectToUri(node.uri, useSSL: node.useSSL);
int secondsWaited = 0;
while (!electrumClient.isConnected) {
await Future.delayed(const Duration(seconds: 1));
secondsWaited++;
if (secondsWaited > 5) {
break;
}
}
} catch (e) { } catch (e) {
print(e.toString());
syncStatus = FailedSyncStatus(); syncStatus = FailedSyncStatus();
} }
} }

View file

@ -3,8 +3,6 @@ import 'dart:io';
import 'dart:ui'; import 'dart:ui';
import 'package:cake_wallet/core/wallet_loading_service.dart'; import 'package:cake_wallet/core/wallet_loading_service.dart';
import 'package:cake_wallet/entities/preferences_key.dart';
import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/utils/device_info.dart'; import 'package:cake_wallet/utils/device_info.dart';
import 'package:cake_wallet/utils/feature_flag.dart'; import 'package:cake_wallet/utils/feature_flag.dart';
@ -20,7 +18,6 @@ import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:cake_wallet/main.dart'; import 'package:cake_wallet/main.dart';
import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/di.dart';
import 'package:shared_preferences/shared_preferences.dart';
const moneroSyncTaskKey = "com.fotolockr.cakewallet.monero_sync_task"; const moneroSyncTaskKey = "com.fotolockr.cakewallet.monero_sync_task";
const mwebSyncTaskKey = "com.fotolockr.cakewallet.mweb_sync_task"; const mwebSyncTaskKey = "com.fotolockr.cakewallet.mweb_sync_task";
@ -34,17 +31,18 @@ const notificationChannelId = 'cake_service';
const notificationChannelName = 'CAKE BACKGROUND SERVICE'; const notificationChannelName = 'CAKE BACKGROUND SERVICE';
const notificationChannelDescription = 'Cake Wallet Background Service'; const notificationChannelDescription = 'Cake Wallet Background Service';
const DELAY_SECONDS_BEFORE_SYNC_START = 15; const DELAY_SECONDS_BEFORE_SYNC_START = 15;
const spNodeNotificationMessage =
"Currently configured Bitcoin node does not support Silent Payments. skipping wallet";
const spNotificationId = 888; void setMainNotification(
const spNodeNotificationMessage = "Currently configured Bitcoin node does not support Silent Payments. skipping wallet"; FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin, {
required String title,
required String content,
void setNotificationStandby(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async { }) async {
flutterLocalNotificationsPlugin.cancelAll();
flutterLocalNotificationsPlugin.show( flutterLocalNotificationsPlugin.show(
notificationId, notificationId,
initialNotificationTitle, title,
standbyMessage, content,
const NotificationDetails( const NotificationDetails(
android: AndroidNotificationDetails( android: AndroidNotificationDetails(
notificationChannelId, notificationChannelId,
@ -56,35 +54,36 @@ void setNotificationStandby(FlutterLocalNotificationsPlugin flutterLocalNotifica
); );
} }
void setNotificationStandby(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
flutterLocalNotificationsPlugin.cancelAll();
setMainNotification(
flutterLocalNotificationsPlugin,
title: initialNotificationTitle,
content: standbyMessage,
);
}
void setNotificationReady(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async { void setNotificationReady(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
flutterLocalNotificationsPlugin.cancelAll(); flutterLocalNotificationsPlugin.cancelAll();
flutterLocalNotificationsPlugin.show( setMainNotification(
notificationId, flutterLocalNotificationsPlugin,
initialNotificationTitle, title: initialNotificationTitle,
readyMessage, content: readyMessage,
const NotificationDetails(
android: AndroidNotificationDetails(
notificationChannelId,
notificationChannelName,
icon: 'ic_bg_service_small',
ongoing: true,
),
),
); );
} }
void setSpNodeWarningNotification(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async { void setSpNodeWarningNotification(
flutterLocalNotificationsPlugin.cancelAll(); FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin, int walletNum) async {
flutterLocalNotificationsPlugin.show( flutterLocalNotificationsPlugin.show(
notificationId, notificationId + walletNum,
initialNotificationTitle, initialNotificationTitle,
spNodeNotificationMessage, spNodeNotificationMessage,
const NotificationDetails( NotificationDetails(
android: AndroidNotificationDetails( android: AndroidNotificationDetails(
notificationChannelId, "${notificationChannelId}_$walletNum",
notificationChannelName, "${notificationChannelName}_$walletNum",
icon: 'ic_bg_service_small', icon: 'ic_bg_service_small',
ongoing: true, ongoing: false,
), ),
), ),
); );
@ -92,7 +91,7 @@ void setSpNodeWarningNotification(FlutterLocalNotificationsPlugin flutterLocalNo
@pragma('vm:entry-point') @pragma('vm:entry-point')
Future<void> onStart(ServiceInstance service) async { Future<void> onStart(ServiceInstance service) async {
print("BACKGROUND SERVICE STARTED!"); print("BACKGROUND SERVICE STARTED");
bool bgSyncStarted = false; bool bgSyncStarted = false;
Timer? _syncTimer; Timer? _syncTimer;
@ -103,7 +102,7 @@ Future<void> onStart(ServiceInstance service) async {
FlutterLocalNotificationsPlugin(); FlutterLocalNotificationsPlugin();
service.on('stopService').listen((event) async { service.on('stopService').listen((event) async {
print("STOPPING BACKGROUND SERVICE!"); print("STOPPING BACKGROUND SERVICE");
_syncTimer?.cancel(); _syncTimer?.cancel();
await service.stopSelf(); await service.stopSelf();
}); });
@ -130,7 +129,7 @@ Future<void> onStart(ServiceInstance service) async {
bgSyncStarted = true; bgSyncStarted = true;
await Future.delayed(const Duration(seconds: DELAY_SECONDS_BEFORE_SYNC_START)); await Future.delayed(const Duration(seconds: DELAY_SECONDS_BEFORE_SYNC_START));
print("STARTING SYNC FROM BG!!"); print("STARTING SYNC FROM BG");
try { try {
await initializeAppConfigs(loadWallet: false); await initializeAppConfigs(loadWallet: false);
@ -140,89 +139,73 @@ Future<void> onStart(ServiceInstance service) async {
print("INITIALIZED APP CONFIGS"); print("INITIALIZED APP CONFIGS");
final currentWallet = getIt.get<AppStore>().wallet; // final currentWallet = getIt.get<AppStore>().wallet;
// don't start syncing immediately: // // don't start syncing immediately:
await currentWallet?.stopSync(); // await currentWallet?.stopSync();
final walletLoadingService = getIt.get<WalletLoadingService>(); final walletLoadingService = getIt.get<WalletLoadingService>();
final settingsStore = getIt.get<SettingsStore>(); final settingsStore = getIt.get<SettingsStore>();
final walletListViewModel = getIt.get<WalletListViewModel>(); final walletListViewModel = getIt.get<WalletListViewModel>();
final typeRaw = getIt.get<SharedPreferences>().getInt(PreferencesKey.currentWalletType);
bool syncAll = true;
List<WalletBase?> syncingWallets = []; List<WalletBase?> syncingWallets = [];
if (syncAll) { // get all Monero / Wownero wallets and sync them
/// get all Monero wallets of the user and sync them final List<WalletListItem> moneroWallets = walletListViewModel.wallets
final List<WalletListItem> moneroWallets = walletListViewModel.wallets .where((element) => [WalletType.monero, WalletType.wownero].contains(element.type))
.where((element) => [WalletType.monero, WalletType.wownero].contains(element.type)) .toList();
.toList();
for (int i = 0; i < moneroWallets.length; i++) { for (int i = 0; i < moneroWallets.length; i++) {
final wallet = await walletLoadingService.load(moneroWallets[i].type, moneroWallets[i].name);
final node = settingsStore.getCurrentNode(moneroWallets[i].type);
await wallet.connectToNode(node: node);
wallet.startSync();
syncingWallets.add(wallet);
}
// get all litecoin wallets and sync them:
final List<WalletListItem> litecoinWallets = walletListViewModel.wallets
.where((element) => element.type == WalletType.litecoin)
.toList();
// we only need to sync the first litecoin wallet since they share the same collection of blocks
if (litecoinWallets.isNotEmpty) {
try {
final firstWallet = litecoinWallets.first;
final wallet = await walletLoadingService.load(firstWallet.type, firstWallet.name);
final node = settingsStore.getCurrentNode(WalletType.litecoin);
wallet.connectToNode(node: node);
// calling start sync isn't necessary since it's called after connecting to the node
syncingWallets.add(wallet);
} catch (e) {
// couldn't connect to mwebd (most likely)
print("error syncing litecoin wallet: $e");
}
}
// get all bitcoin wallets and sync them:
final List<WalletListItem> bitcoinWallets =
walletListViewModel.wallets.where((element) => element.type == WalletType.bitcoin).toList();
bool spSupported = true;
for (int i = 0; i < bitcoinWallets.length; i++) {
try {
if (!spSupported) continue;
final wallet = final wallet =
await walletLoadingService.load(moneroWallets[i].type, moneroWallets[i].name); await walletLoadingService.load(bitcoinWallets[i].type, bitcoinWallets[i].name);
final node = settingsStore.getCurrentNode(moneroWallets[i].type); final node = settingsStore.getCurrentNode(WalletType.bitcoin);
await wallet.connectToNode(node: node);
await wallet.startSync();
syncingWallets.add(wallet);
}
// get all litecoin wallets and sync them: bool nodeSupportsSP = await (wallet as ElectrumWallet).getNodeSupportsSilentPayments();
final List<WalletListItem> litecoinWallets = walletListViewModel.wallets if (!nodeSupportsSP) {
.where((element) => element.type == WalletType.litecoin) print("Configured node does not support silent payments, skipping wallet");
.toList(); syncingWallets.add(null);
setSpNodeWarningNotification(flutterLocalNotificationsPlugin, syncingWallets.length - 1);
// we only need to sync the first litecoin wallet since they share the same collection of blocks spSupported = false;
// if (litecoinWallets.isNotEmpty) { continue;
// try {
// final firstWallet = litecoinWallets.first;
// final wallet = await walletLoadingService.load(firstWallet.type, firstWallet.name);
// final node = settingsStore.getCurrentNode(firstWallet.type);
// await wallet.connectToNode(node: node);
// // calling start sync isn't necessary since it's called after connecting to the node
// syncingWallets.add(wallet);
// } catch (e) {
// // couldn't connect to mwebd (most likely)
// print("error syncing litecoin wallet: $e");
// }
// }
final List<WalletListItem> bitcoinWallets = walletListViewModel.wallets
.where((element) => element.type == WalletType.bitcoin)
.toList();
for (int i = 0; i < bitcoinWallets.length; i++) {
try {
final wallet =
await walletLoadingService.load(bitcoinWallets[i].type, bitcoinWallets[i].name);
final node = settingsStore.getCurrentNode(bitcoinWallets[i].type);
// bool nodeSupportsSP = await (wallet as ElectrumWallet).getNodeSupportsSilentPayments();
// TODO: fix this:
bool nodeSupportsSP = node.uriRaw.contains("electrs");
if (!nodeSupportsSP) {
print("Configured node does not support silent payments, skipping wallet");
setSpNodeWarningNotification(flutterLocalNotificationsPlugin);
continue;
}
// await wallet.connectToNode(node: node);
// (wallet as ElectrumWallet).setSilentPaymentsScanning(true);
(wallet as ElectrumWallet).rescan(height: 1);
syncingWallets.add(wallet);
} catch (e) {
print("error syncing bitcoin wallet: $e");
} }
}
} else {
/// if the user chose to sync only active wallet
/// if the current wallet is monero; sync it only
if (typeRaw == WalletType.monero.index || typeRaw == WalletType.wownero.index) {
final name = getIt.get<SharedPreferences>().getString(PreferencesKey.currentWalletName);
final wallet = await walletLoadingService.load(WalletType.values[typeRaw!], name!);
final node = getIt.get<SettingsStore>().getCurrentNode(WalletType.values[typeRaw]);
await wallet.connectToNode(node: node); await wallet.connectToNode(node: node);
await wallet.startSync();
syncingWallets.add(wallet); syncingWallets.add(wallet);
} catch (e) {
print("error syncing bitcoin wallet_$i: $e");
} }
} }
@ -235,7 +218,9 @@ Future<void> onStart(ServiceInstance service) async {
for (int i = 0; i < syncingWallets.length; i++) { for (int i = 0; i < syncingWallets.length; i++) {
final wallet = syncingWallets[i]; final wallet = syncingWallets[i];
final syncProgress = ((wallet!.syncStatus.progress()) * 100).toStringAsPrecision(5); if (wallet == null) continue;
final syncProgress = ((wallet.syncStatus.progress()) * 100).toStringAsPrecision(5);
String prefix = walletTypeToCryptoCurrency(wallet.type).title; String prefix = walletTypeToCryptoCurrency(wallet.type).title;
String title = "$prefix - ${wallet.name}"; String title = "$prefix - ${wallet.name}";
@ -244,10 +229,9 @@ Future<void> onStart(ServiceInstance service) async {
final blocksLeft = (wallet.syncStatus as SyncingSyncStatus).blocksLeft; final blocksLeft = (wallet.syncStatus as SyncingSyncStatus).blocksLeft;
content = "${blocksLeft} Blocks Left"; content = "${blocksLeft} Blocks Left";
} catch (e) { } catch (e) {
print(e);
content = "${syncProgress}% Synced"; content = "${syncProgress}% Synced";
} }
content += " - ${DateTime.now()}"; content += " - ${DateTime.now().toIso8601String()}";
flutterLocalNotificationsPlugin.show( flutterLocalNotificationsPlugin.show(
notificationId + i, notificationId + i,
@ -388,22 +372,21 @@ class BackgroundTasks {
} }
final SyncMode syncMode = settingsStore.currentSyncMode; final SyncMode syncMode = settingsStore.currentSyncMode;
final bool syncAll = settingsStore.currentSyncAll; final bool useNotifications = settingsStore.showSyncNotification;
if (syncMode.type == SyncType.disabled || !FeatureFlag.isBackgroundSyncEnabled) { if (useNotifications) {
bgService.invoke('stopService');
return;
}
if (settingsStore.showSyncNotification) {
flutterLocalNotificationsPlugin flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>() .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission(); ?.requestNotificationsPermission();
} }
bgService.invoke('stopService'); bgService.invoke("stopService");
await initializeService(bgService, settingsStore.showSyncNotification); if (syncMode.type == SyncType.disabled || !FeatureFlag.isBackgroundSyncEnabled) {
return;
}
await initializeService(bgService, useNotifications);
} catch (error, stackTrace) { } catch (error, stackTrace) {
print(error); print(error);
print(stackTrace); print(stackTrace);

View file

@ -238,7 +238,7 @@ Future<void> initialSetup(
navigatorKey: navigatorKey, navigatorKey: navigatorKey,
secureStorage: secureStorage, secureStorage: secureStorage,
); );
await bootstrap(navigatorKey); await bootstrap(navigatorKey, loadWallet: loadWallet);
} }
class App extends StatefulWidget { class App extends StatefulWidget {

View file

@ -27,6 +27,7 @@ Future<void> bootstrap(GlobalKey<NavigatorState> navigatorKey, {bool loadWallet
authenticationStore.installed(); authenticationStore.installed();
} }
// we need to NOT automatically load the default wallet inside the background service:
if (loadWallet) { if (loadWallet) {
startAuthenticationStateChange(authenticationStore, navigatorKey); startAuthenticationStateChange(authenticationStore, navigatorKey);
} }

View file

@ -29,7 +29,6 @@ void startAuthenticationStateChange(
if (state == AuthenticationState.installed && !SettingsStoreBase.walletPasswordDirectInput) { if (state == AuthenticationState.installed && !SettingsStoreBase.walletPasswordDirectInput) {
try { try {
print("LOADING CURRENT WALLET");
await loadCurrentWallet(); await loadCurrentWallet();
} catch (error, stack) { } catch (error, stack) {
loginError = error; loginError = error;