rippled
Loading...
Searching...
No Matches
AccountInfo.cpp
1#include <xrpld/app/main/Application.h>
2#include <xrpld/app/misc/TxQ.h>
3#include <xrpld/rpc/Context.h>
4#include <xrpld/rpc/GRPCHandlers.h>
5#include <xrpld/rpc/detail/RPCHelpers.h>
6
7#include <xrpl/json/json_value.h>
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/protocol/ErrorCodes.h>
10#include <xrpl/protocol/Indexes.h>
11#include <xrpl/protocol/UintTypes.h>
12#include <xrpl/protocol/jss.h>
13
14namespace ripple {
15
16// {
17// account: <ident>,
18// ledger_hash : <ledger>
19// ledger_index : <ledger_index>
20// signer_lists : <bool> // optional (default false)
21// // if true return SignerList(s).
22// queue : <bool> // optional (default false)
23// // if true return information about transactions
24// // in the current TxQ, only if the requested
25// // ledger is open. Otherwise if true, returns an
26// // error.
27// }
28
29// TODO(tom): what is that "default"?
32{
33 auto& params = context.params;
34
35 std::string strIdent;
36 if (params.isMember(jss::account))
37 {
38 if (!params[jss::account].isString())
39 return RPC::invalid_field_error(jss::account);
40 strIdent = params[jss::account].asString();
41 }
42 else if (params.isMember(jss::ident))
43 {
44 if (!params[jss::ident].isString())
45 return RPC::invalid_field_error(jss::ident);
46 strIdent = params[jss::ident].asString();
47 }
48 else
49 return RPC::missing_field_error(jss::account);
50
52 auto result = RPC::lookupLedger(ledger, context);
53
54 if (!ledger)
55 return result;
56
57 // Get info on account.
58 auto id = parseBase58<AccountID>(strIdent);
59 if (!id)
60 {
62 return result;
63 }
64 auto const accountID{std::move(id.value())};
65
66 static constexpr std::
68 lsFlags{
69 {{"defaultRipple", lsfDefaultRipple},
70 {"depositAuth", lsfDepositAuth},
71 {"disableMasterKey", lsfDisableMaster},
72 {"disallowIncomingXRP", lsfDisallowXRP},
73 {"globalFreeze", lsfGlobalFreeze},
74 {"noFreeze", lsfNoFreeze},
75 {"passwordSpent", lsfPasswordSpent},
76 {"requireAuthorization", lsfRequireAuth},
77 {"requireDestinationTag", lsfRequireDestTag}}};
78
79 static constexpr std::
81 disallowIncomingFlags{
82 {{"disallowIncomingNFTokenOffer",
84 {"disallowIncomingCheck", lsfDisallowIncomingCheck},
85 {"disallowIncomingPayChan", lsfDisallowIncomingPayChan},
86 {"disallowIncomingTrustline", lsfDisallowIncomingTrustline}}};
87
89 allowTrustLineClawbackFlag{
90 "allowTrustLineClawback", lsfAllowTrustLineClawback};
91
93 allowTrustLineLockingFlag{
94 "allowTrustLineLocking", lsfAllowTrustLineLocking};
95
96 auto const sleAccepted = ledger->read(keylet::account(accountID));
97 if (sleAccepted)
98 {
99 auto const queue =
100 params.isMember(jss::queue) && params[jss::queue].asBool();
101
102 if (queue && !ledger->open())
103 {
104 // It doesn't make sense to request the queue
105 // with any closed or validated ledger.
107 return result;
108 }
109
110 Json::Value jvAccepted(Json::objectValue);
111 RPC::injectSLE(jvAccepted, *sleAccepted);
112 result[jss::account_data] = jvAccepted;
113
115 for (auto const& lsf : lsFlags)
116 acctFlags[lsf.first.data()] = sleAccepted->isFlag(lsf.second);
117
118 if (ledger->rules().enabled(featureDisallowIncoming))
119 {
120 for (auto const& lsf : disallowIncomingFlags)
121 acctFlags[lsf.first.data()] = sleAccepted->isFlag(lsf.second);
122 }
123
124 if (ledger->rules().enabled(featureClawback))
125 acctFlags[allowTrustLineClawbackFlag.first.data()] =
126 sleAccepted->isFlag(allowTrustLineClawbackFlag.second);
127
128 if (ledger->rules().enabled(featureTokenEscrow))
129 acctFlags[allowTrustLineLockingFlag.first.data()] =
130 sleAccepted->isFlag(allowTrustLineLockingFlag.second);
131
132 result[jss::account_flags] = std::move(acctFlags);
133
134 // The document[https://xrpl.org/account_info.html#account_info] states
135 // that signer_lists is a bool, however assigning any string value
136 // works. Do not allow this. This check is for api Version 2 onwards
137 // only
138 if (context.apiVersion > 1u && params.isMember(jss::signer_lists) &&
139 !params[jss::signer_lists].isBool())
140 {
142 return result;
143 }
144
145 // Return SignerList(s) if that is requested.
146 if (params.isMember(jss::signer_lists) &&
147 params[jss::signer_lists].asBool())
148 {
149 // We put the SignerList in an array because of an anticipated
150 // future when we support multiple signer lists on one account.
151 Json::Value jvSignerList = Json::arrayValue;
152
153 // This code will need to be revisited if in the future we support
154 // multiple SignerLists on one account.
155 auto const sleSigners = ledger->read(keylet::signers(accountID));
156 if (sleSigners)
157 jvSignerList.append(sleSigners->getJson(JsonOptions::none));
158
159 // Documentation states this is returned as part of the account_info
160 // response, but previously the code put it under account_data. We
161 // can move this to the documentated location from apiVersion 2
162 // onwards.
163 if (context.apiVersion == 1)
164 {
165 result[jss::account_data][jss::signer_lists] =
166 std::move(jvSignerList);
167 }
168 else
169 {
170 result[jss::signer_lists] = std::move(jvSignerList);
171 }
172 }
173 // Return queue info if that is requested
174 if (queue)
175 {
176 Json::Value jvQueueData = Json::objectValue;
177
178 auto const txs = context.app.getTxQ().getAccountTxs(accountID);
179 if (!txs.empty())
180 {
181 jvQueueData[jss::txn_count] =
182 static_cast<Json::UInt>(txs.size());
183
184 auto& jvQueueTx = jvQueueData[jss::transactions];
185 jvQueueTx = Json::arrayValue;
186
187 std::uint32_t seqCount = 0;
188 std::uint32_t ticketCount = 0;
191 std::optional<std::uint32_t> lowestTicket;
192 std::optional<std::uint32_t> highestTicket;
193 bool anyAuthChanged = false;
194 XRPAmount totalSpend(0);
195
196 // We expect txs to be returned sorted by SeqProxy. Verify
197 // that with a couple of asserts.
198 SeqProxy prevSeqProxy = SeqProxy::sequence(0);
199 for (auto const& tx : txs)
200 {
202
203 if (tx.seqProxy.isSeq())
204 {
205 XRPL_ASSERT(
206 prevSeqProxy < tx.seqProxy,
207 "rpple::doAccountInfo : first sorted proxy");
208 prevSeqProxy = tx.seqProxy;
209 jvTx[jss::seq] = tx.seqProxy.value();
210 ++seqCount;
211 if (!lowestSeq)
212 lowestSeq = tx.seqProxy.value();
213 highestSeq = tx.seqProxy.value();
214 }
215 else
216 {
217 XRPL_ASSERT(
218 prevSeqProxy < tx.seqProxy,
219 "rpple::doAccountInfo : second sorted proxy");
220 prevSeqProxy = tx.seqProxy;
221 jvTx[jss::ticket] = tx.seqProxy.value();
222 ++ticketCount;
223 if (!lowestTicket)
224 lowestTicket = tx.seqProxy.value();
225 highestTicket = tx.seqProxy.value();
226 }
227
228 jvTx[jss::fee_level] = to_string(tx.feeLevel);
229 if (tx.lastValid)
230 jvTx[jss::LastLedgerSequence] = *tx.lastValid;
231
232 jvTx[jss::fee] = to_string(tx.consequences.fee());
233 auto const spend = tx.consequences.potentialSpend() +
234 tx.consequences.fee();
235 jvTx[jss::max_spend_drops] = to_string(spend);
236 totalSpend += spend;
237 bool const authChanged = tx.consequences.isBlocker();
238 if (authChanged)
239 anyAuthChanged = authChanged;
240 jvTx[jss::auth_change] = authChanged;
241
242 jvQueueTx.append(std::move(jvTx));
243 }
244
245 if (seqCount)
246 jvQueueData[jss::sequence_count] = seqCount;
247 if (ticketCount)
248 jvQueueData[jss::ticket_count] = ticketCount;
249 if (lowestSeq)
250 jvQueueData[jss::lowest_sequence] = *lowestSeq;
251 if (highestSeq)
252 jvQueueData[jss::highest_sequence] = *highestSeq;
253 if (lowestTicket)
254 jvQueueData[jss::lowest_ticket] = *lowestTicket;
255 if (highestTicket)
256 jvQueueData[jss::highest_ticket] = *highestTicket;
257
258 jvQueueData[jss::auth_change_queued] = anyAuthChanged;
259 jvQueueData[jss::max_spend_drops_total] = to_string(totalSpend);
260 }
261 else
262 jvQueueData[jss::txn_count] = 0u;
263
264 result[jss::queue_data] = std::move(jvQueueData);
265 }
266 }
267 else
268 {
269 result[jss::account] = toBase58(accountID);
271 }
272
273 return result;
274}
275
276} // namespace ripple
Represents a JSON value.
Definition json_value.h:131
Value & append(Value const &value)
Append value to array at the end.
virtual TxQ & getTxQ()=0
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:37
static constexpr SeqProxy sequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:57
std::vector< TxDetails > getAccountTxs(AccountID const &account) const
Returns information about the transactions currently in the queue for the account.
Definition TxQ.cpp:1803
T is_same_v
@ arrayValue
array value (ordered list)
Definition json_value.h:26
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:27
unsigned int UInt
Json::Value invalid_field_error(std::string const &name)
Definition ErrorCodes.h:306
void injectSLE(Json::Value &jv, SLE const &sle)
Inject JSON describing ledger entry.
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition ErrorCodes.h:214
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.
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:264
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Keylet signers(AccountID const &account) noexcept
A SignerList.
Definition Indexes.cpp:311
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:51
@ rpcACT_MALFORMED
Definition ErrorCodes.h:71
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:65
@ lsfRequireDestTag
@ lsfPasswordSpent
@ lsfDefaultRipple
@ lsfDisallowIncomingCheck
@ lsfAllowTrustLineClawback
@ lsfDisableMaster
@ lsfDisallowIncomingPayChan
@ lsfDisallowIncomingTrustline
@ lsfAllowTrustLineLocking
@ lsfDisallowIncomingNFTokenOffer
@ lsfGlobalFreeze
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
Json::Value doAccountInfo(RPC::JsonContext &context)
unsigned int apiVersion
Definition Context.h:30
Application & app
Definition Context.h:22
T value(T... args)