This repository contains Python scripts to generate Bitcoin wallet addresses from a 12-word seed phrase (mnemonic). The implementation is based on the Trust Wallet Core's approach for creating HD wallets.
- Overview
- Features
- Requirements
- Installation
- Basic Usage
- Address Types
- Derivation Paths
- Full Example
- Non-BIP39 Seeds
- Troubleshooting
The scripts in this repository implement Bitcoin wallet address generation from mnemonic seed phrases using BIP39, BIP32, BIP44, and related standards. The implementation is based on Trust Wallet Core's approach, which follows the industry-standard Bitcoin HD wallet architecture.
There are two main scripts:
- wallet.py - Basic implementation of HD wallet with Bitcoin address generation
- all_address_types.py - Comprehensive script that generates all major Bitcoin address types
- Generate Bitcoin addresses from BIP39 mnemonic phrases
- Support for non-standard (non-BIP39) seed phrases
- Create addresses for all major Bitcoin address formats:
- P2PKH (Legacy) addresses (1...)
- P2SH (Script Hash) addresses (3...)
- P2WPKH (Native SegWit) addresses (bc1q...)
- P2WSH (SegWit Script Hash) addresses
- P2TR (Taproot) addresses
- Generate addresses for multiple derivation paths
- Output private keys in WIF format
- Extensive error handling and dependency management
- Python 3.6 or higher
- Required Python packages (automatically installed by the scripts):
mnemonic- BIP39 mnemonic generation and handlingbip32utils- BIP32 hierarchical deterministic wallet implementationbase58- Base58 encoding and decodingbech32- Bech32 encoding and decoding for SegWit addressesecdsa- Elliptic curve cryptography
- Clone this repository or download the script files
- Ensure you have Python 3.6+ installed
- The scripts will automatically install the required dependencies when run
To generate addresses from your 12-word seed phrase:
- Open
all_address_types.py - Edit the
my_mnemonicvariable with your 12-word seed phrase - Run the script:
python all_address_types.pyFor non-BIP39 standard seed phrases, ensure the skip_validation parameter is set to True.
You can modify the following parameters:
passphrase- Optional passphrase for additional securityskip_validation- Set toTruefor non-BIP39 compatible seed phrasesnum_addresses- Number of addresses to generate per derivation path
The all_address_types.py script generates the following address types:
-
P2PKH (Compressed) - Standard legacy addresses starting with "1"
- Example:
1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
- Example:
-
P2PKH (Uncompressed) - Legacy addresses with uncompressed public keys
- Example:
1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm
- Example:
-
P2WPKH - Native SegWit addresses starting with "bc1q"
- Example:
bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4
- Example:
-
P2SH-P2WPKH - SegWit nested in P2SH (addresses starting with "3")
- Example:
3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy
- Example:
-
P2SH-P2PKH - P2PKH nested in P2SH
- Example:
3EktnHQD7RiAE6uzMj2ZifT9YgRrkSgzQX
- Example:
-
P2WSH-P2WPKH - P2WPKH nested in P2WSH
- Example:
bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3
- Example:
-
P2WSH-P2PKH - P2PKH nested in P2WSH
- Example:
bc1qft5p2uhsdcdc3l2ua4ap5qqfg4pjaqlp250x7us7a8qqhrxrxfsq7wqu47
- Example:
The scripts generate addresses for the following derivation paths:
- BIP44 (Legacy):
m/44'/0'/0'/0/*andm/44'/0'/0'/1/* - BIP49 (SegWit):
m/49'/0'/0'/0/*andm/49'/0'/0'/1/* - BIP84 (Native SegWit):
m/84'/0'/0'/0/*andm/84'/0'/0'/1/* - BIP86 (Taproot):
m/86'/0'/0'/0/*andm/86'/0'/0'/1/*
Where:
- The first
/0/or/1/indicates external (receiving) or internal (change) addresses - The final
/*is the address index (0, 1, 2, etc.)
Here's a complete example of how to use the code to generate Bitcoin addresses from a seed phrase:
# 1. Import the required module from all_address_types.py
from all_address_types import print_addresses
# 2. Your 12-word seed phrase
my_seed_phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
# 3. Generate and display all address types
print_addresses(
mnemonic=my_seed_phrase,
passphrase="", # Optional passphrase
skip_validation=False, # Set to True for non-BIP39 seed phrases
num_addresses=5 # Generate 5 addresses per path type
)For non-standard seed phrases that don't conform to the BIP39 specification:
- Set
skip_validation=Truewhen calling the functions - The code will use a SHA256 hash of the seed phrase to generate deterministic entropy
- This ensures compatibility with any set of words used as a seed phrase
Example with a non-BIP39 seed phrase:
non_standard_seed = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
print_addresses(mnemonic=non_standard_seed, skip_validation=True)If you encounter dependency issues:
pip install mnemonic bip32utils base58 bech32 ecdsaThe scripts include comprehensive error handling and will display detailed error messages if problems occur. If you encounter any issues:
- Ensure your seed phrase is entered correctly
- Check that the
skip_validationflag is set correctly for your seed phrase type - Review the error message and traceback for specific information
- Taproot (P2TR) address implementation in the basic
wallet.pyscript is limited - Some complex nested address types may require additional libraries for full validation
This README and the associated scripts were created based on an analysis of Trust Wallet Core's HDWallet implementation, adapted for Python to enable Bitcoin address recovery from seed phrases.