<commit_before>#!/usr/bin/env python3

# Copyright (C) 2017-2019 The btclib developers
#
# This file is part of btclib. It is subject to the license terms in the
# LICENSE file found in the top-level directory of this distribution.
#
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

import unittest
import os
import json

from btclib import bip32
from btclib import electrum

class TestMnemonicDictionaries(unittest.TestCase):
    def test_mnemonic(self):
        lang = "en"

        entropy = 0x110aaaa03974d093eda670121023cd0772
        eversion = 'standard'
        # FIXME: is the following mnemonic obtained in Electrum from the above entropy?
        mnemonic = "ability awful fetch liberty company spatial panda hat then canal ball crouch bunker"
        mnemonic2 = electrum.mnemonic_from_entropy(entropy, lang, eversion)
        self.assertEqual(mnemonic, mnemonic2)

        entr = int(electrum.entropy_from_mnemonic(mnemonic, lang), 2)
        self.assertLess(entr-entropy, 0xfff)

        passphrase = ''

        mainnet = True
        xprv= "<KEY>"
        xprv2 = electrum.masterxprv_from_mnemonic(mnemonic, passphrase, mainnet)
        self.assertEqual(xprv2.decode(), xprv)

        # unknown electrum mnemonic version (00c)
        mnemonic = "ability awful fetch liberty company spatial panda hat then canal ball cross video"
        self.assertRaises(ValueError, electrum.masterxprv_from_mnemonic,
                                      mnemonic, passphrase, mainnet)
        #electrum.masterxprv_from_mnemonic(mnemonic, passphrase, mainnet)

        # unknown electrum mnemonic version (00c)
        mnemonic = "ability awful fetch liberty company spatial panda hat then canal ball cross video"
        self.assertRaises(ValueError, electrum.entropy_from_mnemonic, mnemonic, lang)
        #electrum.entropy_from_mnemonic(mnemonic, lang)

        # mnemonic version not in electrum allowed mnemonic versions
        eversion = 'std'
        self.assertRaises(ValueError, electrum.mnemonic_from_entropy,
                                      entropy, lang, eversion)
        #electrum.mnemonic_from_entropy(entropy, lang, eversion)


    def test_vectors(self):
        filename = "electrum_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)
        f.closed

        for test_vector in test_vectors:
            version = test_vector[0]
            mnemonic = test_vector[1]
            passphrase = test_vector[2]
            mxprv = test_vector[3]
            mxpub = test_vector[4]
            address = test_vector[5]  # "./0/0"

            mainnet = True
            mxprv2 = electrum.masterxprv_from_mnemonic(mnemonic, passphrase, mainnet)
            self.assertEqual(mxprv2.decode(), mxprv)
            mxpub2 = bip32.xpub_from_xprv(mxprv2)
            self.assertEqual(mxpub2.decode(), mxpub)
            xpub = bip32.derive(mxpub2, "./0/0")
<commit_msg>extended test coverage<commit_after>#!/usr/bin/env python3

# Copyright (C) 2017-2019 The btclib developers
#
# This file is part of btclib. It is subject to the license terms in the
# LICENSE file found in the top-level directory of this distribution.
#
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

import unittest
import os
import json

from btclib import bip32
from btclib import electrum

class TestMnemonicDictionaries(unittest.TestCase):
    def test_mnemonic(self):
        lang = "en"

        entropy = 0x110aaaa03974d093eda670121023cd0772
        eversion = 'standard'
        # FIXME: is the following mnemonic obtained in Electrum from the above entropy?
        mnemonic = "ability awful fetch liberty company spatial panda hat then canal ball crouch bunker"
        mnemonic2 = electrum.mnemonic_from_entropy(entropy, lang, eversion)
        self.assertEqual(mnemonic, mnemonic2)

        entr = int(electrum.entropy_from_mnemonic(mnemonic, lang), 2)
        self.assertLess(entr-entropy, 0xfff)

        # mnemonic version not in electrum allowed mnemonic versions
        eversion = 'std'
        self.assertRaises(ValueError, electrum.mnemonic_from_entropy,
                                      entropy, lang, eversion)
        #electrum.mnemonic_from_entropy(entropy, lang, eversion)

        # unknown electrum mnemonic version (00c)
        unknown_version = "ability awful fetch liberty company spatial panda hat then canal ball cross video"
        self.assertRaises(ValueError, electrum.entropy_from_mnemonic,
                                      unknown_version, lang)
        #electrum.entropy_from_mnemonic(unknown_version, lang)

        passphrase = ''
        mainnet = True

        # unknown electrum mnemonic version (00c)
        self.assertRaises(ValueError, electrum.masterxprv_from_mnemonic,
                                      unknown_version, passphrase, mainnet)
        #electrum.masterxprv_from_mnemonic(mnemonic, passphrase, mainnet)

        xprv= "<KEY>"
        xprv2 = electrum.masterxprv_from_mnemonic(mnemonic, passphrase, mainnet)
        self.assertEqual(xprv2.decode(), xprv)

        eversion = '2fa'
        mnemonic = electrum.mnemonic_from_entropy(entropy, lang, eversion)
        # 2fa mnemonic version is not managed yet
        self.assertRaises(ValueError, electrum.masterxprv_from_mnemonic,
                                      mnemonic, passphrase, mainnet)
        #electrum.masterxprv_from_mnemonic(mnemonic, passphrase, mainnet)

        eversion = '2fa_segwit'
        mnemonic = electrum.mnemonic_from_entropy(entropy, lang, eversion)
        # 2fa_segwit mnemonic version is not managed yet
        self.assertRaises(ValueError, electrum.masterxprv_from_mnemonic,
                                      mnemonic, passphrase, mainnet)
        #electrum.masterxprv_from_mnemonic(mnemonic, passphrase, mainnet)


    def test_vectors(self):
        filename = "electrum_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)
        f.closed

        for test_vector in test_vectors:
            version = test_vector[0]
            mnemonic = test_vector[1]
            passphrase = test_vector[2]
            mxprv = test_vector[3]
            mxpub = test_vector[4]
            address = test_vector[5]  # "./0/0"

            mainnet = True
            mxprv2 = electrum.masterxprv_from_mnemonic(mnemonic, passphrase, mainnet)
            self.assertEqual(mxprv2.decode(), mxprv)
            mxpub2 = bip32.xpub_from_xprv(mxprv2)
            self.assertEqual(mxpub2.decode(), mxpub)
            xpub = bip32.derive(mxpub2, "./0/0")

{'fraction_non_alphanumeric': 0.07370629370629371, 'fraction_numerical': 0.01902097902097902, 'mean_word_length': 2.4545893719806764, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3063578', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2119, 'n_words': 526}