diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 87885cebe..babe4d9df 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -306,8 +306,10 @@ abstract class ElectrumWalletBase bool? doSingleScan, bool? usingSupportedNode, }) async { - final chainTip = chainTipParam ?? await getUpdatedChainTip(); - + // final chainTip = chainTipParam ?? await getUpdatedChainTip(); + final chainTip = 861586; + print("chainTip: $chainTip"); + if (chainTip == height) { syncStatus = SyncedSyncStatus(); return; @@ -406,7 +408,10 @@ abstract class ElectrumWalletBase nodeSupportsSilentPayments = false; } - print("sp sync: ${message.syncStatus.progress() * 100}%"); + if (message.syncStatus is SyncingSyncStatus) { + print("sp sync: ${(message.syncStatus as SyncingSyncStatus).blocksLeft} blocks left"); + } + syncStatus = message.syncStatus; await walletInfo.updateRestoreHeight(message.height); diff --git a/lib/entities/background_tasks.dart b/lib/entities/background_tasks.dart index a2103e304..79226c4fb 100644 --- a/lib/entities/background_tasks.dart +++ b/lib/entities/background_tasks.dart @@ -35,6 +35,10 @@ const notificationChannelName = 'CAKE BACKGROUND SERVICE'; const notificationChannelDescription = 'Cake Wallet Background Service'; const DELAY_SECONDS_BEFORE_SYNC_START = 15; +const spNotificationId = 888; +const spNodeNotificationMessage = "Currently configured Bitcoin node does not support Silent Payments. skipping wallet"; + + void setNotificationStandby(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async { flutterLocalNotificationsPlugin.cancelAll(); flutterLocalNotificationsPlugin.show( @@ -69,6 +73,23 @@ void setNotificationReady(FlutterLocalNotificationsPlugin flutterLocalNotificati ); } +void setSpNodeWarningNotification(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async { + flutterLocalNotificationsPlugin.cancelAll(); + flutterLocalNotificationsPlugin.show( + notificationId, + initialNotificationTitle, + spNodeNotificationMessage, + const NotificationDetails( + android: AndroidNotificationDetails( + notificationChannelId, + notificationChannelName, + icon: 'ic_bg_service_small', + ongoing: true, + ), + ), + ); +} + @pragma('vm:entry-point') Future onStart(ServiceInstance service) async { print("BACKGROUND SERVICE STARTED!"); @@ -152,19 +173,19 @@ Future onStart(ServiceInstance service) async { .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(firstWallet.type); - await wallet.connectToNode(node: node); - await wallet.startSync(); - syncingWallets.add(wallet); - } catch (e) { - // couldn't connect to mwebd (most likely) - print("error syncing litecoin wallet: $e"); - } - } + // if (litecoinWallets.isNotEmpty) { + // 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 bitcoinWallets = walletListViewModel.wallets .where((element) => element.type == WalletType.bitcoin) @@ -175,10 +196,18 @@ Future onStart(ServiceInstance service) async { final wallet = await walletLoadingService.load(bitcoinWallets[i].type, bitcoinWallets[i].name); final node = settingsStore.getCurrentNode(bitcoinWallets[i].type); - await wallet.connectToNode(node: node); - await wallet.startSync(); - // TODO: use proxy layer: - await (wallet as ElectrumWallet).rescan(height: 1); + + // 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"); @@ -361,17 +390,17 @@ class BackgroundTasks { final SyncMode syncMode = settingsStore.currentSyncMode; final bool syncAll = settingsStore.currentSyncAll; + if (syncMode.type == SyncType.disabled || !FeatureFlag.isBackgroundSyncEnabled) { + bgService.invoke('stopService'); + return; + } + if (settingsStore.showSyncNotification) { flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation() ?.requestNotificationsPermission(); } - if (syncMode.type == SyncType.disabled || !FeatureFlag.isBackgroundSyncEnabled) { - bgService.invoke('stopService'); - return; - } - bgService.invoke('stopService'); await initializeService(bgService, settingsStore.showSyncNotification); diff --git a/lib/entities/load_current_wallet.dart b/lib/entities/load_current_wallet.dart index b6296621d..8f7962af5 100644 --- a/lib/entities/load_current_wallet.dart +++ b/lib/entities/load_current_wallet.dart @@ -22,5 +22,6 @@ Future loadCurrentWallet({String? password}) async { final wallet = await walletLoadingService.load(type, name, password: password); await appStore.changeCurrentWallet(wallet); // TODO: re-enable (need to figure out how to prevent current wallet from being loaded in the background service!) - // getIt.get().registerBackgroundService(); + // STILL TODO:! + getIt.get().registerBackgroundService(); }