make wallet node static inner class

This commit is contained in:
Craig Raw 2020-05-30 11:47:23 +02:00
parent 331b83f7a0
commit 5223fe8d8c
6 changed files with 37 additions and 39 deletions

2
drongo

@ -1 +1 @@
Subproject commit a32410b53831b3e5b111dd8fc82291c7ab30c732 Subproject commit cc4f70002f64bc38307401cfecc548e0ee9adbbe

View file

@ -29,7 +29,7 @@ public class AddressTreeTable extends TreeTableView<Entry> {
public void initialize(NodeEntry rootEntry) { public void initialize(NodeEntry rootEntry) {
getStyleClass().add("address-treetable"); getStyleClass().add("address-treetable");
String address = rootEntry.getNode().getAddress().toString(); String address = rootEntry.getAddress().toString();
RecursiveTreeItem<Entry> rootItem = new RecursiveTreeItem<>(rootEntry, Entry::getChildren); RecursiveTreeItem<Entry> rootItem = new RecursiveTreeItem<>(rootEntry, Entry::getChildren);
setRoot(rootItem); setRoot(rootItem);
@ -92,7 +92,7 @@ public class AddressTreeTable extends TreeTableView<Entry> {
} else { } else {
if(entry instanceof NodeEntry) { if(entry instanceof NodeEntry) {
NodeEntry nodeEntry = (NodeEntry)entry; NodeEntry nodeEntry = (NodeEntry)entry;
Address address = nodeEntry.getNode().getAddress(); Address address = nodeEntry.getAddress();
setText(address.toString()); setText(address.toString());
setContextMenu(new AddressContextMenu(address)); setContextMenu(new AddressContextMenu(address));
} else { } else {

View file

@ -73,24 +73,24 @@ public class ElectrumServer {
} }
public void getHistory(Wallet wallet, KeyPurpose keyPurpose) throws ServerException { public void getHistory(Wallet wallet, KeyPurpose keyPurpose) throws ServerException {
getHistory(wallet.getNode(keyPurpose).getChildren()); getHistory(wallet, wallet.getNode(keyPurpose).getChildren());
getMempool(wallet.getNode(keyPurpose).getChildren()); getMempool(wallet, wallet.getNode(keyPurpose).getChildren());
} }
public void getHistory(Collection<Wallet.Node> nodes) throws ServerException { public void getHistory(Wallet wallet, Collection<Wallet.Node> nodes) throws ServerException {
getReferences("blockchain.scripthash.get_history", nodes); getReferences(wallet, "blockchain.scripthash.get_history", nodes);
} }
public void getMempool(Collection<Wallet.Node> nodes) throws ServerException { public void getMempool(Wallet wallet, Collection<Wallet.Node> nodes) throws ServerException {
getReferences("blockchain.scripthash.get_mempool", nodes); getReferences(wallet, "blockchain.scripthash.get_mempool", nodes);
} }
public void getReferences(String method, Collection<Wallet.Node> nodes) throws ServerException { public void getReferences(Wallet wallet, String method, Collection<Wallet.Node> nodes) throws ServerException {
try { try {
JsonRpcClient client = new JsonRpcClient(getTransport()); JsonRpcClient client = new JsonRpcClient(getTransport());
BatchRequestBuilder<String, ScriptHashTx[]> batchRequest = client.createBatchRequest().keysType(String.class).returnType(ScriptHashTx[].class); BatchRequestBuilder<String, ScriptHashTx[]> batchRequest = client.createBatchRequest().keysType(String.class).returnType(ScriptHashTx[].class);
for(Wallet.Node node : nodes) { for(Wallet.Node node : nodes) {
batchRequest.add(node.getDerivationPath(), method, getScriptHash(node)); batchRequest.add(node.getDerivationPath(), method, getScriptHash(wallet, node));
} }
Map<String, ScriptHashTx[]> result = batchRequest.execute(); Map<String, ScriptHashTx[]> result = batchRequest.execute();
@ -163,26 +163,8 @@ public class ElectrumServer {
} }
} }
public void getHistory(Wallet.Node node) throws ServerException { private String getScriptHash(Wallet wallet, Wallet.Node node) {
getHistory(getScriptHash(node)); byte[] hash = Sha256Hash.hash(wallet.getOutputScript(node).getProgram());
}
public void getHistory(String scriptHash) throws ServerException {
try {
JsonRpcClient client = new JsonRpcClient(getTransport());
List<ScriptHashTx> 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());
byte[] reversed = Utils.reverseBytes(hash); byte[] reversed = Utils.reverseBytes(hash);
return Utils.bytesToHex(reversed); return Utils.bytesToHex(reversed);
} }

View file

@ -1,19 +1,35 @@
package com.sparrowwallet.sparrow.wallet; package com.sparrowwallet.sparrow.wallet;
import com.sparrowwallet.drongo.address.Address;
import com.sparrowwallet.drongo.protocol.Script;
import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.Wallet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class NodeEntry extends Entry { public class NodeEntry extends Entry {
private final Wallet wallet;
private final Wallet.Node node; private final Wallet.Node node;
public NodeEntry(Wallet.Node node) { public NodeEntry(Wallet wallet, Wallet.Node node) {
super(node.getLabel(), node.getChildren().stream().map(NodeEntry::new).collect(Collectors.toList())); super(node.getLabel(), node.getChildren().stream().map(childNode -> new NodeEntry(wallet, childNode)).collect(Collectors.toList()));
this.wallet = wallet;
this.node = node; this.node = node;
labelProperty().addListener((observable, oldValue, newValue) -> node.setLabel(newValue)); 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 @Override
public Long getAmount() { public Long getAmount() {
//TODO: Iterate through TransactionEntries to calculate amount //TODO: Iterate through TransactionEntries to calculate amount

View file

@ -65,7 +65,7 @@ public class ReceiveController extends WalletFormController implements Initializ
this.currentEntry = nodeEntry; this.currentEntry = nodeEntry;
address.setText(nodeEntry.getNode().getAddress().toString()); address.setText(nodeEntry.getAddress().toString());
label.textProperty().bindBidirectional(nodeEntry.labelProperty()); 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?) //TODO: Find last used block height if available (red flag?)
lastUsed.setText("Unknown"); lastUsed.setText("Unknown");
Image qrImage = getQrCode(nodeEntry.getNode().getAddress().toString()); Image qrImage = getQrCode(nodeEntry.getAddress().toString());
if(qrImage != null) { if(qrImage != null) {
qrCode.setImage(qrImage); qrCode.setImage(qrImage);
} }
scriptPubKeyArea.clear(); scriptPubKeyArea.clear();
appendScript(scriptPubKeyArea, nodeEntry.getNode().getOutputScript(), null, null); appendScript(scriptPubKeyArea, nodeEntry.getOutputScript(), null, null);
outputDescriptor.clear(); outputDescriptor.clear();
outputDescriptor.appendText(nodeEntry.getNode().getOutputDescriptor()); outputDescriptor.appendText(nodeEntry.getOutputDescriptor());
} }
private Image getQrCode(String address) { private Image getQrCode(String address) {

View file

@ -51,7 +51,7 @@ public class WalletForm {
purposeEntry = optionalPurposeEntry.get(); purposeEntry = optionalPurposeEntry.get();
} else { } else {
Wallet.Node purposeNode = getWallet().getNode(keyPurpose); Wallet.Node purposeNode = getWallet().getNode(keyPurpose);
purposeEntry = new NodeEntry(purposeNode); purposeEntry = new NodeEntry(getWallet(), purposeNode);
accountEntries.add(purposeEntry); 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); rootEntry.getChildren().add(freshEntry);
return freshEntry; return freshEntry;
} }