From 78b4e2d6b70d42a7776b9f65372817dadc464f21 Mon Sep 17 00:00:00 2001 From: Tritonn204 Date: Thu, 20 Mar 2025 19:50:01 -0500 Subject: [PATCH 1/3] Added initCompleter for Xelis --- crypto_plugins/flutter_libepiccash | 2 +- lib/wallets/wallet/impl/xelis_wallet.dart | 137 +++++++++++------- .../wallet/intermediate/lib_xelis_wallet.dart | 6 +- 3 files changed, 88 insertions(+), 57 deletions(-) diff --git a/crypto_plugins/flutter_libepiccash b/crypto_plugins/flutter_libepiccash index 8830be2ba..0bb1b1ced 160000 --- a/crypto_plugins/flutter_libepiccash +++ b/crypto_plugins/flutter_libepiccash @@ -1 +1 @@ -Subproject commit 8830be2ba661828d743be12df6f33d560448ed6a +Subproject commit 0bb1b1ced6e0d3c66e383698f89825754c692986 diff --git a/lib/wallets/wallet/impl/xelis_wallet.dart b/lib/wallets/wallet/impl/xelis_wallet.dart index a57f03a02..42c681579 100644 --- a/lib/wallets/wallet/impl/xelis_wallet.dart +++ b/lib/wallets/wallet/impl/xelis_wallet.dart @@ -27,6 +27,8 @@ import '../intermediate/lib_xelis_wallet.dart'; import '../wallet.dart'; class XelisWallet extends LibXelisWallet { + Completer? _initCompleter; + XelisWallet(CryptoCurrencyNetwork network) : super(Xelis(network)); // ==================== Overrides ============================================ @@ -65,6 +67,8 @@ class XelisWallet extends LibXelisWallet { ); libXelisWallet = wallet; + + await _finishInit(); } Future _createNewWallet() async { @@ -97,68 +101,94 @@ class XelisWallet extends LibXelisWallet { ); libXelisWallet = wallet; + + await _finishInit(); + } + + Future _existingWallet() async { + print("EXISTING"); + Logging.instance.i("Xelis: opening existing wallet"); + final tablePath = await getPrecomputedTablesPath(); + final tableState = await getTableState(); + final xelisDir = await StackFileSystem.applicationXelisDirectory(); + final String name = walletId; + final String directory = xelisDir.path; + final password = await secureStorageInterface.read( + key: Wallet.mnemonicPassphraseKey(walletId: info.walletId), + ); + + libXelisWallet = await x_wallet.openXelisWallet( + name: name, + directory: directory, + password: password!, + network: cryptoCurrency.network.xelisNetwork, + precomputedTablesPath: tablePath, + l1Low: tableState.currentSize.isLow, + ); + + await _finishInit(); + } + + Future _finishInit() async { + if (await isTableUpgradeAvailable()) { + unawaited(updateTablesToDesiredSize()); + } + + final newReceivingAddress = + await getCurrentReceivingAddress() ?? + Address( + walletId: walletId, + derivationIndex: 0, + derivationPath: null, + value: libXelisWallet!.getAddressStr(), + publicKey: [], + type: AddressType.xelis, + subType: AddressSubType.receiving, + ); + + await mainDB.updateOrPutAddresses([newReceivingAddress]); + + if (info.cachedReceivingAddress != newReceivingAddress.value) { + await info.updateReceivingAddress( + newAddress: newReceivingAddress.value, + isar: mainDB.isar, + ); + } } @override Future init({bool? isRestore}) async { Logging.instance.d("Xelis: init"); - if (libXelisWallet == null) { - if (isRestore == true) { - await _restoreWallet(); - } else { - final bool walletExists = await LibXelisWallet.checkWalletExists(walletId); - if (!walletExists) { - await _createNewWallet(); - } else { - Logging.instance.i("Xelis: opening existing wallet"); - final tablePath = await getPrecomputedTablesPath(); - final tableState = await getTableState(); - final xelisDir = await StackFileSystem.applicationXelisDirectory(); - final String name = walletId; - final String directory = xelisDir.path; - final password = await secureStorageInterface.read( - key: Wallet.mnemonicPassphraseKey(walletId: info.walletId), - ); - - libXelisWallet = await x_wallet.openXelisWallet( - name: name, - directory: directory, - password: password!, - network: cryptoCurrency.network.xelisNetwork, - precomputedTablesPath: tablePath, - l1Low: tableState.currentSize.isLow, - ); - } - } + if (_initCompleter != null) { + await _initCompleter!.future; + return super.init(); + } - if (await isTableUpgradeAvailable()) { - unawaited(updateTablesToDesiredSize()); - } + _initCompleter = Completer(); - final newReceivingAddress = - await getCurrentReceivingAddress() ?? - Address( - walletId: walletId, - derivationIndex: 0, - derivationPath: null, - value: libXelisWallet!.getAddressStr(), - publicKey: [], - type: AddressType.xelis, - subType: AddressSubType.receiving, - ); - - await mainDB.updateOrPutAddresses([newReceivingAddress]); + try { + final bool walletExists = await LibXelisWallet.checkWalletExists(walletId); - if (info.cachedReceivingAddress != newReceivingAddress.value) { - await info.updateReceivingAddress( - newAddress: newReceivingAddress.value, - isar: mainDB.isar, - ); + if (libXelisWallet == null) { + if (isRestore == true) { + await _restoreWallet(); + } else { + if (!walletExists) { + await _createNewWallet(); + } else { + await _existingWallet(); + } + } } + } catch (e) { + _initCompleter!.completeError(e); + rethrow; + } finally { + _initCompleter!.complete(); } - return await super.init(); + return super.init(); } @override @@ -191,7 +221,9 @@ class XelisWallet extends LibXelisWallet { @override Future pingCheck() async { - checkInitialized(); + if (libXelisWallet == null) { + return false; + } try { await libXelisWallet!.getDaemonInfo(); await handleOnline(); @@ -247,7 +279,7 @@ class XelisWallet extends LibXelisWallet { final Map nodeInfo = (json.decode(infoString) as Map).cast(); - pruningHeight = int.parse(nodeInfo['pruned_topoheight'].toString()); + pruningHeight = int.tryParse(nodeInfo['pruned_topoheight']?.toString() ?? '0') ?? 0; return int.parse(nodeInfo['topoheight'].toString()); } @@ -928,6 +960,7 @@ class XelisWallet extends LibXelisWallet { @override Future refresh({int? topoheight}) async { + if (libXelisWallet == null) { return; } await refreshMutex.protect(() async { try { final bool online = await libXelisWallet!.isOnline(); diff --git a/lib/wallets/wallet/intermediate/lib_xelis_wallet.dart b/lib/wallets/wallet/intermediate/lib_xelis_wallet.dart index e494d68db..ce84718ba 100644 --- a/lib/wallets/wallet/intermediate/lib_xelis_wallet.dart +++ b/lib/wallets/wallet/intermediate/lib_xelis_wallet.dart @@ -32,8 +32,6 @@ enum XelisTableSize { } } -enum XelisWalletOpenType { create, restore } - class XelisTableState { final bool isGenerating; final XelisTableSize currentSize; @@ -330,7 +328,7 @@ abstract class LibXelisWallet } @override - Future open({XelisWalletOpenType? openType}) async { + Future open() async { try { await connect(); } catch (e) { @@ -439,4 +437,4 @@ extension XelisTableManagement on LibXelisWallet { } }); } -} +} \ No newline at end of file From 4431d8c689091059e5cb25cb98de66c39574a62e Mon Sep 17 00:00:00 2001 From: Tritonn204 Date: Thu, 20 Mar 2025 19:55:46 -0500 Subject: [PATCH 2/3] re-enabled null checks for Xelis afer open() --- lib/wallets/wallet/impl/xelis_wallet.dart | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/wallets/wallet/impl/xelis_wallet.dart b/lib/wallets/wallet/impl/xelis_wallet.dart index 42c681579..1d113090a 100644 --- a/lib/wallets/wallet/impl/xelis_wallet.dart +++ b/lib/wallets/wallet/impl/xelis_wallet.dart @@ -221,9 +221,6 @@ class XelisWallet extends LibXelisWallet { @override Future pingCheck() async { - if (libXelisWallet == null) { - return false; - } try { await libXelisWallet!.getDaemonInfo(); await handleOnline(); @@ -960,7 +957,6 @@ class XelisWallet extends LibXelisWallet { @override Future refresh({int? topoheight}) async { - if (libXelisWallet == null) { return; } await refreshMutex.protect(() async { try { final bool online = await libXelisWallet!.isOnline(); From 03f4b2fdea5e394c9411d3065f7ebf841dbfcd8f Mon Sep 17 00:00:00 2001 From: Tritonn204 Date: Thu, 20 Mar 2025 21:22:51 -0500 Subject: [PATCH 3/3] removed init finally{} block for Xelis --- lib/wallets/wallet/impl/xelis_wallet.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/wallets/wallet/impl/xelis_wallet.dart b/lib/wallets/wallet/impl/xelis_wallet.dart index 1d113090a..56b774194 100644 --- a/lib/wallets/wallet/impl/xelis_wallet.dart +++ b/lib/wallets/wallet/impl/xelis_wallet.dart @@ -181,11 +181,10 @@ class XelisWallet extends LibXelisWallet { } } } + _initCompleter!.complete(); } catch (e) { _initCompleter!.completeError(e); rethrow; - } finally { - _initCompleter!.complete(); } return super.init();