Author

Topic: Question on structure of Segwit part of transaction (Read 269 times)

staff
Activity: 3458
Merit: 6793
Just writing some code
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.
newbie
Activity: 12
Merit: 1
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.
staff
Activity: 3458
Merit: 6793
Just writing some code
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.
newbie
Activity: 12
Merit: 1
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?
sr. member
Activity: 938
Merit: 452
Check your coin privilege
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?
newbie
Activity: 12
Merit: 1
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?
sr. member
Activity: 938
Merit: 452
Check your coin privilege
I saved this image, here's the structure of a segwit vs non-segwit block :



And a pastebin of both blocks parsed and explained  :

https://pastebin.com/3WwJf597
staff
Activity: 3458
Merit: 6793
Just writing some code
Segwit is specified in BIPs 141, 143, and 144.
newbie
Activity: 12
Merit: 1
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?
Jump to: