diff --git a/cw_bitcoin/lib/electrum.dart b/cw_bitcoin/lib/electrum.dart index 678c7bc18..afbc199a5 100644 --- a/cw_bitcoin/lib/electrum.dart +++ b/cw_bitcoin/lib/electrum.dart @@ -65,8 +65,9 @@ class ElectrumClient { socket!.listen((Uint8List event) { try { + final msg = utf8.decode(event.toList()); final response = - json.decode(utf8.decode(event.toList())) as Map; + json.decode(msg) as Map; _handleResponse(response); } on FormatException catch (e) { final msg = e.message.toLowerCase(); @@ -136,14 +137,14 @@ class ElectrumClient { return []; }); - Future> getBalance(String scriptHash) => + Future> getBalance(String scriptHash) => call(method: 'blockchain.scripthash.get_balance', params: [scriptHash]) .then((dynamic result) { - if (result is Map) { + if (result is Map) { return result; } - return {}; + return {}; }); Future>> getHistory(String scriptHash) => @@ -151,11 +152,11 @@ class ElectrumClient { .then((dynamic result) { if (result is List) { return result.map((dynamic val) { - if (val is Map) { + if (val is Map) { return val; } - return {}; + return {}; }).toList(); } @@ -170,12 +171,12 @@ class ElectrumClient { .then((dynamic result) { if (result is List) { return result.map((dynamic val) { - if (val is Map) { + if (val is Map) { val['address'] = address; return val; } - return {}; + return {}; }).toList(); } @@ -187,11 +188,11 @@ class ElectrumClient { .then((dynamic result) { if (result is List) { return result.map((dynamic val) { - if (val is Map) { + if (val is Map) { return val; } - return {}; + return {}; }).toList(); } @@ -203,26 +204,26 @@ class ElectrumClient { .then((dynamic result) { if (result is List) { return result.map((dynamic val) { - if (val is Map) { + if (val is Map) { return val; } - return {}; + return {}; }).toList(); } return []; }); - Future> getTransactionRaw( + Future> getTransactionRaw( {required String hash}) async => call(method: 'blockchain.transaction.get', params: [hash, true]) .then((dynamic result) { - if (result is Map) { + if (result is Map) { return result; } - return {}; + return {}; }); Future getTransactionHex( diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 516b44f56..ffe378069 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -455,7 +455,13 @@ abstract class ElectrumWalletBase extends WalletBase electrumClient .getListUnspentWithAddress(address.address, networkType) .then((unspent) => unspent - .map((unspent) => BitcoinUnspent.fromJSON(address, unspent))))); + .map((unspent) { + try { + return BitcoinUnspent.fromJSON(address, unspent); + } catch(_) { + return null; + } + }).whereNotNull()))); unspentCoins = unspent.expand((e) => e).toList(); if (unspentCoinsInfo.isEmpty) { @@ -542,8 +548,9 @@ abstract class ElectrumWalletBase extends WalletBase fetchTransactionInfo( + Future fetchTransactionInfo( {required String hash, required int height}) async { + try { final tx = await getTransactionExpanded(hash: hash, height: height); final addresses = walletAddresses.addresses.map((addr) => addr.address).toSet(); return ElectrumTransactionInfo.fromElectrumBundle( @@ -552,6 +559,9 @@ abstract class ElectrumWalletBase extends WalletBase fetchTransactionInfo( - hash: transaction['tx_hash'] as String, - height: transaction['height'] as int))); - + .map((transaction) { + try { + return fetchTransactionInfo( + hash: transaction['tx_hash'] as String, + height: transaction['height'] as int); + } catch(_) { + return Future.value(null); + } + })); return historiesWithDetails.fold>( {}, (acc, tx) { + if (tx == null) { + return acc; + } acc[tx.id] = acc[tx.id]?.updated(tx) ?? tx; return acc; }); @@ -601,7 +619,8 @@ abstract class ElectrumWalletBase extends WalletBase wallet.syncStatus, (SyncStatus status) async { - if (status is ConnectedSyncStatus) { - await wallet.startSync(); + try { + if (status is ConnectedSyncStatus) { + await wallet.startSync(); - if (wallet.type == WalletType.haven) { - await updateHavenRate(fiatConversionStore); + if (wallet.type == WalletType.haven) { + await updateHavenRate(fiatConversionStore); + } } - } - if (status is SyncingSyncStatus) { - await _wakeLock.enableWake(); - } - if (status is SyncedSyncStatus || status is FailedSyncStatus) { - await _wakeLock.disableWake(); + if (status is SyncingSyncStatus) { + await _wakeLock.enableWake(); + } + if (status is SyncedSyncStatus || status is FailedSyncStatus) { + await _wakeLock.disableWake(); + } + } catch(e) { + print(e.toString()); } }); }