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..56b774194 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,93 @@ 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(); + } + } } + _initCompleter!.complete(); + } catch (e) { + _initCompleter!.completeError(e); + rethrow; } - return await super.init(); + return super.init(); } @override @@ -191,7 +220,6 @@ class XelisWallet extends LibXelisWallet { @override Future pingCheck() async { - checkInitialized(); try { await libXelisWallet!.getDaemonInfo(); await handleOnline(); @@ -247,7 +275,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()); } 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