示例#1
0
def test_bip39():
    lang = "en"
    mnem = "abandon abandon atom trust ankle walnut oil across awake bunker divorce abstract"

    raw_entr = bytes.fromhex("0000003974d093eda670121023cd0000")
    mnemonic = bip39.mnemonic_from_entropy(raw_entr, lang)
    assert mnemonic == mnem

    r = bip39.entropy_from_mnemonic(mnemonic, lang)
    size = (len(r) + 7) // 8
    r = int(r, 2).to_bytes(size, byteorder="big")
    assert r == raw_entr

    wrong_mnemonic = mnemonic + " abandon"
    err_msg = "Wrong number of words: "
    with pytest.raises(ValueError, match=err_msg):
        bip39.entropy_from_mnemonic(wrong_mnemonic, lang)

    wr_m = "abandon abandon atom trust ankle walnut oil across awake bunker divorce oil"
    err_msg = "invalid checksum: "
    with pytest.raises(ValueError, match=err_msg):
        bip39.entropy_from_mnemonic(wr_m, lang)

    # Invalid number of bits (130) for BIP39 entropy; must be in ...
    binstr_entropy = "01" * 65  # 130 bits
    err_msg = "Invalid number of bits for BIP39 entropy: "
    with pytest.raises(ValueError, match=err_msg):
        bip39._entropy_checksum(binstr_entropy)
示例#2
0
    def test_bip39(self):
        lang = "en"
        raw_entr = bytes.fromhex("0000003974d093eda670121023cd0000")
        mnemonic = bip39.mnemonic_from_entropy(raw_entr, lang)
        self.assertEqual(mnemonic, "abandon abandon atom trust ankle walnut oil across awake bunker divorce abstract")
        r = bip39.entropy_from_mnemonic(mnemonic, lang)
        size = (len(r)+7) // 8
        r = int(r, 2).to_bytes(size, 'big')
        self.assertEqual(r, raw_entr)

        passphrase = ''

        rootxprv = bip39.rootxprv_from_mnemonic(mnemonic, passphrase, bip32.PRV_VERSION[0])
        exp = b'xprv9s21ZrQH143K3ZxBCax3Wu25iWt3yQJjdekBuGrVa5LDAvbLeCT99U59szPSFdnMe5szsWHbFyo8g5nAFowWJnwe8r6DiecBXTVGHG124G1'
        self.assertEqual(rootxprv, exp)

        rootxprv2 = bip39.rootxprv_from_entropy(raw_entr, passphrase, lang, bip32.PRV_VERSION[0])
        self.assertEqual(rootxprv2, rootxprv)

        rootxprv = bip39.rootxprv_from_mnemonic(mnemonic, passphrase, bip32.PRV_VERSION[0])
        exp = b'xprv9s21ZrQH143K3ZxBCax3Wu25iWt3yQJjdekBuGrVa5LDAvbLeCT99U59szPSFdnMe5szsWHbFyo8g5nAFowWJnwe8r6DiecBXTVGHG124G1'
        self.assertEqual(rootxprv, exp)

        rootxprv2 = bip39.rootxprv_from_entropy(raw_entr, passphrase, lang, bip32.PRV_VERSION[0])
        self.assertEqual(rootxprv2, rootxprv)

        # mnemonic with wrong number of bits
        wrong_mnemonic = mnemonic + " abandon"
        self.assertRaises(ValueError, bip39.entropy_from_mnemonic, wrong_mnemonic, lang)
        #bip39_entropy_from_mnemonic(wrong_mnemonic, lang)

        # invalid mnemonic checksum
        wrong_mnemonic = "abandon abandon atom trust ankle walnut oil across awake bunker divorce walnut"
        self.assertRaises(ValueError, bip39.entropy_from_mnemonic, wrong_mnemonic, lang)
示例#3
0
    def test_bip39(self):
        lang = "en"
        raw_entr = bytes.fromhex("0000003974d093eda670121023cd0000")
        mnemonic = bip39.mnemonic_from_entropy(raw_entr, lang)
        self.assertEqual(
            mnemonic,
            "abandon abandon atom trust ankle walnut oil across awake bunker divorce abstract"
        )
        r = bip39.entropy_from_mnemonic(mnemonic, lang)
        size = (len(r) + 7) // 8
        r = int(r, 2).to_bytes(size, byteorder='big')
        self.assertEqual(r, raw_entr)

        # mnemonic with wrong number of words
        wrong_mnemonic = mnemonic + " abandon"
        self.assertRaises(ValueError, bip39.entropy_from_mnemonic,
                          wrong_mnemonic, lang)
        #bip39_entropy_from_mnemonic(wrong_mnemonic, lang)

        # invalid mnemonic checksum
        wr_m = "abandon abandon atom trust ankle walnut oil across awake bunker divorce walnut"
        self.assertRaises(ValueError, bip39.entropy_from_mnemonic, wr_m, lang)
        #bip39_entropy_from_mnemonic(wrong_mnemonic, lang)

        # Invalid number of bits (130) for BIP39 entropy; must be in ...
        binstr_entropy = '01' * 65  # 130 bits
        self.assertRaises(ValueError, bip39._entropy_checksum, binstr_entropy)
