mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2024-12-23 15:18:11 +00:00
Merge pull request #366 from cclauss/patch-1
long(), print(), xrange() for Python 3
This commit is contained in:
commit
ce1b4904d6
@ -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()
|
||||
|
@ -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))
|
||||
|
@ -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
|
||||
print(input, '=>', hash)
|
||||
|
@ -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))
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
||||
hash_power = float(long(nonce) / elapsed_time)
|
||||
print("Hashing Power: %ld hashes per second" % hash_power)
|
||||
|
@ -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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user