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.
Field Size |
Description |
Data type |
Comments |
4 |
version |
uint32_t |
|
0 or 2 |
flag |
optional uint8_t[2] |
If present, always 0001, and indicates the presence of witness data |
1+ |
tx_in count |
var_int |
|
... |
|
|
|
0+ |
tx_witnesses |
tx_witness[] |
A list of witnesses, one for each input; omitted if flag is omitted above |
4 lock_time |
uint32_t |
... |
|
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
0200000000010179
aaafbe7c9d3b0812
a489facaf77508c0
8c190ec7dfd82f12
9aeb995aca23ab00
00000000fdffffff
020bd21900000000
00160014d2caa7b0
8db89cd62c9af34d
a53332d30e53bb15
98151b0000000000
1600143d4427468c
be7ae396427a1aa9
128fa05b18c7db02
4730440220573fd2
7574cfdde4843476
21e1f48f85ae975c
b8c2265a04496ded
038896822302204a
5e04a3a2d160c315
8caa39b58bfc91ac
64c484078ec0225a
7d4d2d4454661f01
2103d96e3819b522
45e42c76f869c9a8
75f6ea5344cf1aee
2e6b3ab03adcfef0
d80ede3b0b00
Hex Data |
Type |
Meaning |
02000000 |
uint32 |
version 2 |
0001 |
2 octets |
witness flag |
01 |
varint |
count of transaction inputs |
Input 1 |
|
|
79aaafbe7c9d3b08 12a489facaf77508 c08c190ec7dfd82f 129aeb995aca23ab |
32 octets |
Hash of referenced Tx |
00000000 |
uint32 |
index of previous output |
00 |
varint |
length of signature script (0 because segwit?) |
fdffffff |
4 octets |
Sequence (n.b. RBF) |
02 |
varint |
Count of transaction outputs |
Output 1 |
|
|
0bd2190000000000 |
int64 |
Amount in Satoshi (0.01692171 BTC) |
16 |
varint |
length of script (0x16 = 22) |
0014d2caa7b08db8 9cd62c9af34da533 32d30e53bb15 |
22 octets |
locking script |
Output 2 |
|
|
98151b0000000000 |
int64 |
Amount in Satoshi |
16 |
varint |
length of script (0x16 = 22) |
00143d4427468cbe 7ae396427a1aa912 8fa05b18c7db |
22 octets |
locking script |
Witness data |
|
|
02 |
varint |
Count of witness components |
Witness component 1 |
|
|
47 |
varint |
Length of witness component (0x47 = 71) |
30440220573fd275 74cfdde484347621 e1f48f85ae975cb8 c2265a04496ded03 8896822302204a5e 04a3a2d160c3158c aa39b58bfc91ac64 c484078ec0225a7d 4d2d4454661f01 |
71 octets |
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 |
|
|
21 |
varint |
Length of witness component (0x21 = 33) |
03d96e3819b52245 e42c76f869c9a875 f6ea5344cf1aee2e 6b3ab03adcfef0d8 0e |
33 octets |
Witness data. Length is consistent with public key and that is what P2WPKH expects here. |
de3b0b00 |
unit32 |
Lock time: 000b3bde = block 736222 |
This transaction appears as the 9th in block 736223
Notes
Transaction-IDs
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".
Addresses
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) 79...ab
.
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.
Endianness
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).
Witness components
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
Scripts
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