<ol>
<li>Download a block is easy! <a href="https://webbtc.com/block/000000000000000001f942eb4bfa0aeccb6a14c268f4c72d5fff17270da771b9" rel="nofollow noreferrer">Here</a> is a link to an online explorer with a specific block, for your experimental purposes I suggest storing the block in <a href="https://webbtc.com/block/000000000000000001f942eb4bfa0aeccb6a14c268f4c72d5fff17270da771b9.hex" rel="nofollow noreferrer">its raw hex format</a> (or binary).</li>
<li>Blocks don't have a predefined file format. The Bitcoin protocol enforces a format to the blocks, but how they are stored locally doesn't matter. However Bitcoin Core stores its block in blkxxxxx.dat files.. The LevelDB (.ldb) files contain metadata and indexing information about the bitcoin blocks stored in the blkxxxxx.dat files. It's up to each different Bitcoin implementation to decide for themselves how to store the blocks.</li>
<li>Try manually decoding the block in the URL I posted above. <a href="https://bitcoin.org/en/developer-reference#block-headers" rel="nofollow noreferrer">This page</a> contains all the hints you need to get it working. The nonce is 4 bytes and is preceded by 76 bytes of data, so 77 byte to byte 80 is the nonce. Notable, also at the end of the blockheader (= the first 80 bytes). </li>
</ol>

<p>Thanks to the comment @Hypocritus for pointing out a mistake.</p>

CC BY-SA 4.0
2018-05-20T03:31:21.137Z
Needed to correct Penguin's 2. response above. Bitcoin Core stores bitcoin blocks in .dat files (blkxxxxx.dat). The LevelDB (.ldb) files contain metadata and indexing information about the bitcoin blocks stored in the blkxxxxx.dat files. https://en.bitcoin.it/wiki/Data_directory
Hypocritus
CC BY-SA 4.0
2017-07-03T17:49:55.530Z
2018-05-22T22:13:03.533Z
2018-05-22T22:13:03.533Z
Penquin
<p>Here's how you can check the hash from the command line.  You'll want the <code>xxd</code> package; the command <code>xxd -r -p</code> converts a hex string to its raw equivalent (e.g. the two bytes "5a" goes to the single byte <code>0x5a</code>).</p>

<pre><code>wget -q -O - 'https://blockchain.info/block/000000000000000000ff265a641721e79bbc559d8b0e4894332732109f1a3383?format=hex' \
|xxd -r -p |head -c 80 |sha256sum - |cut -d ' ' -f 1 \
|xxd -r -p |sha256sum - |cut -d ' ' -f 1 |rev
</code></pre>

CC BY-SA 3.0
2017-07-03T18:01:03.953Z
2017-07-03T18:01:03.953Z
Nate Eldredge
<p>This as a "comment" to Nate's proposal (adding a comment destroys the newlines and code formatting).</p>

<p>If there is no xxd, you can go this way:</p>

<pre><code> cat myfile_with_hexchars | tr [:upper:] [:lower:] &gt; myfile_with_hexcodes
 result=$( cat myfile_with_hexcodes | sed 's/[[:xdigit:]]\{2\}/\\x&amp;/g' )
 printf $result &gt; myfile_hex
</code></pre>

<p>any hex editor will help to see result, e.g.:</p>

<pre><code> hexdump -C myfile_hex
</code></pre>

