Python BIP39 Bindings

0.3.0 · active · verified Thu Apr 16

py-bip39-bindings provides fast and safe Python bindings for the `tiny-bip39` Rust crate. It enables the generation, validation, and conversion of BIP39 mnemonic phrases to mini-secrets (seeds) with multi-language support. The library is currently at version 0.3.0 and maintains an active release cadence, frequently updating to support newer Python versions and underlying PyO3 bindings.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate BIP39 mnemonic phrases with varying word counts and languages, validate them, and convert them into a 512-bit seed (mini-secret) using an optional passphrase. It also includes an example of handling invalid mnemonics.

import binascii
from bip39 import bip39_generate, bip39_validate, bip39_to_mini_secret

# 1. Generate a new 12-word mnemonic in English (default)
mnemonic_en = bip39_generate(12)
print(f"Generated English mnemonic: {mnemonic_en}")

# 2. Validate the mnemonic
is_valid_en = bip39_validate(mnemonic_en)
print(f"Is English mnemonic valid? {is_valid_en}")

# 3. Generate a 24-word mnemonic in French
mnemonic_fr = bip39_generate(24, 'fr')
print(f"Generated French mnemonic: {mnemonic_fr}")

# 4. Validate the French mnemonic
is_valid_fr = bip39_validate(mnemonic_fr, 'fr')
print(f"Is French mnemonic valid? {is_valid_fr}")

# 5. Convert a mnemonic to a mini-secret (seed)
passphrase = "mysecretpassphrase"
seed_array = bip39_to_mini_secret(mnemonic_en, passphrase)
seed_hex = binascii.hexlify(bytearray(seed_array)).decode("ascii")
print(f"Seed for English mnemonic (hex): {seed_hex}")

# Example of an invalid mnemonic
invalid_mnemonic = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zebra"
try:
    bip39_validate(invalid_mnemonic)
except ValueError as e:
    print(f"Validation failed for invalid mnemonic: {e}")

view raw JSON →