20 #include <ripple/app/ledger/LedgerMaster.h>
21 #include <ripple/app/ledger/LedgerToJson.h>
22 #include <ripple/app/ledger/OpenLedger.h>
23 #include <ripple/app/misc/Transaction.h>
24 #include <ripple/app/paths/TrustLine.h>
25 #include <ripple/app/rdb/RelationalDatabase.h>
26 #include <ripple/app/tx/impl/details/NFTokenUtils.h>
27 #include <ripple/ledger/View.h>
28 #include <ripple/net/RPCErr.h>
29 #include <ripple/protocol/AccountID.h>
30 #include <ripple/protocol/Feature.h>
31 #include <ripple/protocol/nftPageMask.h>
32 #include <ripple/resource/Fees.h>
33 #include <ripple/rpc/Context.h>
34 #include <ripple/rpc/DeliveredAmount.h>
35 #include <ripple/rpc/impl/RPCHelpers.h>
36 #include <boost/algorithm/string/case_conv.hpp>
46 auto const publicKey =
52 result = parseBase58<AccountID>(account);
100 if (sle->getFieldAmount(
sfLowLimit).getIssuer() == accountID)
102 else if (sle->getFieldAmount(
sfHighLimit).getIssuer() == accountID)
120 return (sle->getFieldAmount(
sfLowLimit).getIssuer() == accountID) ||
121 (sle->getFieldAmount(
sfHighLimit).getIssuer() == accountID);
131 return sle->getAccountID(
sfAccount) == accountID ||
138 return sle->key() == accountSignerList.
key;
144 return sle->getAccountID(
sfOwner) == accountID;
163 return it != typeFilter.
end();
168 bool iterateNFTPages =
169 (!typeFilter.has_value() ||
171 dirIndex == beast::zero;
176 if (iterateNFTPages && entryIndex != beast::zero)
182 iterateNFTPages =
false;
189 uint32_t mlimit = limit;
194 Keylet const first = entryIndex == beast::zero
219 jvResult[jss::limit] = limit;
235 entryIndex = beast::zero;
252 return mlimit < limit;
258 auto const& entries = dir->getFieldV256(
sfIndexes);
259 auto iter = entries.begin();
263 iter =
std::find(iter, entries.end(), entryIndex);
264 if (iter == entries.end())
272 if (i == mlimit && mlimit < limit)
274 jvResult[jss::limit] = limit;
275 jvResult[jss::marker] =
280 for (; iter != entries.end(); ++iter)
284 if (!typeFilter.has_value() ||
285 typeMatchesFilter(typeFilter.value(), sleNode->getType()))
292 if (++iter != entries.end())
294 jvResult[jss::limit] = limit;
295 jvResult[jss::marker] =
304 auto const nodeIndex = dir->getFieldU64(
sfIndexNext);
315 auto const& e = dir->getFieldV256(
sfIndexes);
318 jvResult[jss::limit] = limit;
319 jvResult[jss::marker] =
331 isValidatedOld(
LedgerMaster& ledgerMaster,
bool standaloneOrReporting)
333 if (standaloneOrReporting)
345 auto& params = context.params;
347 auto indexValue = params[jss::ledger_index];
348 auto hashValue = params[jss::ledger_hash];
351 auto& legacyLedger = params[jss::ledger];
354 if (legacyLedger.asString().size() > 12)
355 hashValue = legacyLedger;
357 indexValue = legacyLedger;
362 if (!hashValue.isString())
366 if (!ledgerHash.parseHex(hashValue.asString()))
368 return getLedger(ledger, ledgerHash, context);
371 auto const index = indexValue.asString();
373 if (index ==
"current" ||
374 (index.empty() && !context.app.config().reporting()))
375 return getLedger(ledger, LedgerShortcut::CURRENT, context);
377 if (index ==
"validated" ||
378 (index.empty() && context.app.config().reporting()))
379 return getLedger(ledger, LedgerShortcut::VALIDATED, context);
381 if (index ==
"closed")
382 return getLedger(ledger, LedgerShortcut::CLOSED, context);
392 template <
class T,
class R>
396 R& request = context.
params;
404 GRPCContext<org::xrpl::rpc::v1::GetLedgerEntryRequest>&);
410 GRPCContext<org::xrpl::rpc::v1::GetLedgerDataRequest>&);
416 GRPCContext<org::xrpl::rpc::v1::GetLedgerRequest>&);
422 org::xrpl::rpc::v1::LedgerSpecifier
const& specifier,
427 using LedgerCase = org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase;
428 LedgerCase ledgerCase = specifier.ledger_case();
431 case LedgerCase::kHash: {
434 return getLedger(ledger, *hash, context);
438 case LedgerCase::kSequence:
439 return getLedger(ledger, specifier.sequence(), context);
440 case LedgerCase::kShortcut:
442 case LedgerCase::LEDGER_NOT_SET: {
443 auto const shortcut = specifier.shortcut();
446 org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_VALIDATED ||
448 org::xrpl::rpc::v1::LedgerSpecifier::
449 SHORTCUT_UNSPECIFIED &&
452 return getLedger(ledger, LedgerShortcut::VALIDATED, context);
457 org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CURRENT ||
459 org::xrpl::rpc::v1::LedgerSpecifier::
460 SHORTCUT_UNSPECIFIED)
462 return getLedger(ledger, LedgerShortcut::CURRENT, context);
466 org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CLOSED)
468 return getLedger(ledger, LedgerShortcut::CLOSED, context);
482 if (ledger ==
nullptr)
492 if (ledger ==
nullptr)
497 if (cur->info().seq == ledgerIndex)
503 if (ledger ==
nullptr)
532 if (shortcut == LedgerShortcut::VALIDATED)
535 if (ledger ==
nullptr)
542 assert(!ledger->open());
546 if (shortcut == LedgerShortcut::CURRENT)
551 "Reporting does not track current ledger"};
553 assert(ledger->open());
555 else if (shortcut == LedgerShortcut::CLOSED)
561 assert(!ledger->open());
568 if (ledger ==
nullptr)
575 static auto const minSequenceGap = 10;
577 if (ledger->info().seq + minSequenceGap <
626 if (!hash || ledger.
info().
hash != *hash)
631 assert(hash->isNonZero());
646 JLOG(stream) <<
"Ledger #" << seq <<
": " << mn.
what();
683 auto& info = ledger->info();
687 result[jss::ledger_hash] =
to_string(info.hash);
688 result[jss::ledger_index] = info.seq;
692 result[jss::ledger_current_index] = info.seq;
695 result[jss::validated] =
704 if (
auto status =
lookupLedger(ledger, context, result))
705 status.inject(result);
714 for (
auto const& jv : jvArray)
718 auto const id = parseBase58<AccountID>(jv.asString());
735 Blob const b(hash.begin(), hash.end());
737 boost::to_lower(md5);
741 jv[jss::urlgravatar] =
742 str(boost::format(
"http://www.gravatar.com/avatar/%s") % md5);
747 jv[jss::Invalid] =
true;
757 limit =
range.rdefault;
758 if (
auto const& jvLimit = context.
params[jss::limit])
760 if (!(jvLimit.isUInt() || (jvLimit.isInt() && jvLimit.asInt() >= 0)))
763 limit = jvLimit.asUInt();
781 if (result.size() == 18 &&
792 using string_to_seed_t =
796 static seed_match_t
const seedTypes[]{
797 {jss::passphrase.c_str(),
800 [](
std::string const& s) {
return parseBase58<Seed>(s); }},
809 seed_match_t
const* seedType =
nullptr;
811 for (
auto const& t : seedTypes)
823 "Exactly one of the following must be specified: " +
830 auto const& param = params[seedType->first];
831 if (!param.isString())
837 auto const fieldContents = param.asString();
851 bool const has_key_type = params.
isMember(jss::key_type);
854 static char const*
const secretTypes[]{
855 jss::passphrase.c_str(),
858 jss::seed_hex.c_str()};
861 char const* secretType =
nullptr;
863 for (
auto t : secretTypes)
872 if (count == 0 || secretType ==
nullptr)
881 "Exactly one of the following must be specified: " +
893 if (!params[jss::key_type].isString())
909 if (strcmp(secretType, jss::secret.c_str()) == 0)
912 "The secret field is not allowed if " +
923 if (strcmp(secretType, jss::seed_hex.c_str()) != 0)
934 rpcBAD_SEED,
"Specified seed is for an Ed25519 wallet.");
951 if (!params[jss::secret].isString())
973 LogicError(
"keypairForSignature: invalid key type");
1002 auto const& p = params[jss::type];
1011 auto const filter = p.asString();
1013 types.begin(), types.end(), [&filter](decltype(types.front())& t) {
1014 return t.first == filter;
1016 if (iter == types.end())
1023 result.second = iter->second;
1043 requestedVersion = jv.
get(jss::api_version, requestedVersion);
1045 if (!(requestedVersion.
isInt() || requestedVersion.
isUInt()) ||
1046 requestedVersion < minVersion || requestedVersion > maxVersion)
1048 requestedVersion = invalidVersion;
1050 return requestedVersion.
asUInt();
1060 auto const hasIndex = context.
params.
isMember(jss::ledger_index);
1066 if ((hasHash && hasIndex) || !(hasHash || hasIndex))
1069 "Exactly one of ledger_hash and ledger_index can be set.");
1076 auto const& jsonHash = context.
params[jss::ledger_hash];
1077 if (!jsonHash.isString() || !ledgerHash.
parseHex(jsonHash.asString()))
1082 auto const& jsonIndex = context.
params[jss::ledger_index];
1083 if (!jsonIndex.isInt())
1095 ledgerIndex = jsonIndex.asInt();
1098 if (ledgerIndex >= ledger->info().seq)
1100 if (ledgerIndex <= 0)
1103 auto const j = context.
app.
journal(
"RPCHandler");
1105 auto neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
1110 auto refHash =
hashOfSeq(*ledger, refIndex, j);
1124 "acquiring ledger containing requested index");
1125 jvResult[jss::acquiring] =
1134 "acquiring ledger containing requested index");
1135 jvResult[jss::acquiring] = il->getJson(0);
1143 neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
1146 ledgerHash = neededHash ? *neededHash : beast::zero;
1162 return il->getJson(0);
1165 rpcNOT_READY,
"findCreate failed to return an inbound ledger");
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
constexpr unsigned int apiInvalidVersion
API version numbers used in later API versions.
const SF_UINT64 sfIndexNext
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
constexpr unsigned int apiVersionIfUnspecified
@ ledgerMaster
ledger master data for signing
@ ltTICKET
A ledger object which describes a ticket.
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
A pair of SHAMap key and LedgerEntryType.
Json::Value getJson(JsonOptions options) const override
const beast::SemanticVersion firstVersion("1.0.0")
API version numbers used in API version 1.
const beast::SemanticVersion goodVersion("1.0.0")
Json::Value rpcError(int iError)
@ ltANY
A special type, matching any ledger entry type.
const SF_UINT64 sfOwnerNode
const SF_ACCOUNT sfDestination
@ ltLEDGER_HASHES
A ledger object that contains a list of ledger hashes.
An immutable linear range of bytes.
@ arrayValue
array value (ordered list)
void injectSLE(Json::Value &jv, SLE const &sle)
Inject JSON describing ledger entry.
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.
LedgerIndex getValidLedgerIndex()
Resource::Charge & loadType
Value get(UInt index, const Value &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
std::optional< KeyType > keyTypeFromString(std::string const &s)
unsigned int getAPIVersionNumber(Json::Value const &jv, bool betaEnabled)
Retrieve the api version number from the json value.
@ ltSIGNER_LIST
A ledger object which contains a signer list for an account.
LedgerMaster & ledgerMaster
std::string decodeBase58Token(std::string const &s, TokenType type)
Decode a token of given type encoded using Base58Check and the XRPL alphabet.
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
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...
uint128 getFieldH128(SField const &field) const
std::shared_ptr< Ledger const > getLedgerByHash(uint256 const &hash)
A Semantic Version number.
@ ltCHECK
A ledger object which describes a check.
@ ltFEE_SETTINGS
The ledger object which lists the network's fee settings.
static std::optional< base_uint > fromVoidChecked(T const &from)
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.
constexpr unsigned int apiBetaVersion
@ rpcREPORTING_UNSUPPORTED
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.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Json::Value expected_field_error(std::string const &name, std::string const &type)
virtual InboundLedgers & getInboundLedgers()=0
constexpr static std::size_t size()
@ ltDIR_NODE
A ledger object which contains a list of object identifiers.
constexpr uint256 pageMask(std::string_view("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))
Json::Value missing_field_error(std::string const &name)
const SF_VECTOR256 sfIndexes
const SF_UINT64 sfLowNode
@ ltAMENDMENTS
The ledger object which lists details about amendments on the network.
const SF_AMOUNT sfLowLimit
Keylet nftpage_min(AccountID const &owner)
NFT page keylets.
std::optional< Seed > parseRippleLibSeed(Json::Value const &value)
@ ltOFFER
A ledger object which describes an offer on the DEX.
@ objectValue
object value (collection of name/value pairs).
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
error_code_i accountFromStringWithCode(AccountID &result, std::string const &strIdent, bool bStrict)
Decode account ID from string.
virtual LedgerMaster & getLedgerMaster()=0
virtual std::shared_ptr< Ledger const > acquire(uint256 const &hash, std::uint32_t seq, InboundLedger::Reason)=0
@ ltESCROW
A ledger object describing a single escrow.
virtual uint256 getHashByIndex(LedgerIndex ledgerIndex)=0
getHashByIndex Returns the hash of the ledger with the given sequence.
virtual Config & config()=0
@ ltNFTOKEN_OFFER
A ledger object which identifies an offer to buy or sell an NFT.
Keylet page(uint256 const &key, std::uint64_t index) noexcept
A page in a directory.
AccountID calcAccountID(PublicKey const &pk)
virtual RelationalDatabase & getRelationalDatabase()=0
@ ltDEPOSIT_PREAUTH
A ledger object which describes a deposit preauthorization.
Keylet nftpage_max(AccountID const &owner)
A keylet for the owner's last possible NFT page.
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
std::shared_ptr< Ledger const > getLedgerBySeq(std::uint32_t index)
bool isMember(const char *key) const
Return true if the object has a member named key.
const SF_AMOUNT sfHighLimit
virtual std::optional< key_type > succ(key_type const &key, std::optional< key_type > const &last=std::nullopt) const =0
Return the key of the next state item.
std::optional< Seed > parseGenericSeed(std::string const &str, bool rfc1751)
Attempt to parse a string as a seed.
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
LedgerEntryType getType() const
std::shared_ptr< ReadView const > getCurrentLedger()
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
Status represents the results of an operation that might fail.
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
const SF_UINT128 sfEmailHash
Status
Return codes from Backend operations.
Json::Value getJson(LedgerFill const &fill)
Return a new Json::Value representing the ledger with given options.
std::shared_ptr< Ledger const > getClosedLedger()
std::string invalid_field_message(std::string const &name)
@ ltNFTOKEN_PAGE
A ledger object which contains a list of NFTs.
constexpr unsigned int apiMinimumSupportedVersion
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
virtual beast::Journal journal(std::string const &name)=0
bool isValidated(LedgerMaster &ledgerMaster, ReadView const &ledger, Application &app)
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...
Seeds are used to generate deterministic secret keys.
LedgerEntryType
Identifiers for on-ledger objects.
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
std::pair< RPC::Status, LedgerEntryType > chooseLedgerEntryType(Json::Value const ¶ms)
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
@ ltACCOUNT_ROOT
A ledger object which describes an account.
bool isFieldPresent(SField const &field) const
std::uint64_t getStartHint(std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
std::pair< PublicKey, SecretKey > keypairForSignature(Json::Value const ¶ms, Json::Value &error)
std::optional< Seed > getSeedFromRPC(Json::Value const ¶ms, Json::Value &error)
constexpr unsigned int apiMaximumSupportedVersion
std::shared_ptr< Ledger const > getValidatedLedger()
Status ledgerFromSpecifier(T &ledger, org::xrpl::rpc::v1::LedgerSpecifier const &specifier, Context &context)
const SF_UINT256 sfNextPageMin
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
const SF_ACCOUNT sfAccount
@ ltRIPPLE_STATE
A ledger object which describes a bidirectional trust line.
std::string strHex(FwdIt begin, FwdIt end)
hash_set< AccountID > parseAccountIds(Json::Value const &jvArray)
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
LedgerIndex getCandidateLedger(LedgerIndex requested)
Find a ledger index from which we could easily get the requested ledger.
Represents RPC limit parameter values that have a min, default and max.
static Keylet signers(AccountID const &account, std::uint32_t page) noexcept
Json::Value make_param_error(std::string const &message)
Returns a new json object that indicates invalid parameters.
std::optional< AccountID > accountFromStringStrict(std::string const &account)
Get an AccountID from an account ID or public key.
virtual bool open() const =0
Returns true if this reflects an open ledger.
const beast::SemanticVersion lastVersion("1.0.0")
Json::Value invalid_field_error(std::string const &name)
const SF_UINT64 sfHighNode
The context of information needed to call an RPC.
Status ledgerFromRequest(T &ledger, GRPCContext< R > &context)
const Charge feeHighBurdenRPC
Json::Value accountFromString(AccountID &result, std::string const &strIdent, bool bStrict)
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
constexpr auto maxValidatedLedgerAge
bool isRelatedToAccount(ReadView const &ledger, std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Tests if a SLE is owned by accountID.
@ ltPAYCHAN
A ledger object describing a single unidirectional XRP payment channel.
virtual std::shared_ptr< InboundLedger > find(LedgerHash const &hash)=0
std::string asString() const
Returns the unquoted string value.
Number root(Number f, unsigned d)