Skip to content

Instantly share code, notes, and snippets.

@humbdrag
Created January 26, 2022 20:54
Show Gist options
  • Save humbdrag/472e1d6fa3e78a27af6d5d37e748581e to your computer and use it in GitHub Desktop.
Save humbdrag/472e1d6fa3e78a27af6d5d37e748581e to your computer and use it in GitHub Desktop.
Calculate target hash for bitcoin mining
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