mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2026-08-11 00:47:41 +00:00
receive pane
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
package com.sparrowwallet.sparrow;
|
||||
|
||||
import com.sparrowwallet.drongo.protocol.Script;
|
||||
import com.sparrowwallet.drongo.protocol.ScriptChunk;
|
||||
import com.sparrowwallet.sparrow.transaction.ScriptContextMenu;
|
||||
import javafx.geometry.Point2D;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.stage.Popup;
|
||||
import org.fxmisc.richtext.CodeArea;
|
||||
import org.fxmisc.richtext.event.MouseOverTextEvent;
|
||||
import org.fxmisc.richtext.model.TwoDimensional;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import static com.sparrowwallet.drongo.protocol.ScriptType.*;
|
||||
import static com.sparrowwallet.drongo.protocol.ScriptType.P2WSH;
|
||||
import static org.fxmisc.richtext.model.TwoDimensional.Bias.Backward;
|
||||
|
||||
public abstract class BaseController {
|
||||
protected void appendScript(CodeArea codeArea, Script script) {
|
||||
appendScript(codeArea, script, null, null);
|
||||
}
|
||||
|
||||
protected void appendScript(CodeArea codeArea, Script script, Script redeemScript, Script witnessScript) {
|
||||
if(P2PKH.isScriptType(script)) {
|
||||
codeArea.append(script.getChunks().get(0).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append(script.getChunks().get(1).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append("<pkh>", "script-hash");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append(script.getChunks().get(3).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append(script.getChunks().get(4).toString(), "script-opcode");
|
||||
} else if(P2SH.isScriptType(script)) {
|
||||
codeArea.append(script.getChunks().get(0).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append("<sh>", "script-hash");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append(script.getChunks().get(2).toString(), "script-opcode");
|
||||
} else if(P2WPKH.isScriptType(script)) {
|
||||
codeArea.append(script.getChunks().get(0).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append("<wpkh>", "script-hash");
|
||||
} else if(P2WSH.isScriptType(script)) {
|
||||
codeArea.append(script.getChunks().get(0).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append("<wsh>", "script-hash");
|
||||
} else {
|
||||
int signatureCount = 1;
|
||||
int pubKeyCount = 1;
|
||||
for (int i = 0; i < script.getChunks().size(); i++) {
|
||||
ScriptChunk chunk = script.getChunks().get(i);
|
||||
if(chunk.isOpCode()) {
|
||||
codeArea.append(chunk.toString(), "script-opcode");
|
||||
} else if(chunk.isSignature()) {
|
||||
codeArea.append("<signature" + signatureCount++ + ">", "script-signature");
|
||||
} else if(chunk.isPubKey()) {
|
||||
codeArea.append("<pubkey" + pubKeyCount++ + ">", "script-pubkey");
|
||||
} else if(chunk.isScript()) {
|
||||
Script nestedScript = chunk.getScript();
|
||||
if (nestedScript.equals(redeemScript)) {
|
||||
codeArea.append("<RedeemScript>", "script-redeem");
|
||||
} else if (nestedScript.equals(witnessScript)) {
|
||||
codeArea.append("<WitnessScript>", "script-redeem");
|
||||
} else {
|
||||
codeArea.append("(", "script-nest");
|
||||
appendScript(codeArea, nestedScript);
|
||||
codeArea.append(")", "script-nest");
|
||||
}
|
||||
} else {
|
||||
codeArea.append(chunk.toString(), "script-other");
|
||||
}
|
||||
|
||||
if(i < script.getChunks().size() - 1) {
|
||||
codeArea.append(" ", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addScriptPopup(codeArea, script);
|
||||
}
|
||||
|
||||
protected void addScriptPopup(CodeArea area, Script script) {
|
||||
ScriptContextMenu contextMenu = new ScriptContextMenu(area, script);
|
||||
area.setContextMenu(contextMenu);
|
||||
|
||||
Popup popup = new Popup();
|
||||
Label popupMsg = new Label();
|
||||
popupMsg.getStyleClass().add("tooltip");
|
||||
popup.getContent().add(popupMsg);
|
||||
|
||||
area.setMouseOverTextDelay(Duration.ofMillis(150));
|
||||
area.addEventHandler(MouseOverTextEvent.MOUSE_OVER_TEXT_BEGIN, e -> {
|
||||
TwoDimensional.Position position = area.getParagraph(0).getStyleSpans().offsetToPosition(e.getCharacterIndex(), Backward);
|
||||
if(position.getMajor() % 2 == 0) {
|
||||
ScriptChunk hoverChunk = script.getChunks().get(position.getMajor()/2);
|
||||
if(!hoverChunk.isOpCode()) {
|
||||
Point2D pos = e.getScreenPosition();
|
||||
popupMsg.setText(describeScriptChunk(hoverChunk));
|
||||
popup.show(area, pos.getX(), pos.getY() + 10);
|
||||
}
|
||||
}
|
||||
});
|
||||
area.addEventHandler(MouseOverTextEvent.MOUSE_OVER_TEXT_END, e -> {
|
||||
popup.hide();
|
||||
});
|
||||
}
|
||||
|
||||
protected String describeScriptChunk(ScriptChunk chunk) {
|
||||
return chunk.toString();
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,11 @@ package com.sparrowwallet.sparrow.control;
|
||||
import com.sparrowwallet.drongo.Utils;
|
||||
import com.sparrowwallet.drongo.address.Address;
|
||||
import com.sparrowwallet.drongo.protocol.Transaction;
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.sparrow.EventManager;
|
||||
import com.sparrowwallet.sparrow.event.ReceiveActionEvent;
|
||||
import com.sparrowwallet.sparrow.wallet.Entry;
|
||||
import com.sparrowwallet.sparrow.wallet.NodeEntry;
|
||||
import javafx.beans.property.ReadOnlyObjectWrapper;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.beans.property.StringProperty;
|
||||
import javafx.event.Event;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.*;
|
||||
@@ -24,26 +23,24 @@ import org.controlsfx.glyphfont.Glyph;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Locale;
|
||||
|
||||
public class AddressTreeTable extends TreeTableView<AddressTreeTable.Data> {
|
||||
public void initialize(Wallet.Node rootNode) {
|
||||
public class AddressTreeTable extends TreeTableView<Entry> {
|
||||
public void initialize(NodeEntry rootEntry) {
|
||||
getStyleClass().add("address-treetable");
|
||||
|
||||
String address = null;
|
||||
Data rootData = new Data(rootNode);
|
||||
TreeItem<Data> rootItem = new TreeItem<>(rootData);
|
||||
for(Wallet.Node childNode : rootNode.getChildren()) {
|
||||
Data childData = new Data(childNode);
|
||||
TreeItem<Data> childItem = new TreeItem<>(childData);
|
||||
TreeItem<Entry> rootItem = new TreeItem<>(rootEntry);
|
||||
for(Entry childEntry : rootEntry.getChildren()) {
|
||||
TreeItem<Entry> childItem = new TreeItem<>(childEntry);
|
||||
rootItem.getChildren().add(childItem);
|
||||
address = childNode.getAddress().toString();
|
||||
address = rootEntry.getNode().getAddress().toString();
|
||||
}
|
||||
|
||||
rootItem.setExpanded(true);
|
||||
setRoot(rootItem);
|
||||
setShowRoot(false);
|
||||
|
||||
TreeTableColumn<Data, Data> addressCol = new TreeTableColumn<>("Address / Outpoints");
|
||||
addressCol.setCellValueFactory((TreeTableColumn.CellDataFeatures<Data, Data> param) -> {
|
||||
TreeTableColumn<Entry, Entry> addressCol = new TreeTableColumn<>("Address / Outpoints");
|
||||
addressCol.setCellValueFactory((TreeTableColumn.CellDataFeatures<Entry, Entry> param) -> {
|
||||
return new ReadOnlyObjectWrapper<>(param.getValue().getValue());
|
||||
});
|
||||
addressCol.setCellFactory(p -> new DataCell());
|
||||
@@ -54,23 +51,26 @@ public class AddressTreeTable extends TreeTableView<AddressTreeTable.Data> {
|
||||
addressCol.setMinWidth(TextUtils.computeTextWidth(Font.font("Courier"), address, 0.0));
|
||||
}
|
||||
|
||||
TreeTableColumn<Data, String> labelCol = new TreeTableColumn<>("Label");
|
||||
labelCol.setCellValueFactory((TreeTableColumn.CellDataFeatures<Data, String> param) -> {
|
||||
return param.getValue().getValue().getLabelProperty();
|
||||
TreeTableColumn<Entry, String> labelCol = new TreeTableColumn<>("Label");
|
||||
labelCol.setCellValueFactory((TreeTableColumn.CellDataFeatures<Entry, String> param) -> {
|
||||
return param.getValue().getValue().labelProperty();
|
||||
});
|
||||
labelCol.setCellFactory(p -> new LabelCell());
|
||||
labelCol.setSortable(false);
|
||||
getColumns().add(labelCol);
|
||||
|
||||
TreeTableColumn<Data, Long> amountCol = new TreeTableColumn<>("Amount");
|
||||
amountCol.setCellValueFactory((TreeTableColumn.CellDataFeatures<Data, Long> param) -> {
|
||||
TreeTableColumn<Entry, Long> amountCol = new TreeTableColumn<>("Amount");
|
||||
amountCol.setCellValueFactory((TreeTableColumn.CellDataFeatures<Entry, Long> param) -> {
|
||||
return new ReadOnlyObjectWrapper<>(param.getValue().getValue().getAmount());
|
||||
});
|
||||
amountCol.setCellFactory(p -> new AmountCell());
|
||||
amountCol.setSortable(false);
|
||||
getColumns().add(amountCol);
|
||||
|
||||
TreeTableColumn<Data, Void> actionCol = new TreeTableColumn<>("Actions");
|
||||
TreeTableColumn<Entry, Entry> actionCol = new TreeTableColumn<>("Actions");
|
||||
actionCol.setCellValueFactory((TreeTableColumn.CellDataFeatures<Entry, Entry> param) -> {
|
||||
return new ReadOnlyObjectWrapper<>(param.getValue().getValue());
|
||||
});
|
||||
actionCol.setCellFactory(p -> new ActionCell());
|
||||
actionCol.setSortable(false);
|
||||
getColumns().add(actionCol);
|
||||
@@ -79,53 +79,23 @@ public class AddressTreeTable extends TreeTableView<AddressTreeTable.Data> {
|
||||
setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);
|
||||
}
|
||||
|
||||
public static class Data {
|
||||
private final Wallet.Node walletNode;
|
||||
private final SimpleStringProperty labelProperty;
|
||||
private final Long amount;
|
||||
|
||||
public Data(Wallet.Node walletNode) {
|
||||
this.walletNode = walletNode;
|
||||
|
||||
labelProperty = new SimpleStringProperty(walletNode.getLabel());
|
||||
labelProperty.addListener((observable, oldValue, newValue) -> walletNode.setLabel(newValue));
|
||||
|
||||
amount = walletNode.getAmount();
|
||||
}
|
||||
|
||||
public Wallet.Node getWalletNode() {
|
||||
return walletNode;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return labelProperty.get();
|
||||
}
|
||||
|
||||
public StringProperty getLabelProperty() {
|
||||
return labelProperty;
|
||||
}
|
||||
|
||||
public Long getAmount() {
|
||||
return amount;
|
||||
}
|
||||
}
|
||||
|
||||
private static class DataCell extends TreeTableCell<Data, Data> {
|
||||
private static class DataCell extends TreeTableCell<Entry, Entry> {
|
||||
public DataCell() {
|
||||
super();
|
||||
getStyleClass().add("data-cell");
|
||||
getStyleClass().add("address-cell");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateItem(Data data, boolean empty) {
|
||||
super.updateItem(data, empty);
|
||||
protected void updateItem(Entry entry, boolean empty) {
|
||||
super.updateItem(entry, empty);
|
||||
|
||||
if(empty) {
|
||||
setText(null);
|
||||
setGraphic(null);
|
||||
} else {
|
||||
if(data.getWalletNode() != null) {
|
||||
Address address = data.getWalletNode().getAddress();
|
||||
if(entry instanceof NodeEntry) {
|
||||
NodeEntry nodeEntry = (NodeEntry)entry;
|
||||
Address address = nodeEntry.getNode().getAddress();
|
||||
setText(address.toString());
|
||||
setContextMenu(new AddressContextMenu(address));
|
||||
} else {
|
||||
@@ -157,7 +127,7 @@ public class AddressTreeTable extends TreeTableView<AddressTreeTable.Data> {
|
||||
}
|
||||
}
|
||||
|
||||
private static class LabelCell extends TextFieldTreeTableCell<Data, String> {
|
||||
private static class LabelCell extends TextFieldTreeTableCell<Entry, String> {
|
||||
public LabelCell() {
|
||||
super(new DefaultStringConverter());
|
||||
getStyleClass().add("label-cell");
|
||||
@@ -183,10 +153,10 @@ public class AddressTreeTable extends TreeTableView<AddressTreeTable.Data> {
|
||||
// intercept the loss of focus. The default commitEdit(...) method
|
||||
// simply bails if we are not editing...
|
||||
if (!isEditing() && !label.equals(getItem())) {
|
||||
TreeTableView<Data> treeTable = getTreeTableView();
|
||||
TreeTableView<Entry> treeTable = getTreeTableView();
|
||||
if(treeTable != null) {
|
||||
TreeTableColumn<Data, String> column = getTableColumn();
|
||||
TreeTableColumn.CellEditEvent<Data, String> event = new TreeTableColumn.CellEditEvent<>(
|
||||
TreeTableColumn<Entry, String> column = getTableColumn();
|
||||
TreeTableColumn.CellEditEvent<Entry, String> event = new TreeTableColumn.CellEditEvent<>(
|
||||
treeTable, new TreeTablePosition<>(treeTable, getIndex(), column),
|
||||
TreeTableColumn.editCommitEvent(), label
|
||||
);
|
||||
@@ -231,7 +201,7 @@ public class AddressTreeTable extends TreeTableView<AddressTreeTable.Data> {
|
||||
}
|
||||
}
|
||||
|
||||
private static class AmountCell extends TreeTableCell<Data, Long> {
|
||||
private static class AmountCell extends TreeTableCell<Entry, Long> {
|
||||
public AmountCell() {
|
||||
super();
|
||||
getStyleClass().add("amount-cell");
|
||||
@@ -260,8 +230,9 @@ public class AddressTreeTable extends TreeTableView<AddressTreeTable.Data> {
|
||||
}
|
||||
}
|
||||
|
||||
private static class ActionCell extends TreeTableCell<Data, Void> {
|
||||
private static class ActionCell extends TreeTableCell<Entry, Entry> {
|
||||
private final HBox actionBox;
|
||||
private final Button receiveButton;
|
||||
|
||||
public ActionCell() {
|
||||
super();
|
||||
@@ -271,23 +242,27 @@ public class AddressTreeTable extends TreeTableView<AddressTreeTable.Data> {
|
||||
actionBox.setSpacing(8);
|
||||
actionBox.setAlignment(Pos.CENTER);
|
||||
|
||||
Button receiveButton = new Button("");
|
||||
receiveButton = new Button("");
|
||||
Glyph receiveGlyph = new Glyph("FontAwesome", FontAwesome.Glyph.ARROW_DOWN);
|
||||
receiveGlyph.setFontSize(12);
|
||||
receiveButton.setGraphic(receiveGlyph);
|
||||
receiveButton.setOnAction(event -> {
|
||||
EventManager.get().post(new ReceiveActionEvent(getTreeTableView().getTreeItem(getIndex()).getValue().getWalletNode()));
|
||||
NodeEntry nodeEntry = (NodeEntry)getTreeTableView().getTreeItem(getIndex()).getValue();
|
||||
EventManager.get().post(new ReceiveActionEvent(nodeEntry));
|
||||
});
|
||||
|
||||
actionBox.getChildren().add(receiveButton);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateItem(Void item, boolean empty) {
|
||||
super.updateItem(item, empty);
|
||||
protected void updateItem(Entry entry, boolean empty) {
|
||||
super.updateItem(entry, empty);
|
||||
if (empty) {
|
||||
setGraphic(null);
|
||||
} else {
|
||||
actionBox.getChildren().remove(0, actionBox.getChildren().size());
|
||||
if(entry instanceof NodeEntry) {
|
||||
actionBox.getChildren().add(receiveButton);
|
||||
}
|
||||
|
||||
setGraphic(actionBox);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.sparrowwallet.sparrow.control;
|
||||
|
||||
import javafx.animation.FadeTransition;
|
||||
import javafx.beans.InvalidationListener;
|
||||
import javafx.beans.Observable;
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.scene.Cursor;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.input.Clipboard;
|
||||
import javafx.scene.input.ClipboardContent;
|
||||
import javafx.scene.layout.Region;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.util.Duration;
|
||||
import org.controlsfx.control.textfield.CustomTextField;
|
||||
|
||||
public class CopyableTextField extends CustomTextField {
|
||||
private static final Duration FADE_DURATION = Duration.millis(350);
|
||||
|
||||
public CopyableTextField() {
|
||||
super();
|
||||
getStyleClass().add("copyable-text-field");
|
||||
setupCopyButtonField(super.rightProperty());
|
||||
}
|
||||
|
||||
private void setupCopyButtonField(ObjectProperty<Node> rightProperty) {
|
||||
Region copyButton = new Region();
|
||||
copyButton.getStyleClass().addAll("graphic"); //$NON-NLS-1$
|
||||
StackPane copyButtonPane = new StackPane(copyButton);
|
||||
copyButtonPane.getStyleClass().addAll("copy-button"); //$NON-NLS-1$
|
||||
copyButtonPane.setOpacity(0.0);
|
||||
copyButtonPane.setCursor(Cursor.DEFAULT);
|
||||
copyButtonPane.setOnMouseReleased(e -> {
|
||||
ClipboardContent content = new ClipboardContent();
|
||||
content.putString(getText());
|
||||
Clipboard.getSystemClipboard().setContent(content);
|
||||
});
|
||||
|
||||
rightProperty.set(copyButtonPane);
|
||||
|
||||
final FadeTransition fader = new FadeTransition(FADE_DURATION, copyButtonPane);
|
||||
fader.setCycleCount(1);
|
||||
|
||||
textProperty().addListener(new InvalidationListener() {
|
||||
@Override
|
||||
public void invalidated(Observable arg0) {
|
||||
String text = getText();
|
||||
boolean isTextEmpty = text == null || text.isEmpty();
|
||||
boolean isButtonVisible = fader.getNode().getOpacity() > 0;
|
||||
|
||||
if (isTextEmpty && isButtonVisible) {
|
||||
setButtonVisible(false);
|
||||
} else if (!isTextEmpty && !isButtonVisible) {
|
||||
setButtonVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void setButtonVisible( boolean visible ) {
|
||||
fader.setFromValue(visible? 0.0: 1.0);
|
||||
fader.setToValue(visible? 1.0: 0.0);
|
||||
fader.play();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
package com.sparrowwallet.sparrow.event;
|
||||
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.sparrow.wallet.NodeEntry;
|
||||
|
||||
public class ReceiveActionEvent {
|
||||
private Wallet.Node receiveNode;
|
||||
private NodeEntry receiveEntry;
|
||||
|
||||
public ReceiveActionEvent(Wallet.Node receiveNode) {
|
||||
this.receiveNode = receiveNode;
|
||||
public ReceiveActionEvent(NodeEntry receiveEntry) {
|
||||
this.receiveEntry = receiveEntry;
|
||||
}
|
||||
|
||||
public Wallet.Node getReceiveNode() {
|
||||
return receiveNode;
|
||||
public NodeEntry getReceiveEntry() {
|
||||
return receiveEntry;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import com.sparrowwallet.drongo.wallet.WalletModel;
|
||||
import javafx.concurrent.ScheduledService;
|
||||
import javafx.concurrent.Service;
|
||||
import javafx.concurrent.Task;
|
||||
import org.apache.commons.compress.compressors.lz4.FramedLZ4CompressorInputStream;
|
||||
import org.controlsfx.tools.Platform;
|
||||
|
||||
import java.io.*;
|
||||
@@ -20,6 +19,7 @@ import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
@@ -130,7 +130,7 @@ public class Hwi {
|
||||
File tempExec = tempExecPath.toFile();
|
||||
//tempExec.deleteOnExit();
|
||||
OutputStream tempExecStream = new BufferedOutputStream(new FileOutputStream(tempExec));
|
||||
ByteStreams.copy(new FramedLZ4CompressorInputStream(inputStream), tempExecStream);
|
||||
ByteStreams.copy(new GZIPInputStream(inputStream), tempExecStream);
|
||||
inputStream.close();
|
||||
tempExecStream.flush();
|
||||
tempExecStream.close();
|
||||
|
||||
+4
-106
@@ -1,25 +1,17 @@
|
||||
package com.sparrowwallet.sparrow.transaction;
|
||||
|
||||
import com.sparrowwallet.drongo.address.Address;
|
||||
import com.sparrowwallet.drongo.protocol.*;
|
||||
import com.sparrowwallet.drongo.protocol.NonStandardScriptException;
|
||||
import com.sparrowwallet.drongo.protocol.TransactionOutput;
|
||||
import com.sparrowwallet.sparrow.BaseController;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.geometry.Point2D;
|
||||
import javafx.scene.chart.PieChart;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.Tooltip;
|
||||
import javafx.stage.Popup;
|
||||
import org.fxmisc.richtext.CodeArea;
|
||||
import org.fxmisc.richtext.event.MouseOverTextEvent;
|
||||
import org.fxmisc.richtext.model.TwoDimensional;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
import static org.fxmisc.richtext.model.TwoDimensional.Bias.Backward;
|
||||
import static com.sparrowwallet.drongo.protocol.ScriptType.*;
|
||||
|
||||
public abstract class TransactionFormController {
|
||||
public abstract class TransactionFormController extends BaseController {
|
||||
protected void addPieData(PieChart pie, List<TransactionOutput> outputs) {
|
||||
ObservableList<PieChart.Data> outputsPieData = FXCollections.observableArrayList();
|
||||
|
||||
@@ -52,98 +44,4 @@ public abstract class TransactionFormController {
|
||||
data.pieValueProperty().addListener((observable, oldValue, newValue) -> tooltip.setText(newValue + "%"));
|
||||
});
|
||||
}
|
||||
|
||||
protected void appendScript(CodeArea codeArea, Script script) {
|
||||
appendScript(codeArea, script, null, null);
|
||||
}
|
||||
|
||||
protected void appendScript(CodeArea codeArea, Script script, Script redeemScript, Script witnessScript) {
|
||||
if(P2PKH.isScriptType(script)) {
|
||||
codeArea.append(script.getChunks().get(0).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append(script.getChunks().get(1).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append("<pkh>", "script-hash");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append(script.getChunks().get(3).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append(script.getChunks().get(4).toString(), "script-opcode");
|
||||
} else if(P2SH.isScriptType(script)) {
|
||||
codeArea.append(script.getChunks().get(0).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append("<sh>", "script-hash");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append(script.getChunks().get(2).toString(), "script-opcode");
|
||||
} else if(P2WPKH.isScriptType(script)) {
|
||||
codeArea.append(script.getChunks().get(0).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append("<wpkh>", "script-hash");
|
||||
} else if(P2WSH.isScriptType(script)) {
|
||||
codeArea.append(script.getChunks().get(0).toString(), "script-opcode");
|
||||
codeArea.append(" ", "");
|
||||
codeArea.append("<wsh>", "script-hash");
|
||||
} else {
|
||||
int signatureCount = 1;
|
||||
int pubKeyCount = 1;
|
||||
for (int i = 0; i < script.getChunks().size(); i++) {
|
||||
ScriptChunk chunk = script.getChunks().get(i);
|
||||
if(chunk.isOpCode()) {
|
||||
codeArea.append(chunk.toString(), "script-opcode");
|
||||
} else if(chunk.isSignature()) {
|
||||
codeArea.append("<signature" + signatureCount++ + ">", "script-signature");
|
||||
} else if(chunk.isPubKey()) {
|
||||
codeArea.append("<pubkey" + pubKeyCount++ + ">", "script-pubkey");
|
||||
} else if(chunk.isScript()) {
|
||||
Script nestedScript = chunk.getScript();
|
||||
if (nestedScript.equals(redeemScript)) {
|
||||
codeArea.append("<RedeemScript>", "script-redeem");
|
||||
} else if (nestedScript.equals(witnessScript)) {
|
||||
codeArea.append("<WitnessScript>", "script-redeem");
|
||||
} else {
|
||||
codeArea.append("(", "script-nest");
|
||||
appendScript(codeArea, nestedScript);
|
||||
codeArea.append(")", "script-nest");
|
||||
}
|
||||
} else {
|
||||
codeArea.append(chunk.toString(), "script-other");
|
||||
}
|
||||
|
||||
if(i < script.getChunks().size() - 1) {
|
||||
codeArea.append(" ", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addScriptPopup(codeArea, script);
|
||||
}
|
||||
|
||||
protected void addScriptPopup(CodeArea area, Script script) {
|
||||
ScriptContextMenu contextMenu = new ScriptContextMenu(area, script);
|
||||
area.setContextMenu(contextMenu);
|
||||
|
||||
Popup popup = new Popup();
|
||||
Label popupMsg = new Label();
|
||||
popupMsg.getStyleClass().add("tooltip");
|
||||
popup.getContent().add(popupMsg);
|
||||
|
||||
area.setMouseOverTextDelay(Duration.ofMillis(150));
|
||||
area.addEventHandler(MouseOverTextEvent.MOUSE_OVER_TEXT_BEGIN, e -> {
|
||||
TwoDimensional.Position position = area.getParagraph(0).getStyleSpans().offsetToPosition(e.getCharacterIndex(), Backward);
|
||||
if(position.getMajor() % 2 == 0) {
|
||||
ScriptChunk hoverChunk = script.getChunks().get(position.getMajor()/2);
|
||||
if(!hoverChunk.isOpCode()) {
|
||||
Point2D pos = e.getScreenPosition();
|
||||
popupMsg.setText(describeScriptChunk(hoverChunk));
|
||||
popup.show(area, pos.getX(), pos.getY() + 10);
|
||||
}
|
||||
}
|
||||
});
|
||||
area.addEventHandler(MouseOverTextEvent.MOUSE_OVER_TEXT_END, e -> {
|
||||
popup.hide();
|
||||
});
|
||||
}
|
||||
|
||||
protected String describeScriptChunk(ScriptChunk chunk) {
|
||||
return chunk.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class AddressesController extends WalletFormController implements Initial
|
||||
public void initializeView() {
|
||||
Wallet wallet = walletForm.getWallet();
|
||||
|
||||
receiveTable.initialize(wallet.getNodes(KeyPurpose.RECEIVE));
|
||||
changeTable.initialize(wallet.getNodes(KeyPurpose.CHANGE));
|
||||
receiveTable.initialize(getWalletForm().getNodeEntry(KeyPurpose.RECEIVE));
|
||||
changeTable.initialize(getWalletForm().getNodeEntry(KeyPurpose.CHANGE));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.sparrowwallet.sparrow.wallet;
|
||||
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Entry {
|
||||
private final SimpleStringProperty labelProperty;
|
||||
private final List<Entry> children = new ArrayList<>();
|
||||
|
||||
public Entry(String label) {
|
||||
this.labelProperty = new SimpleStringProperty(label);
|
||||
}
|
||||
|
||||
public Entry(SimpleStringProperty labelProperty) {
|
||||
this.labelProperty = labelProperty;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return labelProperty.get();
|
||||
}
|
||||
|
||||
public SimpleStringProperty labelProperty() {
|
||||
return labelProperty;
|
||||
}
|
||||
|
||||
public List<Entry> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public abstract Long getAmount();
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.sparrowwallet.sparrow.wallet;
|
||||
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
|
||||
public class NodeEntry extends Entry {
|
||||
private final Wallet.Node node;
|
||||
|
||||
public NodeEntry(Wallet.Node node) {
|
||||
super(node.getLabel());
|
||||
this.node = node;
|
||||
|
||||
labelProperty().addListener((observable, oldValue, newValue) -> node.setLabel(newValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getAmount() {
|
||||
//TODO: Iterate through TransactionEntries to calculate amount
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Wallet.Node getNode() {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.sparrowwallet.sparrow.wallet;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.client.j2se.MatrixToImageConfig;
|
||||
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.qrcode.QRCodeWriter;
|
||||
import com.sparrowwallet.drongo.KeyPurpose;
|
||||
import com.sparrowwallet.sparrow.EventManager;
|
||||
import com.sparrowwallet.sparrow.control.CopyableLabel;
|
||||
import com.sparrowwallet.sparrow.control.CopyableTextField;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import org.fxmisc.richtext.CodeArea;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class ReceiveController extends WalletFormController implements Initializable {
|
||||
@FXML
|
||||
private CopyableTextField address;
|
||||
|
||||
@FXML
|
||||
private TextField label;
|
||||
|
||||
@FXML
|
||||
private CopyableLabel derivationPath;
|
||||
|
||||
@FXML
|
||||
private CopyableLabel lastUsed;
|
||||
|
||||
@FXML
|
||||
private ImageView qrCode;
|
||||
|
||||
@FXML
|
||||
private CodeArea scriptPubKeyArea;
|
||||
|
||||
private NodeEntry currentEntry;
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
EventManager.get().register(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeView() {
|
||||
|
||||
}
|
||||
|
||||
public void setNodeEntry(NodeEntry nodeEntry) {
|
||||
if(currentEntry != null) {
|
||||
label.textProperty().unbindBidirectional(currentEntry.labelProperty());
|
||||
}
|
||||
|
||||
this.currentEntry = nodeEntry;
|
||||
|
||||
address.setText(nodeEntry.getNode().getAddress().toString());
|
||||
|
||||
label.textProperty().bindBidirectional(nodeEntry.labelProperty());
|
||||
|
||||
derivationPath.setText(nodeEntry.getNode().getDerivationPath());
|
||||
|
||||
//TODO: Find last used block height if available (red flag?)
|
||||
lastUsed.setText("Unknown");
|
||||
|
||||
Image qrImage = getQrCode(nodeEntry.getNode().getAddress().toString());
|
||||
if(qrImage != null) {
|
||||
qrCode.setImage(qrImage);
|
||||
}
|
||||
|
||||
scriptPubKeyArea.clear();
|
||||
appendScript(scriptPubKeyArea, nodeEntry.getNode().getOutputScript(), null, null);
|
||||
}
|
||||
|
||||
private Image getQrCode(String address) {
|
||||
try {
|
||||
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
||||
BitMatrix qrMatrix = qrCodeWriter.encode(address, BarcodeFormat.QR_CODE, 150, 150);
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
MatrixToImageWriter.writeToStream(qrMatrix, "PNG", baos, new MatrixToImageConfig());
|
||||
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
return new Image(bais);
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void getNewAddress(ActionEvent event) {
|
||||
NodeEntry freshEntry = getWalletForm().getFreshNodeEntry(KeyPurpose.RECEIVE, currentEntry);
|
||||
setNodeEntry(freshEntry);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,22 @@
|
||||
package com.sparrowwallet.sparrow.wallet;
|
||||
|
||||
import com.sparrowwallet.drongo.crypto.ECKey;
|
||||
import com.sparrowwallet.drongo.crypto.Pbkdf2KeyDeriver;
|
||||
import com.sparrowwallet.drongo.KeyPurpose;
|
||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||
import com.sparrowwallet.sparrow.io.Storage;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class WalletForm {
|
||||
private final Storage storage;
|
||||
private Wallet oldWallet;
|
||||
private Wallet wallet;
|
||||
|
||||
private final List<NodeEntry> accountEntries = new ArrayList<>();
|
||||
|
||||
public WalletForm(Storage storage, Wallet currentWallet) {
|
||||
this.storage = storage;
|
||||
this.oldWallet = currentWallet;
|
||||
@@ -39,4 +43,39 @@ public class WalletForm {
|
||||
storage.storeWallet(wallet);
|
||||
oldWallet = wallet.copy();
|
||||
}
|
||||
|
||||
public NodeEntry getNodeEntry(KeyPurpose keyPurpose) {
|
||||
NodeEntry purposeEntry;
|
||||
Optional<NodeEntry> optionalPurposeEntry = accountEntries.stream().filter(entry -> entry.getNode().getKeyPurpose().equals(keyPurpose)).findFirst();
|
||||
if(optionalPurposeEntry.isPresent()) {
|
||||
purposeEntry = optionalPurposeEntry.get();
|
||||
} else {
|
||||
Wallet.Node purposeNode = getWallet().getNode(keyPurpose);
|
||||
purposeEntry = new NodeEntry(purposeNode);
|
||||
for(Wallet.Node childNode : purposeNode.getChildren()) {
|
||||
NodeEntry childEntry = new NodeEntry(childNode);
|
||||
purposeEntry.getChildren().add(childEntry);
|
||||
}
|
||||
|
||||
accountEntries.add(purposeEntry);
|
||||
}
|
||||
|
||||
return purposeEntry;
|
||||
}
|
||||
|
||||
public NodeEntry getFreshNodeEntry(KeyPurpose keyPurpose, NodeEntry currentEntry) {
|
||||
NodeEntry rootEntry = getNodeEntry(keyPurpose);
|
||||
Wallet.Node freshNode = getWallet().getFreshNode(keyPurpose, currentEntry == null ? null : currentEntry.getNode());
|
||||
|
||||
for(Entry childEntry : rootEntry.getChildren()) {
|
||||
NodeEntry nodeEntry = (NodeEntry)childEntry;
|
||||
if(nodeEntry.getNode().equals(freshNode)) {
|
||||
return nodeEntry;
|
||||
}
|
||||
}
|
||||
|
||||
NodeEntry freshEntry = new NodeEntry(freshNode);
|
||||
rootEntry.getChildren().add(freshEntry);
|
||||
return freshEntry;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.sparrowwallet.sparrow.wallet;
|
||||
|
||||
public abstract class WalletFormController {
|
||||
import com.sparrowwallet.sparrow.BaseController;
|
||||
|
||||
public abstract class WalletFormController extends BaseController {
|
||||
public WalletForm walletForm;
|
||||
|
||||
public WalletForm getWalletForm() {
|
||||
|
||||
Reference in New Issue
Block a user