CC BY-SA 3.0
2017-10-22T21:07:10.403Z
I think this should rather be a full answer, or an edit to Nate's answer to add it. If you want to make it a full answer, I think you'd only need to recreate a small portion of Nate's post, you could start out with something along the lines of "Inspired by Nate's answer, you can use the following code to solve the same problem without installing the package `xxd`…"
Murch
CC BY-SA 3.0
2017-10-25T06:49:03.270Z
UUOC, and you don't need the tr; `[:xdigit:]` and printf `\x` handle both cases. In fact this only works right if the input is _entirely_ hex pairs so you can just do `printf $(sed 's/../\\&/g' <hexfile) >outfile`
dave_thompson_085
CC BY-SA 3.0
2017-11-01T07:54:14.910Z
thx, I could get it to work this way: printf $(sed 's/[[:xdigit:]]\{2\}/\\x&/g' <hexfile)
pebwindkraft
CC BY-SA 3.0
2017-07-04T06:18:16.177Z
2017-07-04T06:18:16.177Z
pebwindkraft
<p>The blockchain.com link given in the accepted answer no longer works.</p>
<p>From the published API</p>
<p><a href="https://blockchain.info/api/blockchain_api" rel="nofollow noreferrer">https://blockchain.info/api/blockchain_api</a></p>
<p>The URL you need to download a HEX formatted block is as follows.</p>
<p><a href="https://blockchain.info/rawblock/$block_hash" rel="nofollow noreferrer">https://blockchain.info/rawblock/$block_hash</a></p>
<p>This gives you a hex formatted file that can be converted to binary with <code>xxd -r -p</code> as above.</p>
<p>The bitcoin block header is quite straightforward.</p>
<p><a href="https://en.bitcoin.it/wiki/Protocol_documentation#Block_Headers" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Protocol_documentation#Block_Headers</a>
| Size | Desc | Type | Comments     |
| --- | -- | -- | -- |
| 4  | version      |    int32_t     |   Block version information (note, this is signed) |
| 32 | prev_block   | char[32]       | The hash value of the previous block this particular block references |
| 32 | merkle_root  |       char[32] |  The reference to a Merkle tree collection which is a hash of all transactions related to this block |
| 4  | timestamp   |   uint32_t      |  A timestamp recording when this block was created (Will overflow in 2106[2]) |
| 4  | bits         |   uint32_t     |  The calculated difficulty target being used for this block |
| 4  | nonce        |    uint32_t    |  The nonce used to generate this block… to allow variations of the header and compute different hashes  |</p>
<p>To view the nonce:</p>
<pre><code>&gt;&gt;&gt; data = open('block.bin', 'rb').read()
&gt;&gt;&gt; nonce = data[4+32+32+4+4:][:4]
&gt;&gt;&gt; nonce.hex()
'6d1daf7b'
</code></pre>
<p>To calculate the hash code for the header:</p>
<pre><code>&gt;&gt;&gt; import hashlib
&gt;&gt;&gt; from binascii import unhexlify, hexlify
&gt;&gt;&gt; header = data[:4+32+32+4+4+4]
&gt;&gt;&gt; hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
&gt;&gt;&gt; hexlify(hash).decode(&quot;utf-8&quot;)
'83331a9f1032273394480e8b9d55bc9be72117645a26ff000000000000000000'
&gt;&gt;&gt; hexlify(hash[::-1]).decode(&quot;utf-8&quot;)
'000000000000000000ff265a641721e79bbc559d8b0e4894332732109f1a3383'
</code></pre>
<p>This above is from:</p>
<p><a href="https://en.bitcoin.it/wiki/Block_hashing_algorithm" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Block_hashing_algorithm</a></p>
<p>The following blogpost by Andrej Karpathy's has more information about how you can go about verifying the transactions in the block.</p>
<p><a href="https://karpathy.github.io/2021/06/21/blockchain/" rel="nofollow noreferrer">https://karpathy.github.io/2021/06/21/blockchain/</a></p>

