Skip to content

Instantly share code, notes, and snippets.

@astrojarred
Created May 20, 2021 17:05
Show Gist options
  • Save astrojarred/78fc91fadc6ba6f6991c5c23c44f1ae3 to your computer and use it in GitHub Desktop.
Save astrojarred/78fc91fadc6ba6f6991c5c23c44f1ae3 to your computer and use it in GitHub Desktop.
Translates numerical representations of a 15-word bip39 key into a list of words
from bip_utils import Bip39MnemonicValidator, MnemonicFileReader
# install bip_utils with `pip install bip_utils`
# https://pypi.org/project/bip-utils/
def get_key(data, datatype="decimal", index_shift=0):
"""
Translates numerical representations of a 15-word bip39 key into a list of words
Parameters
----------
data : str or list of str or list of int
The input data you want to translate. Can be a binary string,
a list of binaries, a list of decimal numbers, a string of hex numbers,
or a list of hex numbers.
datatype : {"decimal", "hex", "binary"}
Descibes the base of the input data
index_shift : int, optional
Shifts the index of every word by a specified amount after translation
(takes both positive and negative values)
(useful if you do not know if the creater used 0-indexing or not).
Returns
_______
keys : list of str
The input data translated with the bip39 world list,
given as an ordered list of words
is_valid : bool
Whether ths checksum of `keys` is valid or not
"""
if datatype not in ["decimal", "hex", "binary"]:
raise TypeError("datatype must be either `decimal`, `hex`, or `binary.`")
# remove whitespaces from string
if isinstance(data, str):
data = data.replace(" ", "")
if datatype == "binary":
# split the binary data into a list if it is not already
if isinstance(data, str):
data = list(data[0 + i : 11 + i] for i in range(0, len(data), 11))
# convert the binary strings into base10
integer_list = [int(str(i), 2) for i in data]
elif datatype == "hex":
# split the hex data into a list if it is not already
if isinstance(data, str):
data = list(data[0 + i : 3 + i] for i in range(0, len(data), 3))
# convert the hex strings into base10
integer_list = [int(i, 16) for i in data]
else: # i.e. datatype == "deicmal"
integer_list = data
if index_shift: # shift the index of the numbers if necessary
integer_list = [number + index_shift for number in integer_list]
for idx, number in enumerate(integer_list):
if number < 0 or number > 2047:
raise ValueError(
f"Data contains values that are not between 0-2047: `{number}` in position #{idx+1}"
)
keys = [MnemonicFileReader().GetWordAtIdx(number) for number in integer_list]
is_valid = Bip39MnemonicValidator(keys).Validate()
return keys, is_valid
# test the code with variou inputs
# NOTE: this key was randomly generated
binary_string = "101111110011000110001110011011000111100101000111010000101101111010010010000101011011011111000101111011011110101101010111110100110111101011100001110001101100101000101"
binary_list = ['10111111001', '10001100011', '10011011000', '11110010100', '01110100001', '01101111010', '01001000010', '10110110111', '11000101111', '01101111010', '11010101111', '10100110111', '10101110000', '11100011011', '00101000101']
decimal_list = [1529, 1123, 1240, 1940, 929, 890, 578, 1463, 1583, 890, 1711, 1335, 1392, 1819, 325]
hex_string = '5f94634d87943a137a2425b762f37a6af53757071b145'
hex_list = ['0x5f9', '0x463', '0x4d8', '0x794', '0x3a1', '0x37a', '0x242', '0x5b7', '0x62f', '0x37a', '0x6af', '0x537', '0x570', '0x71b', '0x145']
hex_list2 = ['5f9', '463', '4d8', '794', '3a1', '37a', '242', '5b7', '62f', '37a', '6af', '537', '570', '71b', '145']
# should all return ['sand', 'midnight', 'only', 'verb', 'injury', 'hungry', 'embark', 'report', 'shine', 'hungry', 'sting', 'plunge', 'puppy', 'toddler', 'chunk'], True
get_key(binary_string, "binary")
get_key(binary_list, "binary")
get_key(decimal_list, "decimal")
get_key(hex_string, "hex")
get_key(hex_list, "hex")
get_key(hex_list2, "hex")
# testing index shift, can use positive or negative numbers
get_key(decimal_list, "decimal", index_shift=1)
get_key(decimal_list, "decimal", index_shift=-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment