Created
January 26, 2022 20:54
-
-
Save humbdrag/472e1d6fa3e78a27af6d5d37e748581e to your computer and use it in GitHub Desktop.
Calculate target hash for bitcoin mining
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calc_target(bits: str) -> bytes: | |
""" | |
Decompress the target from a compact format. | |
""" | |
bits = bytes.fromhex(bits) | |
# Extract the parts. | |
byte_length = bits[0] - 3 | |
significand = bits[1:] | |
# Scale the significand by byte_length. | |
target = significand + b"\x00" * byte_length | |
# Fill in the leading zeros. | |
target = b"\x00" * (32 - len(target)) + target | |
return target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment