Skip to content

Instantly share code, notes, and snippets.

@kdmukai
Last active April 15, 2025 15:21
Show Gist options
  • Save kdmukai/1a666c945c46ada120fd2cd3c5a1f33c to your computer and use it in GitHub Desktop.
Save kdmukai/1a666c945c46ada120fd2cd3c5a1f33c to your computer and use it in GitHub Desktop.
Using coin flips as 11-bit bip39 wordlist indices
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 8 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment