com.google.bitcoin.core
Class TransactionInput

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

public class TransactionInput
extends ChildMessage
implements Serializable

A transfer of coins from one address to another creates a transaction in which the outputs can be claimed by the recipient in the input of another transaction. You can imagine a transaction as being a module which is wired up to others, the inputs of one have to be wired to the outputs of another. The exceptions are coinbase transactions, which create new coins.

See Also:
Serialized Form

Nested Class Summary
static class TransactionInput.ConnectionResult
           
static class TransactionInput.ConnectMode
           
 
Nested classes/interfaces inherited from class com.google.bitcoin.core.Message
Message.LazyParseException
 
Field Summary
static byte[] EMPTY_ARRAY
           
static long NO_SEQUENCE
           
 
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
TransactionInput(NetworkParameters params, Transaction parentTransaction, byte[] scriptBytes)
          Creates an input that connects to nothing - used only in creation of coinbase transactions.
TransactionInput(NetworkParameters params, Transaction parentTransaction, byte[] payload, int offset)
          Deserializes an input message.
TransactionInput(NetworkParameters params, Transaction parentTransaction, byte[] msg, int offset, boolean parseLazy, boolean parseRetain)
          Deserializes an input message.
TransactionInput(NetworkParameters params, Transaction parentTransaction, byte[] scriptBytes, TransactionOutPoint outpoint)
           
 
Method Summary
protected  void bitcoinSerializeToStream(OutputStream stream)
          Serializes this message to the provided stream.
 TransactionInput.ConnectionResult connect(Map<Sha256Hash,Transaction> transactions, TransactionInput.ConnectMode mode)
          Connects this input to the relevant output of the referenced transaction if it's in the given map.
 void connect(TransactionOutput out)
          Internal use only: connects this TransactionInput to the given output (updates pointers and spent flags)
 TransactionInput.ConnectionResult connect(Transaction transaction, TransactionInput.ConnectMode mode)
          Connects this input to the relevant output of the referenced transaction.
 boolean disconnect()
          If this input is connected, check the output is connected back to this input and release it if so, making it spendable once again.
 TransactionOutput getConnectedOutput()
          Returns the connected output, assuming the input was connected with connect(TransactionOutput) or variants at some point.
 Address getFromAddress()
          Deprecated. 
 TransactionOutPoint getOutpoint()
           
 Transaction getParentTransaction()
           
 byte[] getScriptBytes()
          The "script bytes" might not actually be a script.
 Script getScriptSig()
          Returns the script that is fed to the referenced output (scriptPubKey) script in order to satisfy it: usually contains signatures and maybe keys, but can contain arbitrary data if the output script accepts it.
 long getSequenceNumber()
          Sequence numbers allow participants in a multi-party transaction signing protocol to create new versions of the transaction independently of each other.
 boolean hasSequence()
           
 boolean isCoinBase()
          Coinbase transactions have special inputs with hashes of zero.
protected  void parseLite()
          Perform the most minimal parse possible to calculate the length of the message.
 void setScriptSig(Script scriptSig)
          Set the given program as the scriptSig that is supposed to satisfy the connected output script.
 void setSequenceNumber(long sequence)
          Sequence numbers allow participants in a multi-party transaction signing protocol to create new versions of the transaction independently of each other.
 String toString()
          Returns a human readable debug string.
 void verify()
          For a connected transaction, runs the script against the connected pubkey and verifies they are correct.
 void verify(TransactionOutput output)
          Verifies that this input can spend the given output.
 
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
 

Field Detail

NO_SEQUENCE

public static final long NO_SEQUENCE
See Also:
Constant Field Values

EMPTY_ARRAY

public static final byte[] EMPTY_ARRAY
Constructor Detail

TransactionInput

public TransactionInput(NetworkParameters params,
                        Transaction parentTransaction,
                        byte[] scriptBytes)
Creates an input that connects to nothing - used only in creation of coinbase transactions.


TransactionInput

public TransactionInput(NetworkParameters params,
                        @Nullable
                        Transaction parentTransaction,
                        byte[] scriptBytes,
                        TransactionOutPoint outpoint)

TransactionInput

public TransactionInput(NetworkParameters params,
                        Transaction parentTransaction,
                        byte[] payload,
                        int offset)
                 throws ProtocolException
Deserializes an input message. This is usually part of a transaction message.

Throws:
ProtocolException

TransactionInput

public TransactionInput(NetworkParameters params,
                        Transaction parentTransaction,
                        byte[] msg,
                        int offset,
                        boolean parseLazy,
                        boolean parseRetain)
                 throws ProtocolException
Deserializes an input 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. as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH
Throws:
ProtocolException
Method Detail

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

isCoinBase

public boolean isCoinBase()
Coinbase transactions have special inputs with hashes of zero. If this is such an input, returns true.


getScriptSig

