Mnemonic
The `mnemonic` library is a Python implementation of Bitcoin Improvement Proposal (BIP-0039). It provides functionality to generate mnemonic phrases (seed phrases), convert them into binary seeds for deterministic wallets, and validate existing mnemonic phrases. The current version is 0.21, released in January 2024, and it requires Python 3.8.1 or newer. The library primarily follows a stable release cadence as needed for updates to BIP-0039 or Python compatibility.
Warnings
- breaking Support for Python 2.7 and 3.4 was dropped in version 0.20. Users on older Python versions must either upgrade their Python environment or use an older version of the `mnemonic` library (e.g., 0.19).
- breaking As of version 0.21, 'english' is now the default language if no language is explicitly provided during `Mnemonic` initialization. Prior versions might have behaved differently or raised an error, potentially breaking applications that relied on implicit default language behavior.
- gotcha When using `mnemo.to_seed(words, passphrase='')`, an empty passphrase is often used in examples. For real-world cryptographic applications, it is crucial to use a strong and unique passphrase to enhance the security of the generated seed. An empty passphrase significantly reduces the security against brute-force attacks on the seed.
- gotcha The `generate()` method requires a `strength` parameter (in bits). It is advised to use 128, 256, 512, or 1024 bits for cryptographically secure mnemonics, as these correspond to standard word counts (12, 24 words for 128, 256 bits respectively). Using insufficient strength will result in a less secure mnemonic.
- gotcha Version 0.21 introduced the option to provide custom wordlists. Prior to this, the library relied solely on built-in wordlist files. Directly accessing or manipulating internal wordlist files from older versions could be fragile and break if the library's internal structure changes.
Install
-
pip install mnemonic
Imports
- Mnemonic
from mnemonic import Mnemonic
Quickstart
from mnemonic import Mnemonic
import os
# Initialize Mnemonic with a language
# 'english' is a common choice and default since v0.21
mnemo = Mnemonic("english")
# Generate a 24-word mnemonic phrase (256 bits of strength)
# Strength can be 128, 160, 192, 224, or 256 bits, dictating phrase length.
words = mnemo.generate(strength=256)
print(f"Generated mnemonic: {words}")
# Validate a mnemonic phrase
is_valid = Mnemonic.check(words)
print(f"Is mnemonic valid? {is_valid}")
# Convert the mnemonic to a seed (with an optional passphrase)
# For real applications, use a strong, unique passphrase.
passphrase = os.environ.get('MNEMONIC_PASSPHRASE', '')
seed = mnemo.to_seed(words, passphrase=passphrase)
print(f"Generated seed (hex): {seed.hex()}")
# Alternatively, generate a mnemonic from cryptographic entropy
entropy_bytes = os.urandom(32) # 32 bytes = 256 bits of entropy
words_from_entropy = mnemo.to_mnemonic(entropy_bytes)
print(f"Generated from raw entropy: {words_from_entropy}")