Package org.bitcoinj.script
Class ScriptBuilder
java.lang.Object
org.bitcoinj.script.ScriptBuilder
Tools for the construction of commonly used script types. You don't normally need this as it's hidden behind
convenience methods on Transaction, but they are useful when working with the
protocol at a lower level.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a fresh ScriptBuilder with an empty program.ScriptBuilder(Script template) Creates a fresh ScriptBuilder with the given program as the starting point. -
Method Summary
Modifier and TypeMethodDescriptionaddChunk(int index, ScriptChunk chunk) Adds the given chunk at the given index in the programaddChunk(ScriptChunk chunk) Adds the given chunk to the end of the programprotected ScriptBuilderbigNum(int index, long num) Adds the given number as a push data chunk to the given index in the program.protected ScriptBuilderbigNum(long num) Adds the given number as a push data chunk.build()Creates a new immutable Script based on the state of the builder.static ScriptCreates an empty script.static ScriptcreateInputScript(TransactionSignature signature) Creates a scriptSig that can redeem a P2PK output.static ScriptcreateInputScript(TransactionSignature signature, ECKey pubKey) Creates a scriptSig that can redeem a P2PKH output.static ScriptcreateMultiSigInputScript(List<TransactionSignature> signatures) Create a program that satisfies an OP_CHECKMULTISIG program.static ScriptcreateMultiSigInputScript(TransactionSignature... signatures) Create a program that satisfies an OP_CHECKMULTISIG program.static ScriptcreateMultiSigInputScriptBytes(List<byte[]> signatures) Create a program that satisfies an OP_CHECKMULTISIG program, using pre-encoded signatures.static ScriptcreateMultiSigInputScriptBytes(List<byte[]> signatures, byte[] multisigProgramBytes) Create a program that satisfies an OP_CHECKMULTISIG program, using pre-encoded signatures.static ScriptcreateMultiSigOutputScript(int threshold, List<ECKey> pubkeys) Creates a program that requires at least N of the given keys to sign, using OP_CHECKMULTISIG.static ScriptcreateOpReturnScript(byte[] data) Creates a script of the form OP_RETURN [data].static ScriptCreates a scriptPubKey that locks an output to the given address.static ScriptcreateOutputScript(Address to, Instant creationTime) Creates a scriptPubKey that locks an output to the given address.static ScriptcreateP2PKHOutputScript(byte[] pubKeyHash) Creates a scriptPubKey that locks an output to the given public key hash.static ScriptcreateP2PKHOutputScript(ECKey pubKey) Creates a scriptPubKey that locks an output to a hash of a given public key.static ScriptcreateP2PKOutputScript(byte[] pubKey) Creates a scriptPubKey that locks an output to the given raw public key.static ScriptcreateP2PKOutputScript(ECKey pubKey) Creates a scriptPubKey that locks an output to the given public key.static ScriptcreateP2SHMultiSigInputScript(List<TransactionSignature> signatures, Script multisigProgram) Create a program that satisfies a P2SH OP_CHECKMULTISIG program.static ScriptcreateP2SHOutputScript(byte[] redeemScriptHash) Creates a scriptPubKey that locks an output to the given script hash.static ScriptcreateP2SHOutputScript(int threshold, List<ECKey> pubkeys) Creates a P2SH output script for n-of-m multisig with given public keys and threshold.static ScriptcreateP2SHOutputScript(Script redeemScript) Creates a scriptPubKey that locks an output to a hash of the given redeem script.static ScriptcreateP2WPKHOutputScript(byte[] pubKeyHash) Creates a segwit scriptPubKey that locks an output to the given public key hash.static ScriptcreateP2WPKHOutputScript(ECKey pubKey) Creates a segwit scriptPubKey that locks an output to the hash of a given public key.static ScriptcreateP2WSHOutputScript(byte[] redeemScriptHash) Creates a segwit scriptPubKey that locks an output to the given script hash.static ScriptcreateP2WSHOutputScript(Script redeemScript) Creates a segwit scriptPubKey that locks an output to a hash of the given redeem script.static ScriptcreateRedeemScript(int threshold, List<ECKey> pubkeys) Creates an n-of-m multisig redeem script with given public keys and threshold.creationTime(Instant creationTime) Associates this script to be built with a given creation time.data(byte[] data) Adds a copy of the given byte array as a data element (i.e.data(int index, byte[] data) Adds a copy of the given byte array as a data element (i.e.number(int index, long num) Adds the given number to the given index in the program.number(long num) Adds the given number to the end of the program.op(int opcode) Adds the given opcode to the end of the program.op(int index, int opcode) Adds the given opcode to the given index in the programopFalse()Adds false to the end of the program.opFalse(int index) Adds false to the given index in the program.opTrue()Adds true to the end of the program.opTrue(int index) Adds true to the given index in the program.smallNum(int num) Adds the given number as a OP_N opcode to the end of the program.smallNum(int index, int num) Adds the given number as a OP_N opcode to the given index in the program.static ScriptupdateScriptWithSignature(Script scriptSig, byte[] signature, int targetIndex, int sigsPrefixCount, int sigsSuffixCount) Returns a copy of the given scriptSig with the signature inserted in the given position.
-
Constructor Details
-
ScriptBuilder
public ScriptBuilder()Creates a fresh ScriptBuilder with an empty program. -
ScriptBuilder
Creates a fresh ScriptBuilder with the given program as the starting point.
-
-
Method Details
-
creationTime
Associates this script to be built with a given creation time. This is currently used in the context of watching wallets only, where the scriptPubKeys being watched actually represent public keys and their addresses.- Parameters:
creationTime- creation time to associate the script with- Returns:
- this builder
-
addChunk
Adds the given chunk to the end of the program -
addChunk
Adds the given chunk at the given index in the program -
op
Adds the given opcode to the end of the program. -
op
Adds the given opcode to the given index in the program -
data
Adds a copy of the given byte array as a data element (i.e. PUSHDATA) at the end of the program. -
data
Adds a copy of the given byte array as a data element (i.e. PUSHDATA) at the given index in the program. -
number
Adds the given number to the end of the program. Automatically uses shortest encoding possible. -
number
Adds the given number to the given index in the program. Automatically uses shortest encoding possible. -
smallNum
Adds the given number as a OP_N opcode to the end of the program. Only handles values 0-16 inclusive.- See Also:
-
bigNum
Adds the given number as a push data chunk. This is intended to use for negative numbers or values greater than 16, and although it will accept numbers in the range 0-16 inclusive, the encoding would be considered non-standard.- See Also:
-
smallNum
Adds the given number as a OP_N opcode to the given index in the program. Only handles values 0-16 inclusive.- See Also:
-
bigNum
Adds the given number as a push data chunk to the given index in the program. This is intended to use for negative numbers or values greater than 16, and although it will accept numbers in the range 0-16 inclusive, the encoding would be considered non-standard.- See Also:
-
opTrue
Adds true to the end of the program.- Returns:
- this
-
opTrue
Adds true to the given index in the program.- Parameters:
index- at which insert true- Returns:
- this
-
opFalse
Adds false to the end of the program.- Returns:
- this
-
opFalse
Adds false to the given index in the program.- Parameters:
index- at which insert true- Returns:
- this
-
build
Creates a new immutable Script based on the state of the builder. -
createEmpty
Creates an empty script. -
createOutputScript
Creates a scriptPubKey that locks an output to the given address.- Parameters:
to- address to lock an output tocreationTime- creation time of the scriptPubKey- Returns:
- scriptPubKey that locks an output
-
createOutputScript
Creates a scriptPubKey that locks an output to the given address.- Parameters:
to- address to lock an output to- Returns:
- scriptPubKey that locks an output
-
createInputScript
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 -
createInputScript
Creates a scriptSig that can redeem a P2PK output. If given signature is null, incomplete scriptSig will be created with OP_0 instead of signature -
createMultiSigOutputScript
Creates a program that requires at least N of the given keys to sign, using OP_CHECKMULTISIG. -
createMultiSigInputScript
Create a program that satisfies an OP_CHECKMULTISIG program. -
createMultiSigInputScript
Create a program that satisfies an OP_CHECKMULTISIG program. -
createMultiSigInputScriptBytes
Create a program that satisfies an OP_CHECKMULTISIG program, using pre-encoded signatures. -
createP2SHMultiSigInputScript
public static Script createP2SHMultiSigInputScript(@Nullable List<TransactionSignature> signatures, Script multisigProgram) Create a program that satisfies a P2SH OP_CHECKMULTISIG program. If given signature list is null, incomplete scriptSig will be created with OP_0 instead of signatures -
createMultiSigInputScriptBytes
public static Script createMultiSigInputScriptBytes(List<byte[]> signatures, @Nullable byte[] multisigProgramBytes) Create a program that satisfies an OP_CHECKMULTISIG program, using pre-encoded signatures. Optionally, appends the script program bytes if spending a P2SH output. -
updateScriptWithSignature
public static Script updateScriptWithSignature(Script scriptSig, byte[] signature, int targetIndex, int sigsPrefixCount, int sigsSuffixCount) Returns a copy of the given scriptSig with the signature inserted in the given position. This function assumes that any missing sigs have OP_0 placeholders. If given scriptSig already has all the signatures in place, IllegalArgumentException will be thrown.- Parameters:
targetIndex- where to insert the signaturesigsPrefixCount- how many items to copy verbatim (e.g. initial OP_0 for multisig)sigsSuffixCount- how many items to copy verbatim at end (e.g. redeemScript for P2SH)
-
createP2PKOutputScript
Creates a scriptPubKey that locks an output to the given raw public key. The pubkey can be arbitrary data and may be invalid.This is a special purpose method. For normal P2PK use, it is recommended to construct a pubkey with
ECKey.fromPublicOnly(byte[])and use that oncreateP2PKOutputScript(ECKey).- Parameters:
pubKey- arbitrary pubkey bytes to lock an output to- Returns:
- P2PK scriptPubKey that locks an output
-
createP2PKOutputScript
Creates a scriptPubKey that locks an output to the given public key.- Parameters:
pubKey- pubkey to lock an output to- Returns:
- P2PK scriptPubKey that locks an output
-
createP2PKHOutputScript
Creates a scriptPubKey that locks an output to the given public key hash.- Parameters:
pubKeyHash- 20 hash bytes of the pubkey to lock an output to- Returns:
- P2PKH scriptPubKey that locks an output
-
createP2PKHOutputScript
Creates a scriptPubKey that locks an output to a hash of a given public key.- Parameters:
pubKey- pubkey whose hash to lock an output to- Returns:
- P2PKH scriptPubKey that locks an output
-
createP2WPKHOutputScript
Creates a segwit scriptPubKey that locks an output to the given public key hash.- Parameters:
pubKeyHash- 20 hash bytes of the pubkey to lock an output to- Returns:
- P2WPKH scriptPubKey that locks an output
-
createP2WPKHOutputScript
Creates a segwit scriptPubKey that locks an output to the hash of a given public key. In accordance with the segwit specification, the public key must be compressed.- Parameters:
pubKey- pubkey whose hash to lock an output to- Returns:
- P2WPKH scriptPubKey that locks an output
-
createP2SHOutputScript
Creates a scriptPubKey that locks an output to the given script hash. Read BIP 16 to learn more about this kind of script.- Parameters:
redeemScriptHash- 20 hash bytes of the redeem script to lock an output to- Returns:
- P2SH scriptPubKey that locks an output
-
createP2SHOutputScript
Creates a scriptPubKey that locks an output to a hash of the given redeem script.- Parameters:
redeemScript- redeem script whose hash to lock an output to- Returns:
- P2SH scriptPubKey that locks an output
-
createP2WSHOutputScript
Creates a segwit scriptPubKey that locks an output to the given script hash.- Parameters:
redeemScriptHash- 32 hash bytes of the redeem script to lock an output to- Returns:
- P2WSH scriptPubKey that locks an output
-
createP2WSHOutputScript
Creates a segwit scriptPubKey that locks an output to a hash of the given redeem script.- Parameters:
redeemScript- redeem script whose hash to lock an output to- Returns:
- P2WSH scriptPubKey that locks an output
-
createP2SHOutputScript
Creates a P2SH output script for n-of-m multisig with given public keys and threshold. Given public keys will be placed in redeem script in the lexicographical sorting order.- Parameters:
threshold- The threshold number of keys that must sign (n)pubkeys- list of m public keys to lock an output to- Returns:
- P2SH multisig scriptPubKey that locks an output
-
createRedeemScript
Creates an n-of-m multisig redeem script with given public keys and threshold. Given public keys will be placed in redeem script in the lexicographical sorting order.- Parameters:
threshold- The threshold number of keys that must sign (n)pubkeys- list of m public keys to lock an output to- Returns:
- P2SH multisig scriptPubKey that locks an output
-
createOpReturnScript
Creates a script of the form OP_RETURN [data]. This feature allows you to attach a small piece of data (like a hash of something stored elsewhere) to a zero valued output which can never be spent and thus does not pollute the ledger.
-