<p>The serialization format for witnesses is described in BIP144.</p>
<p>The rough structure is:</p>
<ul>
<li>For each transaction input (implicitly, there is no separate indicator of how many inputs there are), there is a witness stack structure, consisting of:
<ul>
<li>A compactsize encoded number of stack elements</li>
<li>For each stack element:
<ul>
<li>A compactsize encoded length of the stack element</li>
<li>That many bytes for the contents of the stack element.</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Applied to your bytes:</p>
<ul>
<li>The is one input, so one witness stack
<ul>
<li>The first witness stack consists of:
<ul>
<li><code>1</code>: the number of stack elements (1)</li>
<li>For each stack element
<ul>
<li>The first stack element
<ul>
<li><code>32</code>: the length of the first stack element</li>
<li><code>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</code>: the contents of the first witness element (32 times the 0 byte)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><code>0 0 0 0</code>: the transaction's locktime</li>
</ul>
<p>After that, the next transaction starts.</p>

CC BY-SA 4.0
2022-12-13T20:02:29.747Z
Thx, that's what I needed ! I just have a few questions ;)
nem0z
CC BY-SA 4.0
2022-12-13T20:03:35.770Z
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
CC BY-SA 4.0
2022-12-13T20:04:47.767Z
Same question for stack length, does it means a Witnesse can exed exceed 255 byte length ?
nem0z
CC BY-SA 4.0
2022-12-13T20:05:33.010Z
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.
Pieter Wuille
CC BY-SA 4.0
2022-12-13T20:06:13.337Z
Or maybe it's 1 stack / Input => n element / stack (where n is 1 byte) ?
nem0z
CC BY-SA 4.0
2022-12-13T20:07:04.527Z
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.
Pieter Wuille
CC BY-SA 4.0
2022-12-13T20:14:17.410Z
Ok I got it, I did'nt make the connection between compactsize and VarInt.. It's clear now, thx so much !
nem0z
CC BY-SA 4.0
2022-12-13T20:37:53.160Z
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.
Pieter Wuille
CC BY-SA 4.0
2022-12-13T18:27:15.233Z
2022-12-13T18:27:15.233Z
Pieter Wuille
<p>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.</p>

CC BY-SA 4.0
2022-12-13T18:37:42.770Z
2022-12-13T18:37:42.770Z
Murch
<p>I'm trying to parse a bitcoin block.
I took as an example <a href="https://explorer.btc.com/btc/block/607304" rel="nofollow noreferrer">this block from 2019</a>. I'm trying to parse the <a href="https://explorer.btc.com/btc/transaction/fc10f85d934b0b0d37e3635545e72d546c57bc40be9a6dac717cd4bba77ee003" rel="nofollow noreferrer">first transaction</a>.</p>
<p>I don't understand how to calculate how many bytes I have to read for the &quot;Witnesses&quot;?<br />
By &quot;Witnesses&quot;, I refer to <a href="https://en.bitcoin.it/wiki/Transaction#General_format_of_a_Bitcoin_transaction_.28inside_a_block.29" rel="nofollow noreferrer">this table</a></p>
<p>Here is my result :</p>
<pre><code>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 -----*
</code></pre>
<p>Here are the 128 next bytes :</p>
<pre><code>[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]
</code></pre>

<segregated-witness><witness-data>
How can I calculate the length (in bytes) of the Witness data in a transaction?
nem0z
# How can I calculate the length (in bytes) of the Witness data in a transaction?

(Asked by: username_0 on 2022-12-13)

I'm trying to parse a bitcoin block.
I took as an example [this block from 2019](https://explorer.btc.com/btc/block/607304). I'm trying to parse the [first transaction](https://explorer.btc.com/btc/transaction/fc10f85d934b0b0d37e3635545e72d546c57bc40be9a6dac717cd4bba77ee003).

I don't understand how to calculate how many bytes I have to read for the "Witnesses"?  
By "Witnesses", I refer to [this table](https://en.bitcoin.it/wiki/Transaction#General_format_of_a_Bitcoin_transaction_.28inside_a_block.29)

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]

```

**Tags:** segregated-witness, witness-data


---

## 2 answers

### Answer by username_1 on 2022-12-13. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: Thx, that's what I needed ! I just have a few questions ;)  
> **username_0**: Is number of stack elements always 1 byte ? As it must be the same as txIn count which is a VarInt, am I wrong ?  
> **username_0**: Same question for stack length, does it means a Witnesse can exed exceed 255 byte length ?  
> **username_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.  
> **username_0**: Or maybe it's 1 stack / Input => n element / stack (where n is 1 byte) ?  
> **username_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.  
> **username_0**: Ok I got it, I did'nt make the connection between compactsize and VarInt.. It's clear now, thx so much !  
> **username_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.  

---

### Answer by username_2 on 2022-12-13. Score: 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.


---

