Bitcoin Core 30.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 <array>
9#include <cinttypes>
10#include <cstddef>
11#include <limits>
12#include <optional>
13#include <span>
14#include <string>
15#include <string_view>
16
24private:
26 static constexpr uint8_t KEY_TYPES_COUNT{6};
28 static constexpr size_t TOTAL_KEYS_GENERATED{std::numeric_limits<uint8_t>::max() + 1};
30 std::array<std::string, TOTAL_KEYS_GENERATED> keys_str;
31
32public:
33 // We derive the type of key to generate from the 1-byte id parsed from hex.
34 bool IdIsCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 0; }
35 bool IdIsUnCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 1; }
36 bool IdIsXOnlyPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 2; }
37 bool IdIsConstPrivKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 3; }
38 bool IdIsXpub(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 4; }
39 bool IdIsXprv(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 5; }
40
42 void Init();
43
45 std::optional<uint8_t> IdxFromHex(std::string_view hex_characters) const;
46
48 std::optional<std::string> GetDescriptor(std::string_view mocked_desc) const;
49};
50
52constexpr int MAX_DEPTH{2};
53
58bool HasDeepDerivPath(std::span<const uint8_t> buff, int max_depth = MAX_DEPTH);
59
61constexpr int MAX_SUBS{1'000};
63constexpr size_t MAX_NESTED_SUBS{10'000};
64
69bool HasTooManySubFrag(std::span<const uint8_t> buff, int max_subs = MAX_SUBS,
70 size_t max_nested_subs = MAX_NESTED_SUBS);
71
73constexpr int MAX_WRAPPERS{100};
74
79bool HasTooManyWrappers(std::span<const uint8_t> buff, int max_wrappers = MAX_WRAPPERS);
80
83constexpr uint32_t MAX_LEAF_SIZE{200};
84
87bool HasTooLargeLeafSize(std::span<const uint8_t> buff, uint32_t max_leaf_size = MAX_LEAF_SIZE);
88
89#endif // BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H
Converts a mocked descriptor string to a valid one.
Definition: descriptor.h:23
static constexpr uint8_t KEY_TYPES_COUNT
Types are raw (un)compressed pubkeys, raw xonly pubkeys, raw privkeys (WIF), xpubs,...
Definition: descriptor.h:26
bool IdIsUnCompPubKey(uint8_t idx) const
Definition: descriptor.h:35
bool IdIsXOnlyPubKey(uint8_t idx) const
Definition: descriptor.h:36
void Init()
When initializing the target, populate the list of keys.
Definition: descriptor.cpp:15
static constexpr size_t TOTAL_KEYS_GENERATED
How many keys we'll generate in total.
Definition: descriptor.h:28
bool IdIsXpub(uint8_t idx) const
Definition: descriptor.h:38
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:56
bool IdIsXprv(uint8_t idx) const
Definition: descriptor.h:39
bool IdIsCompPubKey(uint8_t idx) const
Definition: descriptor.h:34
std::array< std::string, TOTAL_KEYS_GENERATED > keys_str
256 keys of various types.
Definition: descriptor.h:30
bool IdIsConstPrivKey(uint8_t idx) const
Definition: descriptor.h:37
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:49
constexpr int MAX_SUBS
Default maximum number of sub-fragments.
Definition: descriptor.h:61
bool HasTooManyWrappers(std::span< const uint8_t > buff, int max_wrappers=MAX_WRAPPERS)
Whether the buffer, if it represents a valid descriptor, contains a fragment with more wrappers than ...
Definition: descriptor.cpp:120
constexpr int MAX_WRAPPERS
Default maximum number of wrappers per fragment.
Definition: descriptor.h:73
constexpr size_t MAX_NESTED_SUBS
Maximum number of nested sub-fragments we'll allow in a descriptor.
Definition: descriptor.h:63
constexpr int MAX_DEPTH
Default maximum number of derivation indexes in a single derivation path when limiting its depth.
Definition: descriptor.h:52
bool HasTooLargeLeafSize(std::span< const uint8_t > buff, uint32_t max_leaf_size=MAX_LEAF_SIZE)
Whether the expanded buffer (after calling GetDescriptor() in MockedDescriptorConverter) has a leaf s...
Definition: descriptor.cpp:152
bool HasDeepDerivPath(std::span< const uint8_t > buff, 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:82
bool HasTooManySubFrag(std::span< const uint8_t > buff, int max_subs=MAX_SUBS, 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:96
constexpr uint32_t MAX_LEAF_SIZE
Default maximum leaf size. This should be large enough to cover an extended key, including paths "/",...
Definition: descriptor.h:83