support reordering

This commit is contained in:
Craig Raw 2023-11-22 07:58:37 +02:00
parent 0815484c4c
commit 0bb5b75be5
3 changed files with 50 additions and 4 deletions

View file

@ -39,8 +39,8 @@ public class Transaction extends ChildMessage {
private Sha256Hash cachedTxId;
private Sha256Hash cachedWTxId;
private ArrayList<TransactionInput> inputs;
private ArrayList<TransactionOutput> outputs;
private List<TransactionInput> inputs;
private List<TransactionOutput> outputs;
public Transaction() {
version = 1;
@ -731,4 +731,24 @@ public class Transaction extends ChildMessage {
outputStream.write(new VarInt(bytes.length).encode());
outputStream.write(bytes);
}
public void moveInput(int fromIndex, int toIndex) {
moveItem(inputs, fromIndex, toIndex);
}
public void moveOutput(int fromIndex, int toIndex) {
moveItem(outputs, fromIndex, toIndex);
}
private <T> void moveItem(List<T> list, int fromIndex, int toIndex) {
if(fromIndex < 0 || fromIndex >= list.size() || toIndex < 0 || toIndex >= list.size()) {
throw new IllegalArgumentException("Invalid indices [" + fromIndex + ", " + toIndex + "] provided to list of size " + list.size());
}
T item = list.remove(fromIndex);
list.add(toIndex, item);
cachedTxId = null;
cachedWTxId = null;
}
}

View file

@ -594,6 +594,28 @@ public class PSBT {
}
}
public void moveInput(int fromIndex, int toIndex) {
moveItem(psbtInputs, fromIndex, toIndex);
transaction.moveInput(fromIndex, toIndex);
for(int i = 0; i < psbtInputs.size(); i++) {
psbtInputs.get(i).setIndex(i);
}
}
public void moveOutput(int fromIndex, int toIndex) {
moveItem(psbtOutputs, fromIndex, toIndex);
transaction.moveOutput(fromIndex, toIndex);
}
private <T> void moveItem(List<T> list, int fromIndex, int toIndex) {
if(fromIndex < 0 || fromIndex >= list.size() || toIndex < 0 || toIndex >= list.size()) {
throw new IllegalArgumentException("Invalid indices [" + fromIndex + ", " + toIndex + "] provided to list of size " + list.size());
}
T item = list.remove(fromIndex);
list.add(toIndex, item);
}
public List<PSBTInput> getPsbtInputs() {
return psbtInputs;
}

View file

@ -48,7 +48,7 @@ public class PSBTInput {
private ECKey tapInternalKey;
private final Transaction transaction;
private final int index;
private int index;
private static final Logger log = LoggerFactory.getLogger(PSBTInput.class);
@ -680,10 +680,14 @@ public class PSBTInput {
}
public TransactionOutput getUtxo() {
int vout = (int)transaction.getInputs().get(index).getOutpoint().getIndex();
int vout = (int)getInput().getOutpoint().getIndex();
return getWitnessUtxo() != null ? getWitnessUtxo() : (getNonWitnessUtxo() != null ? getNonWitnessUtxo().getOutputs().get(vout) : null);
}
void setIndex(int index) {
this.index = index;
}
public void clearNonFinalFields() {
partialSignatures.clear();
sigHash = null;