Bitcoin Core  28.1.0
P2P Digital Currency
descriptor.h
Go to the documentation of this file.
1 // Copyright (c) 2023-present The Bitcoin 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 #ifndef BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H
6 #define BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H
7 
8 #include <key_io.h>
9 #include <util/strencodings.h>
10 #include <script/descriptor.h>
11 #include <test/fuzz/fuzz.h>
12 
13 #include <functional>
14 
22 private:
24  static constexpr uint8_t KEY_TYPES_COUNT{6};
26  static constexpr size_t TOTAL_KEYS_GENERATED{std::numeric_limits<uint8_t>::max() + 1};
28  std::array<std::string, TOTAL_KEYS_GENERATED> keys_str;
29 
30 public:
31  // We derive the type of key to generate from the 1-byte id parsed from hex.
32  bool IdIsCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 0; }
33  bool IdIsUnCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 1; }
34  bool IdIsXOnlyPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 2; }
35  bool IdIsConstPrivKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 3; }
36  bool IdIsXpub(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 4; }
37  bool IdIsXprv(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 5; }
38 
40  void Init();
41 
43  std::optional<uint8_t> IdxFromHex(std::string_view hex_characters) const;
44 
46  std::optional<std::string> GetDescriptor(std::string_view mocked_desc) const;
47 };
48 
50 constexpr int MAX_DEPTH{2};
51 
56 bool HasDeepDerivPath(const FuzzBufferType& buff, const int max_depth = MAX_DEPTH);
57 
59 constexpr int MAX_SUBS{1'000};
61 constexpr size_t MAX_NESTED_SUBS{10'000};
62 
67 bool HasTooManySubFrag(const FuzzBufferType& buff, const int max_subs = MAX_SUBS,
68  const size_t max_nested_subs = MAX_NESTED_SUBS);
69 
71 constexpr int MAX_WRAPPERS{100};
72 
77 bool HasTooManyWrappers(const FuzzBufferType& buff, const int max_wrappers = MAX_WRAPPERS);
78 
79 #endif // BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H
std::optional< uint8_t > IdxFromHex(std::string_view hex_characters) const
Parse an id in the keys vectors from a 2-characters hex string.
Definition: descriptor.cpp:44
constexpr size_t MAX_NESTED_SUBS
Maximum number of nested sub-fragments we&#39;ll allow in a descriptor.
Definition: descriptor.h:61
bool HasTooManyWrappers(const FuzzBufferType &buff, const int max_wrappers=MAX_WRAPPERS)
Whether the buffer, if it represents a valid descriptor, contains a fragment with more wrappers than ...
Definition: descriptor.cpp:115
constexpr int MAX_DEPTH
Default maximum number of derivation indexes in a single derivation path when limiting its depth...
Definition: descriptor.h:50
static constexpr size_t TOTAL_KEYS_GENERATED
How many keys we&#39;ll generate in total.
Definition: descriptor.h:26
std::array< std::string, TOTAL_KEYS_GENERATED > keys_str
256 keys of various types.
Definition: descriptor.h:28
bool HasDeepDerivPath(const FuzzBufferType &buff, const int max_depth=MAX_DEPTH)
Whether the buffer, if it represents a valid descriptor, contains a derivation path deeper than a giv...
Definition: descriptor.cpp:77
bool IdIsXpub(uint8_t idx) const
Definition: descriptor.h:36
bool IdIsXprv(uint8_t idx) const
Definition: descriptor.h:37
bool IdIsCompPubKey(uint8_t idx) const
Definition: descriptor.h:32
bool HasTooManySubFrag(const FuzzBufferType &buff, const int max_subs=MAX_SUBS, const size_t max_nested_subs=MAX_NESTED_SUBS)
Whether the buffer, if it represents a valid descriptor, contains a fragment with more sub-fragments ...
Definition: descriptor.cpp:91
bool IdIsXOnlyPubKey(uint8_t idx) const
Definition: descriptor.h:34
std::span< const uint8_t > FuzzBufferType
Definition: fuzz.h:25
void Init()
When initializing the target, populate the list of keys.
Definition: descriptor.cpp:10
std::optional< std::string > GetDescriptor(std::string_view mocked_desc) const
Get an actual descriptor string from a descriptor string whose keys were mocked.
Definition: descriptor.cpp:51
Converts a mocked descriptor string to a valid one.
Definition: descriptor.h:21
constexpr int MAX_WRAPPERS
Default maximum number of wrappers per fragment.
Definition: descriptor.h:71
static constexpr uint8_t KEY_TYPES_COUNT
Types are raw (un)compressed pubkeys, raw xonly pubkeys, raw privkeys (WIF), xpubs, xprvs.
Definition: descriptor.h:24
bool IdIsConstPrivKey(uint8_t idx) const
Definition: descriptor.h:35
bool IdIsUnCompPubKey(uint8_t idx) const
Definition: descriptor.h:33
constexpr int MAX_SUBS
Default maximum number of sub-fragments.
Definition: descriptor.h:59