mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 06:52:36 +01:00
contrib: refactor: simplify block header string routine in linearize-data.py
The string representation of a block header hash is simply the hexlified byte-reversed double SHA256 hash of its serialization.
This commit is contained in:
parent
922c49a138
commit
3f863cfff1
1 changed files with 2 additions and 37 deletions
|
@ -25,44 +25,9 @@ def hex_switchEndian(s):
|
||||||
pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)]
|
pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)]
|
||||||
return b''.join(pairList[::-1]).decode()
|
return b''.join(pairList[::-1]).decode()
|
||||||
|
|
||||||
def uint32(x):
|
|
||||||
return x & 0xffffffff
|
|
||||||
|
|
||||||
def bytereverse(x):
|
|
||||||
return uint32(( ((x) << 24) | (((x) << 8) & 0x00ff0000) |
|
|
||||||
(((x) >> 8) & 0x0000ff00) | ((x) >> 24) ))
|
|
||||||
|
|
||||||
def bufreverse(in_buf):
|
|
||||||
out_words = []
|
|
||||||
for i in range(0, len(in_buf), 4):
|
|
||||||
word = struct.unpack('@I', in_buf[i:i+4])[0]
|
|
||||||
out_words.append(struct.pack('@I', bytereverse(word)))
|
|
||||||
return b''.join(out_words)
|
|
||||||
|
|
||||||
def wordreverse(in_buf):
|
|
||||||
out_words = []
|
|
||||||
for i in range(0, len(in_buf), 4):
|
|
||||||
out_words.append(in_buf[i:i+4])
|
|
||||||
out_words.reverse()
|
|
||||||
return b''.join(out_words)
|
|
||||||
|
|
||||||
def calc_hdr_hash(blk_hdr):
|
|
||||||
hash1 = hashlib.sha256()
|
|
||||||
hash1.update(blk_hdr)
|
|
||||||
hash1_o = hash1.digest()
|
|
||||||
|
|
||||||
hash2 = hashlib.sha256()
|
|
||||||
hash2.update(hash1_o)
|
|
||||||
hash2_o = hash2.digest()
|
|
||||||
|
|
||||||
return hash2_o
|
|
||||||
|
|
||||||
def calc_hash_str(blk_hdr):
|
def calc_hash_str(blk_hdr):
|
||||||
hash = calc_hdr_hash(blk_hdr)
|
blk_hdr_hash = hashlib.sha256(hashlib.sha256(blk_hdr).digest()).digest()
|
||||||
hash = bufreverse(hash)
|
return blk_hdr_hash[::-1].hex()
|
||||||
hash = wordreverse(hash)
|
|
||||||
hash_str = hash.hex()
|
|
||||||
return hash_str
|
|
||||||
|
|
||||||
def get_blk_dt(blk_hdr):
|
def get_blk_dt(blk_hdr):
|
||||||
members = struct.unpack("<I", blk_hdr[68:68+4])
|
members = struct.unpack("<I", blk_hdr[68:68+4])
|
||||||
|
|
Loading…
Add table
Reference in a new issue