rippled
RPCHelpers.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012=2014 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_RPC_RPCHELPERS_H_INCLUDED
21 #define RIPPLE_RPC_RPCHELPERS_H_INCLUDED
22 
23 #include <ripple/beast/core/SemanticVersion.h>
24 #include <ripple/proto/org/xrpl/rpc/v1/xrp_ledger.pb.h>
25 #include <ripple/protocol/TxMeta.h>
26 
27 #include <ripple/app/misc/NetworkOPs.h>
28 #include <ripple/app/misc/TxQ.h>
29 #include <ripple/protocol/SecretKey.h>
30 #include <ripple/rpc/Context.h>
31 #include <ripple/rpc/Status.h>
32 #include <ripple/rpc/impl/Tuning.h>
33 #include <optional>
34 #include <variant>
35 
36 namespace Json {
37 class Value;
38 }
39 
40 namespace ripple {
41 
42 class ReadView;
43 class Transaction;
44 
45 namespace RPC {
46 
47 struct JsonContext;
48 
52 
53 // --> strIdent: public key, account ID, or regular seed.
54 // --> bStrict: Only allow account id or public key.
55 //
56 // Returns a Json::objectValue, containing error information if there was one.
59  AccountID& result,
60  std::string const& strIdent,
61  bool bStrict = false);
62 
71  AccountID& result,
72  std::string const& strIdent,
73  bool bStrict = false);
74 
80 getStartHint(std::shared_ptr<SLE const> const& sle, AccountID const& accountID);
81 
88 bool
90  ReadView const& ledger,
91  std::shared_ptr<SLE const> const& sle,
92  AccountID const& accountID);
93 
103 bool
105  ReadView const& ledger,
106  AccountID const& account,
107  std::optional<std::vector<LedgerEntryType>> const& typeFilter,
108  uint256 dirIndex,
109  uint256 entryIndex,
110  std::uint32_t const limit,
111  Json::Value& jvResult);
112 
117 template <class T>
118 Status
119 getLedger(T& ledger, uint256 const& ledgerHash, Context& context);
120 
125 template <class T>
126 Status
127 getLedger(T& ledger, uint32_t ledgerIndex, Context& context);
128 
134 template <class T>
135 Status
136 getLedger(T& ledger, LedgerShortcut shortcut, Context& context);
137 
146 
152 Status
155  JsonContext&,
156  Json::Value& result);
157 
158 template <class T, class R>
159 Status
160 ledgerFromRequest(T& ledger, GRPCContext<R>& context);
161 
162 template <class T>
163 Status
165  T& ledger,
166  org::xrpl::rpc::v1::LedgerSpecifier const& specifier,
167  Context& context);
168 
170 parseAccountIds(Json::Value const& jvArray);
171 
172 bool
173 isHexTxID(std::string const& txid);
174 
183 void
184 injectSLE(Json::Value& jv, SLE const& sle);
185 
193  unsigned int& limit,
194  Tuning::LimitRange const&,
195  JsonContext const&);
196 
198 getSeedFromRPC(Json::Value const& params, Json::Value& error);
199 
201 parseRippleLibSeed(Json::Value const& params);
202 
209 
232 constexpr unsigned int apiInvalidVersion = 0;
233 constexpr unsigned int apiVersionIfUnspecified = 1;
234 constexpr unsigned int apiMinimumSupportedVersion = 1;
235 constexpr unsigned int apiMaximumSupportedVersion = 2;
236 constexpr unsigned int apiCommandLineVersion = 1; // TODO Bump to 2 later
237 constexpr unsigned int apiBetaVersion = 3;
238 constexpr unsigned int apiMaximumValidVersion = apiBetaVersion;
239 
244 
245 template <class Object>
246 void
247 setVersion(Object& parent, unsigned int apiVersion, bool betaEnabled)
248 {
249  assert(apiVersion != apiInvalidVersion);
250  auto&& object = addObject(parent, jss::version);
251  if (apiVersion == apiVersionIfUnspecified)
252  {
253  object[jss::first] = firstVersion.print();
254  object[jss::good] = goodVersion.print();
255  object[jss::last] = lastVersion.print();
256  }
257  else
258  {
259  object[jss::first] = apiMinimumSupportedVersion;
260  object[jss::last] =
262  }
263 }
264 
266 chooseLedgerEntryType(Json::Value const& params);
267 
282 unsigned int
283 getAPIVersionNumber(const Json::Value& value, bool betaEnabled);
284 
289 
292  Json::Value const& params,
293  Json::Value& error,
294  unsigned int apiVersion = apiVersionIfUnspecified);
295 } // namespace RPC
296 } // namespace ripple
297 
298 #endif
ripple::RPC::apiInvalidVersion
constexpr unsigned int apiInvalidVersion
API version numbers used in later API versions.
Definition: RPCHelpers.h:232
ripple::RPC::apiVersionIfUnspecified
constexpr unsigned int apiVersionIfUnspecified
Definition: RPCHelpers.h:233
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::RPC::CURRENT
@ CURRENT
Definition: RPCHelpers.h:129
ripple::STLedgerEntry
Definition: STLedgerEntry.h:30
ripple::RPC::firstVersion
const beast::SemanticVersion firstVersion("1.0.0")
API version numbers used in API version 1.
Definition: RPCHelpers.h:206
ripple::RPC::goodVersion
const beast::SemanticVersion goodVersion("1.0.0")
Definition: RPCHelpers.h:207
std::string
STL class.
std::shared_ptr
STL class.
ripple::RPC::isHexTxID
bool isHexTxID(std::string const &txid)
std::unordered_set
STL class.
ripple::RPC::keypairForSignature
std::optional< std::pair< PublicKey, SecretKey > > keypairForSignature(Json::Value const &params, Json::Value &error, unsigned int apiVersion)
Definition: RPCHelpers.cpp:796
ripple::RPC::injectSLE
void injectSLE(Json::Value &jv, SLE const &sle)
Inject JSON describing ledger entry.
Definition: RPCHelpers.cpp:674
std::pair
ripple::RPC::LedgerShortcut
LedgerShortcut
Definition: RPCHelpers.h:129
ripple::RPC::getAccountObjects
bool getAccountObjects(ReadView const &ledger, AccountID const &account, std::optional< std::vector< LedgerEntryType >> const &typeFilter, uint256 dirIndex, uint256 entryIndex, std::uint32_t const limit, Json::Value &jvResult)
Gathers all objects for an account in a ledger.
Definition: RPCHelpers.cpp:152
ripple::RPC::getAPIVersionNumber
unsigned int getAPIVersionNumber(Json::Value const &jv, bool betaEnabled)
Retrieve the api version number from the json value.
Definition: RPCHelpers.cpp:993
std::vector
STL class.
beast::SemanticVersion::print
std::string print() const
Produce a string from semantic version components.
Definition: SemanticVersion.cpp:236
ripple::RPC::readLimitField
std::optional< Json::Value > readLimitField(unsigned int &limit, Tuning::LimitRange const &range, JsonContext const &context)
Retrieve the limit value from a JsonContext, or set a default - then restrict the limit by max and mi...
Definition: RPCHelpers.cpp:699
beast::SemanticVersion
A Semantic Version number.
Definition: SemanticVersion.h:35
ripple::RPC::apiCommandLineVersion
constexpr unsigned int apiCommandLineVersion
Definition: RPCHelpers.h:236
ripple::RPC::getLedgerByContext
std::variant< std::shared_ptr< Ledger const >, Json::Value > getLedgerByContext(RPC::JsonContext &context)
Return a ledger based on ledger_hash or ledger_index, or an RPC error.
Definition: RPCHelpers.cpp:1014
ripple::RPC::apiBetaVersion
constexpr unsigned int apiBetaVersion
Definition: RPCHelpers.h:237
ripple::RPC::lookupLedger
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext &context, Json::Value &result)
Look up a ledger from a request and fill a Json::Result with the data representing a ledger.
Definition: RPCHelpers.cpp:623
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::RPC::parseRippleLibSeed
std::optional< Seed > parseRippleLibSeed(Json::Value const &value)
Definition: RPCHelpers.cpp:718
Json
JSON (JavaScript Object Notation).
Definition: DeliverMax.h:28
ripple::RPC::accountFromStringWithCode
error_code_i accountFromStringWithCode(AccountID &result, std::string const &strIdent, bool bStrict)
Decode account ID from string.
Definition: RPCHelpers.cpp:59
ripple::RPC::VALIDATED
@ VALIDATED
Definition: RPCHelpers.h:129
ripple::RPC::CLOSED
@ CLOSED
Definition: RPCHelpers.h:129
std::uint64_t
ripple::NodeStore::Status
Status
Return codes from Backend operations.
Definition: nodestore/Types.h:44
ripple::RPC::apiMinimumSupportedVersion
constexpr unsigned int apiMinimumSupportedVersion
Definition: RPCHelpers.h:234
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RPC::getLedger
Status getLedger(T &ledger, uint256 const &ledgerHash, Context &context)
Get ledger by hash If there is no error in the return value, the ledger pointer will have been filled...
Definition: RPCHelpers.cpp:480
ripple::RPC::apiMaximumValidVersion
constexpr unsigned int apiMaximumValidVersion
Definition: RPCHelpers.h:238
ripple::RPC::chooseLedgerEntryType
std::pair< RPC::Status, LedgerEntryType > chooseLedgerEntryType(Json::Value const &params)
Definition: RPCHelpers.cpp:932
ripple::RPC::getStartHint
std::uint64_t getStartHint(std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
Definition: RPCHelpers.cpp:97
ripple::RPC::getSeedFromRPC
std::optional< Seed > getSeedFromRPC(Json::Value const &params, Json::Value &error)
Definition: RPCHelpers.cpp:737
ripple::RPC::apiMaximumSupportedVersion
constexpr unsigned int apiMaximumSupportedVersion
Definition: RPCHelpers.h:235
ripple::RPC::ledgerFromSpecifier
Status ledgerFromSpecifier(T &ledger, org::xrpl::rpc::v1::LedgerSpecifier const &specifier, Context &context)
Definition: RPCHelpers.cpp:421
optional
ripple::RPC::parseAccountIds
hash_set< AccountID > parseAccountIds(Json::Value const &jvArray)
Definition: RPCHelpers.cpp:658
ripple::RPC::accountFromStringStrict
std::optional< AccountID > accountFromStringStrict(std::string const &account)
Get an AccountID from an account ID or public key.
Definition: RPCHelpers.cpp:43
ripple::RPC::lastVersion
const beast::SemanticVersion lastVersion("1.0.0")
Definition: RPCHelpers.h:208
ripple::RPC::ledgerFromRequest
Status ledgerFromRequest(T &ledger, GRPCContext< R > &context)
Definition: RPCHelpers.cpp:395
ripple::RPC::accountFromString
Json::Value accountFromString(AccountID &result, std::string const &strIdent, bool bStrict)
Definition: RPCHelpers.cpp:87
ripple::RPC::isRelatedToAccount
bool isRelatedToAccount(ReadView const &ledger, std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Tests if a SLE is owned by accountID.
Definition: RPCHelpers.cpp:114
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::RPC::setVersion
void setVersion(Object &parent, unsigned int apiVersion, bool betaEnabled)
Definition: RPCHelpers.h:247
variant