add coinlabel, addresslabel

This commit is contained in:
Craig Raw
2020-04-10 17:00:09 +02:00
parent 4046555db8
commit e7f30bdfe1
13 changed files with 202 additions and 31 deletions
@@ -0,0 +1,79 @@
package com.sparrowwallet.sparrow.control;
import com.sparrowwallet.drongo.Utils;
import com.sparrowwallet.drongo.address.Address;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tooltip;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
public class AddressLabel extends IdLabel {
private final ObjectProperty<Address> addressProperty = new SimpleObjectProperty<>();
private Tooltip tooltip;
private AddressContextMenu contextMenu;
public AddressLabel() {
this("");
}
public AddressLabel(String text) {
super(text);
addressProperty().addListener((observable, oldValue, newValue) -> {
setAddressAsText(newValue);
contextMenu.copyHex.setText("Copy " + newValue.getOutputScriptDataType());
});
tooltip = new Tooltip();
contextMenu = new AddressContextMenu();
}
public final ObjectProperty<Address> addressProperty() {
return addressProperty;
}
public final Address getAddress() {
return addressProperty.get();
}
public final void setAddress(Address value) {
this.addressProperty.set(value);
}
private void setAddressAsText(Address address) {
if(address != null) {
tooltip.setText(address.getScriptType() + " " + Utils.bytesToHex(address.getOutputScriptData()));
setTooltip(tooltip);
setContextMenu(contextMenu);
setText(address.toString());
}
}
private class AddressContextMenu extends ContextMenu {
private MenuItem copyAddress;
private MenuItem copyHex;
public AddressContextMenu() {
copyAddress = new MenuItem("Copy Address");
copyAddress.setOnAction(AE -> {
hide();
ClipboardContent content = new ClipboardContent();
content.putString(getAddress().toString());
Clipboard.getSystemClipboard().setContent(content);
});
copyHex = new MenuItem("Copy Script Output Bytes");
copyHex.setOnAction(AE -> {
hide();
ClipboardContent content = new ClipboardContent();
content.putString(Utils.bytesToHex(getAddress().getOutputScriptData()));
Clipboard.getSystemClipboard().setContent(content);
});
getItems().addAll(copyAddress, copyHex);
}
}
}
@@ -0,0 +1,88 @@
package com.sparrowwallet.sparrow.control;
import com.sparrowwallet.drongo.protocol.Transaction;
import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tooltip;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class CoinLabel extends CopyableLabel {
public static final int MAX_SATS_SHOWN = 1000000;
private DecimalFormat btcFormat = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
private final LongProperty value = new SimpleLongProperty();
private Tooltip tooltip;
private CoinContextMenu contextMenu;
public CoinLabel() {
this("Unknown");
}
public CoinLabel(String text) {
super(text);
btcFormat.setMaximumFractionDigits(8);
valueProperty().addListener((observable, oldValue, newValue) -> setValueAsText((Long)newValue));
tooltip = new Tooltip();
contextMenu = new CoinContextMenu();
}
public final LongProperty valueProperty() {
return value;
}
public final long getValue() {
return value.get();
}
public final void setValue(long value) {
this.value.set(value);
}
private void setValueAsText(Long value) {
setTooltip(tooltip);
setContextMenu(contextMenu);
String satsValue = String.format(Locale.ENGLISH, "%,d",value) + " sats";
String btcValue = btcFormat.format(value.doubleValue() / Transaction.SATOSHIS_PER_BITCOIN) + " BTC";
if(value > MAX_SATS_SHOWN) {
tooltip.setText(satsValue);
setText(btcValue);
} else {
tooltip.setText(btcValue);
setText(satsValue);
}
}
private class CoinContextMenu extends ContextMenu {
private MenuItem copySatsValue;
private MenuItem copyBtcValue;
public CoinContextMenu() {
copySatsValue = new MenuItem("Copy Value in Satoshis");
copySatsValue.setOnAction(AE -> {
hide();
ClipboardContent content = new ClipboardContent();
content.putString(Long.toString(getValue()));
Clipboard.getSystemClipboard().setContent(content);
});
copyBtcValue = new MenuItem("Copy Value in BTC");
copyBtcValue.setOnAction(AE -> {
hide();
ClipboardContent content = new ClipboardContent();
content.putString(btcFormat.format((double)getValue() / Transaction.SATOSHIS_PER_BITCOIN));
Clipboard.getSystemClipboard().setContent(content);
});
getItems().addAll(copySatsValue, copyBtcValue);
}
}
}
@@ -2,6 +2,7 @@ package com.sparrowwallet.sparrow.transaction;
import com.sparrowwallet.drongo.protocol.Transaction;
import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.control.CoinLabel;
import com.sparrowwallet.sparrow.control.IdLabel;
import com.sparrowwallet.sparrow.control.CopyableLabel;
import com.sparrowwallet.sparrow.event.TransactionChangedEvent;
@@ -71,7 +72,7 @@ public class HeadersController extends TransactionFormController implements Init
private CopyableLabel virtualSize;
@FXML
private CopyableLabel fee;
private CoinLabel fee;
@FXML
private CopyableLabel feeRate;
@@ -178,11 +179,9 @@ public class HeadersController extends TransactionFormController implements Init
}
if(feeAmt != null) {
fee.setText(feeAmt + " sats");
fee.setValue(feeAmt);
double feeRateAmt = feeAmt.doubleValue() / tx.getVirtualSize();
feeRate.setText(String.format("%.2f", feeRateAmt) + " sats/vByte");
} else {
fee.setText("Unknown");
}
}
@@ -6,9 +6,7 @@ import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.protocol.*;
import com.sparrowwallet.drongo.psbt.PSBTInput;
import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.control.IdLabel;
import com.sparrowwallet.sparrow.control.CopyableLabel;
import com.sparrowwallet.sparrow.control.RelativeTimelockSpinner;
import com.sparrowwallet.sparrow.control.*;
import com.sparrowwallet.sparrow.event.TransactionChangedEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@@ -40,13 +38,13 @@ public class InputController extends TransactionFormController implements Initia
private Button outpointSelect;
@FXML
private CopyableLabel spends;
private CoinLabel spends;
@FXML
private CopyableLabel from;
@FXML
private IdLabel address;
private AddressLabel address;
@FXML
private CodeArea scriptSigArea;
@@ -127,7 +125,6 @@ public class InputController extends TransactionFormController implements Initia
inputFieldset.setText("Input #" + txInput.getIndex());
outpoint.setText(txInput.getOutpoint().getHash().toString() + ":" + txInput.getOutpoint().getIndex());
spends.setText("Unknown");
from.setVisible(false);
if(psbtInput != null) {
TransactionOutput output = null;
@@ -138,12 +135,12 @@ public class InputController extends TransactionFormController implements Initia
}
if(output != null) {
spends.setText(output.getValue() + " sats");
spends.setValue(output.getValue());
try {
Address[] addresses = output.getScript().getToAddresses();
from.setVisible(true);
if(addresses.length == 1) {
address.setText(addresses[0].getAddress());
address.setAddress(addresses[0]);
} else {
address.setText("multiple addresses");
}
@@ -2,6 +2,7 @@ package com.sparrowwallet.sparrow.transaction;
import com.sparrowwallet.drongo.protocol.*;
import com.sparrowwallet.drongo.psbt.PSBTInput;
import com.sparrowwallet.sparrow.control.CoinLabel;
import com.sparrowwallet.sparrow.control.CopyableLabel;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@@ -19,7 +20,7 @@ public class InputsController extends TransactionFormController implements Initi
private CopyableLabel count;
@FXML
private CopyableLabel total;
private CoinLabel total;
@FXML
private CopyableLabel signatures;
@@ -41,7 +42,6 @@ public class InputsController extends TransactionFormController implements Initi
Transaction tx = inputsForm.getTransaction();
count.setText(Integer.toString(tx.getInputs().size()));
total.setText("Unknown");
signatures.setText("Unknown");
if(inputsForm.getPsbt() != null) {
@@ -84,7 +84,7 @@ public class InputsController extends TransactionFormController implements Initi
for(TransactionOutput output : outputs) {
totalAmt += output.getValue();
}
total.setText(totalAmt + " sats");
total.setValue(totalAmt);
if(showDenominator) {
signatures.setText(foundSigs + "/" + reqSigs);
} else {
@@ -3,7 +3,8 @@ package com.sparrowwallet.sparrow.transaction;
import com.sparrowwallet.drongo.address.Address;
import com.sparrowwallet.drongo.protocol.NonStandardScriptException;
import com.sparrowwallet.drongo.protocol.TransactionOutput;
import com.sparrowwallet.sparrow.control.IdLabel;
import com.sparrowwallet.sparrow.control.AddressLabel;
import com.sparrowwallet.sparrow.control.CoinLabel;
import com.sparrowwallet.sparrow.control.CopyableLabel;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@@ -20,13 +21,13 @@ public class OutputController extends TransactionFormController implements Initi
private Fieldset outputFieldset;
@FXML
private CopyableLabel value;
private CoinLabel value;
@FXML
private CopyableLabel to;
@FXML
private IdLabel address;
private AddressLabel address;
@FXML
private CodeArea scriptPubKeyArea;
@@ -41,13 +42,13 @@ public class OutputController extends TransactionFormController implements Initi
outputFieldset.setText("Output #" + txOutput.getIndex());
value.setText(txOutput.getValue() + " sats");
value.setValue(txOutput.getValue());
to.setVisible(false);
try {
Address[] addresses = txOutput.getScript().getToAddresses();
to.setVisible(true);
if(addresses.length == 1) {
address.setText(addresses[0].getAddress());
address.setAddress(addresses[0]);
} else {
address.setText("multiple addresses");
}
@@ -2,6 +2,7 @@ package com.sparrowwallet.sparrow.transaction;
import com.sparrowwallet.drongo.protocol.Transaction;
import com.sparrowwallet.drongo.protocol.TransactionOutput;
import com.sparrowwallet.sparrow.control.CoinLabel;
import com.sparrowwallet.sparrow.control.CopyableLabel;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@@ -17,7 +18,7 @@ public class OutputsController extends TransactionFormController implements Init
private CopyableLabel count;
@FXML
private CopyableLabel total;
private CoinLabel total;
@FXML
private PieChart outputsPie;
@@ -40,7 +41,7 @@ public class OutputsController extends TransactionFormController implements Init
for(TransactionOutput output : tx.getOutputs()) {
totalAmt += output.getValue();
}
total.setText(totalAmt + " sats");
total.setValue(totalAmt);
if(totalAmt > 0) {
addPieData(outputsPie, tx.getOutputs());