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 | Field and Description |
---|---|
protected List<ScriptChunk> |
chunks |
static long |
MAX_SCRIPT_ELEMENT_SIZE |
protected byte[] |
program |
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,
boolean enforceP2SH)
Verifies that this script (interpreted as a scriptSig) correctly spends the given 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.
|
static int |
decodeFromOpN(byte opcode)
Converts an opcode to its int representation (including OP_1NEGATE and OP_0/OP_FALSE)
|
boolean |
equals(Object obj) |
List<ScriptChunk> |
getChunks()
Returns an immutable list of the scripts parsed form.
|
long |
getCreationTimeSeconds() |
Address |
getFromAddress(NetworkParameters params)
Deprecated.
|
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
|
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).
|
int |
hashCode() |
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 |
isSentToMultiSig()
Returns whether this script matches the format used for multisig outputs: [n] [keys...] [m] CHECKMULTISIG
|
boolean |
isSentToP2SH()
Returns true if this script is of the form OP_HASH160
|
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 long MAX_SCRIPT_ELEMENT_SIZE
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()
public boolean isSentToP2SH()
public byte[] getPubKeyHash() throws ScriptException
This is useful for fetching the destination address of a transaction.
ScriptException
public byte[] getPubKey() throws ScriptException
ScriptException
- if the script is none of the named forms.@Deprecated public Address getFromAddress(NetworkParameters params) throws ScriptException
ScriptException
public Address getToAddress(NetworkParameters params) throws ScriptException
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 static int decodeFromOpN(byte opcode) throws IllegalArgumentException
IllegalArgumentException
- If the opcode is not an OP_N opcodepublic static int getSigOpCount(byte[] program) throws ScriptException
ScriptException
public static long getP2SHSigOpCount(byte[] scriptSig) throws ScriptException
ScriptException
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 Satoshis original implementation. 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 static byte[] removeAllInstancesOf(byte[] inputScript, byte[] chunkToRemove)
public static byte[] removeAllInstancesOfOp(byte[] inputScript, int opCode)
public void correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey, boolean enforceP2SH) 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.enforceP2SH
- Whether "pay to script hash" rules should be enforced. If in doubt, set to true.ScriptException
Copyright © 2014. All rights reserved.