public Script getScriptSig()
                    throws ScriptException
Returns the script that is fed to the referenced output (scriptPubKey) script in order to satisfy it: usually contains signatures and maybe keys, but can contain arbitrary data if the output script accepts it.

Throws:
ScriptException

setScriptSig

public void setScriptSig(Script scriptSig)
Set the given program as the scriptSig that is supposed to satisfy the connected output script.


getFromAddress

@Deprecated
public Address getFromAddress()
                       throws ScriptException
Deprecated. 

Convenience method that returns the from address of this input by parsing the scriptSig. The concept of a "from address" is not well defined in Bitcoin and you should not assume that senders of a transaction can actually receive coins on the same address they used to sign (e.g. this is not true for shared wallets).

Throws:
ScriptException

getSequenceNumber

public long getSequenceNumber()
Sequence numbers allow participants in a multi-party transaction signing protocol to create new versions of the transaction independently of each other. Newer versions of a transaction can replace an existing version that's in nodes memory pools if the existing version is time locked. See the Contracts page on the Bitcoin wiki for examples of how you can use this feature to build contract protocols. Note that as of 2012 the tx replacement feature is disabled so sequence numbers are unusable.


setSequenceNumber

public void setSequenceNumber(long sequence)
Sequence numbers allow participants in a multi-party transaction signing protocol to create new versions of the transaction independently of each other. Newer versions of a transaction can replace an existing version that's in nodes memory pools if the existing version is time locked. See the Contracts page on the Bitcoin wiki for examples of how you can use this feature to build contract protocols. Note that as of 2012 the tx replacement feature is disabled so sequence numbers are unusable.


getOutpoint

public TransactionOutPoint getOutpoint()
Returns:
The previous output transaction reference, as an OutPoint structure. This contains the data needed to connect to the output of the transaction we're gathering coins from.

getScriptBytes

public byte[] getScriptBytes()
The "script bytes" might not actually be a script. In coinbase transactions where new coins are minted there is no input transaction, so instead the scriptBytes contains some extra stuff (like a rollover nonce) that we don't care about much. The bytes are turned into a Script object (cached below) on demand via a getter.

Returns:
the scriptBytes

getParentTransaction

public Transaction getParentTransaction()
Returns:
The Transaction that owns this input.

toString

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

Overrides:
toString in class Object

connect

public TransactionInput.ConnectionResult connect(Map<Sha256Hash,Transaction> transactions,
                                                 TransactionInput.ConnectMode mode)
Connects this input to the relevant output of the referenced transaction if it's in the given map. Connecting means updating the internal pointers and spent flags. If the mode is to ABORT_ON_CONFLICT then the spent output won't be changed, but the outpoint.fromTx pointer will still be updated.

Parameters:
transactions - Map of txhash->transaction.
mode - Whether to abort if there's a pre-existing connection or not.
Returns:
NO_SUCH_TX if the prevtx wasn't found, ALREADY_SPENT if there was a conflict, SUCCESS if not.

connect

public TransactionInput.ConnectionResult connect(Transaction transaction,
                                                 TransactionInput.ConnectMode mode)
Connects this input to the relevant output of the referenced transaction. Connecting means updating the internal pointers and spent flags. If the mode is to ABORT_ON_CONFLICT then the spent output won't be changed, but the outpoint.fromTx pointer will still be updated.

Parameters:
transaction - The transaction to try.
mode - Whether to abort if there's a pre-existing connection or not.
Returns:
NO_SUCH_TX if transaction is not the prevtx, ALREADY_SPENT if there was a conflict, SUCCESS if not.

connect

public void connect(TransactionOutput out)
Internal use only: connects this TransactionInput to the given output (updates pointers and spent flags)


disconnect

public boolean disconnect()
If this input is connected, check the output is connected back to this input and release it if so, making it spendable once again.

Returns:
true if the disconnection took place, false if it was not connected.

hasSequence

public boolean hasSequence()
Returns:
true if this transaction's sequence number is set (ie it may be a part of a time-locked transaction)

verify

public void verify()
            throws VerificationException
For a connected transaction, runs the script against the connected pubkey and verifies they are correct.

Throws:
ScriptException - if the script did not verify.
VerificationException - If the outpoint doesn't match the given output.

verify

public void verify(TransactionOutput output)
            throws VerificationException
Verifies that this input can spend the given output. Note that this input must be a part of a transaction. Also note that the consistency of the outpoint will be checked, even if this input has not been connected.

Parameters:
output - the output that this input is supposed to spend.
Throws:
ScriptException - If the script doesn't verify.
VerificationException - If the outpoint doesn't match the given output.

getConnectedOutput

@Nullable
public TransactionOutput getConnectedOutput()
Returns the connected output, assuming the input was connected with connect(TransactionOutput) or variants at some point. If it wasn't connected, then this method returns null.



Copyright © 2014. All rights reserved.