com.google.bitcoin.core
Class Script

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

public class Script
extends Object

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.

This implementation of the scripting language is incomplete. It contains enough support to run standard transactions generated by the official client, but non-standard transactions will fail.


Field Summary
static int OP_CHECKSIG
           
static int OP_DUP
           
static int OP_EQUALVERIFY
           
static int OP_HASH160
           
static int OP_PUSHDATA1
           
static int OP_PUSHDATA2
           
static int OP_PUSHDATA4
           
 
Constructor Summary
Script(NetworkParameters params, byte[] programBytes, int offset, int length)
          Construct a Script using the given network parameters and a range of the programBytes array.
 
Method Summary
 Address getFromAddress()
          Convenience wrapper around getPubKey.
 byte[] getPubKey()
          If a program has two data buffers (constants) and nothing else, the second one is returned.
 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.
 Address getToAddress()
          Gets the destination address from this script, if it's in the required form (see getPubKey).
 boolean isSentToIP()
          Returns true if this transaction is of a format that means it was a direct IP to IP transaction.
static Script join(Script a, Script b)
          Concatenates two scripts to form a new one.
 boolean run(Transaction context)
          Runs the script with the given Transaction as the "context".
 void setTracing(boolean value)
          If true, running a program will log its instructions.
 String toString()
          Returns the program opcodes as a string, for example "[1234] DUP HAHS160"
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

OP_PUSHDATA1

public static final int OP_PUSHDATA1
See Also:
Constant Field Values

OP_PUSHDATA2

public static final int OP_PUSHDATA2
See Also:
Constant Field Values

OP_PUSHDATA4

public static final int OP_PUSHDATA4
See Also:
Constant Field Values

OP_DUP

public static final int OP_DUP
See Also:
Constant Field Values

OP_HASH160

public static final int OP_HASH160
See Also:
Constant Field Values

OP_EQUALVERIFY

public static final int OP_EQUALVERIFY
See Also:
Constant Field Values

OP_CHECKSIG

public static final int OP_CHECKSIG
See Also:
Constant Field Values
Constructor Detail

Script

public Script(NetworkParameters params,
              byte[] programBytes,
              int offset,
              int length)
       throws ScriptException
Construct a Script using the given network parameters and a range of the programBytes array.

Parameters:
params - Network parameters.
programBytes - Array of program bytes from a transaction.
offset - How many bytes into programBytes to start reading from.
length - How many bytes to read.
Throws:
ScriptException
Method Detail

join

public static Script join(Script a,
                          Script b)
                   throws ScriptException
Concatenates two scripts to form a new one. This is used when verifying transactions.

Throws:
ScriptException

setTracing

public void setTracing(boolean value)
If true, running a program will log its instructions.


toString

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

Overrides:
toString in class Object

isSentToIP

public boolean isSentToIP()
Returns true if this transaction is of a format that means it was a direct IP to IP transaction. These transactions are deprecated and no longer used, support for creating them has been removed from the official client.


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
If a program has two data buffers (constants) and nothing else, the second one is returned. For a scriptSig this should be the public key of the sender. This is useful for fetching the source address of a transaction.

Throws:
ScriptException

getFromAddress

public Address getFromAddress()
                       throws ScriptException
Convenience wrapper around getPubKey. Only works for scriptSigs.

Throws:
ScriptException

getToAddress

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

Throws:
ScriptException

run

public boolean run(Transaction context)
            throws ScriptException
Runs the script with the given Transaction as the "context". Some operations like CHECKSIG require a transaction to operate on (eg to hash). The context transaction is typically the transaction having its inputs verified, ie the one where the scriptSig comes from.

Throws:
ScriptException


Copyright © 2011. All Rights Reserved.