Bitcoin Hierarchical Deterministic wallet in Python
Here we start a series of articles that entirely cover topics on how to start working with a blockchain network; from local creation of keys and addresses to transaction creation, signing, and broadcasting to the network. We take Bitcoin as the best example of the UTXO model, though then we’ll trace the same for Account-based blockchain networks.
Thanks to Gaurav Agrawal for the fascinating post.
In this article, we will create a bitcoin HD (Hierarchical Deterministic) wallet using python language. To learn how HD wallet works, you can check this article.
Prerequisite
- Python
- Basic understanding of programming
Download and install python’s latest version from its official website.
We need to install a python library called bitcoinlib
to create and manage bitcoin wallets. To install the library run following command.
pip install bitcoinlib
Bitcoin HD Wallet with Python
Using HD wallets, we can generate a complete suite of crypto wallets for different cryptocurrency networks using just one seed. However, in today’s article, we will only focus on Bitcoin and how to create a simple HD wallet.
So let’s create our first Bitcoin HD wallet.
from bitcoinlib.wallets import HDWalletwallet = HDWallet.create('Wallet1')key1 = wallet.new_key()print(key1.address)
In the above script, we are performing the following steps:
- Importing
bitcoinlib
library - create a new wallet
- Generating a new HD Key.
- Create a new address
To run the above program, save it in a file with .py extension. (Ex- hd_wallet1.py
). Then run that file using the command below.
python hd_wallet1.py
Bitcoin HD Wallet with Mnemonics
Mnemonics are very popular because they are easy to remember. Let’s see how to create a Bitcoin wallet using mnemonics.
from bitcoinlib.wallets import HDWallet, wallet_deletefrom bitcoinlib.mnemonic import Mnemonicpassphrase = Mnemonic().generate()print(passphrase)wallet = HDWallet.create("mWallet1", keys=passphrase, network='bitcoin')key1 = wallet.new_key()print(key1.address)
In the above script, we are performing the following steps.
- Importing relevant classes from
bitcoinlib
- Generating a random mnemonic, you can pass your own
- Creating a new wallet named
mWallet1
- Generating keys
- Getting bitcoin public address
To run this program, save it in a file with .py extension. (Ex- hd_wallet2.py
). Then run that file using the command below.
python hd_wallet2.py
To learn more about the bitcoinlib
library check out the official documentation.
In the next article, we will talk about the Bitcoin Multi-sig wallet and how to create one.
Note: Never post your keys or mnemonic publically.
Also, Read
- The Best Crypto Trading Bots
- Deribit Review | Options, Fees, APIs and Testnet
- FTX Crypto Exchange Review
- The Best Bitcoin Hardware wallet
- Crypto Copy Trading Platforms
- The Best Crypto Tax Software
- Best Crypto Trading Platforms
- Best Crypto Lending Platforms
- Ledger vs Trezor
- BlockFi vs Celsius vs Hodlnaut
- Bitsgap review — A Crypto Trading Bot That Makes Easy Money
- Quadency Review- A Crypto Trading Bot Made For Professionals
- PrimeXBT Review | Leverage Trading, Fee and Covesting
- HaasOnline Review and Get a 10% discount
- The Idiots Guide to Margin Trading on Bitmex
- eToro Review | Trade Stocks, Crypto, ETFs, CFDs, and commodities
- Bitmex Advanced Margin Trading Guide
- Best Crypto APIs for Developers
- Best Blockchain Analysis Tools
- Crypto arbitrage guide: How to make money as a beginner
- Top Bitcoin Node Providers
- Best Crypto Charting Tool
- What are the best books to learn about Bitcoin?