## Calculate hash of block header

9

4

I'm trying to calculate the hash of block #502871. This is the block:

{
"result": {
"hash": "00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9",
"confirmations": 35,
"strippedsize": 976261,
"size": 1064277,
"weight": 3993060,
"height": 502871,
"version": 536870912,
"versionHex": "20000000",
"tx": [
"6fef5a38c0a76d4bfb6644afed809fe760abcbe9c1e030b7b09bd876f8d28765",
...
"b136d220a049fd9a42261153c7d5e21e12b791c27eed9cb9e41acaea672a6af5",
"7dbaf4d2b31d4a000b9f2028dd350e46da427b8c1721f030af67d5e47f322da5"
],
"time": 1515252561,
"mediantime": 1515249802,
"nonce": 45291998,
"bits": "180091c1",
"difficulty": 1931136454487.716,
"chainwork": "000000000000000000000000000000000000000000e0ef1d9aa7eeab21ce8628",
"previousblockhash": "00000000000000000061abcd4f51d81ddba5498cff67fed44b287de0990b7266",
},
"error": null,
"id": null


}

So according to https://en.bitcoin.it/wiki/Block_hashing_algorithm I need these values:

 versionHex: 20000000
previousblockhash:
00000000000000000061abcd4f51d81ddba5498cff67fed44b287de0990b7266
time: 1515252561
bits: 180091c1
nonce: 45291998


Formatting the values I get:

version: 02000000
previousblockhash: 66720b99e07d284bd4fe67ff8c49a5db1dd8514fcdab61000000000000000000
time: (5A50EB51)_16 -> 51eb505a
bits: c1910018
nonce: (02B319DE)_16 -> de19b302


And when I calculate the hash:

from hashlib import sha256
import hashlib


The result is

0a80d313f7c8bab35b1442a194516c3ddff9ddc674c752ccfaffb1c83866a073


But not the the expected one:

00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9


Please, what am I doing wrong? Thanks.

Please remove the answer from the question. :-) – wizzwizz4 – 2018-01-07T18:05:32.313

9

The version is wrong:

I have 02000000

But the one that appears on the block is

Version 0x20000000


Doing the formatting:

00000020


Calculating the hash of the block:

from hashlib import sha256
import hashlib

00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9

00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9