diff --git a/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java b/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java index 8c959f52b..f3e53bf18 100644 --- a/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java +++ b/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java @@ -317,16 +317,20 @@ public class ScriptBuilder { else throw new IllegalStateException("Cannot handle " + scriptType); } else if (to instanceof SegwitAddress) { - // OP_0 - SegwitAddress toSegwit = (SegwitAddress) to; - smallNum(toSegwit.getWitnessVersion()); - data(toSegwit.getWitnessProgram()); + p2whOutputScript((SegwitAddress) to); } else { throw new IllegalStateException("Cannot handle " + to); } return this; } + private ScriptBuilder p2whOutputScript(SegwitAddress address) { + checkState(chunks.isEmpty()); + // OP_0 + return smallNum(address.getWitnessVersion()) + .data(address.getWitnessProgram()); + } + /** * Creates a scriptSig that can redeem a P2PKH output. * If given signature is null, incomplete scriptSig will be created with OP_0 instead of signature