<p><strong>Feb2018:</strong> This is a very generic question of an older request, I answer it anyhow, maybe someone will be searching for this info, just like I was doing :-)</p>

<p>The main point is, that the transaction is not part of the block header. 
The block starts with a blocksize number of 4 bytes, a block header of 80 bytes, and a transaction counter (var_int, 1-9 Bytes). Then follows all the transactions in the block. The block header starts with a 4 bytes version field, followed by the previous block hash (32 bytes) and the merkle tree (32bytes). This is followed by three fields of 4 bytes each: difficulty, timestamp, and nonce. </p>

<p>In your example, the block should be composed this way:</p>

<p><strong>Block size</strong>: 00000264 (612 Bytes)</p>

<p><strong>Version</strong>: 01000030</p>

<p><strong>prev_block</strong>: cd0594ebccfe15b205125165cc3b66986ed5b2a311cd88050000000000000000</p>

<p><strong>Merkle root</strong>: 37022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb582</p>

<p><strong>Timestamp</strong>: 7f476357</p>

<p><strong>nbits</strong>: a09b0518</p>

<p><strong>Nonce</strong>: ae5ae1c1</p>

<p><strong>tx counter</strong>: 2</p>

<p><strong>transaction</strong>: 010000000106af2297b4c9a602be8d866ce644f5a03a4485e973309a7d8a0a64fbd985d1c4010000006b483045022100c37ea6fde87034c0dcc5a62f5fdc8c5292b5b2340c327a4c5d5ff6c1f84a748102203a2ed49ecd0a30f10a1ef77704fcd2c853eecf29c3afd9eea9d44e4fd48d39a50121034ef202077f758c66ad3bf6901c844fd88df6e9889be09b963af1a89fda49f4d3feffffff0271c24413000000001976a914c619073e11f2937f344482ed78a791ec93ca8da988acc0ac0d0d000000001976a914efceeab4cc7010eb07421f7351bf2c22570a5ef488ac9c5f0600</p>

<p>Without the first field, this is a length of 612 bytes, which must be converted into a 4 bytes, which is placed at the beginning of the block. So the full block would be:</p>

<p><code>&lt;block size&gt;</code></p>

<p>01000030</p>

<p>cd0594ebccfe15b205125165cc3b66986ed5b2a311cd88050000000000000000</p>

<p><code>&lt;merkle tree&gt;</code></p>

<p>7f476357</p>

<p>a09b0518</p>

<p>ae5ae1c1</p>

<p>2</p>

<p><code>&lt;coinbase ... &gt;</code></p>

<p>010000000106af2297b4c9a602be8d866ce644f5a03a4485e973309a7d8a0a64fbd985d1c4010000006b483045022100c37ea6fde87034c0dcc5a62f5fdc8c5292b5b2340c327a4c5d5ff6c1f84a748102203a2ed49ecd0a30f10a1ef77704fcd2c853eecf29c3afd9eea9d44e4fd48d39a50121034ef202077f758c66ad3bf6901c844fd88df6e9889be09b963af1a89fda49f4d3feffffff0271c24413000000001976a914c619073e11f2937f344482ed78a791ec93ca8da988acc0ac0d0d000000001976a914efceeab4cc7010eb07421f7351bf2c22570a5ef488ac9c5f0600</p>

<p>Then the merkle tree needs to be re-calculated, and double sha256'd. The length need to be calculated, and put at the very beginning, and the "coinbase tx" is missing (funds calculations!). </p>

<p>The details of the structure can be found here in these two reference pages:
<a href="https://bitcoin.org/en/developer-guide#block-chain-overview" rel="nofollow noreferrer">https://bitcoin.org/en/developer-guide#block-chain-overview</a>
<a href="https://bitcoin.org/en/developer-reference#block-chain" rel="nofollow noreferrer">https://bitcoin.org/en/developer-reference#block-chain</a></p>

CC BY-SA 3.0
2018-02-20T09:47:39.423Z
2018-02-20T09:47:39.423Z
pebwindkraft
<p>I am writing a program (in golang) to mine bitcoins. Supposing I have <a href="https://blockchain.info/rawtx/66d32609f3912a44390a13940d53e1ecce465a0cce47f43e47dc434947864647" rel="noreferrer">this dummy transaction</a>, how may I encode it so it can be used in the block im trying to mine?</p>
<p>Also, supposing I have a block header as below:</p>
<blockquote>
<p>// binary header data encoded as a hex string:</p>
<p>000000000000000003e6d3647001d1f455d585cdd3c298093902fe52ea4529ba01000030cd0594ebccfe15b205125165cc3b66986ed5b2a311cd8805000000000000000037022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb5827f476357a09b0518ae5ae1c1</p>
<p>// <a href="https://blockchain.info/block-index/1114939/000000000000000003e6d3647001d1f455d585cdd3c298093902fe52ea4529ba?format=json" rel="noreferrer">original header</a> data that has been encoded to build the above header:</p>
<p><strong>Version</strong>: 01000030</p>
<p><strong>prev_block</strong>: cd0594ebccfe15b205125165cc3b66986ed5b2a311cd88050000000000000000</p>
<p><strong>Merkle root</strong>: 37022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb582</p>
<p><strong>Timestamp</strong>: 7f476357</p>
<p><strong>nbits</strong>:  a09b0518</p>
<p><strong>Nonce</strong>:  ae5ae1c1</p>
<p><strong>Block header</strong>: 01000030cd0594ebccfe15b205125165cc3b66986ed5b2a311cd8805000000000000000037022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb5827f476357a09b0518ae5ae1c1</p>
<p><strong>computed_hash</strong>: 000000000000000003e6d3647001d1f455d585cdd3c298093902fe52ea4529ba</p>
</blockquote>
<p>How may I add our dummy transaction to the given header (suppose the header already have the merkle root in it)? What if I wish to add multiple transactions like our dummy transaction data to our block header ?</p>