CC BY-SA 4.0
2021-11-30T11:43:09.130Z
currently working link for hex format is:
https://blockchain.info/rawblock/$blockhash?format=hex
e.g.
https://blockchain.info/rawblock/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f?format=hex
premek.v
CC BY-SA 4.0
2021-07-03T03:59:06.110Z
2021-07-03T03:59:06.110Z
John Lehmann
<blockquote>
<p>However this is JSON, I imagine I can't use it to verify the nonce.</p>
</blockquote>
<p>You can. You can build the block header using the data at the beginning of the JSON object and then hash that. Of course it would be easier to get the block hex, which you can get by going to the block hash on blockchain.info and appending <code>?format=hex</code> to the url. For example, for the block height you linked, the hex of that block can be found here: <a href="https://blockchain.info/rawblock/000000000000000000ff265a641721e79bbc559d8b0e4894332732109f1a3383?format=hex" rel="nofollow noreferrer">https://blockchain.info/rawblock/000000000000000000ff265a641721e79bbc559d8b0e4894332732109f1a3383?format=hex</a></p>
<blockquote>
<p>How can I download a block?</p>
</blockquote>
<p>Get the block hex. That is the raw data for the block itself. You could make it a file by putting the hex in a hex editor or just save the hex as a string in a text file.</p>
<blockquote>
<p>What is the file format of such block?</p>
</blockquote>
<p>Blocks don't have a file format. They are just blobs of binary data which you will need to understand how to parse. <a href="https://bitcoin.org/en/developer-reference#serialized-blocks" rel="nofollow noreferrer">https://bitcoin.org/en/developer-reference#serialized-blocks</a> explains the block serialization.</p>
<blockquote>
<p>At what bytes start the nonce field I can increment, for example with a Python script?</p>
</blockquote>
<p>The part that is hashed is the block header, the first 80 bytes of the block. The nonce is the last 4 bytes of that header. You can read about the format of the block header here: <a href="https://bitcoin.org/en/developer-reference#block-headers" rel="nofollow noreferrer">https://bitcoin.org/en/developer-reference#block-headers</a></p>
<blockquote>
<p>Once I get to put the right (and known :) ) nonce how can verify that the block is solved? I mean, its hash should starts with lots of zeros, could I treat the block as a string and pass directly to the python hashlib function?</p>
</blockquote>
<p>No. The block header is not a string. It is a blob of binary data. In python, the easiest thing to do is to make it a bytes object and then pass it in to hashlib.</p>
<p>If a block's proof of work is valid (note that does not mean the block itself is valid), then after you hash it with SHA256 Double, you treat the output as an integer and compare it to the nBits field of the header. The nBits field a 4 byte field beginning 8 bytes from the end of the header and is a compact representation of the target. You can read about how to interpret it here: <a href="https://bitcoin.org/en/developer-reference#target-nbits" rel="nofollow noreferrer">https://bitcoin.org/en/developer-reference#target-nbits</a>. The block hash (treated as a large integer) must be less than the target specified by the nBits field.</p>

CC BY-SA 3.0
2017-07-03T17:51:54.473Z
You beat me to it.
Penquin
CC BY-SA 4.0
2017-07-03T17:42:42.177Z
2023-08-15T19:48:41.980Z
2023-08-15T19:48:41.980Z
Ava Chow
<p>I just started studying bitcoin.</p>

<p>I would like to be able to download a single block from the Bitcoin network.</p>

<p>This should be an old block, already mined, and which I can know the nonce.</p>

<p>Then I would like, via bash linux commands, to verify that such nonce solves the block.</p>

<p>Thanks to a question asked here, I know I could look at</p>

<p><code>https://blockchain.info/block-height/474044?format=json</code></p>

<p>However this is JSON, I imagine I can't use it to verify the nonce.</p>

<p>Hence my question is:</p>

<ol>
<li>How can I download a block?</li>
<li>What is the file format of such block?</li>
<li>At what bytes start the nonce field I can increment, for example with a Python script?</li>
<li>Once I get to put the right (and known :) ) nonce how can verify that the block is solved? I mean, its hash should starts with lots of zeros, could I treat the block as a string and pass directly to the python hashlib function?</li>
</ol>

<p>Thanks in advance.</p>

<blockchain.info><block><mining-theory>
Download single and specific block for study purposes
Jacquelyn.Marquardt
# Download single and specific block for study purposes

(Asked by: username_0 on 2017-07-03)

I just started studying bitcoin.

I would like to be able to download a single block from the Bitcoin network.

This should be an old block, already mined, and which I can know the nonce.

Then I would like, via bash linux commands, to verify that such nonce solves the block.

Thanks to a question asked here, I know I could look at

`https://blockchain.info/block-height/474044?format=json`

However this is JSON, I imagine I can't use it to verify the nonce.

Hence my question is:

