transaction history support

This commit is contained in:
Craig Raw 2020-05-30 11:22:17 +02:00
parent 11978e1f48
commit a32410b538
4 changed files with 86 additions and 7 deletions

View file

@ -68,6 +68,8 @@ jar {
attributes "Main-Class": "com.sparrowwallet.drongo.Main" attributes "Main-Class": "com.sparrowwallet.drongo.Main"
} }
exclude('logback.xml')
archiveBaseName = 'drongo' archiveBaseName = 'drongo'
archiveVersion = '0.1' archiveVersion = '0.1'
} }

View file

@ -0,0 +1,57 @@
package com.sparrowwallet.drongo.wallet;
import java.util.Objects;
public class TransactionReference implements Comparable<TransactionReference> {
private final String transactionId;
private final Integer height;
private final Long fee;
public TransactionReference(String transactionId) {
this(transactionId, 0, 0L);
}
public TransactionReference(String transactionId, Integer height) {
this(transactionId, height, 0L);
}
public TransactionReference(String transactionId, Integer height, Long fee) {
this.transactionId = transactionId;
this.height = height;
this.fee = fee;
}
public String getTransactionId() {
return transactionId;
}
public Integer getHeight() {
return height;
}
public Long getFee() {
return fee;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TransactionReference that = (TransactionReference) o;
return transactionId.equals(that.transactionId);
}
@Override
public int hashCode() {
return Objects.hash(transactionId);
}
@Override
public int compareTo(TransactionReference reference) {
return height - reference.height;
}
public TransactionReference copy() {
return new TransactionReference(transactionId, height, fee);
}
}

View file

@ -11,6 +11,7 @@ import com.sparrowwallet.drongo.policy.Policy;
import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.policy.PolicyType;
import com.sparrowwallet.drongo.protocol.Script; import com.sparrowwallet.drongo.protocol.Script;
import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.protocol.ScriptType;
import com.sparrowwallet.drongo.protocol.Transaction;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -23,7 +24,8 @@ public class Wallet {
private ScriptType scriptType; private ScriptType scriptType;
private Policy defaultPolicy; private Policy defaultPolicy;
private List<Keystore> keystores = new ArrayList<>(); private List<Keystore> keystores = new ArrayList<>();
private final Set<Node> accountNodes = new TreeSet<>(); private final Set<Node> purposeNodes = new TreeSet<>();
private final Map<String, Transaction> transactions = new HashMap<>();
public Wallet() { public Wallet() {
} }
@ -80,16 +82,20 @@ public class Wallet {
this.keystores = keystores; this.keystores = keystores;
} }
private Set<Node> getAccountNodes() { private Set<Node> getPurposeNodes() {
return accountNodes; return purposeNodes;
}
public Map<String, Transaction> getTransactions() {
return transactions;
} }
public Node getNode(KeyPurpose keyPurpose) { public Node getNode(KeyPurpose keyPurpose) {
Node purposeNode; Node purposeNode;
Optional<Node> optionalPurposeNode = accountNodes.stream().filter(node -> node.getKeyPurpose().equals(keyPurpose)).findFirst(); Optional<Node> optionalPurposeNode = purposeNodes.stream().filter(node -> node.getKeyPurpose().equals(keyPurpose)).findFirst();
if(optionalPurposeNode.isEmpty()) { if(optionalPurposeNode.isEmpty()) {
purposeNode = new Node(keyPurpose); purposeNode = new Node(keyPurpose);
accountNodes.add(purposeNode); purposeNodes.add(purposeNode);
} else { } else {
purposeNode = optionalPurposeNode.get(); purposeNode = optionalPurposeNode.get();
} }
@ -272,9 +278,9 @@ public class Wallet {
for(Keystore keystore : keystores) { for(Keystore keystore : keystores) {
copy.getKeystores().add(keystore.copy()); copy.getKeystores().add(keystore.copy());
} }
for(Wallet.Node node : accountNodes) { for(Wallet.Node node : purposeNodes) {
Node nodeCopy = copy.copyNode(node); Node nodeCopy = copy.copyNode(node);
copy.getAccountNodes().add(nodeCopy); copy.getPurposeNodes().add(nodeCopy);
} }
return copy; return copy;
} }
@ -286,6 +292,9 @@ public class Wallet {
for(Node child : node.getChildren()) { for(Node child : node.getChildren()) {
copy.getChildren().add(copyNode(child)); copy.getChildren().add(copyNode(child));
} }
for(TransactionReference reference : node.getHistory()) {
copy.getHistory().add(reference.copy());
}
return copy; return copy;
} }
@ -349,6 +358,7 @@ public class Wallet {
private String label; private String label;
private Long amount; private Long amount;
private Set<Node> children = new TreeSet<>(); private Set<Node> children = new TreeSet<>();
private Set<TransactionReference> history = new TreeSet<>();
private transient KeyPurpose keyPurpose; private transient KeyPurpose keyPurpose;
private transient int index = -1; private transient int index = -1;
@ -431,6 +441,14 @@ public class Wallet {
this.children = children; this.children = children;
} }
public Set<TransactionReference> getHistory() {
return history;
}
public void setHistory(Set<TransactionReference> history) {
this.history = history;
}
public Address getAddress() { public Address getAddress() {
return Wallet.this.getAddress(keyPurpose, index); return Wallet.this.getAddress(keyPurpose, index);
} }

View file

@ -2,6 +2,8 @@ open module com.sparrowwallet.drongo {
requires org.bouncycastle.provider; requires org.bouncycastle.provider;
requires de.mkammerer.argon2; requires de.mkammerer.argon2;
requires slf4j.api; requires slf4j.api;
requires json.simple;
requires jeromq;
exports com.sparrowwallet.drongo; exports com.sparrowwallet.drongo;
exports com.sparrowwallet.drongo.psbt; exports com.sparrowwallet.drongo.psbt;
exports com.sparrowwallet.drongo.protocol; exports com.sparrowwallet.drongo.protocol;