From b56925d0d2f435101329368a3000b93e79c50394 Mon Sep 17 00:00:00 2001 From: likho Date: Tue, 6 Feb 2024 16:33:10 +0200 Subject: [PATCH] Resolve error when rescanning a wallet --- lib/electrumx_rpc/electrumx_client.dart | 14 +++++-- .../wallet/impl/bitcoincash_wallet.dart | 37 ++++++++++++------- .../electrumx_interface.dart | 4 +- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/lib/electrumx_rpc/electrumx_client.dart b/lib/electrumx_rpc/electrumx_client.dart index 773eae455..975175cdb 100644 --- a/lib/electrumx_rpc/electrumx_client.dart +++ b/lib/electrumx_rpc/electrumx_client.dart @@ -600,7 +600,6 @@ class ElectrumXClient { scripthash, ], ); - result = response["result"]; retryCount--; } @@ -749,15 +748,22 @@ class ElectrumXClient { return {"rawtx": response["result"] as String}; } - if (response["result"] == null) { + if (response is List) { Logging.instance.log( "getTransaction($txHash) returned null response", level: LogLevel.Error, ); throw 'getTransaction($txHash) returned null response'; + } else { + if (response["result"] == null) { + Logging.instance.log( + "getTransaction($txHash) returned null response", + level: LogLevel.Error, + ); + throw 'getTransaction($txHash) returned null response'; + } + return Map.from(response["result"] as Map); } - - return Map.from(response["result"] as Map); } catch (e) { Logging.instance.log( "getTransaction($txHash) response: $response", diff --git a/lib/wallets/wallet/impl/bitcoincash_wallet.dart b/lib/wallets/wallet/impl/bitcoincash_wallet.dart index d617dc64f..acdfe5af8 100644 --- a/lib/wallets/wallet/impl/bitcoincash_wallet.dart +++ b/lib/wallets/wallet/impl/bitcoincash_wallet.dart @@ -174,22 +174,31 @@ class BitcoincashWallet extends Bip39HDWallet coin: cryptoCurrency.coin, ); - final prevOutJson = Map.from( - (inputTx["vout"] as List).firstWhere((e) => e["n"] == vout) - as Map); + try { + final prevOutJson = Map.from( + (inputTx["vout"] as List).firstWhere((e) => e["n"] == vout) + as Map); + final prevOut = OutputV2.fromElectrumXJson( + prevOutJson, + decimalPlaces: cryptoCurrency.fractionDigits, + walletOwns: false, // doesn't matter here as this is not saved + ); + + outpoint = OutpointV2.isarCantDoRequiredInDefaultConstructor( + txid: txid, + vout: vout, + ); + valueStringSats = prevOut.valueStringSats; + addresses.addAll(prevOut.addresses); + } catch (e, s) { + Logging.instance.log( + "Error getting prevOutJson" + ": $e\n$s", + level: LogLevel.Warning, + ); + } - final prevOut = OutputV2.fromElectrumXJson( - prevOutJson, - decimalPlaces: cryptoCurrency.fractionDigits, - walletOwns: false, // doesn't matter here as this is not saved - ); - outpoint = OutpointV2.isarCantDoRequiredInDefaultConstructor( - txid: txid, - vout: vout, - ); - valueStringSats = prevOut.valueStringSats; - addresses.addAll(prevOut.addresses); } InputV2 input = InputV2.isarCantDoRequiredInDefaultConstructor( diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart index 6c66f5d28..0d15efb7a 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart @@ -1023,7 +1023,9 @@ mixin ElectrumXInterface on Bip39HDWallet { print("123123123"); print(counts); } - int count = counts["${_id}_$k"]!; + + int count = (counts["${_id}_$k"] == null) ? 0 : counts["${_id}_$k"]!; + if (count > 0) { iterationsAddressArray.add(txCountCallArgs["${_id}_$k"]!);