1. How can I download a block?
2. What is the file format of such block?
3. At what bytes start the nonce field I can increment, for example with a Python script?
4. Once I get to put the right (and known :) ) nonce how can verify that the block is solved? I mean, its hash should starts with lots of zeros, could I treat the block as a string and pass directly to the python hashlib function?

Thanks in advance.

**Tags:** blockchain.info, block, mining-theory


---

## 5 answers

### Answer by username_9 on 2017-07-03. Score: 12 | Accepted answer

> However this is JSON, I imagine I can't use it to verify the nonce.

You can. You can build the block header using the data at the beginning of the JSON object and then hash that. Of course it would be easier to get the block hex, which you can get by going to the block hash on blockchain.info and appending `?format=hex` to the url. For example, for the block height you linked, the hex of that block can be found here: <https://blockchain.info/rawblock/000000000000000000ff265a641721e79bbc559d8b0e4894332732109f1a3383?format=hex>

> How can I download a block?

Get the block hex. That is the raw data for the block itself. You could make it a file by putting the hex in a hex editor or just save the hex as a string in a text file.

> What is the file format of such block?

Blocks don't have a file format. They are just blobs of binary data which you will need to understand how to parse. <https://bitcoin.org/en/developer-reference#serialized-blocks> explains the block serialization.

> At what bytes start the nonce field I can increment, for example with a Python script?

The part that is hashed is the block header, the first 80 bytes of the block. The nonce is the last 4 bytes of that header. You can read about the format of the block header here: <https://bitcoin.org/en/developer-reference#block-headers>

> Once I get to put the right (and known :) ) nonce how can verify that the block is solved? I mean, its hash should starts with lots of zeros, could I treat the block as a string and pass directly to the python hashlib function?

No. The block header is not a string. It is a blob of binary data. In python, the easiest thing to do is to make it a bytes object and then pass it in to hashlib.

If a block's proof of work is valid (note that does not mean the block itself is valid), then after you hash it with SHA256 Double, you treat the output as an integer and compare it to the nBits field of the header. The nBits field a 4 byte field beginning 8 bytes from the end of the header and is a compact representation of the target. You can read about how to interpret it here: <https://bitcoin.org/en/developer-reference#target-nbits>. The block hash (treated as a large integer) must be less than the target specified by the nBits field.

> **Comments:**
> **username_1**: You beat me to it.  

---

### Answer by username_1 on 2017-07-03. Score: 5

