Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmukai committed Mar 2, 2024
1 parent 9535f21 commit 1ed703b
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions src/seedsigner/helpers/mnemonic_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,29 @@

from embit import bip39
from embit.wordlists.bip39 import WORDLIST as WORDLIST__ENGLISH
from seedsigner.models.settings_definition import SettingsConstants

"""
This is SeedSigner's internal mnemonic generation utility but is also meant to
function as an independently-executable CLI to facilitate external verification of
SeedSigner's mnemonic generation for a given input entropy.
Therefore its module imports should be kept to the bare minimum.
## Running as a standalone script
Install the `embit` library:
```
pip3 install embit
```
And then run:
```
python3 mnemonic_generation.py -h
```
"""

This is SeedSigner's internal mnemonic generation utility.
It can also be run as an independently-executable CLI to facilitate external
verification of SeedSigner's results for a given input entropy.
# Hard-coded value from SettingsConstants to avoid dependencies
WORDLIST_LANGUAGE__ENGLISH = 'en'
see: docs/dice_verification.md (the "Command Line Tool" section).
"""

DICE__NUM_ROLLS__12WORD = 50
DICE__NUM_ROLLS__24WORD = 99



def _get_wordlist(wordlist_language_code) -> list[str]:
"""
Convenience method to fetch the wordlist for the given language code without
requiring any SeedSigner module dependencies for when this is run as a
standalone CLI.
"""
if wordlist_language_code == WORDLIST_LANGUAGE__ENGLISH:
if wordlist_language_code == SettingsConstants.WORDLIST_LANGUAGE__ENGLISH:
return WORDLIST__ENGLISH
else:
# Nested import to avoid dependency on Seed model when running this script standalone
Expand All @@ -46,7 +34,7 @@ def _get_wordlist(wordlist_language_code) -> list[str]:



def calculate_checksum(mnemonic: list | str, wordlist_language_code: str = WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
def calculate_checksum(mnemonic: list | str, wordlist_language_code: str = SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
"""
Provide 12- or 24-word mnemonic, returns complete mnemonic w/checksum as a list.
Expand Down Expand Up @@ -83,12 +71,12 @@ def calculate_checksum(mnemonic: list | str, wordlist_language_code: str = WORDL



def generate_mnemonic_from_bytes(entropy_bytes, wordlist_language_code: str = WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
def generate_mnemonic_from_bytes(entropy_bytes, wordlist_language_code: str = SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
return bip39.mnemonic_from_bytes(entropy_bytes, wordlist=_get_wordlist(wordlist_language_code)).split()



def generate_mnemonic_from_dice(roll_data: str, wordlist_language_code: str = WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
def generate_mnemonic_from_dice(roll_data: str, wordlist_language_code: str = SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
"""
Takes a string of 50 or 99 dice rolls and returns a 12- or 24-word mnemonic.
Expand All @@ -109,7 +97,7 @@ def generate_mnemonic_from_dice(roll_data: str, wordlist_language_code: str = WO



def generate_mnemonic_from_coin_flips(coin_flips: str, wordlist_language_code: str = WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
def generate_mnemonic_from_coin_flips(coin_flips: str, wordlist_language_code: str = SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
"""
Takes a string of 128 or 256 0s and 1s and returns a 12- or 24-word mnemonic.
Expand All @@ -128,7 +116,7 @@ def generate_mnemonic_from_coin_flips(coin_flips: str, wordlist_language_code: s



def get_partial_final_word(coin_flips: str, wordlist_language_code: str = WORDLIST_LANGUAGE__ENGLISH) -> str:
def get_partial_final_word(coin_flips: str, wordlist_language_code: str = SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) -> str:
""" Look up the partial final word for the given coin flips.
7 coin flips: 0101010 + **** where the final 4 bits will be replaced with the checksum
3 coin flips: 010 + ******** where the final 8 bits will be replaced with the checksum
Expand All @@ -142,11 +130,9 @@ def get_partial_final_word(coin_flips: str, wordlist_language_code: str = WORDLI

# Note: This currently isn't being used since we're now chaining hashed bytes for the
# image-based entropy and aren't just ingesting a single image.
def generate_mnemonic_from_image(image, wordlist_language_code: str = WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
def generate_mnemonic_from_image(image, wordlist_language_code: str = SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
import hashlib
hash = hashlib.sha256(image.tobytes())

# Return as a list
return bip39.mnemonic_from_bytes(hash.digest(), wordlist=_get_wordlist(wordlist_language_code)).split()


0 comments on commit 1ed703b

Please sign in to comment.