def local_sha256(secondhalf, midstate_data, target_hash):
    #TODO: make sure the nonce is started with 0x00000fff so that it can be started properly
    print "Local sha256"
    new_target = serial.get_target()
    print "Target received:", new_target
    nonce = 0
    while nonce <= 0xffffffff:
        nonce_str = chr(nonce & 0xff) + chr((nonce >> 8) & 0xff) + chr(
            (nonce >> 16) & 0xff) + chr((nonce >> 24) & 0xff)
        new_data = nonce_str + util.hex2bin(
            '800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000280'
        )
        second_head = secondhalf + new_data
        temp_hash = midstate.calculateMidstate(second_head, midstate_data, 64)
        block_hash = hashlib.sha256(temp_hash).digest()
        if util.block_check_target(util.bin2hex(block_hash), new_target):
            print 'nonce_str: ', nonce_str
            print 'Target hash found for nonce = ', nonce
            print 'block_hash = ', util.bin2hex(block_hash)
            print 'target_hash = ', new_target
            return None, 0

        nonce += 1

    return None, 0
def block_submission(block_template, block_header, nonce, target_hash):
    nonce_str = chr(nonce & 0xff) + chr((nonce >> 8) & 0xff) + chr((nonce >> 16) & 0xff) + chr((nonce >> 24) & 0xff)
    block_hash = compute_double_hash_lib_call(block_header+nonce_str)
    if util.block_check_target(util.bin2hex(block_hash), target_hash):
        #Submit the data again
        block_template['nonce'] = nonce
        block_template['hash'] = bin2hex(block_hash)
        print "Solved a block! Block hash:", mined_block['hash']
        submission = create_block_for_submission(mined_block)
        print "Submitting:", submission, "\n"
        rpc_submitblock(submission)
    return
def double_hash(block_header, target_hash):
    print 'Double hash'
    nonce = 0
    while nonce <= 0xffffffff:
        # Update the block header with the new 32-bit nonce
        block_header = block_header[0:76] + chr(nonce & 0xff) + chr((nonce >> 8) & 0xff) + chr((nonce >> 16) & 0xff) + chr((nonce >> 24) & 0xff)
        #block_header = block_header[0:76] + chr(nonce >> 24 & 0xff) + chr((nonce >> 16) & 0xff) + chr((nonce >> 8) & 0xff) + chr(nonce  & 0xff)
        # Recompute the block hash
        block_hash = compute_double_hash_lib_call(block_header)
        if util.block_check_target(block_hash, target_hash):
            print "nonce: ", nonce
            print util.bin2hex(block_hash)
            break
        nonce += 1
 def debug_hash(self, nonce):
     n_nonce = struct.pack("<L", nonce)
     new_data = util.hex2bin(
         '800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000280'
     )
     new_data = n_nonce + new_data
     second_head = util.hex2bin(self.data_remaining) + new_data
     data_temp = midstate.calculateMidstate(second_head,
                                            util.hex2bin(self.midstate_hex),
                                            64)
     final_hash = hashlib.sha256(data_temp).digest()[::-1]
     print DEBUG_STRING, util.bin2hex(final_hash)
     # Check if it the block meets the target target hash
     if util.block_check_target(final_hash, util.hex2bin(self.target_hex)):
         return (final_hash, nonce)
     return (None, nonce)