From 0d3440e7ba2081530155967ea4a8dfe772c7dc10 Mon Sep 17 00:00:00 2001 From: Carl Beekhuizen Date: Mon, 28 Mar 2022 11:24:52 +0200 Subject: [PATCH] Adds check that multiple mnemonic languages aren't detected --- .../key_handling/key_derivation/mnemonic.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/staking_deposit/key_handling/key_derivation/mnemonic.py b/staking_deposit/key_handling/key_derivation/mnemonic.py index 49f5ada..378bafd 100644 --- a/staking_deposit/key_handling/key_derivation/mnemonic.py +++ b/staking_deposit/key_handling/key_derivation/mnemonic.py @@ -108,6 +108,7 @@ def reconstruct_mnemonic(mnemonic: str, words_path: str) -> Optional[str]: languages = determine_mnemonic_language(mnemonic, words_path) except ValueError: return None + reconstructed_mnemonic = None for language in languages: try: word_list = abbreviate_words(_get_word_list(language, words_path)) @@ -122,12 +123,17 @@ def reconstruct_mnemonic(mnemonic: str, words_path: str) -> Optional[str]: entropy_bits = entropy.to_bytes(checksum_length * 4, 'big') full_word_list = _get_word_list(language, words_path) if _get_checksum(entropy_bits) == checksum: - return ' '.join([_index_to_word(full_word_list, index) for index in word_indices]) + """ + This check guarantees that only one language has a valid mnemonic. + It is needed to ensure abbrivated words aren't valid in multiple languages + """ + assert reconstructed_mnemonic is None + reconstructed_mnemonic = ' '.join([_index_to_word(full_word_list, index) for index in word_indices]) else: pass except ValueError: pass - return None + return reconstructed_mnemonic def get_mnemonic(*, language: str, words_path: str, entropy: Optional[bytes]=None) -> str: