From 7d3aabb0505646d5efc0ae4886998578427e2b80 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 23 Jul 2020 14:15:22 +0200 Subject: [PATCH] signature count updating on signed keystores list change --- drongo | 2 +- .../event/FinalizeTransactionEvent.java | 4 +- .../transaction/HeadersController.java | 4 +- .../sparrow/transaction/InputController.java | 58 ++++++----- .../sparrow/transaction/InputForm.java | 3 +- .../sparrow/transaction/InputsController.java | 97 +++++++++++-------- 6 files changed, 96 insertions(+), 72 deletions(-) diff --git a/drongo b/drongo index 8bc4e3b3..5cd203d3 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit 8bc4e3b3dcf4c826d148aeafd556ecdbe8673939 +Subproject commit 5cd203d3668d8e720244686433cbd44d614f0343 diff --git a/src/main/java/com/sparrowwallet/sparrow/event/FinalizeTransactionEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/FinalizeTransactionEvent.java index e63a40b0..4aaf9cec 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/FinalizeTransactionEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/FinalizeTransactionEvent.java @@ -3,11 +3,11 @@ package com.sparrowwallet.sparrow.event; import com.sparrowwallet.drongo.psbt.PSBT; import com.sparrowwallet.drongo.wallet.Wallet; -public class FinalizePSBTEvent { +public class FinalizeTransactionEvent { private final PSBT psbt; private final Wallet signingWallet; - public FinalizePSBTEvent(PSBT psbt, Wallet signingWallet) { + public FinalizeTransactionEvent(PSBT psbt, Wallet signingWallet) { this.psbt = psbt; this.signingWallet = signingWallet; } diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java index ef4f53fc..2ddf9657 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java @@ -430,7 +430,7 @@ public class HeadersController extends TransactionFormController implements Init } public void finalizeTransaction(ActionEvent event) { - EventManager.get().post(new FinalizePSBTEvent(headersForm.getPsbt(), signingWallet.getValue())); + EventManager.get().post(new FinalizeTransactionEvent(headersForm.getPsbt(), signingWallet.getValue())); } public void showPSBT(ActionEvent event) { @@ -554,7 +554,7 @@ public class HeadersController extends TransactionFormController implements Init } @Subscribe - public void finalizePSBT(FinalizePSBTEvent event) { + public void finalizeTransaction(FinalizeTransactionEvent event) { if(headersForm.getPsbt() == event.getPsbt()) { version.setDisable(true); locktimeNoneType.setDisable(true); diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java index 63a6ba78..6f769ab7 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java @@ -7,9 +7,11 @@ import com.sparrowwallet.drongo.crypto.ECKey; import com.sparrowwallet.drongo.protocol.*; import com.sparrowwallet.drongo.psbt.PSBTInput; import com.sparrowwallet.drongo.wallet.BlockTransaction; +import com.sparrowwallet.drongo.wallet.Keystore; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.control.*; import com.sparrowwallet.sparrow.event.*; +import javafx.collections.ListChangeListener; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.geometry.Pos; @@ -130,6 +132,12 @@ public class InputController extends TransactionFormController implements Initia initializeScriptFields(txInput, psbtInput); initializeStatusFields(txInput); initializeLocktimeFields(txInput); + + if(psbtInput != null) { + inputForm.getSignedKeystores().addListener((ListChangeListener) c -> { + updateSignatures(); + }); + } } private void initializeInputFields(TransactionInput txInput, PSBTInput psbtInput) { @@ -288,28 +296,7 @@ public class InputController extends TransactionFormController implements Initia private void initializeStatusFields(TransactionInput txInput) { Transaction transaction = inputForm.getTransaction(); - signatures.setText("Unknown"); - if(inputForm.getPsbtInput() != null) { - PSBTInput psbtInput = inputForm.getPsbtInput(); - - int reqSigs = -1; - if(psbtInput.getUtxo() != null && psbtInput.getSigningScript() != null) { - try { - reqSigs = psbtInput.getSigningScript().getNumRequiredSignatures(); - } catch (NonStandardScriptException e) { - //TODO: Handle unusual transaction sig - } - } - - int foundSigs = psbtInput.getPartialSignatures().size(); - if(psbtInput.getFinalScriptWitness() != null) { - foundSigs = psbtInput.getFinalScriptWitness().getSignatures().size(); - } else if(psbtInput.getFinalScriptSig() != null) { - foundSigs = psbtInput.getFinalScriptSig().getSignatures().size(); - } - - signatures.setText(foundSigs + "/" + (reqSigs < 0 ? "?" : reqSigs)); - } + updateSignatures(); rbf.setSelected(txInput.isReplaceByFeeEnabled()); rbf.selectedProperty().addListener((observable, oldValue, newValue) -> { @@ -336,6 +323,31 @@ public class InputController extends TransactionFormController implements Initia rbf.setDisable(!inputForm.isEditable()); } + private void updateSignatures() { + signatures.setText("Unknown"); + if(inputForm.getPsbtInput() != null) { + PSBTInput psbtInput = inputForm.getPsbtInput(); + + int reqSigs = -1; + if(psbtInput.getUtxo() != null && psbtInput.getSigningScript() != null) { + try { + reqSigs = psbtInput.getSigningScript().getNumRequiredSignatures(); + } catch (NonStandardScriptException e) { + //TODO: Handle unusual transaction sig + } + } + + int foundSigs = psbtInput.getPartialSignatures().size(); + if(psbtInput.getFinalScriptWitness() != null) { + foundSigs = psbtInput.getFinalScriptWitness().getSignatures().size(); + } else if(psbtInput.getFinalScriptSig() != null) { + foundSigs = psbtInput.getFinalScriptSig().getSignatures().size(); + } + + signatures.setText(foundSigs + "/" + (reqSigs < 0 ? "?" : reqSigs)); + } + } + private void initializeLocktimeFields(TransactionInput txInput) { Transaction transaction = inputForm.getTransaction(); locktimeToggleGroup.selectedToggleProperty().addListener((ov, old_toggle, new_toggle) -> { @@ -496,7 +508,7 @@ public class InputController extends TransactionFormController implements Initia } @Subscribe - public void finalizePSBT(FinalizePSBTEvent event) { + public void finalizeTransaction(FinalizeTransactionEvent event) { if(inputForm.getPsbt() == event.getPsbt()) { rbf.setDisable(true); locktimeNoneType.setDisable(true); diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/InputForm.java b/src/main/java/com/sparrowwallet/sparrow/transaction/InputForm.java index 605f0523..2844edf0 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/InputForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/InputForm.java @@ -12,7 +12,7 @@ import java.io.IOException; public class InputForm extends IndexedTransactionForm { private final TransactionInput transactionInput; - private PSBTInput psbtInput; + private final PSBTInput psbtInput; public InputForm(TransactionData txdata, PSBTInput psbtInput) { super(txdata, txdata.getPsbt().getPsbtInputs().indexOf(psbtInput)); @@ -23,6 +23,7 @@ public class InputForm extends IndexedTransactionForm { public InputForm(TransactionData txdata, TransactionInput transactionInput) { super(txdata, txdata.getTransaction().getInputs().indexOf(transactionInput)); this.transactionInput = transactionInput; + this.psbtInput = null; } public TransactionInput getTransactionInput() { diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java index 0cfc08cc..0c82281d 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java @@ -2,13 +2,16 @@ package com.sparrowwallet.sparrow.transaction; import com.google.common.eventbus.Subscribe; import com.sparrowwallet.drongo.protocol.*; +import com.sparrowwallet.drongo.psbt.PSBT; import com.sparrowwallet.drongo.psbt.PSBTInput; import com.sparrowwallet.drongo.wallet.BlockTransaction; +import com.sparrowwallet.drongo.wallet.Keystore; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.control.CoinLabel; import com.sparrowwallet.sparrow.control.CopyableLabel; import com.sparrowwallet.sparrow.event.BitcoinUnitChangedEvent; import com.sparrowwallet.sparrow.event.BlockTransactionFetchedEvent; +import javafx.collections.ListChangeListener; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.chart.PieChart; @@ -51,54 +54,62 @@ public class InputsController extends TransactionFormController implements Initi signatures.setText("Unknown"); if(inputsForm.getPsbt() != null) { - int reqSigs = 0; - int foundSigs = 0; - boolean showDenominator = true; - - List outputs = new ArrayList<>(); - for(PSBTInput psbtInput : inputsForm.getPsbt().getPsbtInputs()) { - TransactionOutput output = psbtInput.getUtxo(); - if(output != null) { - outputs.add(output); - } - - if(psbtInput.getUtxo() != null && psbtInput.getSigningScript() != null) { - try { - reqSigs += psbtInput.getSigningScript().getNumRequiredSignatures(); - } catch (NonStandardScriptException e) { - showDenominator = false; - //TODO: Handle unusual transaction sig - } - } else { - showDenominator = false; - } - - if(psbtInput.getFinalScriptWitness() != null) { - foundSigs += psbtInput.getFinalScriptWitness().getSignatures().size(); - } else if(psbtInput.getFinalScriptSig() != null) { - foundSigs += psbtInput.getFinalScriptSig().getSignatures().size(); - } else { - foundSigs += psbtInput.getPartialSignatures().size(); - } - } - - long totalAmt = 0; - for(TransactionOutput output : outputs) { - totalAmt += output.getValue(); - } - total.setValue(totalAmt); - if(showDenominator) { - signatures.setText(foundSigs + "/" + reqSigs); - } else { - signatures.setText(foundSigs + "/?"); - } - - addPieData(inputsPie, outputs); + updatePSBTInputs(inputsForm.getPsbt()); + inputsForm.getSignedKeystores().addListener((ListChangeListener) c -> { + updatePSBTInputs(inputsForm.getPsbt()); + }); } else if(inputsForm.getInputTransactions() != null) { updateBlockTransactionInputs(inputsForm.getInputTransactions()); } } + private void updatePSBTInputs(PSBT psbt) { + int reqSigs = 0; + int foundSigs = 0; + boolean showDenominator = true; + + List outputs = new ArrayList<>(); + for(PSBTInput psbtInput : psbt.getPsbtInputs()) { + TransactionOutput output = psbtInput.getUtxo(); + if(output != null) { + outputs.add(output); + } + + if(psbtInput.getUtxo() != null && psbtInput.getSigningScript() != null) { + try { + reqSigs += psbtInput.getSigningScript().getNumRequiredSignatures(); + } catch (NonStandardScriptException e) { + showDenominator = false; + //TODO: Handle unusual transaction sig + } + } else { + showDenominator = false; + } + + if(psbtInput.getFinalScriptWitness() != null) { + foundSigs += psbtInput.getFinalScriptWitness().getSignatures().size(); + } else if(psbtInput.getFinalScriptSig() != null) { + foundSigs += psbtInput.getFinalScriptSig().getSignatures().size(); + } else { + foundSigs += psbtInput.getPartialSignatures().size(); + } + } + + long totalAmt = 0; + for(TransactionOutput output : outputs) { + totalAmt += output.getValue(); + } + total.setValue(totalAmt); + if(showDenominator) { + signatures.setText(foundSigs + "/" + reqSigs); + } else { + signatures.setText(foundSigs + "/?"); + } + + + addPieData(inputsPie, outputs); + } + private void updateBlockTransactionInputs(Map inputTransactions) { List outputs = new ArrayList<>();