Bitcoin Core  27.99.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 
58 #endif // BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H
Converts a mocked descriptor string to a valid one.
Definition: descriptor.h:21
static constexpr uint8_t KEY_TYPES_COUNT
Types are raw (un)compressed pubkeys, raw xonly pubkeys, raw privkeys (WIF), xpubs,...
Definition: descriptor.h:24
bool IdIsUnCompPubKey(uint8_t idx) const
Definition: descriptor.h:33
bool IdIsXOnlyPubKey(uint8_t idx) const
Definition: descriptor.h:34
void Init()
When initializing the target, populate the list of keys.
Definition: descriptor.cpp:7
static constexpr size_t TOTAL_KEYS_GENERATED
How many keys we'll generate in total.
Definition: descriptor.h:26
bool IdIsXpub(uint8_t idx) const
Definition: descriptor.h:36
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:48
bool IdIsXprv(uint8_t idx) const
Definition: descriptor.h:37
bool IdIsCompPubKey(uint8_t idx) const
Definition: descriptor.h:32
std::array< std::string, TOTAL_KEYS_GENERATED > keys_str
256 keys of various types.
Definition: descriptor.h:28
bool IdIsConstPrivKey(uint8_t idx) const
Definition: descriptor.h:35
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:41
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:98
constexpr int MAX_DEPTH
Default maximum number of derivation indexes in a single derivation path when limiting its depth.
Definition: descriptor.h:50
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:74