From 5223fe8d8ccec0226bab34eeade60356fa4ff784 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sat, 30 May 2020 11:47:23 +0200 Subject: [PATCH] make wallet node static inner class --- drongo | 2 +- .../sparrow/control/AddressTreeTable.java | 4 +- .../sparrow/io/ElectrumServer.java | 38 +++++-------------- .../sparrow/wallet/NodeEntry.java | 20 +++++++++- .../sparrow/wallet/ReceiveController.java | 8 ++-- .../sparrow/wallet/WalletForm.java | 4 +- 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/drongo b/drongo index a32410b5..cc4f7000 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit a32410b53831b3e5b111dd8fc82291c7ab30c732 +Subproject commit cc4f70002f64bc38307401cfecc548e0ee9adbbe diff --git a/src/main/java/com/sparrowwallet/sparrow/control/AddressTreeTable.java b/src/main/java/com/sparrowwallet/sparrow/control/AddressTreeTable.java index 885abd8c..6a5ec20b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/AddressTreeTable.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/AddressTreeTable.java @@ -29,7 +29,7 @@ public class AddressTreeTable extends TreeTableView { public void initialize(NodeEntry rootEntry) { getStyleClass().add("address-treetable"); - String address = rootEntry.getNode().getAddress().toString(); + String address = rootEntry.getAddress().toString(); RecursiveTreeItem rootItem = new RecursiveTreeItem<>(rootEntry, Entry::getChildren); setRoot(rootItem); @@ -92,7 +92,7 @@ public class AddressTreeTable extends TreeTableView { } else { if(entry instanceof NodeEntry) { NodeEntry nodeEntry = (NodeEntry)entry; - Address address = nodeEntry.getNode().getAddress(); + Address address = nodeEntry.getAddress(); setText(address.toString()); setContextMenu(new AddressContextMenu(address)); } else { diff --git a/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java b/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java index 5d4d49ed..6effb440 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java @@ -73,24 +73,24 @@ public class ElectrumServer { } public void getHistory(Wallet wallet, KeyPurpose keyPurpose) throws ServerException { - getHistory(wallet.getNode(keyPurpose).getChildren()); - getMempool(wallet.getNode(keyPurpose).getChildren()); + getHistory(wallet, wallet.getNode(keyPurpose).getChildren()); + getMempool(wallet, wallet.getNode(keyPurpose).getChildren()); } - public void getHistory(Collection nodes) throws ServerException { - getReferences("blockchain.scripthash.get_history", nodes); + public void getHistory(Wallet wallet, Collection nodes) throws ServerException { + getReferences(wallet, "blockchain.scripthash.get_history", nodes); } - public void getMempool(Collection nodes) throws ServerException { - getReferences("blockchain.scripthash.get_mempool", nodes); + public void getMempool(Wallet wallet, Collection nodes) throws ServerException { + getReferences(wallet, "blockchain.scripthash.get_mempool", nodes); } - public void getReferences(String method, Collection nodes) throws ServerException { + public void getReferences(Wallet wallet, String method, Collection nodes) throws ServerException { try { JsonRpcClient client = new JsonRpcClient(getTransport()); BatchRequestBuilder batchRequest = client.createBatchRequest().keysType(String.class).returnType(ScriptHashTx[].class); for(Wallet.Node node : nodes) { - batchRequest.add(node.getDerivationPath(), method, getScriptHash(node)); + batchRequest.add(node.getDerivationPath(), method, getScriptHash(wallet, node)); } Map result = batchRequest.execute(); @@ -163,26 +163,8 @@ public class ElectrumServer { } } - public void getHistory(Wallet.Node node) throws ServerException { - getHistory(getScriptHash(node)); - } - - public void getHistory(String scriptHash) throws ServerException { - try { - JsonRpcClient client = new JsonRpcClient(getTransport()); - List txList = client.onDemand(ELectrumXService.class).getHistory(scriptHash); - for(ScriptHashTx tx : txList) { - System.out.println(tx); - } - } catch (IllegalStateException e) { - throw new ServerException(e.getCause()); - } catch (Exception e) { - throw new ServerException(e); - } - } - - private String getScriptHash(Wallet.Node node) { - byte[] hash = Sha256Hash.hash(node.getOutputScript().getProgram()); + private String getScriptHash(Wallet wallet, Wallet.Node node) { + byte[] hash = Sha256Hash.hash(wallet.getOutputScript(node).getProgram()); byte[] reversed = Utils.reverseBytes(hash); return Utils.bytesToHex(reversed); } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java b/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java index a56d4a46..7c2406e2 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java @@ -1,19 +1,35 @@ package com.sparrowwallet.sparrow.wallet; +import com.sparrowwallet.drongo.address.Address; +import com.sparrowwallet.drongo.protocol.Script; import com.sparrowwallet.drongo.wallet.Wallet; import java.util.stream.Collectors; public class NodeEntry extends Entry { + private final Wallet wallet; private final Wallet.Node node; - public NodeEntry(Wallet.Node node) { - super(node.getLabel(), node.getChildren().stream().map(NodeEntry::new).collect(Collectors.toList())); + public NodeEntry(Wallet wallet, Wallet.Node node) { + super(node.getLabel(), node.getChildren().stream().map(childNode -> new NodeEntry(wallet, childNode)).collect(Collectors.toList())); + this.wallet = wallet; this.node = node; labelProperty().addListener((observable, oldValue, newValue) -> node.setLabel(newValue)); } + public Address getAddress() { + return wallet.getAddress(node); + } + + public Script getOutputScript() { + return wallet.getOutputScript(node); + } + + public String getOutputDescriptor() { + return wallet.getOutputDescriptor(node); + } + @Override public Long getAmount() { //TODO: Iterate through TransactionEntries to calculate amount diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java index a13820c2..5aae3eea 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java @@ -65,7 +65,7 @@ public class ReceiveController extends WalletFormController implements Initializ this.currentEntry = nodeEntry; - address.setText(nodeEntry.getNode().getAddress().toString()); + address.setText(nodeEntry.getAddress().toString()); label.textProperty().bindBidirectional(nodeEntry.labelProperty()); @@ -74,16 +74,16 @@ public class ReceiveController extends WalletFormController implements Initializ //TODO: Find last used block height if available (red flag?) lastUsed.setText("Unknown"); - Image qrImage = getQrCode(nodeEntry.getNode().getAddress().toString()); + Image qrImage = getQrCode(nodeEntry.getAddress().toString()); if(qrImage != null) { qrCode.setImage(qrImage); } scriptPubKeyArea.clear(); - appendScript(scriptPubKeyArea, nodeEntry.getNode().getOutputScript(), null, null); + appendScript(scriptPubKeyArea, nodeEntry.getOutputScript(), null, null); outputDescriptor.clear(); - outputDescriptor.appendText(nodeEntry.getNode().getOutputDescriptor()); + outputDescriptor.appendText(nodeEntry.getOutputDescriptor()); } private Image getQrCode(String address) { diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java index 7910cabc..6ef64b24 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java @@ -51,7 +51,7 @@ public class WalletForm { purposeEntry = optionalPurposeEntry.get(); } else { Wallet.Node purposeNode = getWallet().getNode(keyPurpose); - purposeEntry = new NodeEntry(purposeNode); + purposeEntry = new NodeEntry(getWallet(), purposeNode); accountEntries.add(purposeEntry); } @@ -69,7 +69,7 @@ public class WalletForm { } } - NodeEntry freshEntry = new NodeEntry(freshNode); + NodeEntry freshEntry = new NodeEntry(getWallet(), freshNode); rootEntry.getChildren().add(freshEntry); return freshEntry; }