From 51259a879c5abeccff1759c8d9b13313547ad815 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Mon, 6 Jan 2025 12:02:49 -0500 Subject: [PATCH] [skip ci] working --- cw_monero/lib/monero_subaddress_list.dart | 37 +++++------ cw_monero/lib/monero_wallet.dart | 75 +++++++++------------- cw_monero/lib/monero_wallet_addresses.dart | 21 +++--- 3 files changed, 62 insertions(+), 71 deletions(-) diff --git a/cw_monero/lib/monero_subaddress_list.dart b/cw_monero/lib/monero_subaddress_list.dart index bc6f223d7..da434b075 100644 --- a/cw_monero/lib/monero_subaddress_list.dart +++ b/cw_monero/lib/monero_subaddress_list.dart @@ -37,8 +37,10 @@ abstract class MoneroSubaddressListBase with Store { subaddresses.clear(); subaddresses.addAll(getAll()); _isUpdating = false; - } catch (e) { + } catch (e, s) { _isUpdating = false; + printV("here"); + printV(s.toString()); rethrow; } } @@ -59,7 +61,7 @@ abstract class MoneroSubaddressListBase with Store { return Subaddress( id: id, address: address, - balance: (s.received/1e12).toStringAsFixed(6), + balance: (s.received / 1e12).toStringAsFixed(6), txCount: s.txCount, label: label); }).toList(); @@ -137,26 +139,25 @@ abstract class MoneroSubaddressListBase with Store { final address = s.address; final label = s.label; return Subaddress( - id: id, - address: address, - balance: (s.received/1e12).toStringAsFixed(6), - txCount: s.txCount, - label: id == 0 && - label.toLowerCase() == 'Primary account'.toLowerCase() - ? 'Primary address' - : label); - }).toList().reversed.toList(); + id: id, + address: address, + balance: (s.received / 1e12).toStringAsFixed(6), + txCount: s.txCount, + label: id == 0 && label.toLowerCase() == 'Primary account'.toLowerCase() + ? 'Primary address' + : label); + }) + .toList() + .reversed + .toList(); } Future _newSubaddress({required int accountIndex, required String label}) async { await subaddress_list.addSubaddress(accountIndex: accountIndex, label: label); - return subaddress_list - .getAllSubaddresses() - .where((s) { - final address = s.address; - return !_usedAddresses.contains(address); - }) - .isNotEmpty; + return subaddress_list.getAllSubaddresses().where((s) { + final address = s.address; + return !_usedAddresses.contains(address); + }).isNotEmpty; } } diff --git a/cw_monero/lib/monero_wallet.dart b/cw_monero/lib/monero_wallet.dart index 91c48fb1b..d42fb8037 100644 --- a/cw_monero/lib/monero_wallet.dart +++ b/cw_monero/lib/monero_wallet.dart @@ -158,10 +158,15 @@ abstract class MoneroWalletBase } } - _autoSaveTimer = - Timer.periodic(Duration(seconds: _autoSaveInterval), (_) async => await save()); - // update transaction details after restore - walletAddresses.subaddressList.update(accountIndex: walletAddresses.account?.id ?? 0); + bool isMainThread = Isolate.current.debugName == "main"; + printV("isMainThread: $isMainThread"); + + if (isMainThread) { + _autoSaveTimer = + Timer.periodic(Duration(seconds: _autoSaveInterval), (_) async => await save()); + // update transaction details after restore + walletAddresses.subaddressList.update(accountIndex: walletAddresses.account?.id ?? 0); + } } @override @@ -198,11 +203,17 @@ abstract class MoneroWalletBase @override Future startSync({bool isBackgroundSync = false}) async { - if (isBackgroundSync) { + bool isMainThread = Isolate.current.debugName == "main"; + printV("isMainThread: $isMainThread"); + printV(Isolate.current.debugName); + + if (!isMainThread) { try { syncStatus = AttemptingSyncStatus(); monero_wallet.startBackgroundSync(); isBackgroundSyncing = true; + _setListeners(); + _listener?.start(); return; } catch (e) { isBackgroundSyncing = false; @@ -212,24 +223,6 @@ abstract class MoneroWalletBase } } - try { - _assertInitialHeight(); - } catch (_) { - // our restore height wasn't correct, so lets see if using the backup works: - try { - await resetCache(name); // Resetting the cache removes the TX Keys and Polyseed - _assertInitialHeight(); - } catch (e) { - // we still couldn't get a valid height from the backup?!: - // try to use the date instead: - try { - _setHeightFromDate(); - } catch (_) { - // we still couldn't get a valid sync height :/ - } - } - } - try { syncStatus = AttemptingSyncStatus(); monero_wallet.startRefresh(); @@ -280,9 +273,9 @@ abstract class MoneroWalletBase bool needExportOutputs(int amount) { // viewOnlyBalance - balance that we can spend // TODO(mrcyjanek): remove hasUnknownKeyImages when we cleanup coin control - return (monero.Wallet_viewOnlyBalance(wptr!, - accountIndex: walletAddresses.account!.id) < amount) || - monero.Wallet_hasUnknownKeyImages(wptr!); + return (monero.Wallet_viewOnlyBalance(wptr!, accountIndex: walletAddresses.account!.id) < + amount) || + monero.Wallet_hasUnknownKeyImages(wptr!); } @override @@ -303,8 +296,8 @@ abstract class MoneroWalletBase final inputs = []; final outputs = _credentials.outputs; final hasMultiDestination = outputs.length > 1; - final unlockedBalance = monero_wallet.getUnlockedBalance( - accountIndex: walletAddresses.account!.id); + final unlockedBalance = + monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account!.id); PendingTransactionDescription pendingTransactionDescription; @@ -350,10 +343,8 @@ abstract class MoneroWalletBase preferredInputs: inputs); } else { final output = outputs.first; - final address = - output.isParsedAddress ? output.extractedAddress : output.address; - final amount = - output.sendAll ? null : output.cryptoAmount!.replaceAll(',', '.'); + final address = output.isParsedAddress ? output.extractedAddress : output.address; + final amount = output.sendAll ? null : output.cryptoAmount!.replaceAll(',', '.'); // if ((formattedAmount != null && unlockedBalance < formattedAmount) || // (formattedAmount == null && unlockedBalance <= 0)) { @@ -363,15 +354,13 @@ abstract class MoneroWalletBase // 'You do not have enough unlocked balance. Unlocked: $formattedBalance. Transaction amount: ${output.cryptoAmount}.'); // } - if (inputs.isEmpty) MoneroTransactionCreationException( - 'No inputs selected'); - pendingTransactionDescription = - await transaction_history.createTransaction( - address: address!, - amount: amount, - priorityRaw: _credentials.priority.serialize(), - accountIndex: walletAddresses.account!.id, - preferredInputs: inputs); + if (inputs.isEmpty) MoneroTransactionCreationException('No inputs selected'); + pendingTransactionDescription = await transaction_history.createTransaction( + address: address!, + amount: amount, + priorityRaw: _credentials.priority.serialize(), + accountIndex: walletAddresses.account!.id, + preferredInputs: inputs); } // final status = monero.PendingTransaction_status(pendingTransactionDescription); @@ -742,8 +731,8 @@ abstract class MoneroWalletBase Future _askForUpdateTransactionHistory() async => await updateTransactions(); - int _getUnlockedBalance() => monero_wallet.getUnlockedBalance( - accountIndex: walletAddresses.account!.id); + int _getUnlockedBalance() => + monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account!.id); int _getFrozenBalance() { var frozenBalance = 0; diff --git a/cw_monero/lib/monero_wallet_addresses.dart b/cw_monero/lib/monero_wallet_addresses.dart index 14b8a2b9b..f891d495f 100644 --- a/cw_monero/lib/monero_wallet_addresses.dart +++ b/cw_monero/lib/monero_wallet_addresses.dart @@ -85,19 +85,20 @@ abstract class MoneroWalletAddressesBase extends WalletAddresses with Store { addressesMap.clear(); addressInfos.clear(); - accountList.accounts.forEach((account) { - _subaddressList.update(accountIndex: account.id); - _subaddressList.subaddresses.forEach((subaddress) { - addressesMap[subaddress.address] = subaddress.label; - addressInfos[account.id] ??= []; - addressInfos[account.id]?.add(AddressInfo( - address: subaddress.address, label: subaddress.label, accountIndex: account.id)); - }); - }); + // accountList.accounts.forEach((account) { + // _subaddressList.update(accountIndex: account.id); + // _subaddressList.subaddresses.forEach((subaddress) { + // addressesMap[subaddress.address] = subaddress.label; + // addressInfos[account.id] ??= []; + // addressInfos[account.id]?.add(AddressInfo( + // address: subaddress.address, label: subaddress.label, accountIndex: account.id)); + // }); + // }); await saveAddressesInBox(); - } catch (e) { + } catch (e, s) { printV(e.toString()); + printV(s.toString()); } }