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.
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.
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 |
Witness component 2 |
|
|
21 |
varint |
Length of witness component (0x21 = 33) |
03d96e3819b52245 e42c76f869c9a875 f6ea5344cf1aee2e 6b3ab03adcfef0d8 0e |
33 octets |
witness |
de3b0b00 |
unit32 |
Lock time: 000b3bde = block 736222 |
This transaction appears as the 9th in block 736223