com.google.bitcoin.core
Class TransactionOutput

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

public class TransactionOutput
extends ChildMessage
implements Serializable

A TransactionOutput message contains a scriptPubKey that controls who is able to spend its value. It is a sub-part of the Transaction message.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class com.google.bitcoin.core.Message
Message.LazyParseException
 
Field Summary
 
Fields inherited from class com.google.bitcoin.core.Message
bytes, checksum, cursor, length, MAX_SIZE, offset, params, parsed, parseLazy, parseRetain, protocolVersion, recached, UNKNOWN_LENGTH
 
Constructor Summary
TransactionOutput(NetworkParameters params, Transaction parent, BigInteger value, Address to)
          Creates an output that sends 'value' to the given address (public key hash).
TransactionOutput(NetworkParameters params, Transaction parent, BigInteger value, byte[] scriptBytes)
           
TransactionOutput(NetworkParameters params, Transaction parent, BigInteger value, ECKey to)
          Creates an output that sends 'value' to the given public key using a simple CHECKSIG script (no addresses).
TransactionOutput(NetworkParameters params, Transaction parent, byte[] payload, int offset)
          Deserializes a transaction output message.
TransactionOutput(NetworkParameters params, Transaction parent, byte[] msg, int offset, boolean parseLazy, boolean parseRetain)
          Deserializes a transaction output message.
 
Method Summary
protected  void bitcoinSerializeToStream(OutputStream stream)
          Serializes this message to the provided stream.
 BigInteger getMinNonDustValue()
          Returns the minimum value for this output to be considered "not dust", i.e.
 BigInteger getMinNonDustValue(BigInteger feePerKbRequired)
          Gets the minimum value for a txout of this size to be considered non-dust by a reference client (and thus relayed).
 Transaction getParentTransaction()
          Returns the transaction that owns this output, or throws NullPointerException if unowned.
 byte[] getScriptBytes()
          The backing script bytes which can be turned into a Script object.
 Script getScriptPubKey()
           
 TransactionInput getSpentBy()
          Returns the connected input.
 BigInteger getValue()
          Returns the value of this output in nanocoins.
 boolean isAvailableForSpending()
          Returns whether markAsSpent(TransactionInput) has been called on this class.
 boolean isMine(Wallet wallet)
          Returns true if this output is to a key, or an address we have the keys for, in the wallet.
 boolean isMineOrWatched(Wallet wallet)
          Returns true if this output is to a key in the wallet or to an address/script we are watching.
 boolean isWatched(Wallet wallet)
          Returns true if this output is to a key, or an address we have the keys for, in the wallet.
 void markAsSpent(TransactionInput input)
          Sets this objects availableForSpending flag to false and the spentBy pointer to the given input.
 void markAsUnspent()
          Resets the spent pointer / availableForSpending flag to null.
protected  void parseLite()
          Perform the most minimal parse possible to calculate the length of the message.
 void setValue(BigInteger value)
          Sets the value of this output in nanocoins.
 String toString()
          Returns a human readable debug string.
 
Methods inherited from class com.google.bitcoin.core.ChildMessage
adjustLength, adjustLength, setParent, unCache
 
Methods inherited from class com.google.bitcoin.core.Message
bitcoinSerialize, bitcoinSerialize, ensureParsed, getHash, getMessageSize, getParams, isCached, isParsed, isRecached, maybeParse, unsafeBitcoinSerialize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TransactionOutput

public TransactionOutput(NetworkParameters params,
                         @Nullable
                         Transaction parent,
                         byte[] payload,
                         int offset)
                  throws ProtocolException
Deserializes a transaction output message. This is usually part of a transaction message.

Throws:
ProtocolException

TransactionOutput

public TransactionOutput(NetworkParameters params,
                         Transaction parent,
                         byte[] msg,
                         int offset,
                         boolean parseLazy,
                         boolean parseRetain)
                  throws ProtocolException
Deserializes a transaction output message. This is usually part of a transaction message.

Parameters:
params - NetworkParameters object.
msg - Bitcoin protocol formatted byte array containing message content.
offset - The location of the first msg byte within the array.
parseLazy - Whether to perform a full parse immediately or delay until a read is requested.
parseRetain - Whether to retain the backing byte array for quick reserialization. If true and the backing byte array is invalidated due to modification of a field then the cached bytes may be repopulated and retained if the message is serialized again in the future.
Throws:
ProtocolException

TransactionOutput

public TransactionOutput(NetworkParameters params,
                         Transaction parent,
                         BigInteger value,
                         Address to)
