Mnemonics¶
- class hdwallet.mnemonics.MNEMONICS¶
A class that manages a dictionary of mnemonic classes.
This class provides methods to retrieve names and classes of various mnemonic implementations, as well as methods to validate and access specific mnemonic classes by name.
Here are available mnemonic names and classes:
Name
Class
Algorand
BIP39
Electrum-V1
Electrum-V2
Monero
- classmethod names() List[str]¶
Get the list of mnemonic class names.
- Returns:
A list of mnemonic class names.
- Return type:
List[str]
- classmethod classes() List[Type[IMnemonic]]¶
Get the list of mnemonic classes.
- Returns:
A list of mnemonic classes.
- Return type:
List[Type[IMnemonic]]
- classmethod mnemonic(name: str) Type[IMnemonic]¶
Retrieve an mnemonic class by its name.
- Parameters:
name (str) – The name of the mnemonic class.
- Returns:
The mnemonic class corresponding to the given name.
- Return type:
Type[IMnemonic]
- classmethod is_mnemonic(name) bool¶
Check if a given name is a valid mnemonic name.
- Parameters:
name (str) – The name to check.
- Returns:
True if the name is valid, False otherwise.
- Return type:
bool
>>> from hdwallet.mnemonics import MNEMONICS
>>> MNEMONICS.names()
['Algorand', 'BIP39', 'Electrum-V1', 'Electrum-V2', 'Monero']
>>> MNEMONICS.classes()
[<class 'hdwallet.mnemonics.algorand.mnemonic.AlgorandMnemonic'>, <class 'hdwallet.mnemonics.bip39.mnemonic.BIP39Mnemonic'>, <class 'hdwallet.mnemonics.electrum.v1.mnemonic.ElectrumV1Mnemonic'>, <class 'hdwallet.mnemonics.electrum.v2.mnemonic.ElectrumV2Mnemonic'>, <class 'hdwallet.mnemonics.monero.mnemonic.MoneroMnemonic'>]
>>> from hdwallet.mnemonics import BIP39Mnemonic
>>> MNEMONICS.mnemonic(name="BIP39")
<class 'hdwallet.mnemonics.bip39.mnemonic.BIP39Mnemonic'>
>>> MNEMONICS.mnemonic(name="BIP39") == BIP39Mnemonic
True
>>> MNEMONICS.is_mnemonic(name="BIP39")
True
- class hdwallet.mnemonics.imnemonic.IMnemonic(mnemonic: str | List[str], **kwargs)¶
- mnemonic() str¶
Get the mnemonic as a single string.
- Returns:
The mnemonic as a single string joined by spaces.
- Return type:
str
- mnemonic_type() str¶
Retrieves the type of the mnemonic.
- Returns:
The type of the mnemonic.
- Return type:
str
- language() str¶
Get the formatted language value.
- Returns:
The formatted language string where each part is capitalized.
- Return type:
str
- words() int¶
- Returns:
The words.
- Return type:
int
- classmethod get_words_list_by_language(language: str, wordlist_path: Dict[str, str] | None = None) List[str]¶
Retrieves the word list for the specified language.
- Parameters:
language (str) – The language for which to get the word list.
wordlist_path (Optional[Dict[str, str]]) – Optional dictionary mapping language names to file paths of their word lists.
- Returns:
A list of words for the specified language.
- Return type:
List[str]
- classmethod find_language(mnemonic: List[str], wordlist_path: Dict[str, str] | None = None) str | Tuple[List[str], str]¶
Finds the language of the given mnemonic by checking against available word lists.
- Parameters:
mnemonic (List[str]) – The mnemonic to check, represented as a list of words.
wordlist_path (Optional[Dict[str, str]]) – Optional dictionary mapping language names to file paths of their word lists.
- Returns:
A tuple containing the word list and the language name if found.
- Return type:
Union[str, Tuple[List[str], str]]
- classmethod is_valid(mnemonic: str | List[str], **kwargs) bool¶
Checks if the given mnemonic is valid.
- Parameters:
mnemonic (str) – The mnemonic to check.
kwargs – Additional keyword arguments.
- Returns:
True if the strength is valid, False otherwise.
- Return type:
bool
- classmethod is_valid_language(language: str) bool¶
Checks if the given language is valid.
- Parameters:
language (str) – The language to check.
- Returns:
True if the strength is valid, False otherwise.
- Return type:
bool
- classmethod is_valid_words(words: int) bool¶
Checks if the given words is valid.
- Parameters:
words (int) – The words to check.
- Returns:
True if the strength is valid, False otherwise.
- Return type:
bool
- classmethod normalize(mnemonic: str | List[str]) List[str]¶
Normalizes the given mnemonic by splitting it into a list of words if it is a string.
- Parameters:
mnemonic (Union[str, List[str]]) – The mnemonic value, which can be a single string of words or a list of words.
- Returns:
A list of words from the mnemonic.
- Return type:
List[str]
- class hdwallet.mnemonics.algorand.mnemonic.AlgorandMnemonic(mnemonic: str | List[str], **kwargs)¶
Used to generate and manage mnemonics specifically for the Algorand blockchain, ensuring secure key derivation and backup.
Here are available
ALGORAND_MNEMONIC_WORDS:Name
Value
TWENTY_FIVE
25
Here are available
ALGORAND_MNEMONIC_LANGUAGES:Name
Value
ENGLISH
english
- classmethod name() str¶
Get the name of the mnemonic class.
- Returns:
The name of the mnemonic class.
- Return type:
str
- classmethod from_words(words: int, language: str) str¶
Generates a mnemonic phrase from a specified number of words and language.
- Parameters:
words (int) – The number of words in the mnemonic phrase.
language (str) – The language for which to generate the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod from_entropy(entropy: str | bytes | IEntropy, language: str, **kwargs) str¶
Generates a mnemonic phrase from entropy data.
- Parameters:
entropy (Union[str, bytes, IEntropy]) – The entropy data used to generate the mnemonic phrase.
language (str) – The language for which to generate the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod encode(entropy: str | bytes, language: str) str¶
Encodes entropy data into a mnemonic phrase using the specified language.
This method converts the provided entropy data into a mnemonic phrase based on the specified language. It ensures the entropy has a valid strength and includes a checksum in the mnemonic phrase.
- Parameters:
entropy (Union[str, bytes]) – The entropy data to be encoded. Can be a string or bytes.
language (str) – The language for the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod decode(mnemonic: str, **kwargs) str¶
Decodes a mnemonic phrase into entropy data.
- Parameters:
mnemonic (str) – The mnemonic phrase to decode.
kwargs – Additional keyword arguments (language, checksum).
- Returns:
The decoded entropy data.
- Return type:
str
- classmethod normalize(mnemonic: str | List[str]) List[str]¶
Normalizes the given mnemonic by splitting it into a list of words if it is a string.
- Parameters:
mnemonic (Union[str, List[str]]) – The mnemonic value, which can be a single string of words or a list of words.
- Returns:
A list of words from the mnemonic.
- Return type:
List[str]
>>> from hdwallet.mnemonics.algorand import AlgorandMnemonic, ALGORAND_MNEMONIC_WORDS, ALGORAND_MNEMONIC_LANGUAGES
>>> AlgorandMnemonic.name()
'Algorand'
>>> mnemonic: str = AlgorandMnemonic.from_words(words=ALGORAND_MNEMONIC_WORDS.TWENTY_FIVE, language=ALGORAND_MNEMONIC_LANGUAGES.ENGLISH)
>>> AlgorandMnemonic.from_entropy(entropy="65234f4ec655b087dd74d186126e301d73d563961890b2f718476e1a32522329", language=ALGORAND_MNEMONIC_LANGUAGES.ENGLISH)
'hole develop cheese fragile gaze giggle plunge sphere express reunion oblige crack priority ocean seven mosquito wagon glow castle plunge goddess stand empower ability empower'
>>> AlgorandMnemonic.encode(entropy="65234f4ec655b087dd74d186126e301d73d563961890b2f718476e1a32522329", language=ALGORAND_MNEMONIC_LANGUAGES.ENGLISH)
'hole develop cheese fragile gaze giggle plunge sphere express reunion oblige crack priority ocean seven mosquito wagon glow castle plunge goddess stand empower ability empower'
>>> mnemonic
'horse catalog image easy adult include daughter model peace correct identify buyer gravity captain grass scale jazz settle impact injury govern trumpet door above suspect'
>>> algorand_mnemonic: AlgorandMnemonic = AlgorandMnemonic(mnemonic=mnemonic)
>>> algorand_mnemonic.mnemonic()
'horse catalog image easy adult include daughter model peace correct identify buyer gravity captain grass scale jazz settle impact injury govern trumpet door above suspect'
>>> algorand_mnemonic.language()
'English'
>>> algorand_mnemonic.words()
25
>>> AlgorandMnemonic.decode(mnemonic=mnemonic)
'6efb88e25ce481c9f9868e0e250ce1f601b388bc0cc0bb1bb1e3428732a62f88'
>>> AlgorandMnemonic.is_valid_language(language="english")
True
>>> AlgorandMnemonic.is_valid_words(words=25)
True
>>> AlgorandMnemonic.is_valid(mnemonic="hole develop cheese fragile gaze giggle plunge sphere express reunion oblige crack priority ocean seven mosquito wagon glow castle plunge goddess stand empower ability empower")
True
- class hdwallet.mnemonics.bip39.mnemonic.BIP39Mnemonic(mnemonic: str | List[str], **kwargs)¶
Implements the BIP39 standard, allowing the creation of mnemonic phrases for generating deterministic keys, widely used across various cryptocurrencies.
Here are available
BIP39_MNEMONIC_WORDS:Name
Value
TWELVE
12
FIFTEEN
15
EIGHTEEN
18
TWENTY_ONE
21
TWENTY_FOUR
24
Here are available
BIP39_MNEMONIC_LANGUAGES:Name
Value
CHINESE_SIMPLIFIED
chinese-simplified
CHINESE_TRADITIONAL
chinese-traditional
CZECH
czech
ENGLISH
english
FRENCH
french
ITALIAN
italian
JAPANESE
japanese
KOREAN
korean
PORTUGUESE
portuguese
RUSSIAN
russian
SPANISH
spanish
TURKISH
turkish
- classmethod name() str¶
Get the name of the mnemonic class.
- Returns:
The name of the entropy class.
- Return type:
str
- classmethod from_words(words: int, language: str) str¶
Generates a mnemonic phrase from a specified number of words.
This method generates a mnemonic phrase based on the specified number of words and language.
- Parameters:
words (int) – The number of words for the mnemonic phrase.
language (str) – The language for the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod from_entropy(entropy: str | bytes | IEntropy, language: str, **kwargs) str¶
Generates from entropy data.
- Parameters:
entropy (Union[str, bytes, IEntropy]) – The entropy data used to generate the mnemonic phrase.
language (str) – The language for the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod encode(entropy: str | bytes, language: str) str¶
Encodes entropy into a mnemonic phrase.
This method converts a given entropy value into a mnemonic phrase according to the specified language.
- Parameters:
entropy (Union[str, bytes]) – The entropy to encode into a mnemonic phrase.
language (str) – The language for the mnemonic phrase.
- Returns:
The encoded mnemonic phrase.
- Return type:
str
- classmethod decode(mnemonic: str, checksum: bool = False, words_list: List[str] | None = None, words_list_with_index: dict | None = None) str¶
Decodes a mnemonic phrase into its corresponding entropy.
This method converts a given mnemonic phrase back into its original entropy value. It also verifies the checksum to ensure the mnemonic is valid.
- Parameters:
mnemonic (str) – The mnemonic phrase to decode.
checksum (bool) – Whether to include the checksum in the returned entropy.
words_list (Optional[List[str]]) – Optional list of words used to decode the mnemonic. If not provided, the method will use the default word list for the language detected.
words_list_with_index (Optional[dict]) – Optional dictionary mapping words to their indices for decoding. If not provided, the method will use the default mapping.
- Returns:
The decoded entropy as a string.
- Return type:
str
- classmethod is_valid(mnemonic: str | List[str], words_list: List[str] | None = None, words_list_with_index: dict | None = None) bool¶
Validates a mnemonic phrase.
This method checks whether the provided mnemonic phrase is valid by attempting to decode it. If the decoding is successful without raising any errors, the mnemonic is considered valid.
- Parameters:
mnemonic (Union[str, List[str]]) – The mnemonic phrase to validate. It can be a string or a list of words.
words_list (Optional[List[str]]) – Optional list of words to be used for validation. If not provided, the method will use the default word list.
words_list_with_index (Optional[dict]) – Optional dictionary mapping words to their indices for validation. If not provided, the method will use the default mapping.
- Returns:
True if the mnemonic phrase is valid, False otherwise.
- Return type:
bool
- classmethod normalize(mnemonic: str | List[str]) List[str]¶
Normalizes the given mnemonic by splitting it into a list of words if it is a string.
- Parameters:
mnemonic (Union[str, List[str]]) – The mnemonic value, which can be a single string of words or a list of words.
- Returns:
A list of words from the mnemonic.
- Return type:
List[str]
>>> from hdwallet.mnemonics.bip39 import BIP39Mnemonic, BIP39_MNEMONIC_WORDS, BIP39_MNEMONIC_LANGUAGES
>>> BIP39Mnemonic.name()
'BIP39'
>>> mnemonic: str = BIP39Mnemonic.from_words(words=BIP39_MNEMONIC_WORDS.TWELVE, language=BIP39_MNEMONIC_LANGUAGES.CZECH)
>>> BIP39Mnemonic.from_entropy(entropy="1ab96e3aebfce1014e3bdddeeb7510c1", language=BIP39_MNEMONIC_LANGUAGES.CZECH)
'datel subtropy otrhat tundra synek obvod hranice nozdra uvalit lebka kajuta odeslat'
>>> BIP39Mnemonic.encode(entropy="1ab96e3aebfce1014e3bdddeeb7510c1", language=BIP39_MNEMONIC_LANGUAGES.CZECH)
'datel subtropy otrhat tundra synek obvod hranice nozdra uvalit lebka kajuta odeslat'
>>> mnemonic
'bobek soulad tabule masopust modlitba hmyz bezmoc podvod psanec hladovka nadace gramofon'
>>> bip39_mnemonic: BIP39Mnemonic = BIP39Mnemonic(mnemonic=mnemonic)
>>> bip39_mnemonic.mnemonic()
'bobek soulad tabule masopust modlitba hmyz bezmoc podvod psanec hladovka nadace gramofon'
>>> bip39_mnemonic.language()
'Czech'
>>> bip39_mnemonic.words()
12
>>> BIP39Mnemonic.decode(mnemonic=mnemonic)
'0a58af3c3326c06881ece7aa4655c497'
>>> BIP39Mnemonic.is_valid_language(language="czech")
True
>>> BIP39Mnemonic.is_valid_words(words=12)
True
>>> BIP39Mnemonic.is_valid(mnemonic="datel subtropy otrhat tundra synek obvod hranice nozdra uvalit lebka kajuta odeslat")
True
- class hdwallet.mnemonics.electrum.v1.mnemonic.ElectrumV1Mnemonic(mnemonic: str | List[str], **kwargs)¶
Manages the first version of Electrum mnemonics, used for seed phrases in the Electrum Bitcoin wallet, providing a simple and efficient way to backup keys.
Here are available
ELECTRUM_V1_MNEMONIC_WORDS:Name
Value
TWELVE
12
Here are available
ELECTRUM_V1_MNEMONIC_LANGUAGES:Name
Value
ENGLISH
english
- classmethod name() str¶
Get the name of the mnemonic class.
- Returns:
The name of the mnemonic class.
- Return type:
str
- classmethod from_words(words: int, language: str) str¶
Generates a mnemonic phrase from a specified number of words and language.
This method generates a mnemonic phrase by first validating the number of words against the allowed word list sizes. It then generates entropy based on the specified number of words and uses it to create a mnemonic phrase in the specified language.
- Parameters:
words (int) – The number of words in the mnemonic phrase.
language (str) – The language for which to generate the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod from_entropy(entropy: str | bytes | IEntropy, language: str, **kwargs) str¶
Generates a mnemonic phrase from entropy data.
This method generates a mnemonic phrase from the given entropy data using the specified language’s word list.
- Parameters:
entropy (Union[str, bytes, IEntropy]) – The entropy data used to generate the mnemonic phrase, either as a string or bytes.
language (str) – The language for which to generate the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod encode(entropy: str | bytes, language: str) str¶
Generates a mnemonic phrase from entropy data.
This method generates a mnemonic phrase from the given entropy data using the specified language’s word list.
- Parameters:
entropy (Union[str, bytes]) – The entropy data used to generate the mnemonic phrase, either as a string or bytes.
language (str) – The language for which to generate the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod decode(mnemonic: str, words_list: List[str] | None = None, words_list_with_index: dict | None = None) str¶
Decodes a mnemonic phrase back into entropy data.
This method decodes the given mnemonic phrase into its original entropy data, using the provided word list and index mapping or fetching them based on the mnemonic’s language if not provided.
- Parameters:
mnemonic (str) – The mnemonic phrase to decode, either as a space-separated string or a list of words.
words_list (Optional[List[str]], optional) – Optional list of valid words for the mnemonic phrase, normalized and in the correct order. If not provided, uses cls.get_words_list_by_language to fetch the list based on the default language.
words_list_with_index (Optional[dict], optional) – Optional dictionary mapping words to their indices for quick lookup. If not provided, constructs this mapping based on words_list.
- Returns:
The decoded entropy data as a byte string.
- Return type:
str
- classmethod is_valid(mnemonic: str | List[str], words_list: List[str] | None = None, words_list_with_index: dict | None = None) bool¶
Checks if the given mnemonic phrase is valid.
This method decodes the mnemonic phrase and verifies its validity using the specified word lists and index mappings.
- Parameters:
mnemonic (Union[str, List[str]]) – The mnemonic phrase to check, either as a space-separated string or a list of words.
words_list (Optional[List[str]], optional) – Optional list of valid words for the mnemonic phrase, normalized and in the correct order. If not provided, uses cls.get_words_list_by_language to fetch the list based on the default language.
words_list_with_index (Optional[dict], optional) – Optional dictionary mapping words to their indices for quick lookup. If not provided, constructs this mapping based on words_list.
- Returns:
True if the mnemonic phrase is valid, False otherwise.
- Return type:
bool
- classmethod normalize(mnemonic: str | List[str]) List[str]¶
Normalizes the given mnemonic by splitting it into a list of words if it is a string.
- Parameters:
mnemonic (Union[str, List[str]]) – The mnemonic value, which can be a single string of words or a list of words.
- Returns:
A list of words from the mnemonic.
- Return type:
List[str]
>>> from hdwallet.mnemonics.electrum.v1 import ElectrumV1Mnemonic, ELECTRUM_V1_MNEMONIC_WORDS, ELECTRUM_V1_MNEMONIC_LANGUAGES
>>> ElectrumV1Mnemonic.name()
'Electrum-V1'
>>> mnemonic: str = ElectrumV1Mnemonic.from_words(words=BIP39_MNEMONIC_WORDS.TWELVE, language=ELECTRUM_V1_MNEMONIC_LANGUAGES.ENGLISH)
>>> ElectrumV1Mnemonic.from_entropy(entropy="724bf9ce32db1baa801761c4f11fe901", language=ELECTRUM_V1_MNEMONIC_LANGUAGES.ENGLISH)
'spring hopefully foot effort complete awake stand sheep deserve any soft perfect'
>>> ElectrumV1Mnemonic.encode(entropy="724bf9ce32db1baa801761c4f11fe901", language=ELECTRUM_V1_MNEMONIC_LANGUAGES.ENGLISH)
'spring hopefully foot effort complete awake stand sheep deserve any soft perfect'
>>> mnemonic
'vast floor creation tonight company express around surface mean ode mother red'
>>> ev1_mnemonic: ElectrumV1Mnemonic = ElectrumV1Mnemonic(mnemonic=mnemonic)
>>> ev1_mnemonic.mnemonic()
'vast floor creation tonight company express around surface mean ode mother red'
>>> ev1_mnemonic.language()
'English'
>>> ev1_mnemonic.words()
12
>>> ElectrumV1Mnemonic.decode(mnemonic=mnemonic)
'9ffb4bb2ca4ae6aea3a368a3fd93ebbf'
>>> ElectrumV1Mnemonic.is_valid_language(language="english")
True
>>> ElectrumV1Mnemonic.is_valid_words(words=12)
True
>>> ElectrumV1Mnemonic.is_valid(mnemonic="spring hopefully foot effort complete awake stand sheep deserve any soft perfect")
True
- class hdwallet.mnemonics.electrum.v2.mnemonic.ElectrumV2Mnemonic(mnemonic: str | List[str], **kwargs)¶
An update to the ElectrumV1Mnemonic, this class supports the second version of Electrum’s mnemonic system, offering enhanced features for secure seed generation.
Here are available
ELECTRUM_V2_MNEMONIC_LANGUAGES:Name
Value
TWELVE
12
TWENTY_FOUR
24
Here are available
ELECTRUM_V2_MNEMONIC_LANGUAGES:Name
Value
CHINESE_SIMPLIFIED
chinese-simplified
ENGLISH
english
PORTUGUESE
portuguese
SPANISH
spanish
Here are available
ELECTRUM_V2_MNEMONIC_TYPES:Name
Value
STANDARD
standard
SEGWIT
segwit
STANDARD_2FA
standard-2fa
SEGWIT_2FA
segwit-2fa
- classmethod name() str¶
Get the name of the mnemonic class.
- Returns:
The name of the entropy class.
- Return type:
str
- classmethod from_words(words: int, language: str, mnemonic_type: str = 'standard', max_attempts: int = 1000000000000000000000000000000000000000000000000000000000000) str¶
Generates a mnemonic phrase from a specified number of words and language.
This method generates a mnemonic phrase with a specific number of words in the specified language. It ensures the generated mnemonic meets the strength requirements for the given mnemonic type.
- Parameters:
words (int) – The number of words in the mnemonic phrase.
language (str) – The language for which to generate the mnemonic phrase.
mnemonic_type (str, optional) – The type of mnemonic phrase to generate. Defaults to STANDARD.
max_attempts (int, optional) – The maximum number of attempts to adjust entropy to meet strength requirements. Defaults to 10^60.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod from_entropy(entropy: str | bytes | IEntropy, language: str, mnemonic_type: str = 'standard', max_attempts: int = 1000000000000000000000000000000000000000000000000000000000000) str¶
Generates a mnemonic phrase from entropy data, ensuring it meets the required strength.
This method generates a mnemonic phrase from the provided entropy data, validating its strength and ensuring it adheres to the specified mnemonic type and language.
- Parameters:
entropy (Union[str, bytes, IEntropy]) – The entropy data used to generate the mnemonic phrase. It can be a string, bytes, or an instance of IEntropy.
language (str) – The language for which to generate the mnemonic phrase.
mnemonic_type (str, optional) – The type of mnemonic phrase to generate. Defaults to STANDARD.
max_attempts (int, optional) – The maximum number of attempts to adjust entropy to meet strength requirements. Defaults to 10^60.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod encode(entropy: str | bytes, language: str, mnemonic_type: str = 'standard', words_list: List[str] | None = None, bip39_words_list: List[str] | None = None, bip39_words_list_with_index: dict | None = None, electrum_v1_words_list: List[str] | None = None, electrum_v1_words_list_with_index: dict | None = None) str¶
Generates a mnemonic phrase from entropy data.
This method generates a mnemonic phrase from provided entropy data, ensuring it meets the requirements of the specified mnemonic type and language.
- Parameters:
entropy (Union[str, bytes]) – The entropy data used to generate the mnemonic phrase.
language (str) – The language for which to generate the mnemonic phrase.
mnemonic_type (str, optional) – The type of mnemonic phrase to generate. Defaults to STANDARD.
words_list (List[str], optional) – Optional list of words to use for generating the mnemonic.
bip39_words_list (List[str], optional) – Optional list of BIP39 words for validation.
bip39_words_list_with_index (dict, optional) – Optional dictionary mapping BIP39 words to indices.
electrum_v1_words_list (List[str], optional) – Optional list of Electrum V1 words for validation.
electrum_v1_words_list_with_index (dict, optional) – Optional dictionary mapping Electrum V1 words to indices.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod decode(mnemonic: str, mnemonic_type: str = 'standard') str¶
Decodes a mnemonic phrase into its original entropy value.
This method decodes the mnemonic phrase by converting it back into its original entropy value, validating its type, and ensuring its integrity.
- Parameters:
mnemonic (str) – The mnemonic phrase to decode.
mnemonic_type (str, optional) – The type of mnemonic to decode. Defaults to STANDARD.
- Returns:
The decoded entropy as a string.
- Return type:
str
- classmethod is_valid(mnemonic: str | List[str], mnemonic_type: str = 'standard', bip39_words_list: List[str] | None = None, bip39_words_list_with_index: dict | None = None, electrum_v1_words_list: List[str] | None = None, electrum_v1_words_list_with_index: dict | None = None) bool¶
Checks if the given mnemonic is valid according to the specified mnemonic type.
This method validates the mnemonic against BIP39 and Electrum V1 mnemonics, and then checks if it matches the specified mnemonic type.
- Parameters:
mnemonic (str or List[str]) – The mnemonic phrase to check.
mnemonic_type (str, optional) – The type of mnemonic to check against. Defaults to STANDARD.
bip39_words_list (List[str], optional) – Optional list of BIP39 words for validation.
bip39_words_list_with_index (dict, optional) – Optional dictionary mapping BIP39 words to indices.
electrum_v1_words_list (List[str], optional) – Optional list of Electrum V1 words for validation.
electrum_v1_words_list_with_index (dict, optional) – Optional dictionary mapping Electrum V1 words to indices.
- Returns:
True if the mnemonic is valid according to the specified type, False otherwise.
- Return type:
bool
- classmethod is_type(mnemonic: str | List[str], mnemonic_type: str = 'standard') bool¶
Checks if the given mnemonic matches the specified mnemonic type.
- Parameters:
mnemonic (str or List[str]) – The mnemonic phrase to check.
mnemonic_type (str, optional) – The type of mnemonic to check against. Defaults to STANDARD.
- Returns:
True if the mnemonic matches the specified type, False otherwise.
- Return type:
bool
- mnemonic_type() str¶
Retrieves the type of the mnemonic.
- Returns:
The type of the mnemonic.
- Return type:
str
- classmethod normalize(mnemonic: str | List[str]) List[str]¶
Normalizes the given mnemonic by splitting it into a list of words if it is a string.
- Parameters:
mnemonic (Union[str, List[str]]) – The mnemonic value, which can be a single string of words or a list of words.
- Returns:
A list of words from the mnemonic.
- Return type:
List[str]
>>> from hdwallet.mnemonics.electrum.v2 import ElectrumV2Mnemonic, ELECTRUM_V2_MNEMONIC_WORDS, ELECTRUM_V2_MNEMONIC_LANGUAGES, ELECTRUM_V2_MNEMONIC_TYPES
>>> ElectrumV2Mnemonic.name()
'Electrum-V2'
>>> mnemonic: str = ElectrumV2Mnemonic.from_words(words=BIP39_MNEMONIC_WORDS.TWELVE, language=ELECTRUM_V2_MNEMONIC_LANGUAGES.CHINESE_SIMPLIFIED, mnemonic_type=ELECTRUM_V2_MNEMONIC_TYPES.SEGWIT)
>>> ElectrumV2Mnemonic.from_entropy(entropy="0c3a7d6111221a9a9f3f309ee2680aa54a", language=ELECTRUM_V2_MNEMONIC_LANGUAGES.CHINESE_SIMPLIFIED)
'凭 八 响 帐 乙 牢 碗 头 术 透 德 砖'
>>> ElectrumV2Mnemonic.encode(entropy="0c3a7d6111221a9a9f3f309ee2680aa54a", language=ELECTRUM_V2_MNEMONIC_LANGUAGES.CHINESE_SIMPLIFIED, mnemonic_type=ELECTRUM_V2_MNEMONIC_TYPES.SEGWIT)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "X:\#repos\python-hdwallet\hdwallet\mnemonics\electrum\v2\mnemonic.py", line 321, in encode
raise EntropyError("Entropy bytes are not suitable for generating a valid mnemonic")
hdwallet.exceptions.EntropyError: Entropy bytes are not suitable for generating a valid mnemonic
>>> mnemonic
'星 应 棋 翻 粗 组 路 吃 派 掷 础 鸣'
>>> ev2_mnemonic: ElectrumV2Mnemonic = ElectrumV2Mnemonic(mnemonic=mnemonic, mnemonic_type=ELECTRUM_V2_MNEMONIC_TYPES.SEGWIT)
>>> ev2_mnemonic.mnemonic()
'星 应 棋 翻 粗 组 路 吃 派 掷 础 鸣'
>>> ev2_mnemonic.language()
'Chinese-Simplified'
>>> ev2_mnemonic.words()
12
>>> ev2_mnemonic.mnemonic_type()
'segwit'
>>> ElectrumV2Mnemonic.decode(mnemonic=mnemonic, mnemonic_type=ELECTRUM_V2_MNEMONIC_TYPES.SEGWIT)
'0d04acbd49d24da36471c666f79a43436b'
>>> ElectrumV2Mnemonic.is_valid_language(language="chinese-simplified")
True
>>> ElectrumV2Mnemonic.is_valid_words(words=12)
True
>>> ElectrumV2Mnemonic.is_type(mnemonic="纠 八 响 帐 乙 牢 碗 头 术 透 德 砖", mnemonic_type=ELECTRUM_V2_MNEMONIC_TYPES.SEGWIT)
True
>>> ElectrumV2Mnemonic.is_valid(mnemonic="纠 八 响 帐 乙 牢 碗 头 术 透 德 砖", mnemonic_type=ELECTRUM_V2_MNEMONIC_TYPES.SEGWIT)
True
- class hdwallet.mnemonics.monero.mnemonic.MoneroMnemonic(mnemonic: str | List[str], **kwargs)¶
Designed for Monero’s mnemonic system, focusing on privacy and security, enabling users to recover their wallets through unique seed phrases.
Here are available
MONERO_MNEMONIC_WORDS:Name
Value
TWELVE
12
THIRTEEN
13
TWENTY_FOUR
24
TWENTY_FIVE
25
Here are available
MONERO_MNEMONIC_LANGUAGES:Name
Value
CHINESE_SIMPLIFIED
chinese-simplified
DUTCH
dutch
ENGLISH
english
FRENCH
french
GERMAN
chinese-german
ITALIAN
italian
JAPANESE
japanese
PORTUGUESE
portuguese
RUSSIAN
russian
SPANISH
spanish
- classmethod name() str¶
Get the name of the mnemonic class.
- Returns:
The name of the entropy class.
- Return type:
str
- classmethod from_words(words: int, language: str) str¶
Generates a mnemonic phrase from a specified number of words and language.
- Parameters:
words (int) – The number of words in the mnemonic phrase.
language (str) – The language for which to generate the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod from_entropy(entropy: str | bytes | IEntropy, language: str, checksum: bool = False) str¶
Generates a mnemonic phrase from entropy data.
- Parameters:
entropy (Union[str, bytes, IEntropy]) – The entropy data used to generate the mnemonic phrase.
language (str) – The language for which to generate the mnemonic phrase.
checksum (bool) – Whether to include a checksum in the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod encode(entropy: str | bytes, language: str, checksum: bool = False) str¶
Generates a mnemonic phrase from entropy data.
- Parameters:
entropy (Union[str, bytes]) – The entropy data used to generate the mnemonic phrase.
language (str) – The language for which to generate the mnemonic phrase.
checksum (bool) – Whether to include a checksum in the mnemonic phrase.
- Returns:
The generated mnemonic phrase.
- Return type:
str
- classmethod decode(mnemonic: str, **kwargs) str¶
Decodes a mnemonic phrase into entropy data.
- Parameters:
mnemonic (str) – The mnemonic phrase to decode.
kwargs – Additional keyword arguments (language, checksum).
- Returns:
The decoded entropy data.
- Return type:
str
- classmethod normalize(mnemonic: str | List[str]) List[str]¶
Normalizes the given mnemonic by splitting it into a list of words if it is a string.
- Parameters:
mnemonic (Union[str, List[str]]) – The mnemonic value, which can be a single string of words or a list of words.
- Returns:
A list of words from the mnemonic.
- Return type:
List[str]
>>> from hdwallet.mnemonics.monero import MoneroMnemonic, MONERO_MNEMONIC_WORDS, MONERO_MNEMONIC_LANGUAGES
>>> MoneroMnemonic.name()
'Monero'
>>> mnemonic: str = MoneroMnemonic.from_words(words=BIP39_MNEMONIC_WORDS.TWELVE, language=MONERO_MNEMONIC_LANGUAGES.DUTCH)
>>> MoneroMnemonic.from_entropy(entropy="cb88cbdc872bb038f0984e611b539f03", language=MONERO_MNEMONIC_LANGUAGES.DUTCH)
'leguaan nullijn knaven pablo hekman nylon rein diode napijn tuma tout tulp'
>>> MoneroMnemonic.encode(entropy="cb88cbdc872bb038f0984e611b539f03", language=MONERO_MNEMONIC_LANGUAGES.DUTCH)
'leguaan nullijn knaven pablo hekman nylon rein diode napijn tuma tout tulp'
>>> mnemonic
'waas figurante zacharias stukadoor geslaagd lenen cuisine ultiem klagelijk zijwaarts kabinet galei'
>>> monero_mnemonic: MoneroMnemonic = MoneroMnemonic(mnemonic=mnemonic)
>>> monero_mnemonic.mnemonic()
'waas figurante zacharias stukadoor geslaagd lenen cuisine ultiem klagelijk zijwaarts kabinet galei'
>>> monero_mnemonic.language()
'Dutch'
>>> monero_mnemonic.words()
12
>>> MoneroMnemonic.decode(mnemonic=mnemonic)
'335bd0b1dce8c92aac09b99129cebbe1'
>>> MoneroMnemonic.is_valid_language(language="dutch")
True
>>> MoneroMnemonic.is_valid_words(words=13)
True
>>> MoneroMnemonic.is_valid(mnemonic="leguaan nullijn knaven pablo hekman nylon rein diode napijn tuma tout tulp nylon")
True