From 82be3a52dc3ed9cdae25fbe982e30a2ed7a9ed6c Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 19 May 2022 11:23:40 +0200 Subject: [PATCH] show signature status on transaction tab for loaded transactions when offline --- .../transaction/HeadersController.java | 18 +++--- .../sparrow/transaction/InputController.java | 2 +- .../sparrow/transaction/InputsController.java | 2 +- .../transaction/TransactionController.java | 59 ++++++++++++++++--- 4 files changed, 64 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java index 04b9b3b3..3275b02c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java @@ -1218,16 +1218,18 @@ public class HeadersController extends TransactionFormController implements Init } } - Long feeAmt = calculateFee(event.getInputTransactions()); - if(feeAmt != null) { - updateFee(feeAmt); - } + if(!event.getInputTransactions().isEmpty()) { + Long feeAmt = calculateFee(event.getInputTransactions()); + if(feeAmt != null) { + updateFee(feeAmt); + } - Map allFetchedInputTransactions = new HashMap<>(event.getInputTransactions()); - if(headersForm.getInputTransactions() != null) { - allFetchedInputTransactions.putAll(headersForm.getInputTransactions()); + Map allFetchedInputTransactions = new HashMap<>(event.getInputTransactions()); + if(headersForm.getInputTransactions() != null) { + allFetchedInputTransactions.putAll(headersForm.getInputTransactions()); + } + transactionDiagram.update(getWalletTransaction(allFetchedInputTransactions)); } - transactionDiagram.update(getWalletTransaction(allFetchedInputTransactions)); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java index ba599c3d..3bde5890 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java @@ -513,7 +513,7 @@ public class InputController extends TransactionFormController implements Initia @Subscribe public void blockTransactionFetched(BlockTransactionFetchedEvent event) { - if(event.getTxId().equals(inputForm.getTransaction().getTxId()) && inputForm.getIndex() >= event.getPageStart() && inputForm.getIndex() < event.getPageEnd()) { + if(event.getTxId().equals(inputForm.getTransaction().getTxId()) && !event.getInputTransactions().isEmpty() && inputForm.getIndex() >= event.getPageStart() && inputForm.getIndex() < event.getPageEnd()) { updateOutpoint(event.getInputTransactions()); if(inputForm.getPsbt() == null) { updateSpends(event.getInputTransactions()); diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java index 4703bab3..2e4f31e9 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java @@ -164,7 +164,7 @@ public class InputsController extends TransactionFormController implements Initi @Subscribe public void blockTransactionFetched(BlockTransactionFetchedEvent event) { - if(event.getTxId().equals(inputsForm.getTransaction().getTxId()) && inputsForm.getPsbt() == null) { + if(event.getTxId().equals(inputsForm.getTransaction().getTxId()) && !event.getInputTransactions().isEmpty() && inputsForm.getPsbt() == null) { updateBlockTransactionInputs(event.getInputTransactions()); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/TransactionController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/TransactionController.java index e2825c5f..ab0d9d06 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/TransactionController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/TransactionController.java @@ -6,6 +6,7 @@ import com.sparrowwallet.drongo.psbt.PSBT; import com.sparrowwallet.drongo.psbt.PSBTInput; import com.sparrowwallet.drongo.psbt.PSBTOutput; import com.sparrowwallet.drongo.wallet.BlockTransaction; +import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.TransactionTabData; @@ -30,6 +31,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.URL; import java.util.*; +import java.util.stream.Collectors; public class TransactionController implements Initializable { private static final Logger log = LoggerFactory.getLogger(TransactionController.class); @@ -57,6 +59,8 @@ public class TransactionController implements Initializable { private int selectedInputIndex = -1; private int selectedOutputIndex = -1; + private boolean transactionsFetched; + @Override public void initialize(URL location, ResourceBundle resources) { EventManager.get().register(this); @@ -68,6 +72,16 @@ public class TransactionController implements Initializable { transactionMasterDetail.setShowDetailNode(Config.get().isShowTransactionHex()); txhex.setTransaction(getTransaction()); highlightTxHex(); + fetchTransactions(); + + transactionMasterDetail.sceneProperty().addListener((observable, oldScene, newScene) -> { + if(oldScene == null && newScene != null) { + transactionMasterDetail.setDividerPosition(AppServices.isReducedWindowHeight(transactionMasterDetail) ? 0.9 : 0.82); + } + }); + } + + private void fetchTransactions() { fetchThisAndInputBlockTransactions(0, Math.min(getTransaction().getInputs().size(), PageForm.PAGE_SIZE)); fetchOutputBlockTransactions(0, Math.min(getTransaction().getOutputs().size(), PageForm.PAGE_SIZE)); @@ -76,12 +90,6 @@ public class TransactionController implements Initializable { } else if(TransactionView.OUTPUT.equals(initialView) && initialIndex >= PageForm.PAGE_SIZE) { fetchOutputBlockTransactions(initialIndex, initialIndex + 1); } - - transactionMasterDetail.sceneProperty().addListener((observable, oldScene, newScene) -> { - if(oldScene == null && newScene != null) { - transactionMasterDetail.setDividerPosition(AppServices.isReducedWindowHeight(transactionMasterDetail) ? 0.9 : 0.82); - } - }); } private void initializeTxTree() { @@ -353,6 +361,7 @@ public class TransactionController implements Initializable { ElectrumServer.TransactionReferenceService transactionReferenceService = new ElectrumServer.TransactionReferenceService(references); transactionReferenceService.setOnSucceeded(successEvent -> { + transactionsFetched = true; Map transactionMap = transactionReferenceService.getValue(); BlockTransaction thisBlockTx = null; Map inputTransactions = new HashMap<>(); @@ -387,6 +396,28 @@ public class TransactionController implements Initializable { }); EventManager.get().post(new TransactionReferencesStartedEvent(getTransaction(), indexStart, maxIndex)); transactionReferenceService.start(); + } else if(!AppServices.isConnected()) { + BlockTransaction blockTx = null; + Set inputReferences = getTransaction().getInputs().stream().map(input -> input.getOutpoint().getHash()).collect(Collectors.toSet()); + Map inputTransactions = new HashMap<>(); + + for(Wallet wallet : AppServices.get().getOpenWallets().keySet()) { + Map walletTransactions = wallet.getWalletTransactions(); + if(blockTx == null && walletTransactions.get(getTransaction().getTxId()) != null) { + blockTx = walletTransactions.get(getTransaction().getTxId()); + } + for(Sha256Hash inputReference : inputReferences) { + if(inputTransactions.get(inputReference) == null && walletTransactions.get(inputReference) != null) { + inputTransactions.put(inputReference, walletTransactions.get(inputReference)); + } + } + } + + if(inputTransactions.size() == inputReferences.size()) { + EventManager.get().post(new BlockTransactionFetchedEvent(getTransaction(), blockTx, inputTransactions, 0, getTransaction().getInputs().size())); + } else { + EventManager.get().post(new BlockTransactionFetchedEvent(getTransaction(), blockTx, Collections.emptyMap(), 0, getTransaction().getInputs().size())); + } } } @@ -500,7 +531,7 @@ public class TransactionController implements Initializable { @Subscribe public void blockTransactionFetched(BlockTransactionFetchedEvent event) { - if(event.getTxId().equals(getTransaction().getTxId())) { + if(event.getTxId().equals(getTransaction().getTxId()) && !event.getInputTransactions().isEmpty()) { if(event.getBlockTransaction() != null && (!Sha256Hash.ZERO_HASH.equals(event.getBlockTransaction().getBlockHash()) || txdata.getBlockTransaction() == null)) { txdata.setBlockTransaction(event.getBlockTransaction()); } @@ -547,4 +578,18 @@ public class TransactionController implements Initializable { } } } + + @Subscribe + public void newConnection(ConnectionEvent event) { + if(!transactionsFetched) { + fetchTransactions(); + } + } + + @Subscribe + public void openWallets(OpenWalletsEvent event) { + if(!transactionsFetched) { + fetchTransactions(); + } + } } \ No newline at end of file