0

I'm trying to parse a bitcoin block. I took as an example this block from 2019. I'm trying to parse the first transaction.

I don't understand how to calculate how many bytes I have to read for the "Witnesses"?
By "Witnesses", I refer to this table

Here is my result :

magic_no (4 bytes) : d9b4bef9
block_size (4 bytes) : 1236518
version (4 bytes) : 568442880
prev_hash (32 bytes) : 0000000000000000001437c1e52bc699fb299cac6d2a61a88210b995e45d603e
hash_markle_root (32 bytes) : ed3aba45d11a87c888ffb76a6af7cc1fbddd4a299f285379640c29188110fcf3
time (4 bytes) : 1575857336
bits (4 bytes) : 387308498
nonce (4 bytes) : 2292915727
Tx count (3 bytes) : 2735
Version (4 bytes) : [0 0 0 1]
Flag (2 bytes) : [1 0]
Tx In Count (1 bytes) : 1

*----- START TX Inputs -----*

*----- TxIn 1 -----*

Previous Tx Hash (32 bytes) : 0000000000000000000000000000000000000000000000000000000000000000
Previous Txout Index (4 bytes) : 4294967295
Tx In Script Len (1 bytes) : 85
Tx In Script (85 bytes) : 0348440941d77b6b2f7e726e41d77b6b2e3f8d4a2f4254432e544f502ffabe6d6d19437c0ebcb8916cb51c14eccc000373c8c56bac89feca39a132755acf337f158000000000000000e300518d901a000000000000
Sequence no (4 bytes) : ffffffff
*----- END TX Inputs -----*

Tx Out counter (1 bytes) : 3

*----- START TX Outputs -----*

*----- TxOut 1 -----*
Amount (8 bytes) : 1262724620
Tx Out Script Len (1 bytes) : 25
Tx In Script (25 bytes) : 76a914ba507bae8f1643d2556000ca26b9301b9069dc6b88ac

*----- TxOut 2 -----*
Amount (8 bytes) : 0
Tx Out Script Len (1 bytes) : 38
Tx In Script (38 bytes) : 6a24aa21a9ed36e74c1ca41a78209321759be06054f3ae9b3cd9feaa0f3979f5d9b5d02f286c

*----- TxOut 3 -----*
Amount (8 bytes) : 0
Tx Out Script Len (1 bytes) : 38
Tx In Script (38 bytes) : 6a24b9e11b6d04079a724511b5eac6db73004c5326521f1cc7f0104294285aec8a0dd3c59881

*----- END TX Outputs -----*

Here are the 128 next bytes :

[1 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 1 82 246 143 178 43 131 54 243 136 69 149 88 83 79 230 217 254 150 64 45 100 31 124 170 90 141 101 98 30 236 144 89 1 0 0 0 106 71 48 68 2 32 3 14 228 13 162 14 179 94 25 153 151 242 101 101 181 13 61 83 105 57 168 195 125 68 13 39 247 201 82 62 126 228 2 32 118 168 227 118 230 74 50 52 141]

2 Answers 2

2

The serialization format for witnesses is described in BIP144.

The rough structure is:

  • For each transaction input (implicitly, there is no separate indicator of how many inputs there are), there is a witness stack structure, consisting of:
    • A compactsize encoded number of stack elements
    • For each stack element:
      • A compactsize encoded length of the stack element
      • That many bytes for the contents of the stack element.

Applied to your bytes:

  • The is one input, so one witness stack
    • The first witness stack consists of:
      • 1: the number of stack elements (1)
      • For each stack element
        • The first stack element
          • 32: the length of the first stack element
          • 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0: the contents of the first witness element (32 times the 0 byte)
  • 0 0 0 0: the transaction's locktime

After that, the next transaction starts.

8
  • Thx, that's what I needed ! I just have a few questions ;)
    – nem0z
    Dec 13, 2022 at 20:02
  • Is number of stack elements always 1 byte ? As it must be the same as txIn count which is a VarInt, am I wrong ?
    – nem0z
    Dec 13, 2022 at 20:03
  • 1
    The numbers of witness stacks is always equal to the number of inputs (one per input). The number of elements in one stack is variable, and can range from 0 to roughly 4 million in theory. It is compactsize encoded, just like the number of inputs or outputs, and can be 1, 3, or 5 bytes. The length of stack elements also ranges from 0 to roughly 4 million in theory. Dec 13, 2022 at 20:05
  • 1
    I believe your questions are addressed in my answer. Nothing is guaranteed to be 1 byte, every size/length/count is compactsize encoded, which may be 1, 3, or 5 bytes, depending on the value represented. Dec 13, 2022 at 20:07
  • 1
    Ah yes, in the Bitcoin Core source code there are two variable-length integer formats, "compactsize" is the original one used in the P2P protocol, and another one "varint" which is only used in the local UTXO set database. Dec 13, 2022 at 20:37
1

If there is a witness section in a transaction, the transaction must have one witness stack for each input. You therefore will expect the same number of witness stacks as the input counter. Each witness stack begins by telling you the number of elements in the stack. Each element in the stack starts by telling you its length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.