Branch data Line data Source code
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 <functional> 9 : : #include <key_io.h> 10 : : #include <util/strencodings.h> 11 : : #include <script/descriptor.h> 12 : : 13 : : /** 14 : : * Converts a mocked descriptor string to a valid one. Every key in a mocked descriptor key is 15 : : * represented by 2 hex characters preceded by the '%' character. We parse the two hex characters 16 : : * as an index in a list of pre-generated keys. This list contains keys of the various types 17 : : * accepted in descriptor keys expressions. 18 : : */ 19 : 0 : class MockedDescriptorConverter { 20 : : private: 21 : : //! Types are raw (un)compressed pubkeys, raw xonly pubkeys, raw privkeys (WIF), xpubs, xprvs. 22 : : static constexpr uint8_t KEY_TYPES_COUNT{6}; 23 : : //! How many keys we'll generate in total. 24 : : static constexpr size_t TOTAL_KEYS_GENERATED{std::numeric_limits<uint8_t>::max() + 1}; 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 : 428 : bool IdIsCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 0; } 31 : 514 : bool IdIsUnCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 1; } 32 : 256 : bool IdIsXOnlyPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 2; } 33 : 127 : 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 : 84 : 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 : : 40 : : //! Parse an id in the keys vectors from a 2-characters hex string. 41 : : std::optional<uint8_t> IdxFromHex(std::string_view hex_characters) const; 42 : : 43 : : //! Get an actual descriptor string from a descriptor string whose keys were mocked. 44 : : std::optional<std::string> GetDescriptor(std::string_view mocked_desc) const; 45 : : }; 46 : : 47 : : #endif // BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H