Line data Source code
1 : // Copyright (c) 2014-2025 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <test/data/bip39_vectors.json.h> 6 : 7 : #include <wallet/bip39.h> 8 : #include <key.h> 9 : #include <key_io.h> 10 : #include <test/util/json.h> 11 : #include <test/util/setup_common.h> 12 : #include <util/strencodings.h> 13 : 14 : #include <boost/test/unit_test.hpp> 15 : 16 : #include <univalue.h> 17 : 18 : namespace wallet { 19 146 : BOOST_FIXTURE_TEST_SUITE(bip39_tests, BasicTestingSetup) 20 : 21 : // https://github.com/trezor/python-mnemonic/blob/b502451a33a440783926e04428115e0bed87d01f/vectors.json 22 149 : BOOST_AUTO_TEST_CASE(bip39_vectors) 23 : { 24 1 : UniValue tests = read_json(std::string(json_tests::bip39_vectors, json_tests::bip39_vectors + sizeof(json_tests::bip39_vectors))); 25 : 26 25 : for (unsigned int i = 0; i < tests.size(); i++) { 27 : // printf("%d\n", i); 28 24 : const UniValue& test = tests[i]; 29 24 : std::string strTest = test.write(); 30 24 : if (test.size() < 4) // Allow for extra stuff (useful for comments) 31 : { 32 0 : BOOST_ERROR("Bad test: " << strTest); 33 0 : continue; 34 : } 35 : 36 24 : std::vector<uint8_t> vData = ParseHex(test[0].get_str()); 37 24 : SecureVector data(vData.begin(), vData.end()); 38 : 39 24 : SecureString m = CMnemonic::FromData(data, data.size()); 40 24 : std::string strMnemonic = test[1].get_str(); 41 24 : SecureString mnemonic(strMnemonic.begin(), strMnemonic.end()); 42 : 43 : // printf("%s\n%s\n", m.c_str(), mnemonic.c_str()); 44 24 : BOOST_CHECK(m == mnemonic); 45 24 : BOOST_CHECK(CMnemonic::Check(mnemonic)); 46 : 47 24 : SecureVector seed; 48 24 : SecureString passphrase("TREZOR"); 49 24 : CMnemonic::ToSeed(mnemonic, passphrase, seed); 50 : // printf("seed: %s\n", HexStr(seed).c_str()); 51 24 : BOOST_CHECK(HexStr(seed) == test[2].get_str()); 52 : 53 24 : CExtKey key; 54 24 : CExtPubKey pubkey; 55 : 56 24 : key.SetSeed(MakeByteSpan(seed)); 57 24 : pubkey = key.Neuter(); 58 : 59 : // printf("CBitcoinExtKey: %s\n", EncodeExtKey(key).c_str()); 60 24 : BOOST_CHECK(EncodeExtKey(key) == test[3].get_str()); 61 24 : } 62 1 : } 63 : 64 146 : BOOST_AUTO_TEST_SUITE_END() 65 : } // namespace wallet