Class TransactionInput
- java.lang.Object
-
- org.bitcoinj.core.TransactionInput
-
public class TransactionInput extends java.lang.Object
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.
Instances of this class are not safe for use by multiple threads.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TransactionInput.ConnectionResult
static class
TransactionInput.ConnectMode
-
Field Summary
Fields Modifier and Type Field Description static long
NO_SEQUENCE
Magic sequence number that indicates there is no sequence number.static long
SEQUENCE_LOCKTIME_DISABLE_FLAG
BIP68: If this flag set, sequence is NOT interpreted as a relative lock-time.static long
SEQUENCE_LOCKTIME_MASK
BIP68: If sequence encodes a relative lock-time, this mask is applied to extract that lock-time from the sequence field.static long
SEQUENCE_LOCKTIME_TYPE_FLAG
BIP68: If sequence encodes a relative lock-time and this flag is set, the relative lock-time has units of 512 seconds, otherwise it specifies blocks with a granularity of 1.
-
Constructor Summary
Constructors Constructor Description TransactionInput(Transaction parentTransaction, byte[] scriptBytes, TransactionOutPoint outpoint)
TransactionInput(Transaction parentTransaction, byte[] scriptBytes, TransactionOutPoint outpoint, Coin value)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description byte[]
bitcoinSerialize()
Deprecated.useserialize()
void
clearScriptBytes()
Clear input scripts, e.g.static TransactionInput
coinbaseInput(Transaction parentTransaction, byte[] scriptBytes)
Creates an input that connects to nothing - used only in creation of coinbase transactions.TransactionInput.ConnectionResult
connect(java.util.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.boolean
equals(java.lang.Object o)
TransactionOutput
getConnectedOutput()
Returns the connected output, assuming the input was connected withconnect(TransactionOutput)
or variants at some point.RedeemData
getConnectedRedeemData(KeyBag keyBag)
Alias for getOutpoint().getConnectedRedeemData(keyBag)Transaction
getConnectedTransaction()
Returns the connected transaction, assuming the input was connected withconnect(TransactionOutput)
or variants at some point.int
getIndex()
Gets the index of this input in the parent transaction, or throws if this input is freestanding.int
getMessageSize()
Deprecated.UsemessageSize()
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.Coin
getValue()
TransactionWitness
getWitness()
Get the transaction witness of this input.int
hashCode()
boolean
hasRelativeLockTime()
Returns whether this input, if it belongs to a version 2 (or higher) transaction, has relative lock-time enabled.boolean
hasSequence()
boolean
hasWitness()
Determine if the transaction has witnesses.boolean
isCoinBase()
Coinbase transactions have special inputs with hashes of zero.boolean
isOptInFullRBF()
Returns whether this input will cause a transaction to opt into the full replace-by-fee semantics.DefaultRiskAnalysis.RuleViolation
isStandard()
Returns either RuleViolation.NONE if the input is standard, or which rule makes it non-standard if so.int
messageSize()
Return the size of the serialized message.static TransactionInput
read(java.nio.ByteBuffer payload, Transaction parentTransaction)
Deserialize this transaction input from a given payload.byte[]
serialize()
Allocates a byte array and writes this transaction input into it.protected void
setParent(Transaction parent)
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.void
setWitness(TransactionWitness witness)
Set the transaction witness of an input.java.lang.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.java.nio.ByteBuffer
write(java.nio.ByteBuffer buf)
Write this transaction input into the given buffer.
-
-
-
Field Detail
-
NO_SEQUENCE
public static final long NO_SEQUENCE
Magic sequence number that indicates there is no sequence number.- See Also:
- Constant Field Values
-
SEQUENCE_LOCKTIME_DISABLE_FLAG
public static final long SEQUENCE_LOCKTIME_DISABLE_FLAG
BIP68: If this flag set, sequence is NOT interpreted as a relative lock-time.- See Also:
- Constant Field Values
-
SEQUENCE_LOCKTIME_TYPE_FLAG
public static final long SEQUENCE_LOCKTIME_TYPE_FLAG
BIP68: If sequence encodes a relative lock-time and this flag is set, the relative lock-time has units of 512 seconds, otherwise it specifies blocks with a granularity of 1.- See Also:
- Constant Field Values
-
SEQUENCE_LOCKTIME_MASK
public static final long SEQUENCE_LOCKTIME_MASK
BIP68: If sequence encodes a relative lock-time, this mask is applied to extract that lock-time from the sequence field.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TransactionInput
public TransactionInput(@Nullable Transaction parentTransaction, byte[] scriptBytes, TransactionOutPoint outpoint)
-
TransactionInput
public TransactionInput(@Nullable Transaction parentTransaction, byte[] scriptBytes, TransactionOutPoint outpoint, @Nullable Coin value)
-
-
Method Detail
-
coinbaseInput
public static TransactionInput coinbaseInput(Transaction parentTransaction, byte[] scriptBytes)
Creates an input that connects to nothing - used only in creation of coinbase transactions.- Parameters:
parentTransaction
- parent transactionscriptBytes
- arbitrary bytes in the script
-
read
public static TransactionInput read(java.nio.ByteBuffer payload, Transaction parentTransaction) throws java.nio.BufferUnderflowException, ProtocolException
Deserialize this transaction input from a given payload.- Parameters:
payload
- payload to deserialize fromparentTransaction
- parent transaction of the input- Returns:
- read message
- Throws:
java.nio.BufferUnderflowException
- if the read message extends beyond the remaining bytes of the payloadProtocolException
-
getIndex
public int getIndex()
Gets the index of this input in the parent transaction, or throws if this input is freestanding. Iterates over the parents list to discover this.
-
write
public java.nio.ByteBuffer write(java.nio.ByteBuffer buf) throws java.nio.BufferOverflowException
Write this transaction input into the given buffer.- Parameters:
buf
- buffer to write into- Returns:
- the buffer
- Throws:
java.nio.BufferOverflowException
- if the input doesn't fit the remaining buffer
-
serialize
public byte[] serialize()
Allocates a byte array and writes this transaction input into it.- Returns:
- byte array containing the transaction input
-
bitcoinSerialize
@Deprecated public byte[] bitcoinSerialize()
Deprecated.useserialize()
-
messageSize
public int messageSize()
Return the size of the serialized message. Note that if the message was deserialized from a payload, this size can differ from the size of the original payload.- Returns:
- size of the serialized message in bytes
-
getMessageSize
@Deprecated public int getMessageSize()
Deprecated.UsemessageSize()
-
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.
-
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.
-
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.
-
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
-
clearScriptBytes
public void clearScriptBytes()
Clear input scripts, e.g. in preparation for signing.
-
getParentTransaction
public Transaction getParentTransaction()
- Returns:
- The Transaction that owns this input.
-
getValue
@Nullable public Coin getValue()
- Returns:
- Value of the output connected to this input, if known. Null if unknown.
-
getWitness
public TransactionWitness getWitness()
Get the transaction witness of this input.- Returns:
- the witness of the input
-
setWitness
public void setWitness(TransactionWitness witness)
Set the transaction witness of an input.
-
hasWitness
public boolean hasWitness()
Determine if the transaction has witnesses.- Returns:
- true if the transaction has witnesses
-
getConnectedRedeemData
@Nullable public RedeemData getConnectedRedeemData(KeyBag keyBag) throws ScriptException
Alias for getOutpoint().getConnectedRedeemData(keyBag)- Throws:
ScriptException
- See Also:
TransactionOutPoint.getConnectedRedeemData(KeyBag)
-
connect
public TransactionInput.ConnectionResult connect(java.util.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 to 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)
-
isOptInFullRBF
public boolean isOptInFullRBF()
Returns whether this input will cause a transaction to opt into the full replace-by-fee semantics.
-
hasRelativeLockTime
public boolean hasRelativeLockTime()
Returns whether this input, if it belongs to a version 2 (or higher) transaction, has relative lock-time enabled.
-
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 withconnect(TransactionOutput)
or variants at some point. If it wasn't connected, then this method returns null.
-
getConnectedTransaction
@Nullable public Transaction getConnectedTransaction()
Returns the connected transaction, assuming the input was connected withconnect(TransactionOutput)
or variants at some point. If it wasn't connected, then this method returns null.
-
isStandard
public DefaultRiskAnalysis.RuleViolation isStandard()
Returns either RuleViolation.NONE if the input is standard, or which rule makes it non-standard if so. The "IsStandard" rules control whether the default Bitcoin Core client blocks relay of a tx / refuses to mine it, however, non-standard transactions can still be included in blocks and will be accepted as valid if so.
This method simply calls
DefaultRiskAnalysis.isInputStandard(this)
.
-
setParent
protected final void setParent(@Nullable Transaction parent)
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
Returns a human-readable debug string.- Overrides:
toString
in classjava.lang.Object
-
-