Bitcoin mining consist of finding a nonce to add to previous block header and after 2x sha256 resulting in number lower than target.
Now my questis, where can I find previous mined block headers and solution nonce? Something that I check validity by myself.
There are innumerable ways to retrieve information about Bitcoin blocks, including previous block hash. If you're not running your own full node software, you can utilize some external blockchain explorer API to fulfill your needs. Of course, you may also need some additional software to massage the data you get from APIs because not all of them have advanced filtering to extract only the required data. For example, here is a simple script in Python for extracting block hash from specific block height:
#!/usr/bin/env python3
# a small script showing on of the ways to calculate bitcoin block header hash
# Reference: https://en.bitcoin.it/wiki/Block_hashing_algorithm
import hashlib
import requests
height = '784952'
response = requests.get(
f'https://api.blockchair.com/bitcoin/raw/block/{height}')
if response.status_code == 200:
block = response.json()['data'][height]['decoded_raw_block']
block_dict = {
'version': int(block['versionHex'], 16).to_bytes(4, 'little'),
'prev_block': int(block['previousblockhash'], 16).to_bytes(32, 'little'),
'merkle_root': int(block['merkleroot'], 16).to_bytes(32, 'little'),
'time': int(block['time']).to_bytes(4, 'little'),
'bits': int(block['bits'], 16).to_bytes(4, 'little'),
'nonce': int(block['nonce']).to_bytes(4, 'little')
}
header_bin = b''.join(chunk for chunk in block_dict.values())
header_hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()[::-1].hex()
print(header_hash)
You can modify it to use some other endpopoint, for example
https://chain.api.btc.com/v3/block/latest for retrieving only latest block hashes.