def test_powhash(self): teststart = '700000005d385ba114d079970b29a9418fd0549e7d68a95c7f168621a314201000000000578586d149fd07b22f3a8a347c516de7052f034d2b76ff68e0d6ecff9b77a45489e3fd511732011df0731000' testbin = unhexlify(teststart) hash_bin = dcrypt_hash.getPoWHash(testbin) print hexlify(hash_bin) self.assertEqual( hash_bin, unhexlify( '25c3208eec754b936736d5377726d52d38ac5dee3b4f7345d204123d5bc87a29' ))
prehash = returnheader() starthash = time.time() while True: if nonce % 7 == 0: header = prehash + hexifynonce(nonce) pos(1,1) print 'nonce: ' + hexifynonce(nonce) pos(3,1) print 'header: ' + header # hash fn with timers pretimer = datetime.datetime.now() hashbin = binascii.unhexlify(header) posthash = dcrypt_hash.getPoWHash(hashbin) posthashhex = binascii.hexlify(posthash[:32]) posttimer = datetime.datetime.now() hashtime = posttimer - pretimer pos(7,1) print 'hashed: ' + posthashhex pos (10,1) print '%d h/s ' % (1000000/hashtime.microseconds) if posthash < binascii.unhexlify(target): pos(12,1) print posthashhex print target finishhash = time.time() print 'BLOCK (took ' + str(int(finishhash-starthash)) + 's to solve)'
def __init__(self, ds): magic = "6e8b92a5" self.magic = ds.read_bytes(4).hex() # Acid test if self.magic != magic: print("FAILED {}\n{}".format(magic, ds.read_bytes(512).hex())) return {} # Good to go, find out the length of this block self.size = int(struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex(), 16) # Save point self.start = ds.read_cursor # Read the raw data for the block self.data = ds.read_bytes(self.size) # Store the hex representation for later output # Hash it with dcrypt to get the block hash and note the value self.blockhash = dcrypt_hash.getPoWHash(self.data) # **REDO FROM START** ds.seek_file(self.start) # Parse the data # Standard block rubric self.version = struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex() self.prev_header_hash = ds.read_bytes(32)[::-1].hex() self.merkle_root_hash = ds.read_bytes(32)[::-1].hex() timestampbits = ds.read_bytes(4)[::-1].hex() self.timestamp = int(timestampbits, 16) self.nbits = ds.read_bytes(4)[::-1].hex() self.nonce = ds.read_bytes(4)[::-1].hex() self.nonceint = int(self.nonce, 16) # Proof of burn stuff self.fproofofburn = ds.read_boolean() self.hashburnblock = ds.read_bytes(32)[::-1].hex() self.burnhash = ds.read_bytes(32)[::-1].hex() self.burnblockheight = ds.read_int32() self.burnctx = ds.read_int32() self.burnctxout = ds.read_int32() self.neffectiveburncoins = ds.read_int64() self.nburnbits = ds.read_uint32() # Transactions self.tx_count = int(ds.read_compact_size()) self.txs = [ dict( tx_version = ds.read_bytes(4)[::-1].hex(), tx_ntime = int(ds.read_bytes(4)[::-1].hex(), 16), # tx_input = int(ds.read_compact_size()), vins=[ self.read_txin(ds) for vin in range(0, int(ds.read_compact_size()))], vouts = [ self.read_txout(ds) for vout in range(0, int(ds.read_compact_size()))], tnburnbits=ds.read_bytes(4)[::-1].hex()) for txnum in range(1, self.tx_count+1)] # Block signature self.block_sig_length = ds.read_bytes(1)[::-1].hex() block_sig_lengthint = int(self.block_sig_length, 16) self.block_sig = ds.read_bytes(block_sig_lengthint).hex()
def read_block_steps(self, ds, blockheight=0): # Original approach, superseded by Block class approach # Read file # https://bitcoin.org/en/developer-reference#block-headers # https://en.bitcoin.it/wiki/Protocol_specification#block magic = ds.read_bytes(4).hex() # Acid test if magic != "6e8b92a5": print("FAILED {}\n{}".format(magic, ds.read_bytes(512).hex())) return {} # Good to go, find out the length of this block block_size = int(struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex(), 16) # Save point blockstart = ds.read_cursor # Read the raw data for the block blockhex_bin = ds.read_bytes(block_size) # Store the hex representation for later output blockhex = blockhex_bin.hex() # Hash it with dcrypt to get the block hash and note the value bhash = dcrypt_hash.getPoWHash(blockhex_bin) blockhash = bhash[::-1].hex() # **REDO FROM START** ds.seek_file(blockstart) # Parse the data # Standard block rubric version = struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex() prev_header_hash = ds.read_bytes(32)[::-1].hex() merkle_root_hash = ds.read_bytes(32)[::-1].hex() timestamp = ds.read_bytes(4)[::-1].hex() timestampint = int(timestamp, 16) formatted_timestamp = datetime.fromtimestamp(int(timestamp, 16)).isoformat() nbits = ds.read_bytes(4)[::-1].hex() nonce = ds.read_bytes(4)[::-1].hex() nonceint = int(nonce, 16) # Proof of burn stuff fproofofburn = ds.read_boolean() hashburnblock = ds.read_bytes(32)[::-1].hex() burnhash = ds.read_bytes(32)[::-1].hex() burnblockheight = ds.read_int32() burnctx = ds.read_int32() burnctxout = ds.read_int32() neffectiveburncoins = ds.read_int64() nburnbits = ds.read_uint32() # Transactions txcount = ds.read_compact_size() txcountint = int(txcount) txstr = "" # Iterate over txs for txnum in range(1, txcountint+1): tx_version = ds.read_bytes(4)[::-1].hex() tx_ntime = ds.read_bytes(4)[::-1].hex() tx_ntimeint = int(tx_ntime, 16) formatted_tx_ntime = datetime.fromtimestamp(int(tx_ntime, 16)).isoformat() tx_input = ds.read_compact_size() input = int(tx_input) vinstr = "" # Iterate over tx inputs for txin in range(0, input): tx_prev_output_hash = ds.read_bytes(32)[::-1].hex() tx_prev_output_sequence = ds.read_bytes(4)[::-1].hex() coinbase_tx_length = ds.read_bytes(1)[::-1].hex() coinbase_tx_lengthint = int(coinbase_tx_length, 16) coinbase_tx = ds.read_bytes(int((coinbase_tx_length), 16)).hex() sequence = ds.read_bytes(4)[::-1].hex() vinstr += vin_template.format(**locals()) no_of_outputs = ds.read_compact_size() voutstr = "" # Iterate over tx outputs for j in range(0, int(no_of_outputs)): btc_amt = ds.read_bytes(8)[::-1].hex() btc_amtint = int(btc_amt, 16) pk_script_len = ds.read_compact_size() pk_script_lenint = int(pk_script_len) pk_script = ds.read_bytes(int(pk_script_lenint)).hex() voutstr += vout_template.format(**locals()) # Allegedly tnburnbits = ds.read_bytes(4)[::-1].hex() # Append to output string txstr += tx_template.format(**locals()) # Block signature length and data block_sig_length = ds.read_bytes(1)[::-1].hex() block_sig_lengthint = int(block_sig_length, 16) block_sig = ds.read_bytes(int(block_sig_length, 16)).hex() # print(block_template.format(**locals()),txstr,cbs_template.format(**locals())) # return the serlialised data return '\n'.join([block_template.format(**locals()), txstr, cbs_template.format(**locals())])
def read_block_steps(self, ds, blockheight=0): # Original approach, superseded by Block class approach # Read file # https://bitcoin.org/en/developer-reference#block-headers # https://en.bitcoin.it/wiki/Protocol_specification#block magic = ds.read_bytes(4).hex() # Acid test if magic != "6e8b92a5": print("FAILED {}\n{}".format(magic, ds.read_bytes(512).hex())) return {} # Good to go, find out the length of this block block_size = int( struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex(), 16) # Save point blockstart = ds.read_cursor # Read the raw data for the block blockhex_bin = ds.read_bytes(block_size) # Store the hex representation for later output blockhex = blockhex_bin.hex() # Hash it with dcrypt to get the block hash and note the value bhash = dcrypt_hash.getPoWHash(blockhex_bin) blockhash = bhash[::-1].hex() # **REDO FROM START** ds.seek_file(blockstart) # Parse the data # Standard block rubric version = struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex() prev_header_hash = ds.read_bytes(32)[::-1].hex() merkle_root_hash = ds.read_bytes(32)[::-1].hex() timestamp = ds.read_bytes(4)[::-1].hex() timestampint = int(timestamp, 16) formatted_timestamp = datetime.fromtimestamp(int(timestamp, 16)).isoformat() nbits = ds.read_bytes(4)[::-1].hex() nonce = ds.read_bytes(4)[::-1].hex() nonceint = int(nonce, 16) # Proof of burn stuff fproofofburn = ds.read_boolean() hashburnblock = ds.read_bytes(32)[::-1].hex() burnhash = ds.read_bytes(32)[::-1].hex() burnblockheight = ds.read_int32() burnctx = ds.read_int32() burnctxout = ds.read_int32() neffectiveburncoins = ds.read_int64() nburnbits = ds.read_uint32() # Transactions txcount = ds.read_compact_size() txcountint = int(txcount) txstr = "" # Iterate over txs for txnum in range(1, txcountint + 1): tx_version = ds.read_bytes(4)[::-1].hex() tx_ntime = ds.read_bytes(4)[::-1].hex() tx_ntimeint = int(tx_ntime, 16) formatted_tx_ntime = datetime.fromtimestamp(int(tx_ntime, 16)).isoformat() tx_input = ds.read_compact_size() input = int(tx_input) vinstr = "" # Iterate over tx inputs for txin in range(0, input): tx_prev_output_hash = ds.read_bytes(32)[::-1].hex() tx_prev_output_sequence = ds.read_bytes(4)[::-1].hex() coinbase_tx_length = ds.read_bytes(1)[::-1].hex() coinbase_tx_lengthint = int(coinbase_tx_length, 16) coinbase_tx = ds.read_bytes(int((coinbase_tx_length), 16)).hex() sequence = ds.read_bytes(4)[::-1].hex() vinstr += vin_template.format(**locals()) no_of_outputs = ds.read_compact_size() voutstr = "" # Iterate over tx outputs for j in range(0, int(no_of_outputs)): btc_amt = ds.read_bytes(8)[::-1].hex() btc_amtint = int(btc_amt, 16) pk_script_len = ds.read_compact_size() pk_script_lenint = int(pk_script_len) pk_script = ds.read_bytes(int(pk_script_lenint)).hex() voutstr += vout_template.format(**locals()) # Allegedly tnburnbits = ds.read_bytes(4)[::-1].hex() # Append to output string txstr += tx_template.format(**locals()) # Block signature length and data block_sig_length = ds.read_bytes(1)[::-1].hex() block_sig_lengthint = int(block_sig_length, 16) block_sig = ds.read_bytes(int(block_sig_length, 16)).hex() # print(block_template.format(**locals()),txstr,cbs_template.format(**locals())) # return the serlialised data return '\n'.join([ block_template.format(**locals()), txstr, cbs_template.format(**locals()) ])
def __init__(self, ds): magic = "6e8b92a5" self.magic = ds.read_bytes(4).hex() # Acid test if self.magic != magic: print("FAILED {}\n{}".format(magic, ds.read_bytes(512).hex())) return {} # Good to go, find out the length of this block self.size = int( struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex(), 16) # Save point self.start = ds.read_cursor # Read the raw data for the block self.data = ds.read_bytes(self.size) # Store the hex representation for later output # Hash it with dcrypt to get the block hash and note the value self.blockhash = dcrypt_hash.getPoWHash(self.data) # **REDO FROM START** ds.seek_file(self.start) # Parse the data # Standard block rubric self.version = struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex() self.prev_header_hash = ds.read_bytes(32)[::-1].hex() self.merkle_root_hash = ds.read_bytes(32)[::-1].hex() timestampbits = ds.read_bytes(4)[::-1].hex() self.timestamp = int(timestampbits, 16) self.nbits = ds.read_bytes(4)[::-1].hex() self.nonce = ds.read_bytes(4)[::-1].hex() self.nonceint = int(self.nonce, 16) # Proof of burn stuff self.fproofofburn = ds.read_boolean() self.hashburnblock = ds.read_bytes(32)[::-1].hex() self.burnhash = ds.read_bytes(32)[::-1].hex() self.burnblockheight = ds.read_int32() self.burnctx = ds.read_int32() self.burnctxout = ds.read_int32() self.neffectiveburncoins = ds.read_int64() self.nburnbits = ds.read_uint32() # Transactions self.tx_count = int(ds.read_compact_size()) self.txs = [ dict( tx_version=ds.read_bytes(4)[::-1].hex(), tx_ntime=int(ds.read_bytes(4)[::-1].hex(), 16), # tx_input = int(ds.read_compact_size()), vins=[ self.read_txin(ds) for vin in range(0, int(ds.read_compact_size())) ], vouts=[ self.read_txout(ds) for vout in range(0, int(ds.read_compact_size())) ], tnburnbits=ds.read_bytes(4)[::-1].hex()) for txnum in range(1, self.tx_count + 1) ] # Block signature self.block_sig_length = ds.read_bytes(1)[::-1].hex() block_sig_lengthint = int(self.block_sig_length, 16) self.block_sig = ds.read_bytes(block_sig_lengthint).hex()
def block_header_hash(chain, header): import dcrypt_hash return dcrypt_hash.getPoWHash(header)
def read_blockdata(self, ds, blockheight=1): # Read file # https://bitcoin.org/en/developer-reference#block-headers # https://en.bitcoin.it/wiki/Protocol_specification#block magic = ds.read_bytes(4).hex() if magic != "6e8b92a5": print("FAILED {}\n{}".format(magic, ds.read_bytes(512).hex())) return {} block_size = int(struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex(), 16) blockstart = ds.read_cursor blockhex_bin = ds.read_bytes(block_size) blockhex = blockhex_bin.hex() bhash = dcrypt_hash.getPoWHash(blockhex_bin) blockhash = bhash[::-1].hex() ds.seek_file(blockstart) version = struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex() prev_header_hash = ds.read_bytes(32)[::-1].hex() merkle_root_hash = ds.read_bytes(32)[::-1].hex() timestamp = ds.read_bytes(4)[::-1].hex() timestampint = int(timestamp, 16) formatted_timestamp = datetime.fromtimestamp(int(timestamp, 16)).isoformat() nbits = ds.read_bytes(4)[::-1].hex() nonce = ds.read_bytes(4)[::-1].hex() nonceint = int(nonce, 16) fproofofburn = ds.read_boolean() hashburnblock = ds.read_bytes(32)[::-1].hex() burnhash = ds.read_bytes(32)[::-1].hex() burnblockheight = ds.read_int32() burnctx = ds.read_int32() burnctxout = ds.read_int32() neffectiveburncoins = ds.read_int64() nburnbits = ds.read_uint32() txcount = ds.read_compact_size() txcountint = int(txcount) txstr = "" for txnum in range(1, txcountint+1): tx_version = ds.read_bytes(4)[::-1].hex() tx_ntime = ds.read_bytes(4)[::-1].hex() tx_ntimeint = int(tx_ntime, 16) formatted_tx_ntime = datetime.fromtimestamp(int(tx_ntime, 16)).isoformat() tx_input = ds.read_compact_size() input = int(tx_input) vinstr = "" for txin in range(0, input): tx_prev_output_hash = ds.read_bytes(32)[::-1].hex() tx_prev_output_sequence = ds.read_bytes(4)[::-1].hex() coinbase_tx_length = ds.read_bytes(1)[::-1].hex() coinbase_tx_lengthint = int(coinbase_tx_length, 16) coinbase_tx = ds.read_bytes(int((coinbase_tx_length), 16)).hex() sequence = ds.read_bytes(4)[::-1].hex() vinstr += vin_template.format(**locals()) no_of_outputs = ds.read_compact_size() voutstr = "" for j in range(0, int(no_of_outputs)): btc_amt = ds.read_bytes(8)[::-1].hex() btc_amtint = int(btc_amt, 16) pk_script_len = ds.read_compact_size() pk_script_lenint = int(pk_script_len) pk_script = ds.read_bytes(int(pk_script_lenint)).hex() voutstr += vout_template.format(**locals()) tnburnbits = ds.read_bytes(4)[::-1].hex() txstr += tx_template.format(**locals()) block_sig_length = ds.read_bytes(1)[::-1].hex() block_sig_lengthint = int(block_sig_length, 16) block_sig = ds.read_bytes(int(block_sig_length, 16)).hex() # print(block_template.format(**locals()),txstr,cbs_template.format(**locals())) return '\n'.join([block_template.format(**locals()), txstr, cbs_template.format(**locals())])
def read_blockdata(self, ds, blockheight=1): # Read file # https://bitcoin.org/en/developer-reference#block-headers # https://en.bitcoin.it/wiki/Protocol_specification#block magic = ds.read_bytes(4).hex() if magic != "6e8b92a5": print("FAILED {}\n{}".format(magic, ds.read_bytes(512).hex())) return {} block_size = int( struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex(), 16) blockstart = ds.read_cursor blockhex_bin = ds.read_bytes(block_size) blockhex = blockhex_bin.hex() bhash = dcrypt_hash.getPoWHash(blockhex_bin) blockhash = bhash[::-1].hex() ds.seek_file(blockstart) version = struct.pack('>l', *struct.unpack('<l', ds.read_bytes(4))).hex() prev_header_hash = ds.read_bytes(32)[::-1].hex() merkle_root_hash = ds.read_bytes(32)[::-1].hex() timestamp = ds.read_bytes(4)[::-1].hex() timestampint = int(timestamp, 16) formatted_timestamp = datetime.fromtimestamp(int(timestamp, 16)).isoformat() nbits = ds.read_bytes(4)[::-1].hex() nonce = ds.read_bytes(4)[::-1].hex() nonceint = int(nonce, 16) fproofofburn = ds.read_boolean() hashburnblock = ds.read_bytes(32)[::-1].hex() burnhash = ds.read_bytes(32)[::-1].hex() burnblockheight = ds.read_int32() burnctx = ds.read_int32() burnctxout = ds.read_int32() neffectiveburncoins = ds.read_int64() nburnbits = ds.read_uint32() txcount = ds.read_compact_size() txcountint = int(txcount) txstr = "" for txnum in range(1, txcountint + 1): tx_version = ds.read_bytes(4)[::-1].hex() tx_ntime = ds.read_bytes(4)[::-1].hex() tx_ntimeint = int(tx_ntime, 16) formatted_tx_ntime = datetime.fromtimestamp(int(tx_ntime, 16)).isoformat() tx_input = ds.read_compact_size() input = int(tx_input) vinstr = "" for txin in range(0, input): tx_prev_output_hash = ds.read_bytes(32)[::-1].hex() tx_prev_output_sequence = ds.read_bytes(4)[::-1].hex() coinbase_tx_length = ds.read_bytes(1)[::-1].hex() coinbase_tx_lengthint = int(coinbase_tx_length, 16) coinbase_tx = ds.read_bytes(int((coinbase_tx_length), 16)).hex() sequence = ds.read_bytes(4)[::-1].hex() vinstr += vin_template.format(**locals()) no_of_outputs = ds.read_compact_size() voutstr = "" for j in range(0, int(no_of_outputs)): btc_amt = ds.read_bytes(8)[::-1].hex() btc_amtint = int(btc_amt, 16) pk_script_len = ds.read_compact_size() pk_script_lenint = int(pk_script_len) pk_script = ds.read_bytes(int(pk_script_lenint)).hex() voutstr += vout_template.format(**locals()) tnburnbits = ds.read_bytes(4)[::-1].hex() txstr += tx_template.format(**locals()) block_sig_length = ds.read_bytes(1)[::-1].hex() block_sig_lengthint = int(block_sig_length, 16) block_sig = ds.read_bytes(int(block_sig_length, 16)).hex() # print(block_template.format(**locals()),txstr,cbs_template.format(**locals())) return '\n'.join([ block_template.format(**locals()), txstr, cbs_template.format(**locals()) ])