-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitcoinBlockCheck.py
More file actions
44 lines (38 loc) · 1.69 KB
/
bitcoinBlockCheck.py
File metadata and controls
44 lines (38 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
## we need dateutil and tim to convert to UNIX timestamp
import dateutil
from dateutil.parser import parse
import time
## hashlibe is for the hashing obv
import hashlib
##str2le convert the hex to little endian
def str2le(s):
return "".join(reversed([s[i:i+2] for i in range(0, len(s), 2)]))
## the rest of the code is inspired from https://en.bitcoin.it/wiki/Block_hashing_algorithm
## the following numbers are for block 435411 see:
## https://blockchain.info/block/0000000000000000007da0f9cf1286306bb974fd0028fd163dcfd4c8813e98d8
## Blockheader on website:
version_ = "536870912"
hashPrevBlock_ = "00000000000000000152cd3a2ca9a0e9495d74570a84251e2f653e19c333362a"
hashMerkleRoot_ = "21cca5a86df0d269f93fe4c861776f73d0b83e5083803fc3a49f8ad01660e767"
blocktime_ = "2016-10-22 12:00:42"
bits_ = "402931908"
nonce_ = "3038619565"
## Blockheader:
version = str2le(hex(int(version_))[2:])
hashPrevBlock = str2le(hashPrevBlock_)#.decode("hex")
hashMerkleRoot = str2le(hashMerkleRoot_)#.decode("hex")
##converting timestamp "2016-10-22 12:00:42" unix is a pain
##BEWARE blockchain.info is in UTC but other explorer may not (ex blockexplorer.com is +2 hours)
unix = int(time.mktime(dateutil.parser.parse(blocktime_, parserinfo=None) .timetuple()))
timeUnix = str2le(hex(unix)[2:])
bits = str2le(hex(int(bits_))[2:])
nonce = str2le(hex(int(nonce_))[2:])
##Now we have the proper header in hex:
header_hex = version + hashPrevBlock + hashMerkleRoot + timeUnix + bits + nonce
##let's hash!:
header_bin = header_hex.decode('hex')
hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
hash_hex = hash.encode('hex_codec')
hash_final = hash[::-1].encode('hex_codec')
##And this is where the magic happens:
print hash_final