diff --git a/cw_monero/lib/api/wallet.dart b/cw_monero/lib/api/wallet.dart index 1c7e8c684..813d63645 100644 --- a/cw_monero/lib/api/wallet.dart +++ b/cw_monero/lib/api/wallet.dart @@ -80,11 +80,18 @@ String getSeedLegacy(String? language) { Map>> addressCache = {}; String getAddress({int accountIndex = 0, int addressIndex = 0}) { - // printV("getaddress: ${accountIndex}/${addressIndex}: ${monero.Wallet_numSubaddresses(wptr!, accountIndex: accountIndex)}: ${monero.Wallet_address(wptr!, accountIndex: accountIndex, addressIndex: addressIndex)}"); + + int count = 0; + while (monero.Wallet_numSubaddresses(wptr!, accountIndex: accountIndex) - 1 < addressIndex) { printV("adding subaddress"); monero.Wallet_addSubaddress(wptr!, accountIndex: accountIndex); + if (count > 10) { + throw Exception("Failed to add subaddress"); + } + count++; } + addressCache[wptr!.address] ??= {}; addressCache[wptr!.address]![accountIndex] ??= {}; addressCache[wptr!.address]![accountIndex]![addressIndex] ??= @@ -167,6 +174,8 @@ void setupBackgroundSync( backgroundCachePassword: backgroundCachePassword); } +bool isBackgroundSyncing() => monero.Wallet_isBackgroundSyncing(wptr!); + void startBackgroundSync() { monero.Wallet_startBackgroundSync(wptr!); } @@ -175,6 +184,10 @@ void stopBackgroundSync(String walletPassword) { monero.Wallet_stopBackgroundSync(wptr!, walletPassword); } +void stopSync() { + monero.Wallet_init(wptr!, daemonAddress: ""); +} + void setRefreshFromBlockHeight({required int height}) => monero.Wallet_setRefreshFromBlockHeight(wptr!, refresh_from_block_height: height); diff --git a/cw_monero/lib/monero_wallet.dart b/cw_monero/lib/monero_wallet.dart index f08f6b0c8..f50a121e6 100644 --- a/cw_monero/lib/monero_wallet.dart +++ b/cw_monero/lib/monero_wallet.dart @@ -138,6 +138,7 @@ abstract class MoneroWalletBase Timer? _autoSaveTimer; List unspentCoins; String _password; + bool isBackgroundSyncing = false; Future init() async { await walletAddresses.init(); @@ -202,8 +203,10 @@ abstract class MoneroWalletBase try { syncStatus = AttemptingSyncStatus(); monero_wallet.startBackgroundSync(); + isBackgroundSyncing = true; return; } catch (e) { + isBackgroundSyncing = false; syncStatus = FailedSyncStatus(); printV(e); rethrow; @@ -280,11 +283,11 @@ abstract class MoneroWalletBase syncStatus = NotConnectedSyncStatus(); _listener?.stop(); if (isBackgroundSync) { + isBackgroundSyncing = false; monero_wallet.stopBackgroundSync(password); return; } - // TODO: find a better way to stop syncing than setting an invalid address: - monero_wallet.setupNode(address: ""); + monero_wallet.stopSync(); } @override @@ -397,7 +400,7 @@ abstract class MoneroWalletBase Future save() async { await walletAddresses.updateUsedSubaddress(); - if (isEnabledAutoGenerateSubaddress) { + if (isEnabledAutoGenerateSubaddress && !isBackgroundSyncing) { walletAddresses.updateUnusedSubaddress( accountIndex: walletAddresses.account?.id ?? 0, defaultLabel: walletAddresses.account?.label ?? ''); diff --git a/lib/entities/background_tasks.dart b/lib/entities/background_tasks.dart index af2673bb1..c81cbf8f0 100644 --- a/lib/entities/background_tasks.dart +++ b/lib/entities/background_tasks.dart @@ -209,8 +209,7 @@ Future onStart(ServiceInstance service) async { 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.stopSync(isBackgroundSync: true); + await wallet.stopSync(isBackgroundSync: false);// stop regular sync process if it's been started syncingWallets.add(wallet); }