ScriptBuilder: Make use of createP2SHOutputScript().

This commit is contained in:
Andreas Schildbach
2019-01-30 01:52:37 +01:00
parent 0db2a5f183
commit a8129b68f9
2 changed files with 7 additions and 12 deletions

View File

@@ -254,28 +254,24 @@ public class ScriptBuilder {
/** Creates a scriptPubKey that encodes payment to the given address. */
public static Script createOutputScript(Address to) {
ScriptBuilder builder = new ScriptBuilder();
if (to instanceof LegacyAddress) {
ScriptType scriptType = to.getOutputScriptType();
if (scriptType == ScriptType.P2PKH) {
if (scriptType == ScriptType.P2PKH)
return createP2PKHOutputScript(to.getHash());
} else if (scriptType == ScriptType.P2SH) {
// OP_HASH160 <scriptHash> OP_EQUAL
builder.op(OP_HASH160);
builder.data(to.getHash());
builder.op(OP_EQUAL);
} else {
else if (scriptType == ScriptType.P2SH)
return createP2SHOutputScript(to.getHash());
else
throw new IllegalStateException("Cannot handle " + scriptType);
}
} else if (to instanceof SegwitAddress) {
ScriptBuilder builder = new ScriptBuilder();
// OP_0 <pubKeyHash|scriptHash>
SegwitAddress toSegwit = (SegwitAddress) to;
builder.smallNum(toSegwit.getWitnessVersion());
builder.data(toSegwit.getWitnessProgram());
return builder.build();
} else {
throw new IllegalStateException("Cannot handle " + to);
}
return builder.build();
}
/** Creates a scriptPubKey that encodes payment to the given raw public key. */

View File

@@ -405,8 +405,7 @@ public class TransactionTest {
HEX.encode(redeemScript.getProgram()));
byte[] p2wpkhHash = Utils.sha256hash160(redeemScript.getProgram());
Script scriptPubKey = ScriptBuilder.createOutputScript(
LegacyAddress.fromScriptHash(netParams, p2wpkhHash));
Script scriptPubKey = ScriptBuilder.createP2SHOutputScript(p2wpkhHash);
assertEquals("a9144733f37cf4db86fbc2efed2500b4f4e49f31202387",
HEX.encode(scriptPubKey.getProgram()));