How to Implement Bitcoin Keys and Addresses in Python?

How to Implement Bitcoin Keys and Addresses in Python?

Introduction

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)

Description

There are some advantages and disadvantages of the pybitcointools library.

Advantages:

  • 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

Disadvantages:

  • 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'

else:

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:", 

bitcoin.pubkey_to_address(hex_compressed_public_key)

Following Example shows the output from running this code. Running key-to-address-ecc-example.py

$ python key-to-address-ecc-example.py

Private Key (hex) is:

3aba4162c7251c891207b747840551a71939b0de081f85c4e44cf7c13e41daa6

Private Key (decimal) is:

26563230048437957592232553826663696440606756685920117476832299673293013768870

Private Key (WIF) is:

5JG9hT3beGTJuUAmCQEmNaxAuMacCTfXuw1R3FCXig23RQHMr4K

Private Key Compressed (hex) is:

3aba4162c7251c891207b747840551a71939b0de081f85c4e44cf7c13e41daa601

Private Key (WIF-Compressed) is:

KyBsPXxTuVD82av65KZkrGrWi5qLMah5SdNq6uftawDbgKa2wv6S

Public Key (x,y) coordinates is:

(41637322786646325214887832269588396900663353932545912953362782457239403430124L,

16388935128781238405526710466724741593761085120864331449066658622400339362166L)

Public Key (hex) is:

045c0de3b9c8ab18dd04e3511243ec2952002dbfadc864b9628910169d9b9b00ec↵

243bcefdd4347074d44bd7356d6a53c495737dd96295e2a9374bf5f02ebfc176

Compressed Public Key (hex) is:

025c0de3b9c8ab18dd04e3511243ec2952002dbfadc864b9628910169d9b9b00ec

Bitcoin Address (b58check) is:

1thMirt546nngXqyPEz532S8fLwbozud8

Compressed Bitcoin Address (b58check) is:

14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3
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