Bitcoin Core 28.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
22private:
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
30public:
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
50constexpr int MAX_DEPTH{2};
51
56bool HasDeepDerivPath(const FuzzBufferType& buff, const int max_depth = MAX_DEPTH);
57
59constexpr int MAX_SUBS{1'000};
61constexpr size_t MAX_NESTED_SUBS{10'000};
62
67bool HasTooManySubFrag(const FuzzBufferType& buff, const int max_subs = MAX_SUBS,
68 const size_t max_nested_subs = MAX_NESTED_SUBS);
69
71constexpr int MAX_WRAPPERS{100};
72
77bool HasTooManyWrappers(const FuzzBufferType& buff, const int max_wrappers = MAX_WRAPPERS);
78
79#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:10
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:51
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:44
std::span< const uint8_t > FuzzBufferType
Definition: fuzz.h:25
constexpr int MAX_SUBS
Default maximum number of sub-fragments.
Definition: descriptor.h:59
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
constexpr int MAX_WRAPPERS
Default maximum number of wrappers per fragment.
Definition: descriptor.h:71
constexpr size_t MAX_NESTED_SUBS
Maximum number of nested sub-fragments we'll allow in a descriptor.
Definition: descriptor.h:61
constexpr int MAX_DEPTH
Default maximum number of derivation indexes in a single derivation path when limiting its depth.
Definition: descriptor.h:50
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
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