HD Wallet Generator
hdwallet is a Python library that implements a Hierarchical Deterministic (HD) Wallet generator, supporting over 200 cryptocurrencies. It is currently at version 3.6.1 and sees active development with minor releases every few weeks to months, typically adding new cryptocurrencies, enhancements, and bug fixes.
Common errors
-
AttributeError: 'HDWallet' object has no attribute 'ecc'
cause Attempting to access the `ecc` attribute directly after the v3.5.1 refactor.fixThe attribute was renamed to `eccs`. Update your code to use `hdwallet_instance.eccs` if you need to access it, though direct access to internal attributes is generally discouraged. -
ImportError: cannot import name 'generate_mnemonic' from 'hdwallet'
cause Trying to import utility functions directly from the top-level `hdwallet` package.fixUtility functions like `generate_mnemonic` are located in `hdwallet.utils`. Correct the import to `from hdwallet.utils import generate_mnemonic`. -
ValueError: Invalid WIF private key: checksum mismatch
cause Attempting to import or use a WIF (Wallet Import Format) private key that was either generated incorrectly by an older version of `hdwallet` or is simply malformed according to the updated WIF standards in v3.1.0/v3.2.0.fixEnsure the WIF string is correctly formatted according to current standards. If it was generated by an older `hdwallet` version, verify if the new library can still process it, or re-generate/re-export from a reliable source.
Warnings
- breaking The internal `ecc` attribute was renamed to `eccs` and the `const` module was refactored in v3.5.1. Direct access to these internal components might break existing code.
- breaking The Wallet Import Format (WIF) implementations were significantly upgraded across v3.1.0 and v3.2.0. This can affect how WIF keys are generated, validated, and converted, potentially leading to incorrect keys if old logic is relied upon.
- breaking The default address assignment logic was modified in v3.2.0 to align with standard HD wallet configurations. This means addresses generated by identical input (mnemonic, path) might differ from those generated by older `hdwallet` versions.
- gotcha The `raw` property of Point objects (used in ECC operations) was changed to `raw_encode` in v3.5.1. If you were accessing raw point data, this will change the method name.
Install
-
pip install hdwallet
Imports
- HDWallet
from hdwallet import HDWallet
- generate_mnemonic
from hdwallet import generate_mnemonic
from hdwallet.utils import generate_mnemonic
- BIP44Derivation
from hdwallet import BIP44Derivation
from hdwallet.derivations import BIP44Derivation
Quickstart
from hdwallet import HDWallet
from hdwallet.symbols import ETH as SYMBOL
from hdwallet.derivations import BIP44Derivation
from hdwallet.utils import generate_mnemonic, is_mnemonic_valid
# 1. Generate a new mnemonic phrase (12 words)
mnemonic = generate_mnemonic(language="english", strength=128)
# 2. Optionally, validate the mnemonic
if not is_mnemonic_valid(mnemonic, language="english"):
raise ValueError("Invalid mnemonic phrase")
# 3. Create an HDWallet instance
hdwallet = HDWallet(symbol=SYMBOL)
# 4. From mnemonic, get the root key
hdwallet.from_mnemonic(mnemonic=mnemonic, language="english", passphrase=None)
# 5. Derive a child key using a BIP44 path for Ethereum
# Example: m/44'/60'/0'/0/0
hdwallet.from_path(path=BIP44Derivation.m / BIP44Derivation.coin_types.ETH / BIP44Derivation.PURPOSE.root / BIP44Derivation.ACCOUNT.root / BIP44Derivation.CHANGE.root / BIP44Derivation.ADDRESS.root)
# 6. Get wallet details
print(f"Mnemonic: {hdwallet.mnemonic()}")
print(f"Base HD path: {hdwallet.base_hd_path()}")
print(f"Address: {hdwallet.address()}")
print(f"Private Key: {hdwallet.private_key()}")
print(f"Public Key: {hdwallet.public_key()}")