Skip to content

jmwallet.mnemonic

jmwallet.mnemonic

BIP39 mnemonic generation from operating-system entropy.

Attributes

SUPPORTED_ENTROPY_BITS = frozenset({128, 160, 192, 224, 256}) module-attribute

Classes

EntropySourceError

Bases: RuntimeError

Raised when the operating-system entropy source violates its contract.

Source code in jmwallet/src/jmwallet/mnemonic.py
12
13
class EntropySourceError(RuntimeError):
    """Raised when the operating-system entropy source violates its contract."""

Functions:

generate_wallet_entropy(strength: int = 256) -> bytes

Return a supported BIP39 entropy length from the operating-system CSPRNG.

Source code in jmwallet/src/jmwallet/mnemonic.py
16
17
18
19
20
21
22
23
24
25
26
27
28
def generate_wallet_entropy(strength: int = 256) -> bytes:
    """Return a supported BIP39 entropy length from the operating-system CSPRNG."""
    if strength not in SUPPORTED_ENTROPY_BITS:
        allowed = ", ".join(str(bits) for bits in sorted(SUPPORTED_ENTROPY_BITS))
        raise ValueError(f"strength must be one of: {allowed}")

    expected_length = strength // 8
    entropy = secrets.token_bytes(expected_length)
    if len(entropy) != expected_length:
        raise EntropySourceError(
            f"entropy source returned {len(entropy)} bytes, expected {expected_length}"
        )
    return entropy

generate_wallet_mnemonic(strength: int = 256) -> str

Generate an English BIP39 mnemonic from operating-system entropy.

Source code in jmwallet/src/jmwallet/mnemonic.py
40
41
42
def generate_wallet_mnemonic(strength: int = 256) -> str:
    """Generate an English BIP39 mnemonic from operating-system entropy."""
    return mnemonic_from_entropy(generate_wallet_entropy(strength))

mnemonic_from_entropy(entropy: bytes) -> str

Encode supported raw entropy as an English BIP39 mnemonic.

Source code in jmwallet/src/jmwallet/mnemonic.py
31
32
33
34
35
36
37
def mnemonic_from_entropy(entropy: bytes) -> str:
    """Encode supported raw entropy as an English BIP39 mnemonic."""
    strength = len(entropy) * 8
    if strength not in SUPPORTED_ENTROPY_BITS:
        allowed = ", ".join(str(bits // 8) for bits in sorted(SUPPORTED_ENTROPY_BITS))
        raise ValueError(f"entropy length must be one of these byte counts: {allowed}")
    return Mnemonic("english").to_mnemonic(entropy)