1. Download a block is easy! [Here](https://webbtc.com/block/000000000000000001f942eb4bfa0aeccb6a14c268f4c72d5fff17270da771b9) is a link to an online explorer with a specific block, for your experimental purposes I suggest storing the block in [its raw hex format](https://webbtc.com/block/000000000000000001f942eb4bfa0aeccb6a14c268f4c72d5fff17270da771b9.hex) (or binary).
2. Blocks don't have a predefined file format. The Bitcoin protocol enforces a format to the blocks, but how they are stored locally doesn't matter. However Bitcoin Core stores its block in blkxxxxx.dat files.. The LevelDB (.ldb) files contain metadata and indexing information about the bitcoin blocks stored in the blkxxxxx.dat files. It's up to each different Bitcoin implementation to decide for themselves how to store the blocks.
3. Try manually decoding the block in the URL I posted above. [This page](https://bitcoin.org/en/developer-reference#block-headers) contains all the hints you need to get it working. The nonce is 4 bytes and is preceded by 76 bytes of data, so 77 byte to byte 80 is the nonce. Notable, also at the end of the blockheader (= the first 80 bytes).

Thanks to the comment @username_2 for pointing out a mistake.

> **Comments:**
> **username_2**: Needed to correct Penguin's 2. response above. Bitcoin Core stores bitcoin blocks in .dat files (blkxxxxx.dat). The LevelDB (.ldb) files contain metadata and indexing information about the bitcoin blocks stored in the blkxxxxx.dat files. https://en.bitcoin.it/wiki/Data_directory  

---

### Answer by username_3 on 2017-07-03. Score: 3

Here's how you can check the hash from the command line. You'll want the `xxd` package; the command `xxd -r -p` converts a hex string to its raw equivalent (e.g. the two bytes "5a" goes to the single byte `0x5a`).

```
wget -q -O - 'https://blockchain.info/block/000000000000000000ff265a641721e79bbc559d8b0e4894332732109f1a3383?format=hex' \
|xxd -r -p |head -c 80 |sha256sum - |cut -d ' ' -f 1 \
|xxd -r -p |sha256sum - |cut -d ' ' -f 1 |rev

```


---

### Answer by username_4 on 2017-07-04. Score: 1

This as a "comment" to username_3's proposal (adding a comment destroys the newlines and code formatting).

If there is no xxd, you can go this way:

```
 cat myfile_with_hexchars | tr [:upper:] [:lower:] > myfile_with_hexcodes
 result=$( cat myfile_with_hexcodes | sed 's/[[:xdigit:]]\{2\}/\\x&/g' )
 printf $result > myfile_hex

```

any hex editor will help to see result, e.g.:

```
 hexdump -C myfile_hex

```

> **Comments:**
> **username_5**: I think this should rather be a full answer, or an edit to username_3's answer to add it. If you want to make it a full answer, I think you'd only need to recreate a small portion of username_3's post, you could start out with something along the lines of "Inspired by username_3's answer, you can use the following code to solve the same problem without installing the package `xxd`…"  
> **username_6**: UUOC, and you don't need the tr; `[:xdigit:]` and printf `\x` handle both cases. In fact this only works right if the input is _entirely_ hex pairs so you can just do `printf $(sed 's/../\\&/g' <hexfile) >outfile`  
> **username_4**: thx, I could get it to work this way: printf $(sed 's/[[:xdigit:]]\{2\}/\\x&/g' <hexfile)  

---

### Answer by username_7 on 2021-07-03. Score: 1

The blockchain.com link given in the accepted answer no longer works.

From the published API

<https://blockchain.info/api/blockchain_api>

The URL you need to download a HEX formatted block is as follows.

<https://blockchain.info/rawblock/$block_hash>

This gives you a hex formatted file that can be converted to binary with `xxd -r -p` as above.

The bitcoin block header is quite straightforward.

<https://en.bitcoin.it/wiki/Protocol_documentation#Block_Headers>
| Size | Desc | Type | Comments |
| --- | -- | -- | -- |
| 4 | version | int32\_t | Block version information (note, this is signed) |
| 32 | prev\_block | char[32] | The hash value of the previous block this particular block references |
| 32 | merkle\_root | char[32] | The reference to a Merkle tree collection which is a hash of all transactions related to this block |
| 4 | timestamp | uint32\_t | A timestamp recording when this block was created (Will overflow in 2106[2]) |
| 4 | bits | uint32\_t | The calculated difficulty target being used for this block |
| 4 | nonce | uint32\_t | The nonce used to generate this block… to allow variations of the header and compute different hashes |

To view the nonce:

```
>>> data = open('block.bin', 'rb').read()
>>> nonce = data[4+32+32+4+4:][:4]
>>> nonce.hex()
'6d1daf7b'

```

To calculate the hash code for the header:

```
>>> import hashlib
>>> from binascii import unhexlify, hexlify
>>> header = data[:4+32+32+4+4+4]
>>> hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
>>> hexlify(hash).decode("utf-8")
'83331a9f1032273394480e8b9d55bc9be72117645a26ff000000000000000000'
>>> hexlify(hash[::-1]).decode("utf-8")
'000000000000000000ff265a641721e79bbc559d8b0e4894332732109f1a3383'

```

This above is from:

<https://en.bitcoin.it/wiki/Block_hashing_algorithm>

The following blogpost by Andrej Karpathy's has more information about how you can go about verifying the transactions in the block.

<https://karpathy.github.io/2021/06/21/blockchain/>

> **Comments:**
> **username_8**: currently working link for hex format is: https://blockchain.info/rawblock/$blockhash?format=hex e.g. https://blockchain.info/rawblock/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f?format=hex  

---

