|
7 | 7 | #include <pubkey.h>
|
8 | 8 | #include <script/descriptor.h>
|
9 | 9 | #include <test/fuzz/fuzz.h>
|
| 10 | +#include <test/fuzz/util/descriptor.h> |
10 | 11 | #include <util/chaintype.h>
|
11 | 12 | #include <util/strencodings.h>
|
12 | 13 |
|
13 |
| -//! Types are raw (un)compressed pubkeys, raw xonly pubkeys, raw privkeys (WIF), xpubs, xprvs. |
14 |
| -static constexpr uint8_t KEY_TYPES_COUNT{6}; |
15 |
| -//! How many keys we'll generate in total. |
16 |
| -static constexpr size_t TOTAL_KEYS_GENERATED{std::numeric_limits<uint8_t>::max() + 1}; |
17 |
| - |
18 |
| -/** |
19 |
| - * Converts a mocked descriptor string to a valid one. Every key in a mocked descriptor key is |
20 |
| - * represented by 2 hex characters preceded by the '%' character. We parse the two hex characters |
21 |
| - * as an index in a list of pre-generated keys. This list contains keys of the various types |
22 |
| - * accepted in descriptor keys expressions. |
23 |
| - */ |
24 |
| -class MockedDescriptorConverter { |
25 |
| - //! 256 keys of various types. |
26 |
| - std::array<std::string, TOTAL_KEYS_GENERATED> keys_str; |
27 |
| - |
28 |
| -public: |
29 |
| - // We derive the type of key to generate from the 1-byte id parsed from hex. |
30 |
| - bool IdIsCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 0; } |
31 |
| - bool IdIsUnCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 1; } |
32 |
| - bool IdIsXOnlyPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 2; } |
33 |
| - bool IdIsConstPrivKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 3; } |
34 |
| - bool IdIsXpub(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 4; } |
35 |
| - bool IdIsXprv(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 5; } |
36 |
| - |
37 |
| - //! When initializing the target, populate the list of keys. |
38 |
| - void Init() { |
39 |
| - // The data to use as a private key or a seed for an xprv. |
40 |
| - std::array<std::byte, 32> key_data{std::byte{1}}; |
41 |
| - // Generate keys of all kinds and store them in the keys array. |
42 |
| - for (size_t i{0}; i < TOTAL_KEYS_GENERATED; i++) { |
43 |
| - key_data[31] = std::byte(i); |
44 |
| - |
45 |
| - // If this is a "raw" key, generate a normal privkey. Otherwise generate |
46 |
| - // an extended one. |
47 |
| - if (IdIsCompPubKey(i) || IdIsUnCompPubKey(i) || IdIsXOnlyPubKey(i) || IdIsConstPrivKey(i)) { |
48 |
| - CKey privkey; |
49 |
| - privkey.Set(UCharCast(key_data.begin()), UCharCast(key_data.end()), !IdIsUnCompPubKey(i)); |
50 |
| - if (IdIsCompPubKey(i) || IdIsUnCompPubKey(i)) { |
51 |
| - CPubKey pubkey{privkey.GetPubKey()}; |
52 |
| - keys_str[i] = HexStr(pubkey); |
53 |
| - } else if (IdIsXOnlyPubKey(i)) { |
54 |
| - const XOnlyPubKey pubkey{privkey.GetPubKey()}; |
55 |
| - keys_str[i] = HexStr(pubkey); |
56 |
| - } else { |
57 |
| - keys_str[i] = EncodeSecret(privkey); |
58 |
| - } |
59 |
| - } else { |
60 |
| - CExtKey ext_privkey; |
61 |
| - ext_privkey.SetSeed(key_data); |
62 |
| - if (IdIsXprv(i)) { |
63 |
| - keys_str[i] = EncodeExtKey(ext_privkey); |
64 |
| - } else { |
65 |
| - const CExtPubKey ext_pubkey{ext_privkey.Neuter()}; |
66 |
| - keys_str[i] = EncodeExtPubKey(ext_pubkey); |
67 |
| - } |
68 |
| - } |
69 |
| - } |
70 |
| - } |
71 |
| - |
72 |
| - //! Parse an id in the keys vectors from a 2-characters hex string. |
73 |
| - std::optional<uint8_t> IdxFromHex(std::string_view hex_characters) const { |
74 |
| - if (hex_characters.size() != 2) return {}; |
75 |
| - auto idx = ParseHex(hex_characters); |
76 |
| - if (idx.size() != 1) return {}; |
77 |
| - return idx[0]; |
78 |
| - } |
79 |
| - |
80 |
| - //! Get an actual descriptor string from a descriptor string whose keys were mocked. |
81 |
| - std::optional<std::string> GetDescriptor(std::string_view mocked_desc) const { |
82 |
| - // The smallest fragment would be "pk(%00)" |
83 |
| - if (mocked_desc.size() < 7) return {}; |
84 |
| - |
85 |
| - // The actual descriptor string to be returned. |
86 |
| - std::string desc; |
87 |
| - desc.reserve(mocked_desc.size()); |
88 |
| - |
89 |
| - // Replace all occurrences of '%' followed by two hex characters with the corresponding key. |
90 |
| - for (size_t i = 0; i < mocked_desc.size();) { |
91 |
| - if (mocked_desc[i] == '%') { |
92 |
| - if (i + 3 >= mocked_desc.size()) return {}; |
93 |
| - if (const auto idx = IdxFromHex(mocked_desc.substr(i + 1, 2))) { |
94 |
| - desc += keys_str[*idx]; |
95 |
| - i += 3; |
96 |
| - } else { |
97 |
| - return {}; |
98 |
| - } |
99 |
| - } else { |
100 |
| - desc += mocked_desc[i++]; |
101 |
| - } |
102 |
| - } |
103 |
| - |
104 |
| - return desc; |
105 |
| - } |
106 |
| -}; |
107 |
| - |
108 | 14 | //! The converter of mocked descriptors, needs to be initialized when the target is.
|
109 | 15 | MockedDescriptorConverter MOCKED_DESC_CONVERTER;
|
110 | 16 |
|
|
0 commit comments