API Reference
Mnemonic
- class Mnemonic
The primary type in this library - most tasks require creating or using one.
To create a new
Mnemonic
from a randomly generated key, callMnemonic()
.To get a
Mnemonic
instance for an existing mnemonic phrase, including those generated by other software or hardware wallets, usefrom_phrase()
.You can get the HD wallet seed from a
Mnemonic
by instantiatingSeed
. From there you can either get the raw byte value withbytes(seed)
, or the hex representation withSeed.hex()
.You can also get the original entropy value back from a
Mnemonic
withentropy
, but beware that the entropy value is not the same thing as an HD wallet seed, and should never be used that way.- Parameters
mtype (MnemonicType, optional) – The number of words in the seed phrase. Defaults to
MnemonicType.Words12
.language (Language, optional) – The language of the seed phrase. Defaults to
Language.English
.
Example
>>> from pybip39 import Mnemonic, MnemonicType, Language >>> mnemonic = Mnemonic(MnemonicType.Words12, Language.English) >>> phrase = mnemonic.phrase >>> len(phrase.split(" ")) 12
- entropy
The original entropy value of the mnemonic phrase.
Example
>>> from pybip39 import Mnemonic >>> phrase = "park remain person kitchen mule spell knee armed position rail grid ankle" >>> mnemonic = Mnemonic.from_phrase(phrase) >>> mnemonic.entropy b'\xa0V\xb2\x8d=\x89\x15\xa2^\xe0^\xa8v\x1d\x99\x84'
Note
You shouldn’t use the generated entropy as secrets, for that generate a new Seed from the Mnemonic.
- Type
bytes
- static from_entropy(entropy, language)
Create a
Mnemonic
from pre-generated entropy.- Parameters
entropy (bytes) – The entropy value of the mnemonic phrase.
language (Language, optional) – The language of the seed phrase. Defaults to
Language.English
.
- Returns
The mnemonic object.
- Return type
Example
>>> from pybip39 import Mnemonic, MnemonicType, Language >>> entropy = bytes([0x33, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD, 0xE4, 0x5C, 0x90, 0x84, 0x6A, 0x79]) >>> mnemonic = Mnemonic.from_entropy(entropy, Language.English) >>> mnemonic.phrase 'crop cash unable insane eight faith inflict route frame loud box vibrant' >>> mnemonic.hex() '33e46bb13a746ea41cdde45c90846a79'
- static from_phrase(phrase, language)
Create a
Mnemonic
from an existing mnemonic phrase.The phrase supplied will be checked for word length and validated according to the checksum specified in BIP0039
- Parameters
phrase (str) – The seed phrase.
language (Language, optional) – The language of the seed phrase. Defaults to
Language.English
.
- Returns
The mnemonic object.
- Return type
Example
>>> from pybip39 import Mnemonic, Language >>> phrase = "park remain person kitchen mule spell knee armed position rail grid ankle" >>> mnemonic = Mnemonic.from_phrase(phrase, Language.English) >>> mnemonic.phrase 'park remain person kitchen mule spell knee armed position rail grid ankle'
- hex()
Get the hex-formatted mnemonic.
- Returns
The hex-formatted mnemonic.
- Return type
str
- phrase
The mnemonic phrase.
- Type
str
- static validate(phrase, language)
Validate a mnemonic phrase.
The phrase supplied will be checked for word length and validated according to the checksum specified in BIP0039.
Returns
None
if phrase is valid.- Parameters
phrase (str) – The phrase to validate.
language (Language, optional) – The language of the phrase. Defaults to
Language.English
.
- Raises
RuntimeError – If validation fails.
Example
>>> from pybip39 import Mnemonic, Language >>> test_mnemonic = "park remain person kitchen mule spell knee armed position rail grid ankle" >>> Mnemonic.validate(test_mnemonic, Language.English) >>> Mnemonic.validate(test_mnemonic, Language.Spanish) Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: invalid word in phrase
Seed
- class Seed
The secret value used to derive HD wallet addresses from a
Mnemonic
phrase.Because it is not possible to create a
Mnemonic
instance that is invalid, it is therefore impossible to have aSeed
instance that is invalid. This guarantees that only a valid, intact mnemonic phrase can be used to derive HD wallet addresses.To get the raw byte value use
bytes(seed)
. These can be used to derive HD wallet addresses using another crate (deriving HD wallet addresses is outside the scope of this crate and the BIP39 standard).- Parameters
mnemonic (Mnemonic) – The mnemonic to generate the seed from.
password (str) – The seed password.
- hex()
Get the hex-formatted seed.
- Returns
The hex-formatted seed.
- Return type
str
MnemonicType
Language
- class Language
The supported languages for seed phrases.
- ChineseSimplified = Language.ChineseSimplified
- ChineseTraditional = Language.ChineseTraditional
- English = Language.English
- French = Language.French
- Italian = Language.Italian
- Japanese = Language.Japanese
- Korean = Language.Korean
- Spanish = Language.Spanish