示例#4
0
def checksum_validator(lst_filtered_possibilities):
    """
    :param lst_filtered_possibilities: unique mnemonic phrases generated by main.py
    :return: list of mnemonic phrases with valid checksums according to BIP39 spec
    """
    checksum_valid = []
    print("\nValidating...")
    for mnemonic_phrase in lst_filtered_possibilities:
        try:  # BIP39 Filtering - checks whether the phrases produce a valid entropy according to BIP39.
            bip39.entropy_from_mnemonic(" ".join(mnemonic_phrase), "en")
            checksum_valid.append(mnemonic_phrase)
        except ValueError:  # Entropy was invalid
            pass
        except TypeError:
            pass
    print(f'\nFound {len(checksum_valid)} valid mnemonics.')
    return checksum_valid
示例#5
0
    def test_vectors(self):
        """BIP39 test vectors
           https://github.com/trezor/python-mnemonic/blob/master/vectors.json
        """
        fname = "bip39_test_vectors.json"
        filename = path.join(path.dirname(__file__), "test_data", fname)
        with open(filename, "r") as f:
            test_vectors = json.load(f)["english"]
        for test_vector in test_vectors:
            lang = "en"
            entropy = bytes.fromhex(test_vector[0])
            mnemonic = bip39.mnemonic_from_entropy(entropy, lang)
            self.assertEqual(mnemonic.split(), test_vector[1].split())

            raw_entr = bip39.entropy_from_mnemonic(mnemonic, lang)
            size = (len(raw_entr) + 7) // 8
            raw_entr = int(raw_entr, 2).to_bytes(size, byteorder="big")
            self.assertEqual(raw_entr, entropy)

            seed = bip39.seed_from_mnemonic(mnemonic, "TREZOR").hex()
            self.assertEqual(seed, test_vector[2])
示例#6
0
def test_vectors() -> None:
    """BIP39 test vectors

    https://github.com/trezor/python-mnemonic/blob/master/vectors.json
    """
    fname = "bip39_test_vectors.json"
    filename = path.join(path.dirname(__file__), "test_data", fname)
    with open(filename, "r") as file_:
        bip39_test_vectors = json.load(file_)["english"]

    lang = "en"
    # test_vector[3], i.e. the bip32 master private key, is tested in bip32
    for entr, mnemonic, seed, _ in bip39_test_vectors:
        entropy = bytes.fromhex(entr)
        # clean up mnemonic from spurious whitespaces
        mnemonic = " ".join(mnemonic.split())
        assert mnemonic == bip39.mnemonic_from_entropy(entropy, lang)
        assert seed == bip39.seed_from_mnemonic(mnemonic, "TREZOR").hex()

        raw_entr = bip39.entropy_from_mnemonic(mnemonic, lang)
        size = (len(raw_entr) + 7) // 8
        assert entropy == int(raw_entr, 2).to_bytes(size, byteorder="big")
示例#7
0
    def test_vectors(self):
        """BIP39 test vectors
           https://github.com/trezor/python-mnemonic/blob/master/vectors.json
        """
        filename = "bip39_test_vectors.json"
        path_to_filename = os.path.join(os.path.dirname(__file__), "./data/",
                                        filename)
        with open(path_to_filename, 'r') as f:
            test_vectors = json.load(f)["english"]
        f.closed
        for test_vector in test_vectors:
            lang = "en"
            test_vector[0] = bytes.fromhex(test_vector[0])
            mnemonic = bip39.mnemonic_from_entropy(test_vector[0], lang)
            self.assertEqual(mnemonic, test_vector[1])

            raw_entr = bip39.entropy_from_mnemonic(mnemonic, lang)
            size = (len(raw_entr) + 7) // 8
            raw_entr = int(raw_entr, 2).to_bytes(size, 'big')
            self.assertEqual(raw_entr, test_vector[0])

            seed = bip39.seed_from_mnemonic(mnemonic, "TREZOR").hex()
            self.assertEqual(seed, test_vector[2])
print("Probabilities found (filtered, without repetitions): ",
      len(possibilities_filtered))
print("Writing data...")

# Writing filtered possible combos (additional checkpoint)
# with open("possibilities.out.txt", "w") as f:
#     f.write(json.dumps(possibilities_filtered))

print("Validating harvested phrases...\n")

# Getting prepared to be filtered by bip39 function
valid_mnemonics = []

for mnemonic_phrase in possibilities_filtered:
    # BIP-39 Filtering
    try:
        bip39.entropy_from_mnemonic(" ".join(mnemonic_phrase), "en")
        valid_mnemonics.append(mnemonic_phrase)
        print("Found valid BIP39 - ({0}/{1})".format(
            len(valid_mnemonics), len(possibilities_filtered)),
              end="\r")
    except ValueError:
        pass

# Write BIP-39 valid mnemonics
with open("valid_possibilities.out.txt", "w") as f:
    f.write(json.dumps(valid_mnemonics))

# Done
print("\nFound {0} valid mnemonics".format(len(valid_mnemonics)))