1 // Copyright (c) 2014-2020 Daniel Kraft 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_RPC_NAMES_H 6 #define BITCOIN_RPC_NAMES_H 7 8 #include <script/script.h> 9 10 #include <names/encoding.h> 11 #include <rpc/util.h> 12 13 #include <string> 14 #include <vector> 15 16 /** Default value for the -allowexpired argument. */ 17 static constexpr bool DEFAULT_ALLOWEXPIRED = false; 18 19 class CNameData; 20 class COutPoint; 21 class CScript; 22 class UniValue; 23 24 UniValue getNameInfo (const UniValue& options, 25 const valtype& name, const valtype& value, 26 const COutPoint& outp, const CScript& addr); 27 UniValue getNameInfo (const UniValue& options, 28 const valtype& name, const CNameData& data); 29 void addExpirationInfo (int height, UniValue& data); 30 31 #ifdef ENABLE_WALLET 32 class CWallet; 33 void addOwnershipInfo (const CScript& addr, 34 const CWallet* pwallet, 35 UniValue& data); 36 #endif 37 38 /** 39 * Decodes a name given through the RPC interface and throws a 40 * JSONRPCError if it is invalid for the requested encoding. The encoding 41 * is extracted from the options object if it is there with the "nameEncoding" 42 * key, or else the configured default name encoding it used. 43 */ 44 valtype DecodeNameFromRPCOrThrow (const UniValue& val, const UniValue& opt); 45 46 /** 47 * Decodes a value given through the RPC interface and throws an error if it 48 * is invalid. This is the same as DecodeNameFromRPCOrThrow, except that it 49 * extracts the "valueEncoding" from the options and uses the default encoding 50 * for values instead of names. 51 */ 52 valtype DecodeValueFromRPCOrThrow (const UniValue& val, const UniValue& opt); 53 54 /** 55 * Builder class for the RPC results for methods that return information about 56 * names (like name_show, name_scan, name_pending or name_list). Since the 57 * exact fields contained depend on the case, this class 58 * provides a simple and fluent interface to build the right help text for 59 * each case. 60 */ 61 class NameInfoHelp 62 { 63 64 private: 65 66 /** Result fields that have already been added. */ 67 std::vector<RPCResult> fields; 68 69 public: 70 71 explicit NameInfoHelp (); 72 73 NameInfoHelp& withExpiration (); 74 75 /** 76 * Adds a new field for the result. 77 */ 78 NameInfoHelp& withField(const RPCResult & field)79 withField (const RPCResult& field) 80 { 81 fields.push_back (field); 82 return *this; 83 } 84 85 /** 86 * Constructs the final RPCResult for all fields added. 87 */ 88 RPCResult finish()89 finish () 90 { 91 return RPCResult (RPCResult::Type::OBJ, "", "", fields); 92 } 93 94 }; 95 96 /** 97 * Builder class for the help of the "options" argument for name RPCs. 98 */ 99 class NameOptionsHelp 100 { 101 102 private: 103 104 /** Inner RPCArgs for RPCHelpMan. */ 105 std::vector<RPCArg> innerArgs; 106 107 public: 108 109 NameOptionsHelp (); 110 111 /** 112 * Adds the options for write-type RPCs (e.g. name_update). 113 */ 114 NameOptionsHelp& withWriteOptions (); 115 116 NameOptionsHelp& withNameEncoding (); 117 NameOptionsHelp& withValueEncoding (); 118 NameOptionsHelp& withByHash (); 119 120 /** 121 * Variant of withField that also adds the innerArgs field correctly. 122 */ 123 NameOptionsHelp& withArg (const std::string& name, RPCArg::Type type, 124 const std::string& doc); 125 126 /** 127 * Adds a new inner argument with a default value. 128 */ 129 NameOptionsHelp& withArg (const std::string& name, RPCArg::Type type, 130 const std::string& defaultValue, 131 const std::string& doc); 132 133 /** 134 * Constructs the RPCArg object for the options argument described by this 135 * builder instance. 136 */ 137 RPCArg buildRpcArg () const; 138 139 }; 140 141 #endif // BITCOIN_RPC_NAMES_H 142