com.google.bitcoin.script
Class Script

java.lang.Object
  extended by com.google.bitcoin.script.Script

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.


Field Summary
protected  List<ScriptChunk> chunks
           
static long MAX_SCRIPT_ELEMENT_SIZE
           
protected  byte[] program
           
 
Constructor Summary
Script(byte[] programBytes)
          Construct a Script that copies and wraps the programBytes array.
Script(byte[] programBytes, long creationTimeSeconds)
           
 
Method Summary
 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 EQUALVERIFY CHECKSIG then this function retrieves the third element, otherwise it throws a ScriptException.
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 EQUALVERIFY CHECKSIG, ie, payment to an address like 1VayNert3x1KzbpzMGt2qdqrAThiRovi8.
 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 OP_EQUAL, ie, payment to an address like 35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU.
 boolean isSentToRawPubKey()
          Returns true if this script is of the form OP_CHECKSIG.
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)));
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_SCRIPT_ELEMENT_SIZE

public static final long MAX_SCRIPT_ELEMENT_SIZE
See Also:
Constant Field Values

chunks

protected List<ScriptChunk> chunks

program

protected byte[] program
Constructor Detail

Script

public Script(byte[] programBytes)
       throws ScriptException
Construct a Script that copies and wraps the programBytes array. The array is parsed and checked for syntactic validity.

Parameters:
programBytes - Array of program bytes from a transaction.
Throws:
ScriptException

Script

public Script(byte[] programBytes,
              long creationTimeSeconds)
       throws ScriptException
Throws:
ScriptException
Method Detail

getCreationTimeSeconds

public long getCreationTimeSeconds()

setCreationTimeSeconds

public void setCreationTimeSeconds(long creationTimeSeconds)

toString

public String toString()
Returns the program opcodes as a string, for example "[1234] DUP HASH160"

Overrides:
toString in class Object

getProgram

public byte[] getProgram()
Returns the serialized program as a newly created byte array.


getChunks

public List<ScriptChunk> getChunks()
Returns an immutable list of the scripts parsed form.


isSentToRawPubKey

public boolean isSentToRawPubKey()
Returns true if this script is of the form OP_CHECKSIG. This form was originally intended for transactions where the peers talked to each other directly via TCP/IP, but has fallen out of favor with time due to that mode of operation being susceptible to man-in-the-middle attacks. It is still used in coinbase outputs and can be useful more exotic types of transaction, but today most payments are to addresses.


isSentToAddress

public boolean isSentToAddress()
Returns true if this script is of the form DUP HASH160 EQUALVERIFY CHECKSIG, ie, payment to an address like 1VayNert3x1KzbpzMGt2qdqrAThiRovi8. This form was originally intended for the case where you wish to send somebody money with a written code because their node is offline, but over time has become the standard way to make payments due to the short and recognizable base58 form addresses come in.


isSentToP2SH

public boolean isSentToP2SH()
Returns true if this script is of the form OP_HASH160 OP_EQUAL, ie, payment to an address like 35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU. This form was codified as part of BIP13 and BIP16, for pay to script hash type addresses.


getPubKeyHash

public byte[] getPubKeyHash()
                     throws ScriptException
If a program matches the standard template DUP HASH160 EQUALVERIFY CHECKSIG then this function retrieves the third element, otherwise it throws a ScriptException.

This is useful for fetching the destination address of a transaction.

Throws:
ScriptException

getPubKey

public byte[] getPubKey()
                 throws ScriptException
Returns the public key in this script. If a script contains two constants and nothing else, it is assumed to be a scriptSig (input) for a pay-to-address output and the second constant is returned (the first is the signature). If a script contains a constant and an OP_CHECKSIG opcode, the constant is returned as it is assumed to be a direct pay-to-key scriptPubKey (output) and the first constant is the public key.

Throws:
ScriptException - if the script is none of the named forms.

getFromAddress

@Deprecated
public Address getFromAddress(NetworkParameters params)
                       throws ScriptException
Deprecated. 

For 2-element [input] scripts assumes that the paid-to-address can be derived from the public key. The concept of a "from address" isn't well defined in Bitcoin and you should not assume the sender of a transaction can actually receive coins on it. This method may be removed in future.

Throws:
ScriptException

getToAddress

public Address getToAddress(NetworkParameters params)
                     throws ScriptException
Gets the destination address from this script, if it's in the required form (see getPubKey).

Throws:
ScriptException

writeBytes

public static void writeBytes(OutputStream os,
                              byte[] buf)
                       throws 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:
IOException

createMultiSigOutputScript

public 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.


createInputScript

public static byte[] createInputScript(byte[] signature,
                                       byte[] pubkey)

createInputScript

public static byte[] createInputScript(byte[] signature)

decodeFromOpN

public static int decodeFromOpN(byte opcode)
                         throws IllegalArgumentException
Converts an opcode to its int representation (including OP_1NEGATE and OP_0/OP_FALSE)

Throws:
IllegalArgumentException - If the opcode is not an OP_N opcode

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

isPayToScriptHash

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).


isSentToMultiSig

public boolean isSentToMultiSig()
Returns whether this script matches the format used for multisig outputs: [n] [keys...] [m] CHECKMULTISIG


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


correctlySpends

public void correctlySpends(Transaction txContainingThis,
                            long scriptSigIndex,
                            Script scriptPubKey,
                            boolean enforceP2SH)
                     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.
enforceP2SH - Whether "pay to script hash" rules should be enforced. If in doubt, set to true.
Throws:
ScriptException

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object


Copyright © 2014. All rights reserved.