Transaction structure after introduction of Segwit
In an answer to In what format does a block store the transaction data? I gave an example of hand decoding the structure of the earliest Bitcoin transactions from a hex dump.
A Segwit transaction adds some fields that are an optional part of the transaction structure. In non-Segwit transactions, those Segwit fields are absent (have zero length).
The first such optional field is the witness flag just after the version number
The second optional field is the witness data just before the final lock-time field. The witness data has one witness for each input in the transaction. The signature script (unlocking script) in each input is empty because that data is now placed in a witness. Each witness can have several components.
|0 or 2
|If present, always 0001, and indicates the presence of witness data
|A list of witnesses, one for each input; omitted if flag is omitted above
So any parser has to look at 2 bytes following the version number and decide if it is a segwit flag (0x0001 big-endian!) or a tx_in_count varint and maybe part of a tx_in structure.
Example of decoding from hex a Segwit transaction
Just for fun, a randomly chosen example, transaction b0dce2eccbd85f9391e108c8f8f3735cc7b9e6a30f13f82a7fdfaa090d4d093c
|count of transaction inputs
|Hash of referenced Tx
|index of previous output
|length of signature script (0 because segwit?)
|Sequence (n.b. RBF)
|Count of transaction outputs
|Amount in Satoshi (0.01692171 BTC)
|length of script (0x16 = 22)
|Amount in Satoshi
|length of script (0x16 = 22)
|Count of witness components
|Witness component 1
|Length of witness component (0x47 = 71)
|Witness data. The length and the initial bytes reveal that this is a signature. See also witness contents for each tx output type
|Witness component 2
|Length of witness component (0x21 = 33)
|Witness data. Length is consistent with public key and that is what P2WPKH expects here.
|Lock time: 000b3bde = block 736222
This transaction appears as the 9th in block 736223
A Transaction ID is a hash of most of the transaction data. It is usually used by nodes as a retrieval index into a stored list of earlier transactions.
This is what is shown above as "Hash of referenced Tx".
Note that a transaction does not contain Bitcoin addresses. In particular it does not contain sending addresses and amounts. What it cointains is a pointer to an unspent ouput of an earlier transaction, a pointer to a UTXO. The pointer takes the form of the Transaction-ID of the earlier transaction and an index number of the outputs of that transaction. For example it might say this transaction spends the first (index
0) output created in the earlier transaction with hash (TXID)
Blockchain explorers will typically follow that pointer, get amounts and other details from the earlier transaction outputs, calculate the sending addresses and present that information as if it were part of this transaction even though it isn't.
Bitcoin addresses can be regarded as a kind of abstract of a script. An address is a way for a payee to provide a payer with the information needed by the payer to create a locking-script in a transaction that pays the payee.
We see that version 2 is shown in hex as
02000000. This is because the Bitcoin network protocols mostly use little-endian byte ordering rather than the big-endian byte-ordering that most of us find more natural.
02000000 little endian is
00000002 big endian. You just reverse the order of the bytes remembering that one byte is two hex digits. So you reverse the pairs of digits (not individual digits).
SegWit is short for Segregated Witness. It was a change that separated (segregated) certain data into a separate part of the transaction.
The input count is also a count of witnesses (if Segwit flag is set). Each witness starts with a count of witness components.
The witness components are things like digital-signatures that would have been part of the unlocking scripts in pre-segwit transactions.
The exact meaning depends on the transaction output type, which is determined by inspecting the locking script on the earlier transaction whose output is being spent as an input in this transaction.
See list of witness contents for each output type
The locking script and the unlocking script are tiny programs written in the Bitcoin scripting language - which is specially designed to be simple enough that it can't be used to make attacks on the Bitcoin network.
Sometimes other names are used such as ScriptSig (signature script) or ScriptPubKey (public key script) - but these names are historical and are somewhat inappropriate names for the standard scripts in newer types of transaction outputs.
Related questions with relevant answers