Sitemap
Uche Blessed

Uche Blessed

Developer Relations & Software Engineer writing clear, practical content on C#, .NET, Python, JavaScript, and SQL for the developer community.

Creating a Cryptocurrency Wallet Using “pywallet” Python Library

3 min readMar 22, 2024

--

Press enter or click to view image in full size

Introduction

Creating a cryptocurrency wallet using pywallet involves generating a seed phrase (mnemonic), deriving a master key from the seed, and then deriving individual keys for each cryptocurrency address. Here's a comprehensive guide with code examples using pywallet to create a Bitcoin (BTC) wallet:

Step 1: Install pywallet

First, install the pywallet library using terminal (cmd):

pip install pywallet

After the installation is complete, you can verify that pywallet is installed by importing it in a Python script or interactive session:

import pywallet

If no errors occur, the installation was successful.

Note: Depending on the package dependencies and your system configuration, you may need to install additional packages or libraries to complete the installation process. Read more about pywallet on the official documentation page

Press enter or click to view image in full size
PYWallet Supports up to python 3.6

Step 2: Import the wallet module from pywallet

Next, create a new file on your Integrated Development Environment (IDE), preferably pycharm or VS Code.

Import the wallet module from the pywallet library, which contains functions for creating and managing wallets. Save this file as wallet.py

from pywallet import wallet

Step 3: Generate a new mnemonic (12-word seed phrase)

This code generates a new 12-word mnemonic (seed phrase) that can be used to generate the wallet’s private keys. The generate_mnemonic() function returns the mnemonic phrase as a string.

# Generate a new mnemonic (12-word seed phrase)
mnemonic = wallet.generate_mnemonic()
print("Mnemonic:", mnemonic)

Step 4: Derive Master Key

Derive the master key from the seed phrase:

This code creates a new wallet using the generated mnemonic. The create_wallet() function is used to create the wallet. It takes three arguments: the network (in this case, "BTC" for Bitcoin), the seed (mnemonic), and the number of child keys to derive (1 in this case).

# Create a wallet from the mnemonic
w = wallet.create_wallet(network="BTC", seed=mnemonic, children=1)

# Get the master key
master_key = w.get("master")
print("Master Key:", master_key)

Step 5: Derive Bitcoin Address, Public Key, and Private Key

Derive a Bitcoin address, public key, and private key from the master key:

Get Uche Blessed’s stories in your inbox

Join Medium for free to get updates from this writer.

These lines retrieve the wallet’s address, public key, and private key from the wallet object (w) using the get() method. The get() method takes a key name as an argument and returns the corresponding value from the wallet object.

# Get the first Bitcoin address
address = w.get("address")

# Get the public and private keys
public_key = w.get("public_key")
private_key = w.get("private_key")

Step 6: Print the mnemonic, master key, wallet address, public key, and private key

These code prints out the mnemonic phrase, master key, wallet address, public key, and private key to the console.

# Print your wallet details
print("Mnemonic:", mnemonic)
print("Master Key:", master_key)
print("Address:", address)
print("Public Key:", public_key)
print("Private Key:", private_key)

Step 7: Run your Python file.

Navigate to your wallet.py directory, open in terminal and input the following code to run your python file and view your wallet details:

python wallet.py

Full Example: Creating a Bitcoin Wallet

Here’s the complete code for creating a Bitcoin wallet using pywallet:

pip install pywallet
from pywallet import wallet

# Generate a new mnemonic (12-word seed phrase)
mnemonic = wallet.generate_mnemonic()

# Create a wallet from the mnemonic
w = wallet.create_wallet(network="BTC", seed=mnemonic, children=1)

# Get the master key
master_key = w.get("master")

# Get the first Bitcoin address
address = w.get("address")

# Get the public and private keys
public_key = w.get("public_key")
private_key = w.get("private_key")


# Print your wallet details
print("Mnemonic:", mnemonic)
print("Master Key:", master_key)
print("Address:", address)
print("Public Key:", public_key)
print("Private Key:", private_key)
python wallet.py

The pywallet library primarily focuses on Bitcoin (BTC) and its derivatives. It supports the following cryptocurrencies and protocols:

  1. Bitcoin (BTC)
  2. Litecoin (LTC)
  3. Dogecoin (DOGE)
  4. Dash (DASH)
  5. Bitcoin Cash (BCH)
  6. Bitcoin SV (BSV)
  7. Zcash (ZEC)
  8. Ethereum (ETH) — limited support for generating wallets

This tutorial demonstrates how to create a Bitcoin wallet using pywallet. Remember to keep your mnemonic phrase and private keys secure and private.

--

--

Written by Uche Blessed

Developer Relations & Software Engineer writing clear, practical content on C#, .NET, Python, JavaScript, and SQL for the developer community.