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/RPCLedgerHelpers.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
14#include <boost/algorithm/string/case_conv.hpp>
15
16namespace xrpl {
17
30void
31injectSLE(Json::Value& jv, SLE const& sle)
32{
33 jv = sle.getJson(JsonOptions::none);
34 if (sle.getType() == ltACCOUNT_ROOT)
35 {
36 if (sle.isFieldPresent(sfEmailHash))
37 {
38 auto const& hash = sle.getFieldH128(sfEmailHash);
39 Blob const b(hash.begin(), hash.end());
41 boost::to_lower(md5);
42 // VFALCO TODO Give a name and move this constant
43 // to a more visible location. Also
44 // shouldn't this be https?
45 jv[jss::urlgravatar] =
46 str(boost::format("http://www.gravatar.com/avatar/%s") % md5);
47 }
48 }
49 else
50 {
51 jv[jss::Invalid] = true;
52 }
53}
54
55// {
56// account: <ident>,
57// ledger_hash : <ledger>
58// ledger_index : <ledger_index>
59// signer_lists : <bool> // optional (default false)
60// // if true return SignerList(s).
61// queue : <bool> // optional (default false)
62// // if true return information about transactions
63// // in the current TxQ, only if the requested
64// // ledger is open. Otherwise if true, returns an
65// // error.
66// }
67
68// TODO(tom): what is that "default"?
71{
72 auto& params = context.params;
73
74 std::string strIdent;
75 if (params.isMember(jss::account))
76 {
77 if (!params[jss::account].isString())
78 return RPC::invalid_field_error(jss::account);
79 strIdent = params[jss::account].asString();
80 }
81 else if (params.isMember(jss::ident))
82 {
83 if (!params[jss::ident].isString())
84 return RPC::invalid_field_error(jss::ident);
85 strIdent = params[jss::ident].asString();
86 }
87 else
88 return RPC::missing_field_error(jss::account);
89
91 auto result = RPC::lookupLedger(ledger, context);
92
93 if (!ledger)
94 return result;
95
96 // Get info on account.
97 auto id = parseBase58<AccountID>(strIdent);
98 if (!id)
99 {
101 return result;
102 }
103 auto const accountID{std::move(id.value())};
104
105 static constexpr std::
107 lsFlags{
108 {{"defaultRipple", lsfDefaultRipple},
109 {"depositAuth", lsfDepositAuth},
110 {"disableMasterKey", lsfDisableMaster},
111 {"disallowIncomingXRP", lsfDisallowXRP},
112 {"globalFreeze", lsfGlobalFreeze},
113 {"noFreeze", lsfNoFreeze},
114 {"passwordSpent", lsfPasswordSpent},
115 {"requireAuthorization", lsfRequireAuth},
116 {"requireDestinationTag", lsfRequireDestTag}}};
117
118 static constexpr std::
120 disallowIncomingFlags{
121 {{"disallowIncomingNFTokenOffer",
123 {"disallowIncomingCheck", lsfDisallowIncomingCheck},
124 {"disallowIncomingPayChan", lsfDisallowIncomingPayChan},
125 {"disallowIncomingTrustline", lsfDisallowIncomingTrustline}}};
126
128 allowTrustLineClawbackFlag{
129 "allowTrustLineClawback", lsfAllowTrustLineClawback};
130
132 allowTrustLineLockingFlag{
133 "allowTrustLineLocking", lsfAllowTrustLineLocking};
134
135 auto const sleAccepted = ledger->read(keylet::account(accountID));
136 if (sleAccepted)
137 {
138 auto const queue =
139 params.isMember(jss::queue) && params[jss::queue].asBool();
140
141 if (queue && !ledger->open())
142 {
143 // It doesn't make sense to request the queue
144 // with any closed or validated ledger.
146 return result;
147 }
148
149 Json::Value jvAccepted(Json::objectValue);
150 injectSLE(jvAccepted, *sleAccepted);
151 result[jss::account_data] = jvAccepted;
152
154 for (auto const& lsf : lsFlags)
155 acctFlags[lsf.first.data()] = sleAccepted->isFlag(lsf.second);
156
157 for (auto const& lsf : disallowIncomingFlags)
158 acctFlags[lsf.first.data()] = sleAccepted->isFlag(lsf.second);
159
160 if (ledger->rules().enabled(featureClawback))
161 acctFlags[allowTrustLineClawbackFlag.first.data()] =
162 sleAccepted->isFlag(allowTrustLineClawbackFlag.second);
163
164 if (ledger->rules().enabled(featureTokenEscrow))
165 acctFlags[allowTrustLineLockingFlag.first.data()] =
166 sleAccepted->isFlag(allowTrustLineLockingFlag.second);
167
168 result[jss::account_flags] = std::move(acctFlags);
169
170 auto const pseudoFields = getPseudoAccountFields();
171 for (auto const& pseudoField : pseudoFields)
172 {
173 if (sleAccepted->isFieldPresent(*pseudoField))
174 {
175 std::string name = pseudoField->fieldName;
176 if (name.ends_with("ID"))
177 {
178 // Remove the ID suffix from the field name.
179 name = name.substr(0, name.size() - 2);
180 XRPL_ASSERT_PARTS(
181 !name.empty(),
182 "xrpl::doAccountInfo",
183 "name is not empty");
184 }
185 // ValidPseudoAccounts invariant guarantees that only one field
186 // can be set
187 result[jss::pseudo_account][jss::type] = name;
188 break;
189 }
190 }
191
192 // The document[https://xrpl.org/account_info.html#account_info] states
193 // that signer_lists is a bool, however assigning any string value
194 // works. Do not allow this. This check is for api Version 2 onwards
195 // only
196 if (context.apiVersion > 1u && params.isMember(jss::signer_lists) &&
197 !params[jss::signer_lists].isBool())
198 {
200 return result;
201 }
202
203 // Return SignerList(s) if that is requested.
204 if (params.isMember(jss::signer_lists) &&
205 params[jss::signer_lists].asBool())
206 {
207 // We put the SignerList in an array because of an anticipated
208 // future when we support multiple signer lists on one account.
209 Json::Value jvSignerList = Json::arrayValue;
210
211 // This code will need to be revisited if in the future we support
212 // multiple SignerLists on one account.
213 auto const sleSigners = ledger->read(keylet::signers(accountID));
214 if (sleSigners)
215 jvSignerList.append(sleSigners->getJson(JsonOptions::none));
216
217 // Documentation states this is returned as part of the account_info
218 // response, but previously the code put it under account_data. We
219 // can move this to the documented location from apiVersion 2
220 // onwards.
221 if (context.apiVersion == 1)
222 {
223 result[jss::account_data][jss::signer_lists] =
224 std::move(jvSignerList);
225 }
226 else
227 {
228 result[jss::signer_lists] = std::move(jvSignerList);
229 }
230 }
231 // Return queue info if that is requested
232 if (queue)
233 {
234 Json::Value jvQueueData = Json::objectValue;
235
236 auto const txs = context.app.getTxQ().getAccountTxs(accountID);
237 if (!txs.empty())
238 {
239 jvQueueData[jss::txn_count] =
240 static_cast<Json::UInt>(txs.size());
241
242 auto& jvQueueTx = jvQueueData[jss::transactions];
243 jvQueueTx = Json::arrayValue;
244
245 std::uint32_t seqCount = 0;
246 std::uint32_t ticketCount = 0;
249 std::optional<std::uint32_t> lowestTicket;
250 std::optional<std::uint32_t> highestTicket;
251 bool anyAuthChanged = false;
252 XRPAmount totalSpend(0);
253
254 // We expect txs to be returned sorted by SeqProxy. Verify
255 // that with a couple of asserts.
256 SeqProxy prevSeqProxy = SeqProxy::sequence(0);
257 for (auto const& tx : txs)
258 {
260
261 if (tx.seqProxy.isSeq())
262 {
263 XRPL_ASSERT(
264 prevSeqProxy < tx.seqProxy,
265 "doAccountInfo : first sorted proxy");
266 prevSeqProxy = tx.seqProxy;
267 jvTx[jss::seq] = tx.seqProxy.value();
268 ++seqCount;
269 if (!lowestSeq)
270 lowestSeq = tx.seqProxy.value();
271 highestSeq = tx.seqProxy.value();
272 }
273 else
274 {
275 XRPL_ASSERT(
276 prevSeqProxy < tx.seqProxy,
277 "doAccountInfo : second sorted proxy");
278 prevSeqProxy = tx.seqProxy;
279 jvTx[jss::ticket] = tx.seqProxy.value();
280 ++ticketCount;
281 if (!lowestTicket)
282 lowestTicket = tx.seqProxy.value();
283 highestTicket = tx.seqProxy.value();
284 }
285
286 jvTx[jss::fee_level] = to_string(tx.feeLevel);
287 if (tx.lastValid)
288 jvTx[jss::LastLedgerSequence] = *tx.lastValid;
289
290 jvTx[jss::fee] = to_string(tx.consequences.fee());
291 auto const spend = tx.consequences.potentialSpend() +
292 tx.consequences.fee();
293 jvTx[jss::max_spend_drops] = to_string(spend);
294 totalSpend += spend;
295 bool const authChanged = tx.consequences.isBlocker();
296 if (authChanged)
297 anyAuthChanged = authChanged;
298 jvTx[jss::auth_change] = authChanged;
299
300 jvQueueTx.append(std::move(jvTx));
301 }
302
303 if (seqCount)
304 jvQueueData[jss::sequence_count] = seqCount;
305 if (ticketCount)
306 jvQueueData[jss::ticket_count] = ticketCount;
307 if (lowestSeq)
308 jvQueueData[jss::lowest_sequence] = *lowestSeq;
309 if (highestSeq)
310 jvQueueData[jss::highest_sequence] = *highestSeq;
311 if (lowestTicket)
312 jvQueueData[jss::lowest_ticket] = *lowestTicket;
313 if (highestTicket)
314 jvQueueData[jss::highest_ticket] = *highestTicket;
315
316 jvQueueData[jss::auth_change_queued] = anyAuthChanged;
317 jvQueueData[jss::max_spend_drops_total] = to_string(totalSpend);
318 }
319 else
320 jvQueueData[jss::txn_count] = 0u;
321
322 result[jss::queue_data] = std::move(jvQueueData);
323 }
324 }
325 else
326 {
327 result[jss::account] = toBase58(accountID);
329 }
330
331 return result;
332}
333
334} // namespace xrpl
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
Json::Value getJson(JsonOptions options=JsonOptions::none) const override
LedgerEntryType getType() const
uint128 getFieldH128(SField const &field) const
Definition STObject.cpp:608
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:465
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:1802
T empty(T... args)
T ends_with(T... args)
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:284
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:242
void inject_error(error_code_i code, Json::Value &json)
Add or update the json update to reflect the error code.
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext const &context, Json::Value &result)
Looks up a ledger from a request and fills a Json::Value with ledger data.
Keylet signers(AccountID const &account) noexcept
A SignerList.
Definition Indexes.cpp:312
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:166
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::vector< SField const * > const & getPseudoAccountFields()
Definition View.cpp:1199
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:11
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
void injectSLE(Json::Value &jv, SLE const &sle)
Injects JSON describing a ledger entry.
Json::Value doAccountInfo(RPC::JsonContext &context)
@ lsfDisallowIncomingNFTokenOffer
@ lsfAllowTrustLineClawback
@ lsfDepositAuth
@ lsfNoFreeze
@ lsfDisallowIncomingPayChan
@ lsfRequireAuth
@ lsfDisallowXRP
@ lsfDefaultRipple
@ lsfDisallowIncomingCheck
@ lsfPasswordSpent
@ lsfRequireDestTag
@ lsfAllowTrustLineLocking
@ lsfGlobalFreeze
@ lsfDisableMaster
@ lsfDisallowIncomingTrustline
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)
Definition Slice.h:225
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:51
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:65
@ rpcACT_MALFORMED
Definition ErrorCodes.h:71
T size(T... args)
Application & app
Definition Context.h:22
unsigned int apiVersion
Definition Context.h:30
Json::Value params
Definition Context.h:44
T substr(T... args)
T value(T... args)