1#include <xrpld/rpc/detail/RPCHelpers.h>
3#include <xrpld/rpc/Context.h>
4#include <xrpld/rpc/DeliveredAmount.h>
5#include <xrpld/rpc/Role.h>
6#include <xrpld/rpc/Status.h>
7#include <xrpld/rpc/detail/Tuning.h>
9#include <xrpl/basics/Log.h>
10#include <xrpl/basics/Slice.h>
11#include <xrpl/basics/UnorderedContainers.h>
12#include <xrpl/basics/base_uint.h>
13#include <xrpl/basics/contract.h>
14#include <xrpl/beast/utility/Journal.h>
15#include <xrpl/beast/utility/instrumentation.h>
16#include <xrpl/core/ServiceRegistry.h>
17#include <xrpl/ledger/ReadView.h>
18#include <xrpl/protocol/AccountID.h>
19#include <xrpl/protocol/Asset.h>
20#include <xrpl/protocol/ErrorCodes.h>
21#include <xrpl/protocol/Indexes.h>
22#include <xrpl/protocol/Issue.h>
23#include <xrpl/protocol/KeyType.h>
24#include <xrpl/protocol/LedgerFormats.h>
25#include <xrpl/protocol/PublicKey.h>
26#include <xrpl/protocol/RPCErr.h>
27#include <xrpl/protocol/SField.h>
28#include <xrpl/protocol/SecretKey.h>
29#include <xrpl/protocol/Seed.h>
30#include <xrpl/protocol/UintTypes.h>
31#include <xrpl/protocol/jss.h>
32#include <xrpl/protocol/tokens.h>
34#include <boost/algorithm/string/predicate.hpp>
51 if (sle->getType() == ltRIPPLE_STATE)
53 if (sle->getFieldAmount(sfLowLimit).getIssuer() == accountID)
55 return sle->getFieldU64(sfLowNode);
57 if (sle->getFieldAmount(sfHighLimit).getIssuer() == accountID)
59 return sle->getFieldU64(sfHighNode);
63 if (!sle->isFieldPresent(sfOwnerNode))
66 return sle->getFieldU64(sfOwnerNode);
90 return std::ranges::contains(kOwnerDirNodeFields, &field);
104 return page && std::ranges::contains(page->getFieldV256(sfIndexes), sle->key());
108 return sle->isFieldPresent(*field) && pageContainsKey(sle->getFieldU64(*field));
116 for (
auto const& jv : jvArray)
132 for (
auto const& jv : jvArray)
137 auto const mptIssuanceIdStr = jv.asString();
139 if (!mptIssuanceID.
parseHex(mptIssuanceIdStr))
142 result.
insert(mptIssuanceID);
150 limit =
range.rDefault;
154 auto const& jvLimit = context.
params[jss::limit];
155 if (!jvLimit.isUInt() && (!jvLimit.isInt() || jvLimit.asInt() < 0))
158 limit = jvLimit.asUInt();
174 if (!value.isString())
192 static SeedMatchT
const kSeedTypes[]{
203 SeedMatchT
const* seedType =
nullptr;
205 for (
auto const& t : kSeedTypes)
217 "Exactly one of the following must be specified: " +
std::string(jss::passphrase) +
223 auto const& param = params[seedType->first];
224 if (!param.isString())
230 auto const fieldContents = param.
asString();
244 bool const hasKeyType = params.
isMember(jss::key_type);
247 static char const*
const kSecretTypes[]{
248 jss::passphrase.cStr(), jss::secret.cStr(), jss::seed.cStr(), jss::seed_hex.cStr()};
251 char const* secretType =
nullptr;
253 for (
auto t : kSecretTypes)
262 if (count == 0 || secretType ==
nullptr)
271 "Exactly one of the following must be specified: " +
std::string(jss::passphrase) +
282 if (!params[jss::key_type].isString())
305 if (strcmp(secretType, jss::secret.cStr()) == 0)
308 "The secret field is not allowed if " +
std::string(jss::key_type) +
" is used.");
318 if (strcmp(secretType, jss::seed_hex.cStr()) != 0)
347 if (!params[jss::secret].isString())
368 logicError(
"keypairForSignature: invalid key type");
379 static constexpr auto kTypes =
380 std::to_array<std::tuple<char const*, char const*, LedgerEntryType>>({
381#pragma push_macro("LEDGER_ENTRY")
384#define LEDGER_ENTRY(tag, value, name, rpcName, ...) {jss::name, jss::rpcName, tag},
386#include <xrpl/protocol/detail/ledger_entries.macro>
389#pragma pop_macro("LEDGER_ENTRY")
392 auto const& p = params[jss::type];
398 "xrpl::rpc::chooseLedgerEntryType : first valid result type");
407 return boost::iequals(std::get<0>(t), filter) || std::get<1>(t) == filter;
409 if (iter == kTypes.end())
414 "xrpl::rpc::chooseLedgerEntryType : second valid result "
418 result.
second = std::get<2>(*iter);
428 case LedgerEntryType::ltAMENDMENTS:
429 case LedgerEntryType::ltDIR_NODE:
430 case LedgerEntryType::ltFEE_SETTINGS:
431 case LedgerEntryType::ltLEDGER_HASHES:
432 case LedgerEntryType::ltNEGATIVE_UNL:
446 auto const& jv = params[name];
447 auto const [issuerError, assetError] = [&]() {
448 if (name == jss::taker_pays)
453 if (jv.isMember(jss::mpt_issuance_id) &&
454 (jv.isMember(jss::currency) || jv.isMember(jss::issuer)))
460 if (jv.isMember(jss::currency))
464 if (!jv.isMember(jss::currency) ||
472 if (((jv.isMember(jss::issuer)) &&
473 (!jv[jss::issuer].isString() || !
toIssuer(issue.
account, jv[jss::issuer].asString())))
482 else if (jv.isMember(jss::mpt_issuance_id))
485 if (!mptid.
parseHex(jv[jss::mpt_issuance_id].asString()))
A generic endpoint for log messages.
Lightweight wrapper to tag static string.
constexpr char const * cStr() const
bool isNull() const
isNull() tests to see if this field is null.
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
static constexpr std::size_t size()
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
A currency issued by an account.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
std::shared_ptr< STLedgerEntry const > const & ConstRef
Seeds are used to generate deterministic secret keys.
An immutable linear range of bytes.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet page(UInt256 const &root, std::uint64_t const index=0) noexcept
A page in a directory.
API version numbers used in later API versions.
json::Value expectedFieldError(std::string const &name, std::string const &type)
bool containsError(json::Value const &json)
Returns true if the json contains an rpc error specification.
std::pair< rpc::Status, LedgerEntryType > chooseLedgerEntryType(json::Value const ¶ms)
Chooses the ledger entry type based on RPC parameters.
bool isOwnerDirNodeField(SField const &field)
Checks whether an SField is a UINT64 sf*Node owner-directory page-hint field probed by isRelatedToAcc...
std::uint64_t getStartHint(SLE::ConstRef sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
std::string invalidFieldMessage(std::string const &name)
json::Value makeParamError(std::string const &message)
Returns a new json object that indicates invalid parameters.
json::Value invalidFieldError(std::string const &name)
HashSet< MPTID > parseMPTIssuanceIDs(json::Value const &jvArray)
Parses an array of MPT issuance IDs from a JSON value.
ErrorCodeI parseSubUnsubJson(Asset &asset, json::Value const ¶ms, json::StaticString const &name, beast::Journal j)
Parse subscribe/unsubscribe parameters.
std::optional< json::Value > readLimitField(unsigned int &limit, tuning::LimitRange const &range, JsonContext const &context)
Retrieves the limit value from a JsonContext or sets a default.
HashSet< AccountID > parseAccountIds(json::Value const &jvArray)
Parses an array of account IDs from a JSON value.
json::Value missingFieldError(std::string const &name)
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
bool isRelatedToAccount(ReadView const &ledger, SLE::ConstRef sle, AccountID const &accountID)
Tests if a ledger entry (SLE) is owned by the specified account.
std::optional< Seed > getSeedFromRPC(json::Value const ¶ms, json::Value &error)
Extracts a Seed from RPC parameters.
std::optional< Seed > parseXrplLibSeed(json::Value const &value)
Parses a XrplLib seed from RPC parameters.
std::optional< std::pair< PublicKey, SecretKey > > keypairForSignature(json::Value const ¶ms, json::Value &error, unsigned int apiVersion)
Generates a keypair for signature from RPC parameters.
bool isAccountObjectsValidType(LedgerEntryType const &type)
Checks if the type is a valid filtering type for the account_objects method.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
std::optional< KeyType > keyTypeFromString(std::string const &s)
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Slice makeSlice(std::array< T, N > const &a)
json::Value rpcError(ErrorCodeI iError)
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
std::unordered_set< Value, Hash, Pred, Allocator > HashSet
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
AccountID const & noAccount()
A placeholder for empty accounts.
LedgerEntryType
Identifiers for on-ledger objects.
@ ltANY
A special type, matching any ledger entry type.
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
std::string decodeBase58Token(std::string const &s, TokenType type)
bool toIssuer(AccountID &, std::string const &)
Convert hex or base58 string to AccountID.
std::optional< Seed > parseGenericSeed(std::string const &str, bool rfc1751=true)
Attempt to parse a string as a seed.
Status represents the results of an operation that might fail.
static constexpr Code kOK
Represents RPC limit parameter values that have a min, default and max.