Package org.bitcoinj.script
Class Script
- java.lang.Object
-
- org.bitcoinj.script.Script
-
public class Script extends java.lang.Object
Programs embedded inside transactions that control redemption of payments.
Bitcoin transactions don't specify what they do directly. Instead a small binary stack language is used to define programs that when evaluated return whether the transaction "accepts" or rejects the other transactions connected to it.
In SPV mode, scripts are not run, because that would require all transactions to be available and lightweight clients don't have that data. In full mode, this class is used to run the interpreted language. It also has static methods for building scripts.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Script.VerifyFlag
Flags to pass tocorrectlySpends(Transaction, int, TransactionWitness, Coin, Script, Set)
.
-
Field Summary
Fields Modifier and Type Field Description static java.util.EnumSet<Script.VerifyFlag>
ALL_VERIFY_FLAGS
static int
MAX_P2SH_SIGOPS
Max number of sigops allowed in a standard p2sh redeem scriptstatic int
MAX_SCRIPT_ELEMENT_SIZE
static int
SIG_SIZE
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.util.List<ScriptChunk>
chunks()
Gets an immutable list of the scripts parsed form.void
correctlySpends(Transaction txContainingThis, int scriptSigIndex, TransactionWitness witness, Coin value, Script scriptPubKey, java.util.Set<Script.VerifyFlag> verifyFlags)
Verifies that this script (interpreted as a scriptSig) correctly spends the given scriptPubKey.void
correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey, java.util.Set<Script.VerifyFlag> verifyFlags)
Deprecated.Script
createEmptyInputScript(ECKey key, Script redeemScript)
Creates an incomplete scriptSig that, once filled with signatures, can redeem output containing this scriptPubKey.static byte[]
createInputScript(byte[] signature)
static byte[]
createInputScript(byte[] signature, byte[] pubkey)
static byte[]
createMultiSigOutputScript(int threshold, java.util.List<ECKey> pubkeys)
Creates a program that requires at least N of the given keys to sign, using OP_CHECKMULTISIG.java.util.Optional<java.time.Instant>
creationTime()
Gets the creation time of this script, or empty if unknown.static int
decodeFromOpN(int opcode)
static int
encodeToOpN(int value)
boolean
equals(java.lang.Object o)
static void
executeScript(Transaction txContainingThis, long index, Script script, java.util.LinkedList<byte[]> stack, java.util.Set<Script.VerifyFlag> verifyFlags)
Exposes the script interpreter.java.util.List<ScriptChunk>
getChunks()
Deprecated.usechunks()
long
getCreationTimeSeconds()
Deprecated.usecreationTime()
int
getNumberOfBytesRequiredToSpend(ECKey pubKey, Script redeemScript)
Returns number of bytes required to spend this script.int
getNumberOfSignaturesRequiredToSpend()
Returns number of signatures required to satisfy this script.static long
getP2SHSigOpCount(byte[] scriptSig)
Gets the count of P2SH Sig Ops in the Script scriptSigbyte[]
getProgram()
Deprecated.useprogram()
byte[]
getPubKeyHash()
If the program somehow pays to a hash, returns the hash.java.util.List<ECKey>
getPubKeys()
Returns a list of the keys required by this script, assuming a multi-sig script.Script
getScriptSigWithSignature(Script scriptSig, byte[] sigBytes, int index)
Returns a copy of the given scriptSig with the signature inserted in the given position.ScriptType
getScriptType()
Get theScriptType
.int
getSigInsertionIndex(Sha256Hash hash, ECKey signingKey)
Returns the index where a signature by the key should be inserted.static int
getSigOpCount(byte[] program)
Gets the count of regular SigOps in the script program (counting multisig ops as 20)Address
getToAddress(Network network)
Gets the destination address from this script, if it's in the required form.Address
getToAddress(Network network, boolean forcePayToPubKey)
Gets the destination address from this script, if it's in the required form.Address
getToAddress(NetworkParameters params)
Deprecated.Address
getToAddress(NetworkParameters params, boolean forcePayToPubKey)
Deprecated.int
hashCode()
static Script
of(java.util.List<ScriptChunk> chunks)
Wraps given script chunks.static Script
of(java.util.List<ScriptChunk> chunks, java.time.Instant creationTime)
Wraps given script chunks.static Script
parse(byte[] program)
Construct a script that copies and wraps a given program.static Script
parse(byte[] program, java.time.Instant creationTime)
Construct a script that copies and wraps a given program, and a creation time.byte[]
program()
Gets the serialized program as a newly created byte array.static byte[]
removeAllInstancesOf(byte[] inputScript, byte[] chunkToRemove)
Returns the script bytes of inputScript with all instances of the specified script object removedstatic byte[]
removeAllInstancesOfOp(byte[] inputScript, int opCode)
Returns the script bytes of inputScript with all instances of the given op code removedjava.lang.String
toString()
Returns the program opcodes as a string, for example "[1234] DUP HASH160", or "<empty>".static void
writeBytes(java.io.OutputStream os, byte[] buf)
Writes out the given byte buffer to the output stream with the correct opcode prefix To write an integer call writeBytes(out, Utils.reverseBytes(Utils.encodeMPI(val, false)));
-
-
-
Field Detail
-
ALL_VERIFY_FLAGS
public static final java.util.EnumSet<Script.VerifyFlag> ALL_VERIFY_FLAGS
-
MAX_SCRIPT_ELEMENT_SIZE
public static final int MAX_SCRIPT_ELEMENT_SIZE
- See Also:
- Constant Field Values
-
SIG_SIZE
public static final int SIG_SIZE
- See Also:
- Constant Field Values
-
MAX_P2SH_SIGOPS
public static final int MAX_P2SH_SIGOPS
Max number of sigops allowed in a standard p2sh redeem script- See Also:
- Constant Field Values
-
-
Method Detail
-
of
public static Script of(java.util.List<ScriptChunk> chunks)
Wraps given script chunks.- Parameters:
chunks
- chunks to wrap- Returns:
- script that wraps the chunks
-
of
public static Script of(java.util.List<ScriptChunk> chunks, java.time.Instant creationTime)
Wraps given script chunks.- Parameters:
chunks
- chunks to wrapcreationTime
- creation time of the script- Returns:
- script that wraps the chunks
-
parse
public static Script parse(byte[] program) throws ScriptException
Construct a script that copies and wraps a given program. The array is parsed and checked for syntactic validity. Programs like this are e.g. used inTransactionInput
andTransactionOutput
.- Parameters:
program
- array of program bytes- Returns:
- parsed program
- Throws:
ScriptException
- if the program could not be parsed
-
parse
public static Script parse(byte[] program, java.time.Instant creationTime) throws ScriptException
Construct a script that copies and wraps a given program, and a creation time. The array is parsed and checked for syntactic validity. Programs like this are e.g. used inTransactionInput
andTransactionOutput
.- Parameters:
program
- Array of program bytes from a transaction.creationTime
- creation time of the script- Returns:
- parsed program
- Throws:
ScriptException
- if the program could not be parsed
-
program
public byte[] program()
Gets the serialized program as a newly created byte array.- Returns:
- serialized program
-
getProgram
@Deprecated public byte[] getProgram()
Deprecated.useprogram()
-
chunks
public java.util.List<ScriptChunk> chunks()
Gets an immutable list of the scripts parsed form. Each chunk is either an opcode or data element.- Returns:
- script chunks
-
getChunks
@Deprecated public java.util.List<ScriptChunk> getChunks()
Deprecated.usechunks()
-
creationTime
public java.util.Optional<java.time.Instant> creationTime()
Gets the creation time of this script, or empty if unknown.- Returns:
- creation time of this script, or empty if unknown
-
getCreationTimeSeconds
@Deprecated public long getCreationTimeSeconds()
Deprecated.usecreationTime()
-
toString
public java.lang.String toString()
Returns the program opcodes as a string, for example "[1234] DUP HASH160", or "<empty>".- Overrides:
toString
in classjava.lang.Object
-
getPubKeyHash
public byte[] getPubKeyHash() throws ScriptException
If the program somehow pays to a hash, returns the hash.
Otherwise this method throws a ScriptException.
- Throws:
ScriptException
-
getToAddress
public Address getToAddress(Network network) throws ScriptException
Gets the destination address from this script, if it's in the required form.- Throws:
ScriptException
-
getToAddress
@Deprecated public Address getToAddress(NetworkParameters params) throws ScriptException
Deprecated.Gets the destination address from this script, if it's in the required form.- Throws:
ScriptException
-
getToAddress
public Address getToAddress(Network network, boolean forcePayToPubKey) throws ScriptException
Gets the destination address from this script, if it's in the required form.- Parameters:
forcePayToPubKey
- If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer showing addresses rather than pubkeys.- Throws:
ScriptException
-
getToAddress
@Deprecated public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException
Deprecated.Gets the destination address from this script, if it's in the required form.- Parameters:
forcePayToPubKey
- If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer showing addresses rather than pubkeys.- Throws:
ScriptException
-
writeBytes
public static void writeBytes(java.io.OutputStream os, byte[] buf) throws java.io.IOException
Writes out the given byte buffer to the output stream with the correct opcode prefix To write an integer call writeBytes(out, Utils.reverseBytes(Utils.encodeMPI(val, false)));- Throws:
java.io.IOException
-
createMultiSigOutputScript
public static byte[] createMultiSigOutputScript(int threshold, java.util.List<ECKey> pubkeys)
Creates a program that requires at least N of the given keys to sign, using OP_CHECKMULTISIG.
-
createInputScript
public static byte[] createInputScript(byte[] signature, byte[] pubkey)
-
createInputScript
public static byte[] createInputScript(byte[] signature)
-
createEmptyInputScript
public Script createEmptyInputScript(@Nullable ECKey key, @Nullable Script redeemScript)
Creates an incomplete scriptSig that, once filled with signatures, can redeem output containing this scriptPubKey. Instead of the signatures resulting script has OP_0. Having incomplete input script allows to pass around partially signed tx. It is expected that this program later on will be updated with proper signatures.
-
getScriptSigWithSignature
public Script getScriptSigWithSignature(Script scriptSig, byte[] sigBytes, int index)
Returns a copy of the given scriptSig with the signature inserted in the given position.
-
getSigInsertionIndex
public int getSigInsertionIndex(Sha256Hash hash, ECKey signingKey)
Returns the index where a signature by the key should be inserted. Only applicable to a P2SH scriptSig.
-
getPubKeys
public java.util.List<ECKey> getPubKeys()
Returns a list of the keys required by this script, assuming a multi-sig script.- Throws:
ScriptException
- if the script type is not understood or is pay to address or is P2SH (run this method on the "Redeem script" instead).
-
decodeFromOpN
public static int decodeFromOpN(int opcode)
-
encodeToOpN
public static int encodeToOpN(int value)
-
getSigOpCount
public static int getSigOpCount(byte[] program) throws ScriptException
Gets the count of regular SigOps in the script program (counting multisig ops as 20)- Throws:
ScriptException
-
getP2SHSigOpCount
public static long getP2SHSigOpCount(byte[] scriptSig) throws ScriptException
Gets the count of P2SH Sig Ops in the Script scriptSig- Throws:
ScriptException
-
getNumberOfSignaturesRequiredToSpend
public int getNumberOfSignaturesRequiredToSpend()
Returns number of signatures required to satisfy this script.
-
getNumberOfBytesRequiredToSpend
public int getNumberOfBytesRequiredToSpend(@Nullable ECKey pubKey, @Nullable Script redeemScript)
Returns number of bytes required to spend this script. It accepts optional ECKey and redeemScript that may be required for certain types of script to estimate target size.
-
removeAllInstancesOf
public static byte[] removeAllInstancesOf(byte[] inputScript, byte[] chunkToRemove)
Returns the script bytes of inputScript with all instances of the specified script object removed
-
removeAllInstancesOfOp
public static byte[] removeAllInstancesOfOp(byte[] inputScript, int opCode)
Returns the script bytes of inputScript with all instances of the given op code removed
-
executeScript
public static void executeScript(@Nullable Transaction txContainingThis, long index, Script script, java.util.LinkedList<byte[]> stack, java.util.Set<Script.VerifyFlag> verifyFlags) throws ScriptException
Exposes the script interpreter. Normally you should not use this directly, instead useTransactionInput.verify(TransactionOutput)
orcorrectlySpends(Transaction, int, TransactionWitness, Coin, Script, Set)
. This method is useful if you need more precise control or access to the final state of the stack. This interface is very likely to change in future.- Throws:
ScriptException
-
correctlySpends
public void correctlySpends(Transaction txContainingThis, int scriptSigIndex, @Nullable TransactionWitness witness, @Nullable Coin value, Script scriptPubKey, java.util.Set<Script.VerifyFlag> verifyFlags) throws ScriptException
Verifies that this script (interpreted as a scriptSig) correctly spends the given scriptPubKey.- Parameters:
txContainingThis
- The transaction in which this input scriptSig resides. Accessing txContainingThis from another thread while this method runs results in undefined behavior.scriptSigIndex
- The index in txContainingThis of the scriptSig (note: NOT the index of the scriptPubKey).scriptPubKey
- The connected scriptPubKey containing the conditions needed to claim the value.witness
- Transaction witness belonging to the transaction input containing this script. Needed for segwit.value
- Value of the output. Needed for segwit scripts.verifyFlags
- Each flag enables one validation rule.- Throws:
ScriptException
-
correctlySpends
@Deprecated public void correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey, java.util.Set<Script.VerifyFlag> verifyFlags) throws ScriptException
Deprecated.Verifies that this script (interpreted as a scriptSig) correctly spends the given scriptPubKey.- Parameters:
txContainingThis
- The transaction in which this input scriptSig resides. Accessing txContainingThis from another thread while this method runs results in undefined behavior.scriptSigIndex
- The index in txContainingThis of the scriptSig (note: NOT the index of the scriptPubKey).scriptPubKey
- The connected scriptPubKey containing the conditions needed to claim the value.verifyFlags
- Each flag enables one validation rule.- Throws:
ScriptException
-
getScriptType
@Nullable public ScriptType getScriptType()
Get theScriptType
.- Returns:
- The script type, or null if the script is of unknown type
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-