From 0466755883c19a9e679f5e937b2f7db55a73e05b Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 24 Jul 2020 10:09:24 +0200 Subject: [PATCH] handle adding non witness inputs to segwit transactions --- .../java/com/sparrowwallet/drongo/protocol/Transaction.java | 6 +++++- .../sparrowwallet/drongo/protocol/TransactionWitness.java | 4 ++++ src/main/java/com/sparrowwallet/drongo/psbt/PSBT.java | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java b/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java index 560c120..ee0f076 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java @@ -307,7 +307,11 @@ public class Transaction extends ChildMessage { } public TransactionInput addInput(Sha256Hash spendTxHash, long outputIndex, Script script) { - return addInput(new TransactionInput(this, new TransactionOutPoint(spendTxHash, outputIndex), script.getProgram())); + if(isSegwit()) { + return addInput(spendTxHash, outputIndex, script, new TransactionWitness(this)); + } else { + return addInput(new TransactionInput(this, new TransactionOutPoint(spendTxHash, outputIndex), script.getProgram())); + } } public TransactionInput addInput(Sha256Hash spendTxHash, long outputIndex, Script script, TransactionWitness witness) { diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java index 6c8929a..fb4e796 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java @@ -33,6 +33,10 @@ public class TransactionWitness extends ChildMessage { pushes.add(witnessScript.getProgram()); } + public TransactionWitness(Transaction transaction) { + this(transaction, new ArrayList<>()); + } + public TransactionWitness(Transaction transaction, List witnesses) { setParent(transaction); this.pushes = witnesses; diff --git a/src/main/java/com/sparrowwallet/drongo/psbt/PSBT.java b/src/main/java/com/sparrowwallet/drongo/psbt/PSBT.java index 1c151e8..cde1c38 100644 --- a/src/main/java/com/sparrowwallet/drongo/psbt/PSBT.java +++ b/src/main/java/com/sparrowwallet/drongo/psbt/PSBT.java @@ -484,7 +484,7 @@ public class PSBT { if(psbtInput.getFinalScriptWitness() != null) { txInput.setWitness(psbtInput.getFinalScriptWitness()); } else { - txInput.setWitness(new TransactionWitness(transaction, Collections.emptyList())); + txInput.setWitness(new TransactionWitness(transaction)); } } }