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 <key_io.h> 9 : : #include <util/strencodings.h> 10 : : #include <script/descriptor.h> 11 : : 12 : : #include <functional> 13 : : 14 : : /** 15 : : * Converts a mocked descriptor string to a valid one. Every key in a mocked descriptor key is 16 : : * represented by 2 hex characters preceded by the '%' character. We parse the two hex characters 17 : : * as an index in a list of pre-generated keys. This list contains keys of the various types 18 : : * accepted in descriptor keys expressions. 19 : : */ 20 : 0 : class MockedDescriptorConverter { 21 : : private: 22 : : //! Types are raw (un)compressed pubkeys, raw xonly pubkeys, raw privkeys (WIF), xpubs, xprvs. 23 : : static constexpr uint8_t KEY_TYPES_COUNT{6}; 24 : : //! How many keys we'll generate in total. 25 : : static constexpr size_t TOTAL_KEYS_GENERATED{std::numeric_limits<uint8_t>::max() + 1}; 26 : : //! 256 keys of various types. 27 : : std::array<std::string, TOTAL_KEYS_GENERATED> keys_str; 28 : : 29 : : public: 30 : : // We derive the type of key to generate from the 1-byte id parsed from hex. 31 : 0 : bool IdIsCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 0; } 32 : 0 : bool IdIsUnCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 1; } 33 : 0 : bool IdIsXOnlyPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 2; } 34 : 0 : bool IdIsConstPrivKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 3; } 35 : : bool IdIsXpub(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 4; } 36 : 0 : bool IdIsXprv(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 5; } 37 : : 38 : : //! When initializing the target, populate the list of keys. 39 : : void Init(); 40 : : 41 : : //! Parse an id in the keys vectors from a 2-characters hex string. 42 : : std::optional<uint8_t> IdxFromHex(std::string_view hex_characters) const; 43 : : 44 : : //! Get an actual descriptor string from a descriptor string whose keys were mocked. 45 : : std::optional<std::string> GetDescriptor(std::string_view mocked_desc) const; 46 : : }; 47 : : 48 : : #endif // BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H