How to Implement Bitcoin Keys and Addresses in Python?

How to Implement Bitcoin Keys and Addresses in Python?


The most significant and important bitcoin library in Python is pybitcointools by Vitalik Buterin. Pybitcointools is used as a Python library for Crypto coins signatures and transactions. We can make sure its installation by using

pip install cryptos

 This Library helps and supports making and pushing raw transactions for:

  • Bitcoin mainnet
  • Bitcoin testnet
  • Bitcoin Cash mainnet (with replay protection)
  • Bitcoin Cash testnet (with replay protection)
  • Litecoin mainnet
  • Litecoin testnet
  • Dash mainnet
  • Dash testnet
  • Dogecoin mainnet
  • Bitcoin Gold mainnet (with replay protection)


There are some advantages and disadvantages of the pybitcointools library.


  • Methods have an easy and simple interface, inputting and outputting in standard formats
  • Classes for different coins with a common interface
  • Numerous functions may be taken out and used individually
  • Supports binary, hex and base58
  • Transaction deserialization format almost compatible with BitcoinJS
  • Electrum and BIP0032 support
  • Generate and publish a transaction all in a single command-line instruction with full control
  • Includes non-bitcoin-specific conversion and JSON utilities


  • This has not a full node, no idea what blocks are
  • Relies on centralized explorers for blockchain operations

Bitcoin’s elliptic curve equation

                                     y2 mod p = (x3 + 7) mod p

In this equation p is a prime constant and the value is 2256 – 232 – 29 – 28 – 27 – 26 – 24 – 1

The public key is K = k * G

Where G is the generation point on an elliptic curve specified in Bitcoin. The k is the private key, and the calculated public key is also a point on the curve, which is the public key point  (x, y ). The ordinary public key is directly 04xy. By this way, ‘04’, the hexadecimal of x and the hexadecimal of y, are directly put together

How to compress the public key

As the public key is a point on the elliptic curve. Only record x and y may be calculated by equation. Y in the real number field2 would get a pair of opposite solutions. By a finite field based on the prime power p, y2 two solutions with different parities would be obtained. We need to mark it to know which y value of the initial K point is.

Suppose that the public key coordinates are (x, y), the common public key address is 04xy, and the compressed public key address is 02x or 03x.

Example: Key and address generation and formatting by the help of pybitcointools library

We use the pybitcointools library that is imported as bitcoin to generate and display keys and addresses in various formats.

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 show a compressed private key

compressed_private_key = private_key + '01'

print "Private Key Compressed (hex) is: ", compressed_private_key

# Create 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 by the private key to receive a public key point

public_key = bitcoin.fast_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, fix 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)

print "Compressed Bitcoin Address (b58check) is:", 


Following Example shows the output from running this code. Running

$ python

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:

Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 422

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top