From c472ef0e076a45cf4c1f7b3167ba448ed9cc77fd Mon Sep 17 00:00:00 2001
From: Rafael Saes <git@rafael.saes.dev>
Date: Mon, 11 Dec 2023 10:54:07 -0300
Subject: [PATCH] fix: how testnet node gets tx expanded details

---
 cw_bitcoin/lib/electrum.dart        | 12 ++----------
 cw_bitcoin/lib/electrum_wallet.dart | 24 ++++++++++++++----------
 2 files changed, 16 insertions(+), 20 deletions(-)

diff --git a/cw_bitcoin/lib/electrum.dart b/cw_bitcoin/lib/electrum.dart
index f5d546592..b35f3ef21 100644
--- a/cw_bitcoin/lib/electrum.dart
+++ b/cw_bitcoin/lib/electrum.dart
@@ -228,21 +228,13 @@ class ElectrumClient {
         return [];
       });
 
-  Future<dynamic> getTransactionRaw(
-          {required String hash, required NetworkType networkType}) async =>
-      callWithTimeout(
-              method: 'blockchain.transaction.get',
-              params: networkType.bech32 == bitcoin.bech32 ? [hash, true] : [hash],
-              timeout: 10000)
+  Future<Map<String, dynamic>> getTransactionRaw({required String hash}) async =>
+      callWithTimeout(method: 'blockchain.transaction.get', params: [hash, true], timeout: 10000)
           .then((dynamic result) {
         if (result is Map<String, dynamic>) {
           return result;
         }
 
-        if (networkType.bech32 == testnet.bech32 && result is String) {
-          return result;
-        }
-
         return <String, dynamic>{};
       });
 
diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart
index 5538699ac..208219fd1 100644
--- a/cw_bitcoin/lib/electrum_wallet.dart
+++ b/cw_bitcoin/lib/electrum_wallet.dart
@@ -671,9 +671,8 @@ abstract class ElectrumWalletBase
     // Update unspents stored from scanned silent payment transactions
     transactionHistory.transactions.values.forEach((tx) {
       if (tx.unspent != null) {
-        if (!unspentCoins.any((utx) =>
-            utx.hash.contains(tx.unspent!.hash) &&
-            utx.vout == tx.unspent!.vout)) {
+        if (!unspentCoins
+            .any((utx) => utx.hash.contains(tx.unspent!.hash) && utx.vout == tx.unspent!.vout)) {
           unspentCoins.add(tx.unspent!);
         }
       }
@@ -992,19 +991,24 @@ Future<ElectrumTransactionBundle> getTransactionExpanded(
     required int height,
     required ElectrumClient electrumClient,
     required bitcoin.NetworkType networkType}) async {
-  final verboseTransaction =
-      await electrumClient.getTransactionRaw(hash: hash, networkType: networkType);
-
   String transactionHex;
   int? time;
   int confirmations = 0;
-  if (networkType.bech32 == bitcoin.testnet.bech32) {
+  if (networkType.bech32 == bitcoin.bitcoin.bech32) {
+    final verboseTransaction = await electrumClient.getTransactionRaw(hash: hash);
+
     transactionHex = verboseTransaction as String;
-    confirmations = 1;
-  } else {
-    transactionHex = verboseTransaction['hex'] as String;
     time = verboseTransaction['time'] as int?;
     confirmations = verboseTransaction['confirmations'] as int? ?? 0;
+  } else {
+    transactionHex = await electrumClient.getTransactionHex(hash: hash);
+
+    final status = json.decode(
+        (await http.get(Uri.parse("https://blockstream.info/testnet/api/tx/$hash/status"))).body);
+
+    time = status["block_time"] as int?;
+    final tip = await electrumClient.getCurrentBlockChainTip() ?? 0;
+    confirmations = tip - (status["block_height"] as int? ?? 0);
   }
 
   final original = bitcoin.Transaction.fromHex(transactionHex);