I'm able to calculate the block hash in mainnet and testnet, but I don't get the right hash in regtest. I mint +3000 blocks and I get the block with height 3000
bitcoin-cli getblock $(bitcoin-cli getblockhash 3000)
{
"hash": "4221d8b10b0a7eb5e050337c76819abf71113f17e266e88cb12df4bf5ce24b55",
"confirmations": 502,
"strippedsize": 215,
"size": 251,
"weight": 896,
"height": 3000,
"version": 536870912,
"versionHex": "20000000",
"merkleroot": "23b4600b467f18ef0ec18bd73a9b921077486cf67c44c912de114888db184bf9",
"tx": [
"23b4600b467f18ef0ec18bd73a9b921077486cf67c44c912de114888db184bf9"
],
"time": 1599147354,
"mediantime": 1599147353,
"nonce": 0,
"bits": "207fffff",
"difficulty": 4.656542373906925e-10,
"chainwork": "0000000000000000000000000000000000000000000000000000000000001772",
"nTx": 1,
"previousblockhash": "4776b1ccad27e2de9ccbdc5bf4ad4d972a89e27a020db1f7b59abb857d77f3be",
"nextblockhash": "121512fc6bc1395ada64a6c63ba2c2955ad9cc240a1fc0282b95cf2c2651b381"
}
Now I save the values in env variable
ver=`printf 20000000 | tac -rs ..| tr -d '\n'`
prev=`printf 4776b1ccad27e2de9ccbdc5bf4ad4d972a89e27a020db1f7b59abb857d77f3be | tac -rs .. | tr -d '\n'`
mkl=`printf 23b4600b467f18ef0ec18bd73a9b921077486cf67c44c912de114888db184bf9 | tac -rs .. | tr -d '\n'`
time=`printf '%x\n' 1599147354 | tac -rs .. | tr -d '\n'`
bits=`echo 207fffff | tac -rs .. | tr -d '\n'`
nonce=`printf '%x\n' 0 | tac -rs .. | tr -d '\n'`
After that I can concat and get the little endian representation
printf $ver$prev$mkl$time$bits$nonce | xxd -r -p | sha256sum -b | xxd -r -p | sha256sum -b | awk '{printf $1}' | tac -rs ..
The result is: 09665bcb63b6c279948f93937669bf1c8f36cffda0646180ba523847cb642384
. the same operations in mainnet and testnet works properly.