mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-07-30 19:41:24 +00:00
ScriptBuilder: Add createP2PKHOutputScript() helpers.
This commit is contained in:
@@ -258,12 +258,7 @@ public class ScriptBuilder {
|
||||
if (to instanceof LegacyAddress) {
|
||||
ScriptType scriptType = to.getOutputScriptType();
|
||||
if (scriptType == ScriptType.P2PKH) {
|
||||
// OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
|
||||
builder.op(OP_DUP);
|
||||
builder.op(OP_HASH160);
|
||||
builder.data(to.getHash());
|
||||
builder.op(OP_EQUALVERIFY);
|
||||
builder.op(OP_CHECKSIG);
|
||||
return createP2PKHOutputScript(to.getHash());
|
||||
} else if (scriptType == ScriptType.P2SH) {
|
||||
// OP_HASH160 <scriptHash> OP_EQUAL
|
||||
builder.op(OP_HASH160);
|
||||
@@ -438,6 +433,28 @@ public class ScriptBuilder {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a scriptPubKey that sends to the given public key hash.
|
||||
*/
|
||||
public static Script createP2PKHOutputScript(byte[] hash) {
|
||||
checkArgument(hash.length == LegacyAddress.LENGTH);
|
||||
ScriptBuilder builder = new ScriptBuilder();
|
||||
builder.op(OP_DUP);
|
||||
builder.op(OP_HASH160);
|
||||
builder.data(hash);
|
||||
builder.op(OP_EQUALVERIFY);
|
||||
builder.op(OP_CHECKSIG);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a scriptPubKey that sends to the given public key.
|
||||
*/
|
||||
public static Script createP2PKHOutputScript(ECKey key) {
|
||||
checkArgument(key.isCompressed());
|
||||
return createP2PKHOutputScript(key.getPubKeyHash());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a segwit scriptPubKey that sends to the given public key hash.
|
||||
*/
|
||||
|
@@ -337,10 +337,7 @@ public class TransactionTest {
|
||||
assertEquals("30450221008b9d1dc26ba6a9cb62127b02742fa9d754cd3bebf337f7a55d114c8e5cdd30be022040529b194ba3f9281a99f2b1c0a19c0489bc22ede944ccf4ecbab4cc618ef3ed01",
|
||||
HEX.encode(txSig0.encodeToBitcoin()));
|
||||
|
||||
Script scriptCode = new ScriptBuilder()
|
||||
.data(ScriptBuilder.createOutputScript(LegacyAddress.fromKey(netParams, key1))
|
||||
.getProgram())
|
||||
.build();
|
||||
Script scriptCode = new ScriptBuilder().data(ScriptBuilder.createP2PKHOutputScript(key1).getProgram()).build();
|
||||
assertEquals("1976a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac",
|
||||
HEX.encode(scriptCode.getProgram()));
|
||||
|
||||
|
@@ -19,7 +19,6 @@ package org.bitcoinj.script;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import org.bitcoinj.core.LegacyAddress;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.SegwitAddress;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
@@ -39,7 +38,7 @@ public class ScriptPatternTest {
|
||||
@Test
|
||||
public void testCommonScripts() {
|
||||
assertTrue(ScriptPattern.isPayToPubKeyHash(
|
||||
ScriptBuilder.createOutputScript(LegacyAddress.fromKey(MAINNET, keys.get(0)))
|
||||
ScriptBuilder.createP2PKHOutputScript(keys.get(0))
|
||||
));
|
||||
assertTrue(ScriptPattern.isPayToScriptHash(
|
||||
ScriptBuilder.createP2SHOutputScript(2, keys)
|
||||
|
@@ -208,7 +208,7 @@ public class DefaultRiskAnalysisTest {
|
||||
Transaction tx = new Transaction(MAINNET);
|
||||
tx.addInput(MAINNET.getGenesisBlock().getTransactions().get(0).getOutput(0));
|
||||
// A pay to address output
|
||||
tx.addOutput(Coin.CENT, ScriptBuilder.createOutputScript(LegacyAddress.fromKey(MAINNET, key1)));
|
||||
tx.addOutput(Coin.CENT, ScriptBuilder.createP2PKHOutputScript(key1));
|
||||
// A pay to pubkey output
|
||||
tx.addOutput(Coin.CENT, ScriptBuilder.createOutputScript(key1));
|
||||
tx.addOutput(Coin.CENT, ScriptBuilder.createOutputScript(key1));
|
||||
|
Reference in New Issue
Block a user