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/ledger/View.h>
27 #include <ripple/net/RPCErr.h>
28 #include <ripple/protocol/AccountID.h>
29 #include <ripple/protocol/Feature.h>
30 #include <ripple/rpc/Context.h>
31 #include <ripple/rpc/DeliveredAmount.h>
32 #include <ripple/rpc/impl/RPCHelpers.h>
33 #include <boost/algorithm/string/case_conv.hpp>
35 #include <ripple/resource/Fees.h>
45 auto const publicKey =
51 result = parseBase58<AccountID>(account);
99 if (sle->getFieldAmount(
sfLowLimit).getIssuer() == accountID)
101 else if (sle->getFieldAmount(
sfHighLimit).getIssuer() == accountID)
119 return (sle->getFieldAmount(
sfLowLimit).getIssuer() == accountID) ||
120 (sle->getFieldAmount(
sfHighLimit).getIssuer() == accountID);
124 return sle->getAccountID(
sfAccount) == accountID;
129 return sle->key() == accountSignerList.
key;
162 auto const& entries = dir->getFieldV256(
sfIndexes);
163 auto iter = entries.begin();
167 iter =
std::find(iter, entries.end(), entryIndex);
168 if (iter == entries.end())
174 for (; iter != entries.end(); ++iter)
178 auto typeMatchesFilter =
182 typeFilter.
begin(), typeFilter.
end(), ledgerType);
183 return it != typeFilter.
end();
186 if (!typeFilter.has_value() ||
187 typeMatchesFilter(typeFilter.value(), sleNode->getType()))
194 if (++iter != entries.end())
196 jvResult[jss::limit] = limit;
197 jvResult[jss::marker] =
206 auto const nodeIndex = dir->getFieldU64(
sfIndexNext);
217 auto const& e = dir->getFieldV256(
sfIndexes);
220 jvResult[jss::limit] = limit;
221 jvResult[jss::marker] =
233 isValidatedOld(
LedgerMaster& ledgerMaster,
bool standaloneOrReporting)
235 if (standaloneOrReporting)
247 auto& params = context.params;
249 auto indexValue = params[jss::ledger_index];
250 auto hashValue = params[jss::ledger_hash];
253 auto& legacyLedger = params[jss::ledger];
256 if (legacyLedger.asString().size() > 12)
257 hashValue = legacyLedger;
259 indexValue = legacyLedger;
264 if (!hashValue.isString())
268 if (!ledgerHash.parseHex(hashValue.asString()))
270 return getLedger(ledger, ledgerHash, context);
273 auto const index = indexValue.asString();
275 if (index ==
"current" ||
276 (index.empty() && !context.app.config().reporting()))
277 return getLedger(ledger, LedgerShortcut::CURRENT, context);
279 if (index ==
"validated" ||
280 (index.empty() && context.app.config().reporting()))
281 return getLedger(ledger, LedgerShortcut::VALIDATED, context);
283 if (index ==
"closed")
284 return getLedger(ledger, LedgerShortcut::CLOSED, context);
294 template <
class T,
class R>
298 R& request = context.
params;
306 GRPCContext<org::xrpl::rpc::v1::GetLedgerEntryRequest>&);
312 GRPCContext<org::xrpl::rpc::v1::GetLedgerDataRequest>&);
318 GRPCContext<org::xrpl::rpc::v1::GetLedgerRequest>&);
324 org::xrpl::rpc::v1::LedgerSpecifier
const& specifier,
329 using LedgerCase = org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase;
330 LedgerCase ledgerCase = specifier.ledger_case();
333 case LedgerCase::kHash: {
336 return getLedger(ledger, *hash, context);
340 case LedgerCase::kSequence:
341 return getLedger(ledger, specifier.sequence(), context);
342 case LedgerCase::kShortcut:
344 case LedgerCase::LEDGER_NOT_SET: {
345 auto const shortcut = specifier.shortcut();
348 org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_VALIDATED ||
350 org::xrpl::rpc::v1::LedgerSpecifier::
351 SHORTCUT_UNSPECIFIED &&
354 return getLedger(ledger, LedgerShortcut::VALIDATED, context);
359 org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CURRENT ||
361 org::xrpl::rpc::v1::LedgerSpecifier::
362 SHORTCUT_UNSPECIFIED)
364 return getLedger(ledger, LedgerShortcut::CURRENT, context);
368 org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CLOSED)
370 return getLedger(ledger, LedgerShortcut::CLOSED, context);
384 if (ledger ==
nullptr)
394 if (ledger ==
nullptr)
399 if (cur->info().seq == ledgerIndex)
405 if (ledger ==
nullptr)
434 if (shortcut == LedgerShortcut::VALIDATED)
437 if (ledger ==
nullptr)
444 assert(!ledger->open());
448 if (shortcut == LedgerShortcut::CURRENT)
453 "Reporting does not track current ledger"};
455 assert(ledger->open());
457 else if (shortcut == LedgerShortcut::CLOSED)
463 assert(!ledger->open());
470 if (ledger ==
nullptr)
477 static auto const minSequenceGap = 10;
479 if (ledger->info().seq + minSequenceGap <
528 if (!hash || ledger.
info().
hash != *hash)
533 assert(hash->isNonZero());
548 JLOG(stream) <<
"Ledger #" << seq <<
": " << mn.
what();
585 auto& info = ledger->info();
589 result[jss::ledger_hash] =
to_string(info.hash);
590 result[jss::ledger_index] = info.seq;
594 result[jss::ledger_current_index] = info.seq;
597 result[jss::validated] =
606 if (
auto status =
lookupLedger(ledger, context, result))
607 status.inject(result);
616 for (
auto const& jv : jvArray)
620 auto const id = parseBase58<AccountID>(jv.asString());
637 Blob const b(hash.begin(), hash.end());
639 boost::to_lower(md5);
643 jv[jss::urlgravatar] =
644 str(boost::format(
"http://www.gravatar.com/avatar/%s") % md5);
649 jv[jss::Invalid] =
true;
659 limit =
range.rdefault;
660 if (
auto const& jvLimit = context.
params[jss::limit])
662 if (!(jvLimit.isUInt() || (jvLimit.isInt() && jvLimit.asInt() >= 0)))
665 limit = jvLimit.asUInt();
683 if (result.size() == 18 &&
694 using string_to_seed_t =
698 static seed_match_t
const seedTypes[]{
699 {jss::passphrase.c_str(),
702 [](
std::string const& s) {
return parseBase58<Seed>(s); }},
711 seed_match_t
const* seedType =
nullptr;
713 for (
auto const& t : seedTypes)
725 "Exactly one of the following must be specified: " +
732 auto const& param = params[seedType->first];
733 if (!param.isString())
739 auto const fieldContents = param.asString();
753 bool const has_key_type = params.
isMember(jss::key_type);
756 static char const*
const secretTypes[]{
757 jss::passphrase.c_str(),
760 jss::seed_hex.c_str()};
763 char const* secretType =
nullptr;
765 for (
auto t : secretTypes)
774 if (count == 0 || secretType ==
nullptr)
783 "Exactly one of the following must be specified: " +
795 if (!params[jss::key_type].isString())
811 if (strcmp(secretType, jss::secret.c_str()) == 0)
814 "The secret field is not allowed if " +
825 if (strcmp(secretType, jss::seed_hex.c_str()) != 0)
836 rpcBAD_SEED,
"Specified seed is for an Ed25519 wallet.");
853 if (!params[jss::secret].isString())
875 LogicError(
"keypairForSignature: invalid key type");
903 auto const& p = params[jss::type];
912 auto const filter = p.asString();
914 types.begin(), types.end(), [&filter](decltype(types.front())& t) {
915 return t.first == filter;
917 if (iter == types.end())
924 result.second = iter->second;
944 requestedVersion = jv.
get(jss::api_version, requestedVersion);
946 if (!(requestedVersion.
isInt() || requestedVersion.
isUInt()) ||
947 requestedVersion < minVersion || requestedVersion > maxVersion)
949 requestedVersion = invalidVersion;
951 return requestedVersion.
asUInt();
967 if ((hasHash && hasIndex) || !(hasHash || hasIndex))
970 "Exactly one of ledger_hash and ledger_index can be set.");
977 auto const& jsonHash = context.
params[jss::ledger_hash];
978 if (!jsonHash.isString() || !ledgerHash.
parseHex(jsonHash.asString()))
983 auto const& jsonIndex = context.
params[jss::ledger_index];
984 if (!jsonIndex.isInt())
996 ledgerIndex = jsonIndex.asInt();
999 if (ledgerIndex >= ledger->info().seq)
1001 if (ledgerIndex <= 0)
1004 auto const j = context.
app.
journal(
"RPCHandler");
1006 auto neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
1011 auto refHash =
hashOfSeq(*ledger, refIndex, j);
1025 "acquiring ledger containing requested index");
1026 jvResult[jss::acquiring] =
1035 "acquiring ledger containing requested index");
1036 jvResult[jss::acquiring] = il->getJson(0);
1044 neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
1047 ledgerHash = neededHash ? *neededHash : beast::zero;
1063 return il->getJson(0);
1066 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
@ 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.
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.
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
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
bool isOwnedByAccount(ReadView const &ledger, std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Tests if a SLE is owned by accountID.
@ 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.
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
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)
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)
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
@ ltPAYCHAN
A ledger object describing a single unidirectional XRP payment channel.
virtual std::shared_ptr< InboundLedger > find(LedgerHash const &hash)=0
bool getAccountObjects(ReadView const &ledger, AccountID const &account, std::optional< std::vector< LedgerEntryType >> const &typeFilter, uint256 dirIndex, uint256 const &entryIndex, std::uint32_t const limit, Json::Value &jvResult)
Gathers all objects for an account in a ledger.
std::string asString() const
Returns the unquoted string value.
Number root(Number f, unsigned d)