Repository navigation
Expand file tree
/
Copy pathbip39_tests.cpp
More file actions
87 lines (69 loc) · 2.89 KB
/
Copy pathbip39_tests.cpp
File metadata and controls
87 lines (69 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2014-2025 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/data/bip39_vectors.json.h>
#include <wallet/bip39.h>
#include <wallet/bip39_english.h>
#include <key.h>
#include <key_io.h>
#include <test/util/json.h>
#include <test/util/setup_common.h>
#include <util/strencodings.h>
#include <boost/test/unit_test.hpp>
#include <univalue.h>
namespace wallet {
BOOST_FIXTURE_TEST_SUITE(bip39_tests, BasicTestingSetup)
// https://github.com/trezor/python-mnemonic/blob/b502451a33a440783926e04428115e0bed87d01f/vectors.json
BOOST_AUTO_TEST_CASE(bip39_vectors)
{
UniValue tests = read_json(json_tests::bip39_vectors);
for (unsigned int i = 0; i < tests.size(); i++) {
// printf("%d\n", i);
const UniValue& test = tests[i];
std::string strTest = test.write();
if (test.size() < 4) // Allow for extra stuff (useful for comments)
{
BOOST_ERROR("Bad test: " << strTest);
continue;
}
std::vector<uint8_t> vData = ParseHex(test[0].get_str());
SecureVector data(vData.begin(), vData.end());
SecureString m = CMnemonic::FromData(data, data.size());
std::string strMnemonic = test[1].get_str();
SecureString mnemonic(strMnemonic.begin(), strMnemonic.end());
// printf("%s\n%s\n", m.c_str(), mnemonic.c_str());
BOOST_CHECK(m == mnemonic);
BOOST_CHECK(CMnemonic::Check(mnemonic));
SecureVector seed;
SecureString passphrase("TREZOR");
CMnemonic::ToSeed(mnemonic, passphrase, seed);
// printf("seed: %s\n", HexStr(seed).c_str());
BOOST_CHECK(HexStr(seed) == test[2].get_str());
CExtKey key;
CExtPubKey pubkey;
key.SetSeed(MakeByteSpan(seed));
pubkey = key.Neuter();
// printf("CBitcoinExtKey: %s\n", EncodeExtKey(key).c_str());
BOOST_CHECK(EncodeExtKey(key) == test[3].get_str());
}
}
BOOST_AUTO_TEST_CASE(bip39_checksum_validation)
{
for (const int entropy_bytes : {16, 20, 24, 28, 32}) {
const SecureVector entropy(entropy_bytes, 0);
const SecureString mnemonic = CMnemonic::FromData(entropy, entropy_bytes);
BOOST_CHECK(CMnemonic::Check(mnemonic));
const size_t last_word_pos = mnemonic.rfind(' ') + 1;
uint32_t last_word_index{0};
while (mnemonic.substr(last_word_pos) != wordlist[last_word_index]) {
++last_word_index;
}
const int checksum_bits = entropy_bytes / 4;
for (int bit = 0; bit < checksum_bits; ++bit) {
const SecureString bad_checksum = mnemonic.substr(0, last_word_pos) + wordlist[last_word_index ^ (1 << bit)];
BOOST_CHECK_MESSAGE(!CMnemonic::Check(bad_checksum), bad_checksum);
}
}
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace wallet