Skip to content

Instantly share code, notes, and snippets.

@kdmukai
Last active April 15, 2025 15:21
Show Gist options
  • Select an option

  • Save kdmukai/1a666c945c46ada120fd2cd3c5a1f33c to your computer and use it in GitHub Desktop.

Select an option

Save kdmukai/1a666c945c46ada120fd2cd3c5a1f33c to your computer and use it in GitHub Desktop.

Revisions

  1. kdmukai revised this gist Apr 15, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mnemonic_generation.py
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ def get_mnemonic_from_coin_flip_indices(coin_flips: str, wordlist_language_code:
    # convert bit string to bytes
    coin_flip_bytes = bytearray()
    for i in range(0, len(coin_flips), 8):
    # convert 11 bits to an integer
    # convert 8 bits to an integer
    index = int(coin_flips[i:i + 8], 2)
    # convert the index to a byte
    coin_flip_bytes.append(index)
  2. kdmukai created this gist Apr 15, 2025.
    26 changes: 26 additions & 0 deletions mnemonic_generation.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    def get_mnemonic_from_coin_flip_indices(coin_flips: str, wordlist_language_code: str = SettingsConstants.WORDLIST_LANGUAGE__ENGLISH) -> list[str]:
    """
    Interprets a string of 128 or 256 0s and 1s as 11-bit bip39 wordlist indices and
    returns the 12- or 24-word mnemonic.
    Indices are 0-based (i.e. b'00000000000' == 0000 == "abandon")
    Verify with iancoleman.io/bip39 using:
    * "Binary" mode
    * "Mnemonic Length" = "Use raw entropy"
    """
    if len(coin_flips) not in [128, 256]:
    raise Exception("Coin flip string must be 128 or 256 bits")

    # convert bit string to bytes
    coin_flip_bytes = bytearray()
    for i in range(0, len(coin_flips), 8):
    # convert 11 bits to an integer
    index = int(coin_flips[i:i + 8], 2)
    # convert the index to a byte
    coin_flip_bytes.append(index)

    # Convert the indices to their associated words. The final word will consist of the
    # last bits of the provided entropy plus the checksum which will be automatically
    # added by the method below.
    return bip39.mnemonic_from_bytes(coin_flip_bytes, wordlist=Seed.get_wordlist(wordlist_language_code)).split()