HomeBlockchainBlockchain DIYKeys and Addresses in Python

Keys and Addresses in Python

Audio version of the article

Implementing Keys and Addresses in Python

The most comprehensive bitcoin library in Python is “pybitcointools”by Vitalik Buterin (https://github.com/vbuterin/pybitcointools). In the following code example, we use the pybitcointools library (imported as “bitcoin”) to generate and display keys and addresses in various formats:

Example – Key and Address generation and formatting with the pybitcointools library

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')
 valid_private_key = 0 < decoded_private_key < bitcoin.N
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

# Add suffix "01" to indicate a compressed private key
compressed_private_key = private_key + '01'
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

# Multiply the EC generator point G with the private key to get a public key point
public_key = bitcoin.base10_multiply(bitcoin.G, decoded_private_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

# 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'
 compressed_prefix = '03'
hex_compressed_public_key = compressed_prefix + bitcoin.encode(public_key_x, 16)
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)

# Generate compressed bitcoin address from compressed public key
print "Compressed Bitcoin Address (b58check) is:", \

Here’s the output from running this code:

Example – Running key-to-address-ecc-example.py

$ python key-to-address-ecc-example.py
Private Key (hex) is:
Private Key (decimal) is:
Private Key (WIF) is:
Private Key Compressed (hex) is:
Private Key (WIF-Compressed) is:
Public Key (x,y) coordinates is:
Public Key (hex) is:
Compressed Public Key (hex) is:
Bitcoin Address (b58check) is:
Compressed Bitcoin Address (b58check) is:

Here’s another example, using the Python ECDSA library for the Elliptic Curve math and without using any specialized bitcoin libraries:

Example – A script demonstrating Elliptic Curve math used for bitcoin keys

import ecdsa
import random
from ecdsa.util import string_to_number, number_to_string

# secp256k1, http://www.oid-info.com/get/
_b = 0x0000000000000000000000000000000000000000000000000000000000000007L
_a = 0x0000000000000000000000000000000000000000000000000000000000000000L
_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798L
_Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L
curve_secp256k1 = ecdsa.ellipticcurve.CurveFp(_p, _a, _b)
generator_secp256k1 = ecdsa.ellipticcurve.Point(curve_secp256k1, _Gx, _Gy, _r)
oid_secp256k1 = (1, 3, 132, 0, 10)
SECP256k1 = ecdsa.curves.Curve("SECP256k1", curve_secp256k1, generator_secp256k1, oid_secp256k1)
ec_order = _r

curve = curve_secp256k1
generator = generator_secp256k1

def random_secret():
 random_char = lambda: chr(random.randint(0, 255))
 convert_to_int = lambda array: int("".join(array).encode("hex"), 16)
 byte_array = [random_char() for i in range(32)]
 return convert_to_int(byte_array)

def get_point_pubkey(point):
 if point.y() & 1:
 key = '03' + '%064x' % point.x()
 key = '02' + '%064x' % point.x()
 return key.decode('hex')
def get_point_pubkey_uncompressed(point):
 key = '04' + \
 '%064x' % point.x() + \
 '%064x' % point.y()
 return key.decode('hex')

# Generate a new private key.
secret = random_secret()
print "Secret: ", secret

# Get the public key point.
point = secret * generator
print "EC point:", point

print "BTC public key:", get_point_pubkey(point).encode("hex")

# Given the point (x, y) we can create the object using:
point1 = ecdsa.ellipticcurve.Point(curve, point.x(), point.y(), ec_order)
assert point1 == point

Running the script:

Example 4-6. Installing the Python ECDSA library and running the ec_math.py script
$ # Install Python PIP package manager
$ sudo apt-get install python-pip
$ # Install the Python ECDSA library
$ sudo pip install ecdsa
$ # Run the script
$ python ec-math.py
Secret: 38090835015954358862481132628887443905906204995912378278060168703580660294000
EC point: (70048853531867179489857750497606966272382583471322935454624595540007269312627,\
BTC public key: 029ade3effb0a67d5c8609850d797366af428f4a0d5194cb221d807770a1522873

This article has been published from the source link without modifications to the text. Only the headline has been changed.

Source link

- Advertisment -

Most Popular

- Advertisment -