Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: jansma43 on February 16, 2019, 11:03:06 PM



Title: Question on structure of Segwit part of transaction
Post by: jansma43 on February 16, 2019, 11:03:06 PM
Can anyone tell me where I can find a full technical description of the changes in the structure of the bitcoin blockchain for Segwit?

A few years ago I created my personal blockchain browser. Now I am trying to update this browser for Segwit. I have limited knowledge of the cryptography and I just try to analyse the blockchain data. I see that in a Segwit transaction for every transaction input there is a Segwit part. This Segwit part seems to have three different structures identified by the value of the first byte: 0, 2 or 4. Structure 0 has zero length Segwit part, 2 has two variable fields Segwit data and 4 has three variable fields Segwit data. Can someone explain the meaning of these three different Segwit data structures?


Title: Re: Question on structure of Segwit part of transaction
Post by: achow101 on February 17, 2019, 12:21:50 AM
Segwit is specified in BIPs 141 (https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki), 143 (https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki), and 144 (https://github.com/bitcoin/bips/blob/master/bip-0144.mediawiki).


Title: Re: Question on structure of Segwit part of transaction
Post by: KingZee on February 17, 2019, 02:47:58 AM
I saved this image, here's the structure of a segwit vs non-segwit block :

https://i.imgur.com/fb7g8PV.png

And a pastebin of both blocks parsed and explained  :

https://pastebin.com/3WwJf597


Title: Re: Question on structure of Segwit part of transaction
Post by: jansma43 on February 18, 2019, 09:37:52 AM
Thanks for helping out. The structure of the blockchain on my computer however doesn't seem to match the described structure in these examples. I am still running a Bitcoin Core v0.13.1 wallet. Could that explain the difference?


Title: Re: Question on structure of Segwit part of transaction
Post by: KingZee on February 18, 2019, 09:49:33 AM
Thanks for helping out. The structure of the blockchain on my computer however doesn't seem to match the described structure in these examples. I am still running a Bitcoin Core v0.13.1 wallet. Could that explain the difference?

Can you post examples of what you mean?


Title: Re: Question on structure of Segwit part of transaction
Post by: jansma43 on February 18, 2019, 12:31:19 PM
This is the data of the 26th transaction in a block that was created on 28 Aug 2017 21:20:46 GMT:
00 00 00 00 02 00 00 00 00 01 01 88 03 30 8C 8C 79 75 F2 A4 EA 36 8D 1D B4 07 50 41 64 18 F7 43
5D DE 6A 67 44 9D 32 76 79 61 79 01 00 00 00 17 16 00 14 C0 AF C6 18 E6 E5 5E 77 21 01 2D A4 FB
ED FC 41 05 13 8D F1 FF FF FF FF 02 D4 AD 71 EF 04 00 00 00 17 A9 14 FA 2A 70 3A F8 1A 31 25 28
4C 5F C6 45 31 E8 2B 1C 2D CD 99 87 D2 21 12 00 00 00 00 00 19 76 A9 14 63 C5 06 75 7B 4C B6 25
59 FB 7B F2 B1 F0 81 11 54 F4 29 99 88 AC 02 47 30 44 02 20 43 4B B6 B4 D0 FA 71 83 62 5B D5 0F
55 C2 6C 8B 74 B3 12 DF 5F 06 1B 96 88 6F 33 E5 16 AF 3D 92 02 20 7F C7 61 99 99 CB 72 96 35 6A
BE 82 A9 A0 B1 EC 73 1B 66 27 25 2E C8 29 38 B9 09 99 1E 43 C3 B4 01 21 02 00 62 14 1C CC 8E 79
C1 7F C2 0C 0C CE F8 DA 06 1F 58 D3 43 D2 1E D9 2C 00 57 F4 80 FA 52 2C 3D 00 00 00 00

I have analysed the structure as follows:
Locktime of previous tx = 00 00 00 00
Version = 02 00 00 00
Segwit field? = 00 01
Number of inputs = 01
Hash previous tx = 88 03 30 8C 8C 79 75 F2 A4 EA 36 8D 1D B4 07 50 41 64 18 F7 43 5D DE 6A 67 44 9D 32 76 79 61 79
Index of output in prev tx = 01 00 00 00
Script length = 17 (23)
Script = 16 00 14 C0 AF C6 18 E6 E5 5E 77 21 01 2D A4 FB ED FC 41 05 13 8D F1
Sequence = FF FF FF FF
Number of outputs = 02
Amount of first output = D4 AD 71 EF 04 00 00 00 (BTC = 40,17204692)
Script length = 17 (23)
Script = A9 14 FA 2A 70 3A F8 1A 31 25 28 4C 5F C6 45 31 E8 2B 1C 2D CD 99 87
Amount of second output = D2 21 12 00 00 00 00 00 (BTC = 0,01188306)
Script length = 19 (25)
Script = 76 A9 14 63 C5 06 75 7B 4C B6 25 59 FB 7B F2 B1 F0 81 11 54 F4 29 99 88 AC
Segwitcode? = 02
Length of first Segwit field? = 47 (71)
First Segwit field? = 30 44 02 20 43 4B B6 B4 D0 FA 71 83 62 5B D5 0F 55 C2 6C 8B 74 B3 12 DF 5F 06 1B 96 88 6F 33 E5 16 AF 3D 92 02 20 7F C7 61 99 99 CB 72 96 35 6A BE 82 A9 A0 B1 EC 73 1B 66 27 25 2E C8 29 38 B9 09 99 1E 43 C3 B4 01
Length of second Segwit field? = 21 (33)
Second Segwit field? = 02 00 62 14 1C CC 8E 79 C1 7F C2 0C 0C CE F8 DA 06 1F 58 D3 43 D2 1E D9 2C 00 57 F4 80 FA 52 2C 3D
Locktime = 00 00 00 00

The fields with questionmarks have been introduced with the activation of Segwit on August 24 2017. I have seen Segwitcodes 00, 02 and 04. Code 00 appears to have no additional Segwit fields, code 02 appears to have 2 Segwitfields and code 04 appears to have 3 Segwitfields. What do these codes and additional fields mean and are there other codes possible?


Title: Re: Question on structure of Segwit part of transaction
Post by: achow101 on February 18, 2019, 06:41:29 PM
Did you read the BIPs? They explain what those fields are.

Most of what you think are codes or special types are not. They are lengths.

Segwit field? = 00 01
These are the marker (00) and flag (01) byte indicating that the transaction is segwit.

Segwitcode? = 02
No. That is the number of stack elements in the witness field. This is not a fixed number. If higher or lower, there will be more or less stack items that follow it.

Length of first Segwit field? = 47 (71)
First Segwit field? = 30 44 02 20 43 4B B6 B4 D0 FA 71 83 62 5B D5 0F 55 C2 6C 8B 74 B3 12 DF 5F 06 1B 96 88 6F 33 E5 16 AF 3D 92 02 20 7F C7 61 99 99 CB 72 96 35 6A BE 82 A9 A0 B1 EC 73 1B 66 27 25 2E C8 29 38 B9 09 99 1E 43 C3 B4 01
It is not a field. It is the first stack item.

Length of second Segwit field? = 21 (33)
Second Segwit field? = 02 00 62 14 1C CC 8E 79 C1 7F C2 0C 0C CE F8 DA 06 1F 58 D3 43 D2 1E D9 2C 00 57 F4 80 FA 52 2C 3D
It is not a field. It is the second stack item.


Title: Re: Question on structure of Segwit part of transaction
Post by: jansma43 on February 19, 2019, 11:13:40 AM
Thanks very much for explaining! I was confused because when the number of stack elements was 4 the first stack item always had a zero length. So I figured the counter was not a counter but some kind of code.


Title: Re: Question on structure of Segwit part of transaction
Post by: achow101 on February 19, 2019, 03:28:39 PM
Thanks very much for explaining! I was confused because when the number of stack elements was 4 the first stack item always had a zero length.
That's probably for multisig scripts which require that the first stack element be empty due to a bug in the way that OP_CHECKMULTISIG works.