From 5f3a3d446907bf54652fc38ef3193cea871ac780 Mon Sep 17 00:00:00 2001 From: cclauss Date: Thu, 24 Aug 2017 18:14:02 +0200 Subject: [PATCH 1/7] print() function for Python 3 --- code/bip-table.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/code/bip-table.py b/code/bip-table.py index e2dcc3e4..bc0ebdd9 100644 --- a/code/bip-table.py +++ b/code/bip-table.py @@ -17,15 +17,15 @@ while (line[0] != "|"): line = f.readline() while (line[1] == '-'): - line_num = f.readline() - line_layer = f.readline()[2:-1] - line_title = f.readline()[2:-1] - line_owner = f.readline()[2:-1] - line_type = f.readline()[2:-1] + line_num = f.readline() + line_layer = f.readline()[2:-1] + line_title = f.readline()[2:-1] + line_owner = f.readline()[2:-1] + line_type = f.readline()[2:-1] line_status = f.readline()[2:-1] - line = f.readline() + line = f.readline() while (line[0] != "|"): - line = f.readline() + line = f.readline() num = regex_num.match(line_num) alt_num = regex_altnum.match(line_num) @@ -34,6 +34,10 @@ while (line[1] == '-'): elif alt_num: bip_num = alt_num.group(1) - print "|[[bip-{0}]]https://github.com/bitcoin/bips/blob/master/bip-{0:04d}.mediawiki[BIP-{0}] |{1} |{2} |{3} |{4} ".format(int(bip_num), line_title, line_owner, line_type, line_status) - + print("|[[bip-{0}]]https://github.com/bitcoin/bips/blob/master/bip-{0:04d}" + ".mediawiki[BIP-{0}] |{1} |{2} |{3} |{4} ".format(int(bip_num), + line_title, + line_owner, + line_type, + line_status)) f.close() From 06ecaf916842c597928b9714a206c3f12589d7a5 Mon Sep 17 00:00:00 2001 From: cclauss Date: Thu, 24 Aug 2017 18:20:22 +0200 Subject: [PATCH 2/7] print() function for Python 3 --- code/get-utxo.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/code/get-utxo.py b/code/get-utxo.py index 285aba4c..ebce0083 100644 --- a/code/get-utxo.py +++ b/code/get-utxo.py @@ -25,4 +25,7 @@ resp = requests.get('https://blockchain.info/unspent?active=%s' % address) utxo_set = json.loads(resp.text)["unspent_outputs"] for utxo in utxo_set: - print "%s:%d - %ld Satoshis" % (utxo['tx_hash'], utxo['tx_output_n'], utxo['value']) + print("%s:%d - %ld Satoshis" % (utxo['tx_hash'], utxo['tx_output_n'], + utxo['value'])) + # Or try... + # print("{tx_hash}:{tx_output_n} - {value} Satoshis".format(**utxo)) From bdc3743e30c54e2abf3466e9c10a0fb7db7d629b Mon Sep 17 00:00:00 2001 From: cclauss Date: Thu, 24 Aug 2017 18:22:28 +0200 Subject: [PATCH 3/7] print() and range() for Python 3 --- code/hash_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/hash_example.py b/code/hash_example.py index 12012ac7..289628b0 100644 --- a/code/hash_example.py +++ b/code/hash_example.py @@ -6,7 +6,7 @@ import hashlib text = "I am Satoshi Nakamoto" # iterate nonce from 0 to 19 -for nonce in xrange(20): +for nonce in range(20): # add the nonce to the end of the text input = text + str(nonce) @@ -15,4 +15,4 @@ for nonce in xrange(20): hash = hashlib.sha256(input).hexdigest() # show the input and hash result - print input, '=>', hash \ No newline at end of file + print(input, '=>', hash) From cb1053f9051af647eda6682831f45d422c0fde1e Mon Sep 17 00:00:00 2001 From: cclauss Date: Thu, 24 Aug 2017 18:27:27 +0200 Subject: [PATCH 4/7] print() function for Python 3 Also ternary if --- code/key-to-address-ecc-example.py | 32 +++++++++++++----------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/code/key-to-address-ecc-example.py b/code/key-to-address-ecc-example.py index 49b0c851..742ffe71 100644 --- a/code/key-to-address-ecc-example.py +++ b/code/key-to-address-ecc-example.py @@ -5,45 +5,41 @@ valid_private_key = False while not valid_private_key: private_key = bitcoin.random_key() decoded_private_key = bitcoin.decode_privkey(private_key, 'hex') - valid_private_key = 0 < decoded_private_key < bitcoin.N + valid_private_key = 0 < decoded_private_key < bitcoin.N -print "Private Key (hex) is: ", private_key -print "Private Key (decimal) is: ", decoded_private_key +print("Private Key (hex) is: ", private_key) +print("Private Key (decimal) is: ", decoded_private_key) # Convert private key to WIF format wif_encoded_private_key = bitcoin.encode_privkey(decoded_private_key, 'wif') -print "Private Key (WIF) is: ", wif_encoded_private_key +print("Private Key (WIF) is: ", wif_encoded_private_key) # Add suffix "01" to indicate a compressed private key compressed_private_key = private_key + '01' -print "Private Key Compressed (hex) is: ", compressed_private_key +print("Private Key Compressed (hex) is: ", compressed_private_key) # Generate a WIF format from the compressed private key (WIF-compressed) wif_compressed_private_key = bitcoin.encode_privkey( bitcoin.decode_privkey(compressed_private_key, 'hex'), 'wif') -print "Private Key (WIF-Compressed) is: ", wif_compressed_private_key +print("Private Key (WIF-Compressed) is: ", wif_compressed_private_key) # Multiply the EC generator point G with the private key to get a public key point public_key = bitcoin.fast_multiply(bitcoin.G, decoded_private_key) -print "Public Key (x,y) coordinates is:", public_key +print("Public Key (x,y) coordinates is:", public_key) # Encode as hex, prefix 04 -hex_encoded_public_key = bitcoin.encode_pubkey(public_key,'hex') -print "Public Key (hex) is:", hex_encoded_public_key +hex_encoded_public_key = bitcoin.encode_pubkey(public_key, 'hex') +print("Public Key (hex) is:", hex_encoded_public_key) # Compress public key, adjust prefix depending on whether y is even or odd (public_key_x, public_key_y) = public_key -if (public_key_y % 2) == 0: - compressed_prefix = '02' -else: - compressed_prefix = '03' +compressed_prefix = '02' if (public_key_y % 2) == 0 else '03' hex_compressed_public_key = compressed_prefix + bitcoin.encode(public_key_x, 16) -print "Compressed Public Key (hex) is:", hex_compressed_public_key +print("Compressed Public Key (hex) is:", hex_compressed_public_key) # Generate bitcoin address from public key -print "Bitcoin Address (b58check) is:", bitcoin.pubkey_to_address(public_key) +print("Bitcoin Address (b58check) is:", bitcoin.pubkey_to_address(public_key)) # Generate compressed bitcoin address from compressed public key -print "Compressed Bitcoin Address (b58check) is:", \ - bitcoin.pubkey_to_address(hex_compressed_public_key) - +print("Compressed Bitcoin Address (b58check) is:", + bitcoin.pubkey_to_address(hex_compressed_public_key)) From 3c0b24edd78f74715e5ed0de1b9e21ccbd313528 Mon Sep 17 00:00:00 2001 From: cclauss Date: Thu, 24 Aug 2017 18:29:06 +0200 Subject: [PATCH 5/7] print() function for Python 3 --- code/max_money.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code/max_money.py b/code/max_money.py index a54238b4..fcb44509 100644 --- a/code/max_money.py +++ b/code/max_money.py @@ -3,6 +3,7 @@ start_block_reward = 50 # 210000 is around every 4 years with a 10 minute block interval reward_interval = 210000 + def max_money(): # 50 BTC = 50 0000 0000 Satoshis current_reward = 50 * 10**8 @@ -12,5 +13,5 @@ def max_money(): current_reward /= 2 return total -print "Total BTC to ever be created:", max_money(), "Satoshis" +print("Total BTC to ever be created:", max_money(), "Satoshis") From 35fef9a94fe348c1f2086c41001839e4bf6cbafc Mon Sep 17 00:00:00 2001 From: cclauss Date: Thu, 24 Aug 2017 18:37:02 +0200 Subject: [PATCH 6/7] long(), print(), xrange() for Python 3 Also remove trailing whitespace --- code/proof-of-work-example.py | 69 ++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/code/proof-of-work-example.py b/code/proof-of-work-example.py index 12e1fbbd..0431fedf 100755 --- a/code/proof-of-work-example.py +++ b/code/proof-of-work-example.py @@ -4,60 +4,61 @@ import hashlib import time -max_nonce = 2 ** 32 # 4 billion +try: + long # Python 2 + xrange +except NameError: + long = int # Python 3 + xrange = range + +max_nonce = 2 ** 32 # 4 billion + def proof_of_work(header, difficulty_bits): - # calculate the difficulty target - target = 2 ** (256-difficulty_bits) - + target = 2 ** (256 - difficulty_bits) + for nonce in xrange(max_nonce): - hash_result = hashlib.sha256(str(header)+str(nonce)).hexdigest() - + hash_result = hashlib.sha256(str(header) + str(nonce)).hexdigest() + # check if this is a valid result, below the target if long(hash_result, 16) < target: - print "Success with nonce %d" % nonce - print "Hash is %s" % hash_result - return (hash_result,nonce) - - print "Failed after %d (max_nonce) tries" % nonce + print("Success with nonce %d" % nonce) + print("Hash is %s" % hash_result) + return (hash_result, nonce) + + print("Failed after %d (max_nonce) tries" % nonce) return nonce - + if __name__ == '__main__': - nonce = 0 hash_result = '' - - # difficulty from 0 to 31 bits + + # difficulty from 0 to 31 bits for difficulty_bits in xrange(32): - difficulty = 2 ** difficulty_bits - print "Difficulty: %ld (%d bits)" % (difficulty, difficulty_bits) - - print "Starting search..." - + print("Difficulty: %ld (%d bits)" % (difficulty, difficulty_bits)) + print("Starting search...") + # checkpoint the current time start_time = time.time() - + # make a new block which includes the hash from the previous block # we fake a block of transactions - just a string - new_block = 'test block with transactions' + hash_result - + new_block = 'test block with transactions' + hash_result + # find a valid nonce for the new block - (hash_result, nonce) = proof_of_work(new_block, difficulty_bits) - + (hash_result, nonce) = proof_of_work(new_block, difficulty_bits) + # checkpoint how long it took to find a result end_time = time.time() - + elapsed_time = end_time - start_time - print "Elapsed Time: %.4f seconds" % elapsed_time - + print("Elapsed Time: %.4f seconds" % elapsed_time) + if elapsed_time > 0: - + # estimate the hashes per second - hash_power = float(long(nonce)/elapsed_time) - print "Hashing Power: %ld hashes per second" % hash_power - - - \ No newline at end of file + hash_power = float(long(nonce) / elapsed_time) + print("Hashing Power: %ld hashes per second" % hash_power) From df302e9fe5cd7d861808dc949695e219856f68bf Mon Sep 17 00:00:00 2001 From: cclauss Date: Thu, 24 Aug 2017 18:44:48 +0200 Subject: [PATCH 7/7] long() and print() for Python 3 --- code/select-utxo.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/code/select-utxo.py b/code/select-utxo.py index d1c488d3..c70aa38f 100644 --- a/code/select-utxo.py +++ b/code/select-utxo.py @@ -2,8 +2,13 @@ from sys import argv -class OutputInfo: +try: + long # Python 2 +except NameError: + long = int # Python 3 + +class OutputInfo: def __init__(self, tx_hash, tx_index, value): self.tx_hash = tx_hash self.tx_index = tx_index @@ -13,6 +18,7 @@ class OutputInfo: return "<%s:%s with %s Satoshis>" % (self.tx_hash, self.tx_index, self.value) + # Select optimal outputs for a send from unspent outputs list. # Returns output list and remaining change to be sent to # a change address. @@ -44,6 +50,7 @@ def select_outputs_greedy(unspent, min_value): # No results found. return None, 0 + def main(): unspent = [ OutputInfo("ebadfaa92f1fd29e2fe296eda702c48bd11ffd52313e986e99ddad9084062167", 1, 8000000), @@ -54,15 +61,11 @@ def main(): OutputInfo("12b6a7934c1df821945ee9ee3b3326d07ca7a65fd6416ea44ce8c3db0c078c64", 0, 10000000), OutputInfo("7f42eda67921ee92eae5f79bd37c68c9cb859b899ce70dba68c48338857b7818", 0, 16100000), ] - - if len(argv) > 1: - target = long(argv[1]) - else: - target = 55000000 - - print "For transaction amount %d Satoshis (%f bitcoin) use: " % (target, target/10.0**8) - print select_outputs_greedy(unspent, target) + target = long(argv[1]) if len(argv) > 1 else 55000000 + print("For transaction amount %d Satoshis (%f bitcoin) use: " % + (target, target / 10.0 ** 8)) + print(select_outputs_greedy(unspent, target)) + if __name__ == "__main__": main() -