Mnemonic

0.21 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `Mnemonic` class, generate a cryptographically secure mnemonic phrase, validate it, and convert it into a deterministic seed, which is crucial for hierarchical deterministic (HD) wallets.

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}")

view raw JSON →