com.google.bitcoin.core
Class Transaction

java.lang.Object
  extended by com.google.bitcoin.core.Message
      extended by com.google.bitcoin.core.Transaction
All Implemented Interfaces:
Serializable

public class Transaction
extends Message
implements Serializable

A transaction represents the movement of coins from some addresses to some other addresses. It can also represent the minting of new coins. A Transaction object corresponds to the equivalent in the BitCoin C++ implementation.

It implements TWO serialization protocols - the BitCoin proprietary format which is identical to the C++ implementation and is used for reading/writing transactions to the wire and for hashing. It also implements Java serialization which is used for the wallet. This allows us to easily add extra fields used for our own accounting or UI purposes.

See Also:
Serialized Form

Nested Class Summary
static class Transaction.SigHash
          These constants are a part of a scriptSig signature on the inputs.
 
Field Summary
 
Fields inherited from class com.google.bitcoin.core.Message
bytes, cursor, MAX_SIZE, offset, params, protocolVersion
 
Constructor Summary
Transaction(NetworkParameters params, byte[] payloadBytes)
          Creates a transaction from the given serialized bytes, eg, from a block or a tx network message.
Transaction(NetworkParameters params, byte[] payload, int offset)
          Creates a transaction by reading payload starting from offset bytes in.
 
Method Summary
 void addInput(TransactionOutput from)
          Adds an input to this transaction that imports value from the given output.
 void addOutput(TransactionOutput to)
          Adds the given output to this transaction.
 void bitcoinSerializeToStream(OutputStream stream)
          Serializes this message to the provided stream.
 boolean equals(Object other)
           
 Sha256Hash getHash()
          Returns the transaction hash as you see them in the block explorer.
 String getHashAsString()
           
 List<TransactionInput> getInputs()
          Returns a read-only list of the inputs of this transaction.
 BigInteger getValueSentFromMe(Wallet wallet)
          Calculates the sum of the inputs that are spending coins with keys in the wallet.
 BigInteger getValueSentToMe(Wallet wallet)
          Calculates the sum of the outputs that are sending coins to a key in the wallet.
 int hashCode()
           
 boolean isCoinBase()
          A coinbase transaction is one that creates a new coin.
 void signInputs(Transaction.SigHash hashType, Wallet wallet)
          Once a transaction has some inputs and outputs added, the signatures in the inputs can be calculated.
 String toString()
           
 boolean verifyInput(int inputIndex, Transaction connectedTx)
          Given a named input and the transaction output it connects to, runs the script formed from the concatenation of the input and output scripts, returning true if the link is valid.
 
Methods inherited from class com.google.bitcoin.core.Message
bitcoinSerialize
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Transaction

public Transaction(NetworkParameters params,
                   byte[] payloadBytes)
            throws ProtocolException
Creates a transaction from the given serialized bytes, eg, from a block or a tx network message.

Throws:
ProtocolException

Transaction

public Transaction(NetworkParameters params,
                   byte[] payload,
                   int offset)
            throws ProtocolException
Creates a transaction by reading payload starting from offset bytes in. Length of a transaction is fixed.

Throws:
ProtocolException
Method Detail

getInputs

public List<TransactionInput> getInputs()
Returns a read-only list of the inputs of this transaction.


getHash

public Sha256Hash getHash()
Returns the transaction hash as you see them in the block explorer.


getHashAsString

public String getHashAsString()

getValueSentToMe

public BigInteger getValueSentToMe(Wallet wallet)
Calculates the sum of the outputs that are sending coins to a key in the wallet.


getValueSentFromMe

public BigInteger getValueSentFromMe(Wallet wallet)
                              throws ScriptException
Calculates the sum of the inputs that are spending coins with keys in the wallet. This requires the transactions sending coins to those keys to be in the wallet. This method will not attempt to download the blocks containing the input transactions if the key is in the wallet but the transactions are not.

Returns:
sum in nanocoins.
Throws:
ScriptException

isCoinBase

public boolean isCoinBase()
A coinbase transaction is one that creates a new coin. They are the first transaction in each block and their value is determined by a formula that all implementations of BitCoin share. In 2011 the value of a coinbase transaction is 50 coins, but in future it will be less. A coinbase transaction is defined not only by its position in a block but by the data in the inputs.


toString

public String toString()
Overrides:
toString in class Object
Returns:
A human readable version of the transaction useful for debugging.

addInput

public void addInput(TransactionOutput from)
Adds an input to this transaction that imports value from the given output. Note that this input is NOT complete and after every input is added with addInput() and every output is added with addOutput(), signInputs() must be called to finalize the transaction and finish the inputs off. Otherwise it won't be accepted by the network.


addOutput

public void addOutput(TransactionOutput to)
Adds the given output to this transaction. The output must be completely initialized.


signInputs

public void signInputs(Transaction.SigHash hashType,
                       Wallet wallet)
                throws ScriptException
Once a transaction has some inputs and outputs added, the signatures in the inputs can be calculated. The signature is over the transaction itself, to prove the redeemer actually created that transaction, so we have to do this step last.

This method is similar to SignatureHash in script.cpp

Parameters:
hashType - This should always be set to SigHash.ALL currently. Other types are unused.
wallet - A wallet is required to fetch the keys needed for signing.
Throws:
ScriptException

verifyInput

public boolean verifyInput(int inputIndex,
                           Transaction connectedTx)
                    throws ScriptException
Given a named input and the transaction output it connects to, runs the script formed from the concatenation of the input and output scripts, returning true if the link is valid. In this way, we prove that the creator of this transaction is allowed to redeem the output of the connectedTx and thus spend the money.

WARNING: NOT FINISHED

Parameters:
inputIndex - Which input to verify.
connectedTx - The Transaction that the input is connected to.
Throws:
ScriptException

bitcoinSerializeToStream

public void bitcoinSerializeToStream(OutputStream stream)
                              throws IOException
Description copied from class: Message
Serializes this message to the provided stream. If you just want the raw bytes use bitcoinSerialize().

Throws:
IOException

equals

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

hashCode

public int hashCode()
Overrides:
hashCode in class Object


Copyright © 2011. All Rights Reserved.