rippled
Loading...
Searching...
No Matches
VaultInfo.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2025 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#include <xrpld/rpc/Context.h>
21#include <xrpld/rpc/detail/RPCHelpers.h>
22
23#include <xrpl/beast/utility/Zero.h>
24#include <xrpl/json/json_value.h>
25#include <xrpl/protocol/ErrorCodes.h>
26#include <xrpl/protocol/Indexes.h>
27#include <xrpl/protocol/jss.h>
28#include <xrpl/protocol/serialize.h>
29
30namespace ripple {
31
33parseVault(Json::Value const& params, Json::Value& jvResult)
34{
35 auto const hasVaultId = params.isMember(jss::vault_id);
36 auto const hasOwner = params.isMember(jss::owner);
37 auto const hasSeq = params.isMember(jss::seq);
38
39 uint256 uNodeIndex = beast::zero;
40 if (hasVaultId && !hasOwner && !hasSeq)
41 {
42 if (!uNodeIndex.parseHex(params[jss::vault_id].asString()))
43 {
45 return std::nullopt;
46 }
47 // else uNodeIndex holds the value we need
48 }
49 else if (!hasVaultId && hasOwner && hasSeq)
50 {
51 auto const id = parseBase58<AccountID>(params[jss::owner].asString());
52 if (!id)
53 {
55 return std::nullopt;
56 }
57 else if (
58 !(params[jss::seq].isInt() || params[jss::seq].isUInt()) ||
59 params[jss::seq].asDouble() <= 0.0 ||
60 params[jss::seq].asDouble() > double(Json::Value::maxUInt))
61 {
63 return std::nullopt;
64 }
65
66 uNodeIndex = keylet::vault(*id, params[jss::seq].asUInt()).key;
67 }
68 else
69 {
70 // Invalid combination of fields vault_id/owner/seq
72 return std::nullopt;
73 }
74
75 return uNodeIndex;
76}
77
80{
82 auto jvResult = RPC::lookupLedger(lpLedger, context);
83
84 if (!lpLedger)
85 return jvResult;
86
87 auto const uNodeIndex =
88 parseVault(context.params, jvResult).value_or(beast::zero);
89 if (uNodeIndex == beast::zero)
90 {
91 jvResult[jss::error] = "malformedRequest";
92 return jvResult;
93 }
94
95 auto const sleVault = lpLedger->read(keylet::vault(uNodeIndex));
96 auto const sleIssuance = sleVault == nullptr //
97 ? nullptr
98 : lpLedger->read(keylet::mptIssuance(sleVault->at(sfShareMPTID)));
99 if (!sleVault || !sleIssuance)
100 {
101 jvResult[jss::error] = "entryNotFound";
102 return jvResult;
103 }
104
105 Json::Value& vault = jvResult[jss::vault];
106 vault = sleVault->getJson(JsonOptions::none);
107 auto& share = vault[jss::shares];
108 share = sleIssuance->getJson(JsonOptions::none);
109
110 jvResult[jss::vault] = vault;
111 return jvResult;
112}
113
114} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
static UInt const maxUInt
Definition json_value.h:163
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.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:503
T is_same_v
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition ErrorCodes.h:233
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.
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:526
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:564
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
@ rpcACT_MALFORMED
Definition ErrorCodes.h:90
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:84
static Expected< uint256, Json::Value > parseVault(Json::Value const &params, Json::StaticString const fieldName)
Json::Value doVaultInfo(RPC::JsonContext &)
Definition VaultInfo.cpp:79
uint256 key
Definition Keylet.h:40