This commit is contained in:
Matthew Fosse 2024-09-16 13:46:48 -07:00
parent e51d6ea8c0
commit 35c4f3e2f1
3 changed files with 61 additions and 26 deletions

View file

@ -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);

View file

@ -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<void> onStart(ServiceInstance service) async {
print("BACKGROUND SERVICE STARTED!");
@ -152,19 +173,19 @@ Future<void> 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<WalletListItem> bitcoinWallets = walletListViewModel.wallets
.where((element) => element.type == WalletType.bitcoin)
@ -175,10 +196,18 @@ Future<void> 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<AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
}
if (syncMode.type == SyncType.disabled || !FeatureFlag.isBackgroundSyncEnabled) {
bgService.invoke('stopService');
return;
}
bgService.invoke('stopService');
await initializeService(bgService, settingsStore.showSyncNotification);

View file

@ -22,5 +22,6 @@ Future<void> 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<BackgroundTasks>().registerBackgroundService();
// STILL TODO:!
getIt.get<BackgroundTasks>().registerBackgroundService();
}