Skip to content

Commit

Permalink
Merge pull request lhartikk#1 from efokschaner/patch-1
Browse files Browse the repository at this point in the history
Upgrade to python3
  • Loading branch information
efokschaner authored Apr 11, 2021
2 parents dcbe9b9 + e87a631 commit 4f56f27
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions genesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,22 @@ def get_algorithm(options):
sys.exit("Error: Given algorithm must be one of: " + str(supported_algorithms))

def create_input_script(psz_timestamp):
psz_prefix = ""
timestamp_bytes = psz_timestamp.encode('utf-8')
psz_prefix = ''
#use OP_PUSHDATA1 if required
if len(psz_timestamp) > 76: psz_prefix = '4c'
if len(timestamp_bytes) > 76: psz_prefix = '4c'

script_prefix = '04ffff001d0104' + psz_prefix + chr(len(psz_timestamp)).encode('hex')
print (script_prefix + psz_timestamp.encode('hex'))
return (script_prefix + psz_timestamp.encode('hex')).decode('hex')
length_in_hex = hex(len(timestamp_bytes))[2:]
script_prefix = '04ffff001d0104' + psz_prefix + length_in_hex
input_script_hex = script_prefix + timestamp_bytes.hex()
print(input_script_hex)
return bytes.fromhex(input_script_hex)


def create_output_script(pubkey):
script_len = '41'
OP_CHECKSIG = 'ac'
return (script_len + pubkey + OP_CHECKSIG).decode('hex')
return bytes.fromhex(script_len + pubkey + OP_CHECKSIG)


def create_transaction(input_script, output_script,options):
Expand All @@ -84,7 +87,7 @@ def create_transaction(input_script, output_script,options):
Bytes('output_script', 0x43),
UBInt32('locktime'))

tx = transaction.parse('\x00'*(127 + len(input_script)))
tx = transaction.parse(b'\x00'*(127 + len(input_script)))
tx.version = struct.pack('<I', 1)
tx.num_inputs = 1
tx.prev_output = struct.pack('<qqqq', 0,0,0,0)
Expand All @@ -110,7 +113,7 @@ def create_block_header(hash_merkle_root, time, bits, nonce):
Bytes("bits", 4),
Bytes("nonce", 4))

genesisblock = block_header.parse('\x00'*80)
genesisblock = block_header.parse(b'\x00'*80)
genesisblock.version = struct.pack('<I', 1)
genesisblock.hash_prev_block = struct.pack('<qqqq', 0,0,0,0)
genesisblock.hash_merkle_root = hash_merkle_root
Expand All @@ -122,7 +125,7 @@ def create_block_header(hash_merkle_root, time, bits, nonce):

# https://en.bitcoin.it/wiki/Block_hashing_algorithm
def generate_hash(data_block, algorithm, start_nonce, bits):
print 'Searching for genesis hash..'
print('Searching for genesis hash..')
nonce = start_nonce
last_updated = time.time()
# https://en.bitcoin.it/wiki/Difficulty
Expand Down Expand Up @@ -169,7 +172,7 @@ def generate_hashes_from_block(data_block, algorithm):


def is_genesis_hash(header_hash, target):
return int(header_hash.encode('hex_codec'), 16) < target
return int(header_hash.hex(), 16) < target


def calculate_hashrate(nonce, last_updated):
Expand All @@ -185,18 +188,18 @@ def calculate_hashrate(nonce, last_updated):


def print_block_info(options, hash_merkle_root):
print "algorithm: " + (options.algorithm)
print "merkle hash: " + hash_merkle_root[::-1].encode('hex_codec')
print "pszTimestamp: " + options.timestamp
print "pubkey: " + options.pubkey
print "time: " + str(options.time)
print "bits: " + str(hex(options.bits))
print("algorithm: " + (options.algorithm))
print("merkle hash: " + hash_merkle_root[::-1].hex())
print("pszTimestamp: " + options.timestamp)
print("pubkey: " + options.pubkey)
print("time: " + str(options.time))
print("bits: " + str(hex(options.bits)))


def announce_found_genesis(genesis_hash, nonce):
print "genesis hash found!"
print "nonce: " + str(nonce)
print "genesis hash: " + genesis_hash.encode('hex_codec')
print("genesis hash found!")
print("nonce: " + str(nonce))
print("genesis hash: " + genesis_hash.hex())


# GOGOGO!
Expand Down

0 comments on commit 4f56f27

Please sign in to comment.