public class Script extends 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 EnumSet<Script.VerifyFlag> |
ALL_VERIFY_FLAGS |
protected 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,
long scriptSigIndex,
Script scriptPubKey)
Deprecated.
Use
correctlySpends(org.bitcoinj.core.Transaction, long, org.bitcoinj.script.Script, java.util.Set)
instead so that verification flags do not change as new verification options
are added. |
void |
correctlySpends(Transaction txContainingThis,
long scriptSigIndex,
Script scriptPubKey,
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.
|
static byte[] |
createInputScript(byte[] signature) |
static byte[] |
createInputScript(byte[] signature,
byte[] pubkey) |
static byte[] |
createMultiSigOutputScript(int threshold,
List<ECKey> pubkeys)
Creates a program that requires at least N of the given keys to sign, using OP_CHECKMULTISIG.
|
boolean |
equals(Object o) |
static void |
executeScript(Transaction txContainingThis,
long index,
Script script,
LinkedList<byte[]> stack,
boolean enforceNullDummy)
|
static void |
executeScript(Transaction txContainingThis,
long index,
Script script,
LinkedList<byte[]> stack,
Set<Script.VerifyFlag> verifyFlags)
Exposes the script interpreter.
|
List<ScriptChunk> |
getChunks()
Returns an immutable list of the scripts parsed form.
|
BigInteger |
getCLTVPaymentChannelExpiry() |
byte[] |
getCLTVPaymentChannelRecipientPubKey()
Retrieves the recipient public key from a LOCKTIMEVERIFY transaction
|
byte[] |
getCLTVPaymentChannelSenderPubKey()
Retrieves the sender public key from a LOCKTIMEVERIFY transaction
|
long |
getCreationTimeSeconds() |
Address |
getFromAddress(NetworkParameters params)
Deprecated.
|
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[] |
getPubKey()
Returns the public key in this script.
|
byte[] |
getPubKeyHash()
If a program matches the standard template DUP HASH160 <pubkey hash> EQUALVERIFY CHECKSIG
then this function retrieves the third element.
|
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 (see getPubKey).
|
Address |
getToAddress(NetworkParameters params,
boolean forcePayToPubKey)
Gets the destination address from this script, if it's in the required form (see getPubKey).
|
int |
hashCode() |
boolean |
isOpReturn() |
boolean |
isPayToScriptHash()
Whether or not this is a scriptPubKey representing a pay-to-script-hash output.
|
boolean |
isSentToAddress()
Returns true if this script is of the form DUP HASH160
|
boolean |
isSentToCLTVPaymentChannel() |
boolean |
isSentToMultiSig()
Returns whether this script matches the format used for multisig outputs: [n] [keys...] [m] CHECKMULTISIG
|
boolean |
isSentToP2SH()
Deprecated.
|
boolean |
isSentToRawPubKey()
Returns true if this script is of the form
|
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) |
String |
toString()
Returns the program opcodes as a string, for example "[1234] DUP HASH160"
|
static void |
writeBytes(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 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 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 String toString()
public byte[] getProgram()
public List<ScriptChunk> getChunks()
public boolean isSentToRawPubKey()
public boolean isSentToAddress()
@Deprecated public boolean isSentToP2SH()
public byte[] getPubKeyHash() throws ScriptException
If a program matches the standard template DUP HASH160 <pubkey hash> EQUALVERIFY CHECKSIG then this function retrieves the third element. In this case, this is useful for fetching the destination address of a transaction.
If a program matches the standard template HASH160 <script hash> EQUAL then this function retrieves the second element. In this case, this is useful for fetching the hash of the redeem script of a transaction.
Otherwise it throws a ScriptException.
ScriptException
public byte[] getPubKey() throws ScriptException
ScriptException
- if the script is none of the named forms.public byte[] getCLTVPaymentChannelSenderPubKey() throws ScriptException
ScriptException
public byte[] getCLTVPaymentChannelRecipientPubKey() throws ScriptException
ScriptException
public BigInteger getCLTVPaymentChannelExpiry()
@Deprecated public Address getFromAddress(NetworkParameters params) throws 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(OutputStream os, byte[] buf) throws IOException
IOException
public static byte[] createMultiSigOutputScript(int threshold, 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 Script getScriptSigWithSignature(Script scriptSig, byte[] sigBytes, int index)
public int getSigInsertionIndex(Sha256Hash hash, ECKey signingKey)
public 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 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)
public boolean isPayToScriptHash()
Whether or not this is a scriptPubKey representing a pay-to-script-hash output. In such outputs, the logic that controls reclamation is not actually in the output at all. Instead there's just a hash, and it's up to the spending input to provide a program matching that hash. This rule is "soft enforced" by the network as it does not exist in Bitcoin Core. It means blocks containing P2SH transactions that don't match correctly are considered valid, but won't be mined upon, so they'll be rapidly re-orgd out of the chain. This logic is defined by BIP 16.
bitcoinj does not support creation of P2SH transactions today. The goal of P2SH is to allow short addresses even for complex scripts (eg, multi-sig outputs) so they are convenient to work with in things like QRcodes or with copy/paste, and also to minimize the size of the unspent output set (which improves performance of the Bitcoin system).
public boolean isSentToMultiSig()
public boolean isSentToCLTVPaymentChannel()
public static byte[] removeAllInstancesOf(byte[] inputScript, byte[] chunkToRemove)
public static byte[] removeAllInstancesOfOp(byte[] inputScript, int opCode)
public boolean isOpReturn()
@Deprecated public static void executeScript(@Nullable Transaction txContainingThis, long index, Script script, LinkedList<byte[]> stack, boolean enforceNullDummy) throws ScriptException
executeScript(org.bitcoinj.core.Transaction, long, org.bitcoinj.script.Script, java.util.LinkedList, java.util.Set)
instead.TransactionInput.verify(org.bitcoinj.core.TransactionOutput)
or
correctlySpends(org.bitcoinj.core.Transaction, long, Script)
. 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, LinkedList<byte[]> stack, Set<Script.VerifyFlag> verifyFlags) throws ScriptException
TransactionInput.verify(org.bitcoinj.core.TransactionOutput)
or
correctlySpends(org.bitcoinj.core.Transaction, long, Script)
. 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(org.bitcoinj.core.Transaction, long, org.bitcoinj.script.Script, java.util.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, long scriptSigIndex, Script scriptPubKey, 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. If in doubt, use correctlySpends(Transaction, long, Script)
which sets all flags.ScriptException
public Script.ScriptType getScriptType()
Script.ScriptType
.Copyright © 2016. All rights reserved.