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.
Modifier and Type | Class and Description |
---|---|
static class |
Script.ScriptType
Enumeration to encapsulate the type of this script.
|
static class |
Script.VerifyFlag
Flags to pass to
correctlySpends(Transaction, long, Script, Set) . |
Modifier and Type | Field and Description |
---|---|
static java.util.EnumSet<Script.VerifyFlag> |
ALL_VERIFY_FLAGS |
protected java.util.List<ScriptChunk> |
chunks |
static int |
MAX_P2SH_SIGOPS
Max number of sigops allowed in a standard p2sh redeem script
|
static long |
MAX_SCRIPT_ELEMENT_SIZE |
protected byte[] |
program |
static int |
SIG_SIZE |
Constructor and Description |
---|
Script(byte[] programBytes)
Construct a Script that copies and wraps the programBytes array.
|
Script(byte[] programBytes,
long creationTimeSeconds) |
Modifier and Type | Method and Description |
---|---|
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)
Deprecated.
Use
correctlySpends(Transaction, int, TransactionWitness, Coin, Script, Set)
instead so that verification flags do not change as new verification options
are added. |
void |
correctlySpends(Transaction txContainingThis,
long scriptSigIndex,
Script scriptPubKey,
java.util.Set<Script.VerifyFlag> verifyFlags)
Verifies that this script (interpreted as a scriptSig) correctly spends the given scriptPubKey.
|
Script |
createEmptyInputScript(ECKey key,
Script redeemScript)
Creates an incomplete scriptSig that, once filled with signatures, can redeem output containing this scriptPubKey.
|
TransactionWitness |
createEmptyWitness(ECKey key) |
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.
|
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,
boolean enforceNullDummy)
Deprecated.
|
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()
Returns an immutable list of the scripts parsed form.
|
long |
getCreationTimeSeconds() |
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 scriptSig
|
byte[] |
getProgram()
Returns the serialized program as a newly created byte array.
|
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.
|
Script.ScriptType |
getScriptType()
Get the
Script.ScriptType . |
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(NetworkParameters params)
Gets the destination address from this script, if it's in the required form.
|
Address |
getToAddress(NetworkParameters params,
boolean forcePayToPubKey)
Gets the destination address from this script, if it's in the required form.
|
int |
hashCode() |
boolean |
isOpReturn()
Deprecated.
|
boolean |
isPayToScriptHash()
Deprecated.
|
boolean |
isSentToAddress()
Deprecated.
|
boolean |
isSentToMultiSig()
Deprecated.
|
boolean |
isSentToRawPubKey()
Deprecated.
|
static byte[] |
removeAllInstancesOf(byte[] inputScript,
byte[] chunkToRemove)
Returns the script bytes of inputScript with all instances of the specified script object removed
|
static byte[] |
removeAllInstancesOfOp(byte[] inputScript,
int opCode)
Returns the script bytes of inputScript with all instances of the given op code removed
|
void |
setCreationTimeSeconds(long creationTimeSeconds) |
java.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)));
|
public static final java.util.EnumSet<Script.VerifyFlag> ALL_VERIFY_FLAGS
public static final long MAX_SCRIPT_ELEMENT_SIZE
public static final int SIG_SIZE
public static final int MAX_P2SH_SIGOPS
protected java.util.List<ScriptChunk> chunks
protected byte[] program
public Script(byte[] programBytes) throws ScriptException
programBytes
- Array of program bytes from a transaction.ScriptException
public Script(byte[] programBytes, long creationTimeSeconds) throws ScriptException
ScriptException
public long getCreationTimeSeconds()
public void setCreationTimeSeconds(long creationTimeSeconds)
public java.lang.String toString()
toString
in class java.lang.Object
public byte[] getProgram()
public java.util.List<ScriptChunk> getChunks()
@Deprecated public boolean isSentToRawPubKey()
ScriptPattern.isP2PK(Script)
@Deprecated public boolean isSentToAddress()
ScriptPattern.isP2PKH(Script)
public byte[] getPubKeyHash() throws ScriptException
If the program somehow pays to a hash, returns the hash.
Otherwise this method throws a ScriptException.
ScriptException
public Address getToAddress(NetworkParameters params) throws ScriptException
ScriptException
public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException
forcePayToPubKey
- If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer
showing addresses rather than pubkeys.ScriptException
public static void writeBytes(java.io.OutputStream os, byte[] buf) throws java.io.IOException
java.io.IOException
public static byte[] createMultiSigOutputScript(int threshold, java.util.List<ECKey> pubkeys)
public static byte[] createInputScript(byte[] signature, byte[] pubkey)
public static byte[] createInputScript(byte[] signature)
public Script createEmptyInputScript(@Nullable ECKey key, @Nullable Script redeemScript)
public TransactionWitness createEmptyWitness(ECKey key)
public Script getScriptSigWithSignature(Script scriptSig, byte[] sigBytes, int index)
public int getSigInsertionIndex(Sha256Hash hash, ECKey signingKey)
public java.util.List<ECKey> getPubKeys()
ScriptException
- if the script type is not understood or is pay to address or is P2SH (run this method on the "Redeem script" instead).public static int decodeFromOpN(int opcode)
public static int encodeToOpN(int value)
public static int getSigOpCount(byte[] program) throws ScriptException
ScriptException
public static long getP2SHSigOpCount(byte[] scriptSig) throws ScriptException
ScriptException
public int getNumberOfSignaturesRequiredToSpend()
public int getNumberOfBytesRequiredToSpend(@Nullable ECKey pubKey, @Nullable Script redeemScript)
@Deprecated public boolean isPayToScriptHash()
ScriptPattern.isP2SH(Script)
@Deprecated public boolean isSentToMultiSig()
ScriptPattern.isSentToMultisig(Script)
public static byte[] removeAllInstancesOf(byte[] inputScript, byte[] chunkToRemove)
public static byte[] removeAllInstancesOfOp(byte[] inputScript, int opCode)
@Deprecated public boolean isOpReturn()
ScriptPattern.isOpReturn(Script)
@Deprecated public static void executeScript(@Nullable Transaction txContainingThis, long index, Script script, java.util.LinkedList<byte[]> stack, boolean enforceNullDummy) throws ScriptException
executeScript(Transaction, long, Script, LinkedList, Set)
instead.TransactionInput.verify(TransactionOutput)
or
correctlySpends(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.ScriptException
public static void executeScript(@Nullable Transaction txContainingThis, long index, Script script, java.util.LinkedList<byte[]> stack, java.util.Set<Script.VerifyFlag> verifyFlags) throws ScriptException
TransactionInput.verify(TransactionOutput)
or
correctlySpends(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.ScriptException
@Deprecated public void correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey) throws ScriptException
correctlySpends(Transaction, int, TransactionWitness, Coin, Script, Set)
instead so that verification flags do not change as new verification options
are added.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.ScriptException
public void correctlySpends(Transaction txContainingThis, int scriptSigIndex, @Nullable TransactionWitness witness, @Nullable Coin value, Script scriptPubKey, java.util.Set<Script.VerifyFlag> verifyFlags) throws ScriptException
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.ScriptException
public void correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey, java.util.Set<Script.VerifyFlag> verifyFlags) throws ScriptException
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.ScriptException
@Nullable public Script.ScriptType getScriptType()
Script.ScriptType
.public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object