示例#1
0
    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)
示例#2
0
 def test_2(self):
     lang = "en"
     test_indexes = [
         0, 0, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 0
     ]
     entropy = mnemonic_dict.entropy_from_indexes(test_indexes, lang)
     indexes = mnemonic_dict.indexes_from_entropy(entropy, lang)
     self.assertEqual(indexes, test_indexes)
示例#3
0
def bip39_raw_entropy_from_mnemonic(mnemonic: str, lang: str) -> Entropy:
    indexes = mnemonic_dict.indexes_from_mnemonic(mnemonic, lang)
    entropy = mnemonic_dict.entropy_from_indexes(indexes, lang)

    # raw entropy is only the first part of entropy
    raw_entr_bits = int(len(entropy) * 32 / 33)
    assert raw_entr_bits in _allowed_raw_entr_bits, "invalid raw entropy size"
    raw_entr = entropy[:raw_entr_bits]

    # the second one being the checksum, to be verified
    bytes_raw_entr = int(raw_entr, 2).to_bytes(raw_entr_bits // 8, 'big')
    checksum = bip39_raw_entropy_checksum(bytes_raw_entr)
    assert entropy[raw_entr_bits:] == checksum

    return raw_entr
示例#4
0
def raw_entropy_from_mnemonic(mnemonic: str, lang: str) -> Entropy:
    """output raw entropy is returned as binary string"""
    indexes = mnemonic_dict.indexes_from_mnemonic(mnemonic, lang)
    entropy = mnemonic_dict.entropy_from_indexes(indexes, lang)

    # raw entropy is only the first part of entropy
    raw_entr_bits = int(len(entropy) * 32 / 33)
    if raw_entr_bits not in _allowed_raw_entr_bits:
        m = f"mnemonic with wrong number of bits ({raw_entr_bits}); "
        m += f"expected: {_allowed_raw_entr_bits}"
        raise ValueError(m)
    raw_entr = entropy[:raw_entr_bits]

    # the second one being the checksum, to be verified
    bytes_raw_entr = int(raw_entr, 2).to_bytes(raw_entr_bits // 8, 'big')
    checksum = _raw_entropy_checksum(bytes_raw_entr)
    if entropy[raw_entr_bits:] != checksum:
        raise ValueError("invalid mnemonic checksum")

    return raw_entr
示例#5
0
def entropy_from_mnemonic(mnemonic: str, lang: str) -> Entropy:
    """entropy is returned as binary string"""
    indexes = mnemonic_dict.indexes_from_mnemonic(mnemonic, lang)
    entropy = mnemonic_dict.entropy_from_indexes(indexes, lang)
    return entropy
示例#6
0
def electrum_entropy_from_mnemonic(mnemonic: str, lang: str) -> Entropy:
    indexes = mnemonic_dict.indexes_from_mnemonic(mnemonic, lang)
    entropy = mnemonic_dict.entropy_from_indexes(indexes, lang)
    return entropy