BIP-39
Mnemonic phrases in all official wordlists and PBKDF2 seed derivation.
from oxcrypto.hd.bip39 import BIP39
mnemonic = BIP39.ENGLISH.load_entropy(bytes(16))
assert mnemonic.to_phrase() == "abandon " * 11 + "about"
assert BIP39.ENGLISH.load_phrase(mnemonic.to_phrase()).to_entropy() == bytes(16)
seed = mnemonic.to_seed(passphrase="TREZOR")
assert seed.hex().startswith("c55257c360c07c72")A mnemonic encodes 16, 20, 24, 28 or 32 bytes of entropy as 12, 15, 18, 21 or
24 words, with a checksum of the first bits of its SHA-256 hash. generate
draws the entropy from the operating system CSPRNG. load_phrase applies NFKD
normalization, splits on whitespace, and raises ValueError for a wrong word
count, a word outside the selected wordlist, or a failed checksum.
to_seed derives the 64-byte seed with PBKDF2-HMAC-SHA512, 2048 iterations,
and the salt "mnemonic" + passphrase, both NFKD-normalized. Any passphrase
gives a valid seed, so a mistyped passphrase is not detected. to_phrase
joins Japanese words with U+3000 (ideographic space) and other languages with
a space, as BIP-39 recommends.
Variants
BIP39.ENGLISHBIP39.CHINESE_SIMPLIFIEDBIP39.CHINESE_TRADITIONALBIP39.CZECHBIP39.FRENCHBIP39.ITALIANBIP39.JAPANESEBIP39.KOREANBIP39.PORTUGUESEBIP39.SPANISH
Methods
BIP39.generate(*, words: int = 24) -> Mnemonic
BIP39.load_entropy(entropy: bytes, /) -> Mnemonic
BIP39.load_phrase(phrase: str, /) -> Mnemonic
Mnemonic.language -> BIP39
Mnemonic.words -> int
Mnemonic.to_entropy() -> bytes
Mnemonic.to_phrase() -> str
Mnemonic.to_seed(*, passphrase: str = "") -> bytesUpstream
- Standard: BIP-39 (Deployed).
- Crate:
coins-bip390.17.2 with the ten official BIP-39 wordlists. - Tests use the
trezor/python-mnemonicreferencevectors.jsonfor every official wordlist and thebip32JPJapanese vectors with non-ASCII passphrases.
See Hierarchical deterministic keys for zeroization notes.