def test_1(self): lang = "en" d = mnemonic_dict.word_list(lang) self.assertIsInstance(d, list) self.assertEqual(len(d), 2048) length = mnemonic_dict.language_length(lang) self.assertEqual(length, 2048) bpw = mnemonic_dict.bits_per_word(lang) self.assertEqual(bpw, 11) test_mnemonic = "ozone drill grab fiber curtain grace " \ "pudding thank cruise elder eight picnic" test_indexes = [ 1268, 535, 810, 685, 433, 811, 1385, 1790, 421, 570, 567, 1313 ] indexes = mnemonic_dict.indexes_from_mnemonic(test_mnemonic, lang) self.assertEqual(indexes, test_indexes) mnemonic = mnemonic_dict.mnemonic_from_indexes(test_indexes, lang) self.assertEqual(mnemonic, test_mnemonic) entropy = mnemonic_dict.entropy_from_indexes(test_indexes, lang) indexes = mnemonic_dict.indexes_from_entropy(entropy, lang) self.assertEqual(indexes, test_indexes) # entropy must be binary string or int entropy = b'123456789abcdef0' self.assertRaises(TypeError, mnemonic_dict.indexes_from_entropy, entropy, lang)
def electrum_mnemonic_from_raw_entropy(raw_entropy: GenericEntropy, lang: str, eversion: str) -> str: # electrum considers entropy as integer, losing any leading zero # https://github.com/spesmilo/electrum/blob/master/lib/mnemonic.py int_entropy = int_from_entropy(raw_entropy) assert eversion in ELECTRUM_MNEMONIC_VERSIONS, "unknown electrum mnemonic version" invalid = True while invalid: str_entropy = str_from_entropy(int_entropy) indexes = mnemonic_dict.indexes_from_entropy(str_entropy, lang) mnemonic = mnemonic_dict.mnemonic_from_indexes(indexes, lang) # version validity check s = hmac.new(b"Seed version", mnemonic.encode('utf8'), sha512).hexdigest() if s.startswith(ELECTRUM_MNEMONIC_VERSIONS[eversion]): invalid = False # next trial int_entropy += 1 return mnemonic
def mnemonic_from_raw_entropy(raw_entropy: GenericEntropy, lang: str, eversion: str) -> str: # electrum considers entropy as integer, losing any leading zero # https://github.com/spesmilo/electrum/blob/master/lib/mnemonic.py int_entropy = int_from_entropy(raw_entropy) if eversion not in ELECTRUM_MNEMONIC_VERSIONS: m = f"mnemonic version '{eversion}' not in electrum allowed " m += f"mnemonic versions {list(ELECTRUM_MNEMONIC_VERSIONS.keys())}" raise ValueError(m) invalid = True while invalid: str_entropy = str_from_entropy(int_entropy) indexes = mnemonic_dict.indexes_from_entropy(str_entropy, lang) mnemonic = mnemonic_dict.mnemonic_from_indexes(indexes, lang) # version validity check s = hmac.new(b"Seed version", mnemonic.encode('utf8'), sha512).hexdigest() if s.startswith(ELECTRUM_MNEMONIC_VERSIONS[eversion]): invalid = False # next trial int_entropy += 1 return mnemonic
def mnemonic_from_raw_entropy(raw_entr: GenericEntropy, lang: str) -> str: entropy = entropy_from_raw_entropy(raw_entr) indexes = mnemonic_dict.indexes_from_entropy(entropy, lang) mnemonic = mnemonic_dict.mnemonic_from_indexes(indexes, lang) return mnemonic