import hashlib


def proof_of_work(unique_trxn_data, nonce):
    """[CHANGING DIFFICULTY: Look for the 'difficulty = X' row, and change X to another integer]

    The Bitcoin SHA256 proof-of-work. Takes transaction data, and requires the miner to find a nonce (number only
    used once) that will result in a SHA256 hash containing X zero bits in front of it.

    Because SHA256 is a cryptographic hash function, there is no better way to figure out the nonce than to brute
    force guess it. Computers do it really fast. In this demonstration, the program just guesses random numbers until
    a number somehow works.

    Feel free to change the number of zero bits required; you'll notice that the time taken to brute-force guess it
    increases exponentially with the number of zero bits. In my experience, the computer takes more than 10 minutes
    once there are 8 zero bits, at least under my really inefficient program. Bitcoin miners are expected to spend
    10 minutes doing their proof of work, and this is modulated by changing the number of zero bits required."""

    mine_attempt = unique_trxn_data * nonce  # This represents one brute force guess of the nonce.

    sha = hashlib.new("sha256")
    sha.update(str(mine_attempt).encode("ascii"))  # Converts the guess into bits and runs it through SHA256

    # Calibrates number of zero bits required at the front of the SHA256 hash
    difficulty = 5  # Change this to any integer you'd like, to see how the time taken changes!
    solution = "".join(["0" for i in range(difficulty)])

    if sha.hexdigest()[:difficulty] == solution:
        print(f"\nNonce for difficulty of {difficulty} zero bits found.\n"
              f"SHA256 hash is {sha.hexdigest()}")
        return True
    else:
        return False