I Used These 3 Easy Steps to Create a Bitcoin Wallet in Python (Public/Private)

As I write this, Bitcoin is in a deep bear market. That’s the perfect time to learn about the tech and start building!

After listening to a podcast from Lyn Alden today, I wondered if it is possible to programmatically create a Bitcoin wallet, i.e., a public/private key pair.

This can be extremely useful in practice, not only if you want to create an application that uses the “decentralized money layer” to transfer value between two parties in a fully automatic way, but also if you want to quickly create a public/private key pair to send and receive BTC without trusting a third party.

You may not trust that wallet provider after all. It is in the nature of the Bitcoin protocol that if you desperately need it, you’ll need it quickly and without lots of trust assumptions. So better be prepared!

In this project, we’ll answer the following interesting question.

πŸͺ™ Project: How to create a Bitcoin wallet in Python (public/private key pair)?

Step 1: Install Library

Use PIP to install the bitcoinaddress library in your actual or virtual environment.

πŸ” Is It Safe? I investigated the library code from the GitHub repository associated with this library, and I couldn’t find any trust issues. Specifically, I searched for “hacks” in the code, such as sending the public/private key pair to a remote server, but the repository seems to be clean. It is also well-respected in the community, so unlikely to be tampered with. I didn’t check if the public/private key pairs have maximum entropy, i.e., are truly randomly created with all private keys having the same likelihood. I cannot guarantee that this is 100% safe because I don’t know the owner of the library — but it looks safe at first and second glance.

To install the library, here are three of the most common ways:

πŸ‘‰ Python 3
pip3 install bitcoinaddress

πŸ‘‰ Standard Python and Python 2 Installation
pip install bitcoinaddress

πŸ‘‰ Jupyter Notebook Cell
!pip install bitcoinaddress

Here’s what this looks like in my Jupyter Notebook:

🌍 Recommended: 5 Steps to Install a Python Library

Step 2: Import and Create Wallet

The Wallet class from the bitcoinaddress module allows you to easily create a new and random public/private keypair using the Wallet() constructor method, i.e., all you need to create a new random Bitcoin wallet.

from bitcoinaddress import Wallet
wallet = Wallet()

Stay with me. You’re almost done! πŸ’ͺ

Step 3: Print Wallet

Next, print the content of the newly created wallet. This contains all the information you need about the public and private keys and addresses.

print(wallet)

In the following output, I bolded the two relevant lines with the public address and the private key:

Private Key HEX: 6b789bec69f7f90c2ed73c8ee58f1f899b42fde5641359f6b76a27b4406399f7
Private Key WIF: 5JdcnccAMqs1t38VTPyeGHgBQ7KaYGueSqUAmLBTzVqFzh4ssUN
Private Key WIF compressed: KzpcxLACJzfktGQ4bWR1UUbvtzu133DNH2vv6ffC8nG1BFSUFBfr 
            
Public Key: 0415d47844bab349f12ae51a4b7f9d5eeab11ddf5d958e7fc67f6d29a456394be997d31989f6dcca716db63898c739621a86aa4a7bbe74c8936a6f1bbc7937c5c0 
Public Key compressed: 0215d47844bab349f12ae51a4b7f9d5eeab11ddf5d958e7fc67f6d29a456394be9

Public Address 1: 14XyDoAgdGF7xiCrgux5Bd7P993PnXALuW   
Public Address 1 compressed: 1LW26DRtBraVQ5ec7J5D3uQsM3AD3oVHXx   
Public Address 3: 32iX1WnnMkLQLc6beTQ6no5H4J6arvUeBP  
Public Address bc1 P2WPKH: bc1q6hn4e55vfh6ka0z88tpr2jmqze8w4j84axsjh4    
Public Address bc1 P2WSH: bc1qhff5zxmy7rs5mvx037ztg95nnnqe97fet66l65xgsafv89tmz8xssm8tph 

The output of the bitcoinaddress.Wallet() method provides the details of a new bitcoin wallet.

It includes the private key in both HEX and Wallet Import Format (WIF) formats, as well as the compressed version of the WIF.

It also provides the public key, both in uncompressed and compressed formats, as well as three different public addresses generated from the public key.

I actually checked the address on a Blockchain explorer, and it’s the correct one:

I also checked if the public and private addresses match and they seem to do:

Additionally, it provides 2 SegWit addresses generated from the public key; one in Pay-to-Witness-Public-Key-Hash (P2WPKH) format and one in Pay-to-Witness-Script-Hash (P2WSH) format.