<transactions><protocol><block-header>
bitcoin protocol: transaction encoding
G Oliveira
# bitcoin protocol: transaction encoding

(Asked by: username_0 on 2016-06-23)

I am writing a program (in golang) to mine bitcoins. Supposing I have [this dummy transaction](https://blockchain.info/rawtx/66d32609f3912a44390a13940d53e1ecce465a0cce47f43e47dc434947864647), how may I encode it so it can be used in the block im trying to mine?

Also, supposing I have a block header as below:

> // binary header data encoded as a hex string:
>
> 000000000000000003e6d3647001d1f455d585cdd3c298093902fe52ea4529ba01000030cd0594ebccfe15b205125165cc3b66986ed5b2a311cd8805000000000000000037022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb5827f476357a09b0518ae5ae1c1
>
> // [original header](https://blockchain.info/block-index/1114939/000000000000000003e6d3647001d1f455d585cdd3c298093902fe52ea4529ba?format=json) data that has been encoded to build the above header:
>
> **Version**: 01000030
>
> **prev\_block**: cd0594ebccfe15b205125165cc3b66986ed5b2a311cd88050000000000000000
>
> **Merkle root**: 37022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb582
>
> **Timestamp**: 7f476357
>
> **nbits**: a09b0518
>
> **Nonce**: ae5ae1c1
>
> **Block header**: 01000030cd0594ebccfe15b205125165cc3b66986ed5b2a311cd8805000000000000000037022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb5827f476357a09b0518ae5ae1c1
>
> **computed\_hash**: 000000000000000003e6d3647001d1f455d585cdd3c298093902fe52ea4529ba

How may I add our dummy transaction to the given header (suppose the header already have the merkle root in it)? What if I wish to add multiple transactions like our dummy transaction data to our block header ?

**Tags:** transactions, protocol, block-header


---

## 1 answers

### Answer by username_1 on 2018-02-20. Score: 2 | Accepted answer

**Feb2018:** This is a very generic question of an older request, I answer it anyhow, maybe someone will be searching for this info, just like I was doing :-)

The main point is, that the transaction is not part of the block header.
The block starts with a blocksize number of 4 bytes, a block header of 80 bytes, and a transaction counter (var\_int, 1-9 Bytes). Then follows all the transactions in the block. The block header starts with a 4 bytes version field, followed by the previous block hash (32 bytes) and the merkle tree (32bytes). This is followed by three fields of 4 bytes each: difficulty, timestamp, and nonce.

In your example, the block should be composed this way:

**Block size**: 00000264 (612 Bytes)

**Version**: 01000030

**prev\_block**: cd0594ebccfe15b205125165cc3b66986ed5b2a311cd88050000000000000000

**Merkle root**: 37022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb582

**Timestamp**: 7f476357

**nbits**: a09b0518

**Nonce**: ae5ae1c1

**tx counter**: 2

**transaction**: 010000000106af2297b4c9a602be8d866ce644f5a03a4485e973309a7d8a0a64fbd985d1c4010000006b483045022100c37ea6fde87034c0dcc5a62f5fdc8c5292b5b2340c327a4c5d5ff6c1f84a748102203a2ed49ecd0a30f10a1ef77704fcd2c853eecf29c3afd9eea9d44e4fd48d39a50121034ef202077f758c66ad3bf6901c844fd88df6e9889be09b963af1a89fda49f4d3feffffff0271c24413000000001976a914c619073e11f2937f344482ed78a791ec93ca8da988acc0ac0d0d000000001976a914efceeab4cc7010eb07421f7351bf2c22570a5ef488ac9c5f0600

Without the first field, this is a length of 612 bytes, which must be converted into a 4 bytes, which is placed at the beginning of the block. So the full block would be:

`<block size>`

01000030

cd0594ebccfe15b205125165cc3b66986ed5b2a311cd88050000000000000000

`<merkle tree>`

7f476357

a09b0518

ae5ae1c1

2

`<coinbase ... >`

010000000106af2297b4c9a602be8d866ce644f5a03a4485e973309a7d8a0a64fbd985d1c4010000006b483045022100c37ea6fde87034c0dcc5a62f5fdc8c5292b5b2340c327a4c5d5ff6c1f84a748102203a2ed49ecd0a30f10a1ef77704fcd2c853eecf29c3afd9eea9d44e4fd48d39a50121034ef202077f758c66ad3bf6901c844fd88df6e9889be09b963af1a89fda49f4d3feffffff0271c24413000000001976a914c619073e11f2937f344482ed78a791ec93ca8da988acc0ac0d0d000000001976a914efceeab4cc7010eb07421f7351bf2c22570a5ef488ac9c5f0600

Then the merkle tree needs to be re-calculated, and double sha256'd. The length need to be calculated, and put at the very beginning, and the "coinbase tx" is missing (funds calculations!).

The details of the structure can be found here in these two reference pages:
<https://bitcoin.org/en/developer-guide#block-chain-overview>
<https://bitcoin.org/en/developer-reference#block-chain>


---

