From 3882a4b4bd9a738329bfffcaff72b477bb3a2d2f Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 9 Feb 2021 17:31:26 +0200 Subject: [PATCH] support wallet labels copying from past wallets --- .../drongo/wallet/BlockTransaction.java | 6 +++- .../drongo/wallet/BlockTransactionHash.java | 3 +- .../wallet/BlockTransactionHashIndex.java | 8 ++++-- .../drongo/wallet/WalletNode.java | 28 +++++++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java b/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java index 53036c6..5f0705a 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java @@ -18,7 +18,11 @@ public class BlockTransaction extends BlockTransactionHash implements Comparable } public BlockTransaction(Sha256Hash hash, int height, Date date, Long fee, Transaction transaction, Sha256Hash blockHash) { - super(hash, height, date, fee); + this(hash, height, date, fee, transaction, blockHash, null); + } + + public BlockTransaction(Sha256Hash hash, int height, Date date, Long fee, Transaction transaction, Sha256Hash blockHash, String label) { + super(hash, height, date, fee, label); this.transaction = transaction; this.blockHash = blockHash; } diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransactionHash.java b/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransactionHash.java index 4717f34..09af7c2 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransactionHash.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransactionHash.java @@ -16,11 +16,12 @@ public abstract class BlockTransactionHash { private String label; - public BlockTransactionHash(Sha256Hash hash, int height, Date date, Long fee) { + public BlockTransactionHash(Sha256Hash hash, int height, Date date, Long fee, String label) { this.hash = hash; this.height = height; this.date = date; this.fee = fee; + this.label = label; } public Sha256Hash getHash() { diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransactionHashIndex.java b/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransactionHashIndex.java index 0368755..2046c38 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransactionHashIndex.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransactionHashIndex.java @@ -16,7 +16,11 @@ public class BlockTransactionHashIndex extends BlockTransactionHash implements C } public BlockTransactionHashIndex(Sha256Hash hash, int height, Date date, Long fee, long index, long value, BlockTransactionHashIndex spentBy) { - super(hash, height, date, fee); + this(hash, height, date, fee, index, value, spentBy, null); + } + + public BlockTransactionHashIndex(Sha256Hash hash, int height, Date date, Long fee, long index, long value, BlockTransactionHashIndex spentBy, String label) { + super(hash, height, date, fee, label); this.index = index; this.value = value; this.spentBy = spentBy; @@ -92,6 +96,6 @@ public class BlockTransactionHashIndex extends BlockTransactionHash implements C } public BlockTransactionHashIndex copy() { - return new BlockTransactionHashIndex(super.getHash(), super.getHeight(), super.getDate(), super.getFee(), index, value, spentBy == null ? null : spentBy.copy()); + return new BlockTransactionHashIndex(super.getHash(), super.getHeight(), super.getDate(), super.getFee(), index, value, spentBy == null ? null : spentBy.copy(), super.getLabel()); } } diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java b/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java index 0725a68..792d7f8 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java @@ -207,4 +207,32 @@ public class WalletNode implements Comparable { return copy; } + + public void copyLabels(WalletNode pastNode) { + if(pastNode == null) { + return; + } + + if(label == null && pastNode.label != null) { + label = pastNode.label; + } + + for(BlockTransactionHashIndex txo : getTransactionOutputs()) { + Optional optPastTxo = pastNode.getTransactionOutputs().stream().filter(pastTxo -> pastTxo.equals(txo)).findFirst(); + if(optPastTxo.isPresent()) { + BlockTransactionHashIndex pastTxo = optPastTxo.get(); + if(txo.getLabel() == null && pastTxo.getLabel() != null) { + txo.setLabel(pastTxo.getLabel()); + } + if(txo.isSpent() && pastTxo.isSpent() && txo.getSpentBy().getLabel() == null && pastTxo.getSpentBy().getLabel() != null) { + txo.getSpentBy().setLabel(pastTxo.getSpentBy().getLabel()); + } + } + } + + for(WalletNode childNode : getChildren()) { + Optional optPastChildNode = pastNode.getChildren().stream().filter(node -> node.equals(childNode)).findFirst(); + optPastChildNode.ifPresent(childNode::copyLabels); + } + } }