fix: scanning issues

This commit is contained in:
Rafael Saes 2024-02-28 12:48:42 -03:00
parent c5bd0d2dee
commit 0016d436f3

View file

@ -37,7 +37,6 @@ import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart'; import 'package:cw_core/wallet_type.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:hex/hex.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
import 'package:rxdart/subjects.dart'; import 'package:rxdart/subjects.dart';
@ -253,7 +252,7 @@ abstract class ElectrumWalletBase
await _setInitialHeight(); await _setInitialHeight();
} catch (_) {} } catch (_) {}
if ((currentChainTip ?? 0) < walletInfo.restoreHeight) { if ((currentChainTip ?? 0) > walletInfo.restoreHeight) {
_setListeners(walletInfo.restoreHeight, chainTip: currentChainTip); _setListeners(walletInfo.restoreHeight, chainTip: currentChainTip);
} }
} }
@ -278,14 +277,17 @@ abstract class ElectrumWalletBase
} }
@action @action
@override Future<void> _electrumConnect(Node node, {bool? attemptedReconnect}) async {
Future<void> connectToNode({required Node node}) async {
try { try {
syncStatus = ConnectingSyncStatus(); syncStatus = ConnectingSyncStatus();
await electrumClient.connectToUri(node.uri); await electrumClient.connectToUri(node.uri);
electrumClient.onConnectionStatusChange = (bool isConnected) { electrumClient.onConnectionStatusChange = (bool isConnected) async {
if (!isConnected) { if (!isConnected) {
syncStatus = LostConnectionSyncStatus(); syncStatus = LostConnectionSyncStatus();
await electrumClient.close();
if (attemptedReconnect == false) {
await _electrumConnect(node, attemptedReconnect: true);
}
} }
}; };
syncStatus = ConnectedSyncStatus(); syncStatus = ConnectedSyncStatus();
@ -295,6 +297,10 @@ abstract class ElectrumWalletBase
} }
} }
@action
@override
Future<void> connectToNode({required Node node}) => _electrumConnect(node);
Future<EstimatedTxResult> _estimateTxFeeAndInputsToUse( Future<EstimatedTxResult> _estimateTxFeeAndInputsToUse(
int credentialsAmount, int credentialsAmount,
bool sendAll, bool sendAll,
@ -807,14 +813,7 @@ abstract class ElectrumWalletBase
final ins = <BtcTransaction>[]; final ins = <BtcTransaction>[];
for (final vin in original.inputs) { for (final vin in original.inputs) {
try { ins.add(BtcTransaction.fromRaw(await electrumClient.getTransactionHex(hash: vin.txId)));
final id = HEX.encode(HEX.decode(vin.txId).reversed.toList());
final txHex = await electrumClient.getTransactionHex(hash: id);
final tx = BtcTransaction.fromRaw(txHex);
ins.add(tx);
} catch (_) {
ins.add(BtcTransaction.fromRaw(await electrumClient.getTransactionHex(hash: vin.txId)));
}
} }
return ElectrumTransactionBundle(original, return ElectrumTransactionBundle(original,
@ -1065,10 +1064,6 @@ abstract class ElectrumWalletBase
} }
Future<void> _setInitialHeight() async { Future<void> _setInitialHeight() async {
if (walletInfo.isRecovery) {
return;
}
if (walletInfo.restoreHeight == 0) { if (walletInfo.restoreHeight == 0) {
currentChainTip = await electrumClient.getCurrentBlockChainTip(); currentChainTip = await electrumClient.getCurrentBlockChainTip();
if (currentChainTip != null) walletInfo.restoreHeight = currentChainTip!; if (currentChainTip != null) walletInfo.restoreHeight = currentChainTip!;
@ -1191,10 +1186,11 @@ Future<void> startRefresh(ScanData scanData) async {
} }
if (tweaks == null) { if (tweaks == null) {
return scanData.sendPort.send(false); scanData.sendPort.send(SyncResponse(syncHeight,
SyncingSyncStatus.fromHeightValues(currentChainTip, initialSyncHeight, syncHeight)));
} }
for (var i = 0; i < tweaks.length; i++) { for (var i = 0; i < tweaks!.length; i++) {
try { try {
final details = tweaks[i] as Map<String, dynamic>; final details = tweaks[i] as Map<String, dynamic>;
final output_pubkeys = (details["output_pubkeys"] as List<dynamic>); final output_pubkeys = (details["output_pubkeys"] as List<dynamic>);