From ebf7128ae5737c3ae4f9b54ad0df72b9bfa63594 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 12 Nov 2021 15:54:22 +0200 Subject: [PATCH] support utxo sets in wallet tx --- .../sparrowwallet/drongo/wallet/Wallet.java | 6 ++-- .../drongo/wallet/WalletTransaction.java | 31 ++++++++++++------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java index 4703745..07514c3 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -828,7 +828,7 @@ public class Wallet extends Persistable implements Comparable { //The new fee has meant that one of the change outputs is now dust. We pay too high a fee without change, but change is dust when added. if(numSets > 1 && differenceAmt / transaction.getVirtualSize() < noChangeFeeRate * 2) { //Maximize privacy. Pay a higher fee to keep multiple output sets. - return new WalletTransaction(this, transaction, utxoSelectors, selectedUtxos, txPayments, differenceAmt); + return new WalletTransaction(this, transaction, utxoSelectors, selectedUtxoSets, txPayments, differenceAmt); } else { //Maxmize efficiency. Increase value required from inputs and try again. valueRequiredAmt = totalSelectedAmt + 1; @@ -836,10 +836,10 @@ public class Wallet extends Persistable implements Comparable { } } - return new WalletTransaction(this, transaction, utxoSelectors, selectedUtxos, txPayments, changeMap, changeFeeRequiredAmt); + return new WalletTransaction(this, transaction, utxoSelectors, selectedUtxoSets, txPayments, changeMap, changeFeeRequiredAmt); } - return new WalletTransaction(this, transaction, utxoSelectors, selectedUtxos, txPayments, differenceAmt); + return new WalletTransaction(this, transaction, utxoSelectors, selectedUtxoSets, txPayments, differenceAmt); } } diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/WalletTransaction.java b/src/main/java/com/sparrowwallet/drongo/wallet/WalletTransaction.java index 5e86cb8..ce23c0b 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/WalletTransaction.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/WalletTransaction.java @@ -5,10 +5,7 @@ import com.sparrowwallet.drongo.protocol.Sha256Hash; import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.psbt.PSBT; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; /** * WalletTransaction contains a draft transaction along with associated metadata. The draft transaction has empty signatures but is otherwise complete. @@ -18,25 +15,25 @@ public class WalletTransaction { private final Wallet wallet; private final Transaction transaction; private final List utxoSelectors; - private final Map selectedUtxos; + private final List> selectedUtxoSets; private final List payments; private final Map changeMap; private final long fee; private final Map inputTransactions; - public WalletTransaction(Wallet wallet, Transaction transaction, List utxoSelectors, Map selectedUtxos, List payments, long fee) { - this(wallet, transaction, utxoSelectors, selectedUtxos, payments, Collections.emptyMap(), fee); + public WalletTransaction(Wallet wallet, Transaction transaction, List utxoSelectors, List> selectedUtxoSets, List payments, long fee) { + this(wallet, transaction, utxoSelectors, selectedUtxoSets, payments, Collections.emptyMap(), fee); } - public WalletTransaction(Wallet wallet, Transaction transaction, List utxoSelectors, Map selectedUtxos, List payments, Map changeMap, long fee) { - this(wallet, transaction, utxoSelectors, selectedUtxos, payments, changeMap, fee, Collections.emptyMap()); + public WalletTransaction(Wallet wallet, Transaction transaction, List utxoSelectors, List> selectedUtxoSets, List payments, Map changeMap, long fee) { + this(wallet, transaction, utxoSelectors, selectedUtxoSets, payments, changeMap, fee, Collections.emptyMap()); } - public WalletTransaction(Wallet wallet, Transaction transaction, List utxoSelectors, Map selectedUtxos, List payments, Map changeMap, long fee, Map inputTransactions) { + public WalletTransaction(Wallet wallet, Transaction transaction, List utxoSelectors, List> selectedUtxoSets, List payments, Map changeMap, long fee, Map inputTransactions) { this.wallet = wallet; this.transaction = transaction; this.utxoSelectors = utxoSelectors; - this.selectedUtxos = selectedUtxos; + this.selectedUtxoSets = selectedUtxoSets; this.payments = payments; this.changeMap = changeMap; this.fee = fee; @@ -60,9 +57,19 @@ public class WalletTransaction { } public Map getSelectedUtxos() { + if(selectedUtxoSets.size() == 1) { + return selectedUtxoSets.get(0); + } + + Map selectedUtxos = new LinkedHashMap<>(); + selectedUtxoSets.forEach(selectedUtxos::putAll); return selectedUtxos; } + public List> getSelectedUtxoSets() { + return selectedUtxoSets; + } + public List getPayments() { return payments; } @@ -84,7 +91,7 @@ public class WalletTransaction { } public long getTotal() { - return selectedUtxos.keySet().stream().mapToLong(BlockTransactionHashIndex::getValue).sum(); + return getSelectedUtxos().keySet().stream().mapToLong(BlockTransactionHashIndex::getValue).sum(); } public Map getInputTransactions() {