Creates an output that sends 'value' to the given address (public key hash). The amount should be created with something like Utils.toNanoCoins(int, int). Typically you would use Transaction.addOutput(java.math.BigInteger, Address) instead of creating a TransactionOutput directly.


TransactionOutput

public TransactionOutput(NetworkParameters params,
                         Transaction parent,
                         BigInteger value,
                         ECKey to)
Creates an output that sends 'value' to the given public key using a simple CHECKSIG script (no addresses). The amount should be created with something like Utils.toNanoCoins(int, int). Typically you would use Transaction.addOutput(java.math.BigInteger, ECKey) instead of creating an output directly.


TransactionOutput

public TransactionOutput(NetworkParameters params,
                         Transaction parent,
                         BigInteger value,
                         byte[] scriptBytes)
Method Detail

getScriptPubKey

public Script getScriptPubKey()
                       throws ScriptException
Throws:
ScriptException

parseLite

protected void parseLite()
                  throws ProtocolException
Description copied from class: Message
Perform the most minimal parse possible to calculate the length of the message. This is only required for subclasses of ChildClass as root level messages will have their length passed into the constructor.

Implementations should adhere to the following contract: If parseLazy = true the 'length' field must be set before returning. If parseLazy = false the length field must be set either within the parseLite() method OR the parse() method. The overriding requirement is that length must be set to non UNKNOWN_MESSAGE value by the time the constructor exits.

Specified by:
parseLite in class Message
Throws:
ProtocolException

bitcoinSerializeToStream

protected 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

getValue

public BigInteger getValue()
Returns the value of this output in nanocoins. This is the amount of currency that the destination address receives.


setValue

public void setValue(BigInteger value)
Sets the value of this output in nanocoins.


getMinNonDustValue

public BigInteger getMinNonDustValue(BigInteger feePerKbRequired)

Gets the minimum value for a txout of this size to be considered non-dust by a reference client (and thus relayed). See: CTxOut::IsDust() in the reference client. The assumption is that any output that would consume more than a third of its value in fees is not something the Bitcoin system wants to deal with right now, so we call them "dust outputs" and they're made non standard. The choice of one third is somewhat arbitrary and may change in future.

You probably should use getMinNonDustValue() which uses a safe fee-per-kb by default.

Parameters:
feePerKbRequired - The fee required per kilobyte. Note that this is the same as the reference client's -minrelaytxfee * 3 If you want a safe default, use Transaction.REFERENCE_DEFAULT_MIN_TX_FEE*3

getMinNonDustValue

public BigInteger getMinNonDustValue()
Returns the minimum value for this output to be considered "not dust", i.e. the transaction will be relayable and mined by default miners. For normal pay to address outputs, this is 5460 satoshis, the same as Transaction.MIN_NONDUST_OUTPUT.


markAsSpent

public void markAsSpent(TransactionInput input)
Sets this objects availableForSpending flag to false and the spentBy pointer to the given input. If the input is null, it means this output was signed over to somebody else rather than one of our own keys.

Throws:
IllegalStateException - if the transaction was already marked as spent.

markAsUnspent

public void markAsUnspent()
Resets the spent pointer / availableForSpending flag to null.


isAvailableForSpending

public boolean isAvailableForSpending()
Returns whether markAsSpent(TransactionInput) has been called on this class. A Wallet will mark a transaction output as spent once it sees a transaction input that is connected to it. Note that this flag can be false when an output has in fact been spent according to the rest of the network if the spending transaction wasn't downloaded yet, and it can be marked as spent when in reality the rest of the network believes it to be unspent if the signature or script connecting to it was not actually valid.


getScriptBytes

public byte[] getScriptBytes()
The backing script bytes which can be turned into a Script object.

Returns:
the scriptBytes

isMineOrWatched

public boolean isMineOrWatched(Wallet wallet)
Returns true if this output is to a key in the wallet or to an address/script we are watching.


isWatched

public boolean isWatched(Wallet wallet)
Returns true if this output is to a key, or an address we have the keys for, in the wallet.


isMine

public boolean isMine(Wallet wallet)
Returns true if this output is to a key, or an address we have the keys for, in the wallet.


toString

public String toString()
Returns a human readable debug string.

Overrides:
toString in class Object

getSpentBy

public TransactionInput getSpentBy()
Returns the connected input.


getParentTransaction

public Transaction getParentTransaction()
Returns the transaction that owns this output, or throws NullPointerException if unowned.



Copyright © 2014. All rights reserved.