2017-09-06 04:44:52 +00:00
|
|
|
from __future__ import print_function
|
2014-06-02 19:04:18 +00:00
|
|
|
import bitcoin
|
|
|
|
|
|
|
|
# Generate a random private key
|
|
|
|
valid_private_key = False
|
|
|
|
while not valid_private_key:
|
|
|
|
private_key = bitcoin.random_key()
|
|
|
|
decoded_private_key = bitcoin.decode_privkey(private_key, 'hex')
|
2017-08-24 16:27:27 +00:00
|
|
|
valid_private_key = 0 < decoded_private_key < bitcoin.N
|
2014-08-27 20:08:43 +00:00
|
|
|
|
2017-08-24 16:27:27 +00:00
|
|
|
print("Private Key (hex) is: ", private_key)
|
|
|
|
print("Private Key (decimal) is: ", decoded_private_key)
|
2014-06-02 19:04:18 +00:00
|
|
|
|
|
|
|
# Convert private key to WIF format
|
|
|
|
wif_encoded_private_key = bitcoin.encode_privkey(decoded_private_key, 'wif')
|
2017-08-24 16:27:27 +00:00
|
|
|
print("Private Key (WIF) is: ", wif_encoded_private_key)
|
2014-06-02 19:04:18 +00:00
|
|
|
|
|
|
|
# Add suffix "01" to indicate a compressed private key
|
|
|
|
compressed_private_key = private_key + '01'
|
2017-08-24 16:27:27 +00:00
|
|
|
print("Private Key Compressed (hex) is: ", compressed_private_key)
|
2014-06-02 19:04:18 +00:00
|
|
|
|
|
|
|
# Generate a WIF format from the compressed private key (WIF-compressed)
|
2014-08-27 20:08:43 +00:00
|
|
|
wif_compressed_private_key = bitcoin.encode_privkey(
|
2019-12-04 03:39:59 +00:00
|
|
|
bitcoin.decode_privkey(compressed_private_key, 'hex'), 'wif')
|
2017-08-24 16:27:27 +00:00
|
|
|
print("Private Key (WIF-Compressed) is: ", wif_compressed_private_key)
|
2014-06-02 19:04:18 +00:00
|
|
|
|
|
|
|
# Multiply the EC generator point G with the private key to get a public key point
|
2015-03-03 16:57:53 +00:00
|
|
|
public_key = bitcoin.fast_multiply(bitcoin.G, decoded_private_key)
|
2017-08-24 16:27:27 +00:00
|
|
|
print("Public Key (x,y) coordinates is:", public_key)
|
2014-06-02 19:04:18 +00:00
|
|
|
|
|
|
|
# Encode as hex, prefix 04
|
2017-08-24 16:27:27 +00:00
|
|
|
hex_encoded_public_key = bitcoin.encode_pubkey(public_key, 'hex')
|
|
|
|
print("Public Key (hex) is:", hex_encoded_public_key)
|
2014-06-02 19:04:18 +00:00
|
|
|
|
|
|
|
# Compress public key, adjust prefix depending on whether y is even or odd
|
|
|
|
(public_key_x, public_key_y) = public_key
|
2017-08-24 16:27:27 +00:00
|
|
|
compressed_prefix = '02' if (public_key_y % 2) == 0 else '03'
|
2018-01-27 13:56:25 +00:00
|
|
|
hex_compressed_public_key = compressed_prefix + (bitcoin.encode(public_key_x, 16).zfill(64))
|
2017-08-24 16:27:27 +00:00
|
|
|
print("Compressed Public Key (hex) is:", hex_compressed_public_key)
|
2014-06-02 19:04:18 +00:00
|
|
|
|
|
|
|
# Generate bitcoin address from public key
|
2017-08-24 16:27:27 +00:00
|
|
|
print("Bitcoin Address (b58check) is:", bitcoin.pubkey_to_address(public_key))
|
2014-06-02 19:04:18 +00:00
|
|
|
|
|
|
|
# Generate compressed bitcoin address from compressed public key
|
2017-08-24 16:27:27 +00:00
|
|
|
print("Compressed Bitcoin Address (b58check) is:",
|
|
|
|
bitcoin.pubkey_to_address(hex_compressed_public_key))
|