From a42408a3e49e18e0864302a434cef4cf31b3e938 Mon Sep 17 00:00:00 2001 From: kdmukai Date: Wed, 15 Jun 2022 14:51:47 -0500 Subject: [PATCH] mnemonic_generation test case bug fix; additional test case. --- src/seedsigner/helpers/mnemonic_generation.py | 10 +++++++++- tests/test_mnemonic_generation.py | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/seedsigner/helpers/mnemonic_generation.py b/src/seedsigner/helpers/mnemonic_generation.py index 6723b512..dab63a48 100644 --- a/src/seedsigner/helpers/mnemonic_generation.py +++ b/src/seedsigner/helpers/mnemonic_generation.py @@ -9,7 +9,15 @@ from seedsigner.models.seed import Seed def calculate_checksum(mnemonic: list, wordlist_language_code: str) -> List[str]: - """ Provide 12- or 24-word mnemonic, returns complete mnemonic w/checksum as a list """ + """ + Provide 12- or 24-word mnemonic, returns complete mnemonic w/checksum as a list. + + If 11- or 23-words are provided, append word `0000` to end of list as temp final + word. + """ + if len(mnemonic) in [11, 23]: + mnemonic.append(Seed.get_wordlist(wordlist_language_code)[0]) + if len(mnemonic) not in [12, 24]: raise Exception("Pass in a 12- or 24-word mnemonic") diff --git a/tests/test_mnemonic_generation.py b/tests/test_mnemonic_generation.py index a5b3fd0c..a0c04ed8 100644 --- a/tests/test_mnemonic_generation.py +++ b/tests/test_mnemonic_generation.py @@ -43,6 +43,26 @@ def test_calculate_checksum(): +def test_calculate_checksum_with_default_final_word(): + """ 11-word and 23-word mnemonics use word `0000` as a temp final word to complete + the mnemonic. + """ + partial_mnemonic = "crawl focus rescue cable view pledge rather dinner cousin unfair day" + mnemonic1 = mnemonic_generation.calculate_checksum(partial_mnemonic.split(" "), wordlist_language_code=SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) + + partial_mnemonic += " abandon" + mnemonic2 = mnemonic_generation.calculate_checksum(partial_mnemonic.split(" "), wordlist_language_code=SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) + assert mnemonic1 == mnemonic2 + + partial_mnemonic = "bubble father debate ankle injury fence mesh evolve section wet coyote violin pyramid flower rent arrow round clutch myth safe base skin mobile" + mnemonic1 = mnemonic_generation.calculate_checksum(partial_mnemonic.split(" "), wordlist_language_code=SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) + + partial_mnemonic += " abandon" + mnemonic2 = mnemonic_generation.calculate_checksum(partial_mnemonic.split(" "), wordlist_language_code=SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) + assert mnemonic1 == mnemonic2 + + + def test_verify_against_coldcard_sample(): """ https://coldcard.com/docs/verifying-dice-roll-math """ dice_rolls = "123456"