xrpld
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/detail/RPCLedgerHelpers.h>
5
6#include <xrpl/basics/Blob.h>
7#include <xrpl/basics/Slice.h>
8#include <xrpl/basics/StringUtilities.h>
9#include <xrpl/basics/strHex.h>
10#include <xrpl/beast/utility/instrumentation.h>
11#include <xrpl/json/json_forwards.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/ledger/ReadView.h>
14#include <xrpl/ledger/helpers/AccountRootHelpers.h>
15#include <xrpl/protocol/AccountID.h>
16#include <xrpl/protocol/ErrorCodes.h>
17#include <xrpl/protocol/Feature.h>
18#include <xrpl/protocol/Indexes.h>
19#include <xrpl/protocol/LedgerFormats.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/SeqProxy.h>
22#include <xrpl/protocol/Units.h>
23#include <xrpl/protocol/XRPAmount.h>
24#include <xrpl/protocol/jss.h>
25
26#include <array>
27#include <cstdint>
28#include <format>
29#include <memory>
30#include <optional>
31#include <string>
32#include <string_view>
33#include <utility>
34
35namespace xrpl {
36
49void
50injectSLE(json::Value& jv, SLE const& sle)
51{
53 XRPL_ASSERT(sle.getType() == ltACCOUNT_ROOT, "xrpl::injectSLE : sle is account root");
54 if (sle.isFieldPresent(sfEmailHash))
55 {
56 auto const& hash = sle.getFieldH128(sfEmailHash);
57 Blob const b(hash.begin(), hash.end());
59 md5 = toLower(md5);
60 // VFALCO TODO Give a name to this constant and move it
61 // to a more visible location.
62 jv[jss::urlgravatar] = std::format("https://www.gravatar.com/avatar/{}", md5);
63 }
64}
65
66// {
67// account: <ident>,
68// ledger_hash : <ledger>
69// ledger_index : <ledger_index>
70// signer_lists : <bool> // optional (default false)
71// // if true return SignerList(s).
72// queue : <bool> // optional (default false)
73// // if true return information about transactions
74// // in the current TxQ, only if the requested
75// // ledger is open. Otherwise if true, returns an
76// // error.
77// }
78
79// TODO(tom): what is that "default"?
82{
83 auto& params = context.params;
84
85 std::string strIdent;
86 if (params.isMember(jss::account))
87 {
88 if (!params[jss::account].isString())
89 return rpc::invalidFieldError(jss::account);
90 strIdent = params[jss::account].asString();
91 }
92 else if (params.isMember(jss::ident))
93 {
94 if (!params[jss::ident].isString())
95 return rpc::invalidFieldError(jss::ident);
96 strIdent = params[jss::ident].asString();
97 }
98 else
99 {
100 return rpc::missingFieldError(jss::account);
101 }
102
104 auto result = rpc::lookupLedger(ledger, context);
105
106 if (!ledger)
107 return result;
108
109 // Get info on account.
110 auto id = parseBase58<AccountID>(strIdent);
111 if (!id)
112 {
114 return result;
115 }
116 auto const accountID{id.value()};
117
119 {{"defaultRipple", lsfDefaultRipple},
120 {"depositAuth", lsfDepositAuth},
121 {"disableMasterKey", lsfDisableMaster},
122 {"disallowIncomingXRP", lsfDisallowXRP},
123 {"globalFreeze", lsfGlobalFreeze},
124 {"noFreeze", lsfNoFreeze},
125 {"passwordSpent", lsfPasswordSpent},
126 {"requireAuthorization", lsfRequireAuth},
127 {"requireDestinationTag", lsfRequireDestTag}}};
128
130 kDisallowIncomingFlags{
131 {{"disallowIncomingNFTokenOffer", lsfDisallowIncomingNFTokenOffer},
132 {"disallowIncomingCheck", lsfDisallowIncomingCheck},
133 {"disallowIncomingPayChan", lsfDisallowIncomingPayChan},
134 {"disallowIncomingTrustline", lsfDisallowIncomingTrustline}}};
135
136 static constexpr std::pair<std::string_view, LedgerSpecificFlags> kAllowTrustLineClawbackFlag{
137 "allowTrustLineClawback", lsfAllowTrustLineClawback};
138
139 static constexpr std::pair<std::string_view, LedgerSpecificFlags> kAllowTrustLineLockingFlag{
140 "allowTrustLineLocking", lsfAllowTrustLineLocking};
141
142 auto const sleAccepted = ledger->read(keylet::account(accountID));
143 if (sleAccepted)
144 {
145 auto const queue = params.isMember(jss::queue) && params[jss::queue].asBool();
146
147 if (queue && !ledger->open())
148 {
149 // It doesn't make sense to request the queue
150 // with any closed or validated ledger.
152 return result;
153 }
154
156 injectSLE(jvAccepted, *sleAccepted);
157 result[jss::account_data] = jvAccepted;
158
160 for (auto const& lsf : kLsFlags)
161 acctFlags[lsf.first.data()] = sleAccepted->isFlag(lsf.second);
162
163 for (auto const& lsf : kDisallowIncomingFlags)
164 acctFlags[lsf.first.data()] = sleAccepted->isFlag(lsf.second);
165
166 acctFlags[kAllowTrustLineClawbackFlag.first.data()] =
167 sleAccepted->isFlag(kAllowTrustLineClawbackFlag.second);
168
169 if (ledger->rules().enabled(featureTokenEscrow))
170 {
171 acctFlags[kAllowTrustLineLockingFlag.first.data()] =
172 sleAccepted->isFlag(kAllowTrustLineLockingFlag.second);
173 }
174
175 result[jss::account_flags] = std::move(acctFlags);
176
177 auto const pseudoFields = getPseudoAccountFields();
178 for (auto const& pseudoField : pseudoFields)
179 {
180 if (sleAccepted->isFieldPresent(*pseudoField))
181 {
182 std::string name = pseudoField->fieldName;
183 if (name.ends_with("ID"))
184 {
185 // Remove the ID suffix from the field name.
186 name = name.substr(0, name.size() - 2);
187 XRPL_ASSERT_PARTS(!name.empty(), "xrpl::doAccountInfo", "name is not empty");
188 }
189 // ValidPseudoAccounts invariant guarantees that only one field
190 // can be set
191 result[jss::pseudo_account][jss::type] = name;
192 break;
193 }
194 }
195
196 // The document[https://xrpl.org/account_info.html#account_info] states
197 // that signer_lists is a bool, however assigning any string value
198 // works. Do not allow this. This check is for api Version 2 onwards
199 // only
200 if (context.apiVersion > 1u && params.isMember(jss::signer_lists) &&
201 !params[jss::signer_lists].isBool())
202 {
204 return result;
205 }
206
207 // Return SignerList(s) if that is requested.
208 if (params.isMember(jss::signer_lists) && params[jss::signer_lists].asBool())
209 {
210 // We put the SignerList in an array because of an anticipated
211 // future when we support multiple signer lists on one account.
212 json::Value jvSignerList = json::ValueType::Array;
213
214 // This code will need to be revisited if in the future we support
215 // multiple SignerLists on one account.
216 auto const sleSigners = ledger->read(keylet::signerList(accountID));
217 if (sleSigners)
218 jvSignerList.append(sleSigners->getJson(JsonOptions::Values::None));
219
220 // Documentation states this is returned as part of the account_info
221 // response, but previously the code put it under account_data. We
222 // can move this to the documented location from apiVersion 2
223 // onwards.
224 if (context.apiVersion == 1)
225 {
226 result[jss::account_data][jss::signer_lists] = std::move(jvSignerList);
227 }
228 else
229 {
230 result[jss::signer_lists] = std::move(jvSignerList);
231 }
232 }
233 // Return queue info if that is requested
234 if (queue)
235 {
237
238 auto const txs = context.app.getTxQ().getAccountTxs(accountID);
239 if (!txs.empty())
240 {
241 jvQueueData[jss::txn_count] = static_cast<json::UInt>(txs.size());
242
243 auto& jvQueueTx = jvQueueData[jss::transactions];
244 jvQueueTx = json::ValueType::Array;
245
246 std::uint32_t seqCount = 0;
247 std::uint32_t ticketCount = 0;
250 std::optional<std::uint32_t> lowestTicket;
251 std::optional<std::uint32_t> highestTicket;
252 bool anyAuthChanged = false;
253 XRPAmount totalSpend(0);
254
255 // We expect txs to be returned sorted by SeqProxy. Verify
256 // that with a couple of asserts.
257 SeqProxy prevSeqProxy = SeqProxy::rawSequence(0);
258 for (auto const& tx : txs)
259 {
261
262 if (tx.seqProxy.isSeq())
263 {
264 XRPL_ASSERT(
265 prevSeqProxy < tx.seqProxy, "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, "doAccountInfo : second sorted proxy");
277 prevSeqProxy = tx.seqProxy;
278 jvTx[jss::ticket] = tx.seqProxy.value();
279 ++ticketCount;
280 if (!lowestTicket)
281 lowestTicket = tx.seqProxy.value();
282 highestTicket = tx.seqProxy.value();
283 }
284
285 jvTx[jss::fee_level] = to_string(tx.feeLevel);
286 if (tx.lastValid)
287 jvTx[jss::LastLedgerSequence] = *tx.lastValid;
288
289 jvTx[jss::fee] = to_string(tx.consequences.fee());
290 auto const spend = tx.consequences.potentialSpend() + tx.consequences.fee();
291 jvTx[jss::max_spend_drops] = to_string(spend);
292 totalSpend += spend;
293 bool const authChanged = tx.consequences.isBlocker();
294 if (authChanged)
295 anyAuthChanged = authChanged;
296 jvTx[jss::auth_change] = authChanged;
297
298 jvQueueTx.append(std::move(jvTx));
299 }
300
301 if (seqCount != 0u)
302 jvQueueData[jss::sequence_count] = seqCount;
303 if (ticketCount != 0u)
304 jvQueueData[jss::ticket_count] = ticketCount;
305 if (lowestSeq)
306 jvQueueData[jss::lowest_sequence] = *lowestSeq;
307 if (highestSeq)
308 jvQueueData[jss::highest_sequence] = *highestSeq;
309 if (lowestTicket)
310 jvQueueData[jss::lowest_ticket] = *lowestTicket;
311 if (highestTicket)
312 jvQueueData[jss::highest_ticket] = *highestTicket;
313
314 jvQueueData[jss::auth_change_queued] = anyAuthChanged;
315 jvQueueData[jss::max_spend_drops_total] = to_string(totalSpend);
316 }
317 else
318 {
319 jvQueueData[jss::txn_count] = 0u;
320 }
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:117
Value & append(Value const &value)
Append value to array at the end.
LedgerEntryType getType() const
json::Value getJson(JsonOptions options=JsonOptions::Values::None) const override
uint128 getFieldH128(SField const &field) const
Definition STObject.cpp:613
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:37
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
virtual TxQ & getTxQ()=0
std::vector< TxDetails > getAccountTxs(AccountID const &account) const
Returns information about the transactions currently in the queue for the account.
Definition TxQ.cpp:1794
T empty(T... args)
T ends_with(T... args)
T format(T... args)
unsigned int UInt
@ Array
array value (ordered list)
Definition json_value.h:28
@ Object
object value (collection of name/value pairs).
Definition json_value.h:29
Keylet signerList(AccountID const &account) noexcept
A SignerList.
Definition Indexes.cpp:326
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:198
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
json::Value invalidFieldError(std::string const &name)
Definition ErrorCodes.h:285
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:243
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::vector< SField const * > const & getPseudoAccountFields()
Returns the list of fields that define an ACCOUNT_ROOT as a pseudo-account if set.
@ RpcActNotFound
Definition ErrorCodes.h:53
@ RpcActMalformed
Definition ErrorCodes.h:73
@ RpcInvalidParams
Definition ErrorCodes.h:67
void injectSLE(json::Value &jv, SLE const &sle)
Injects JSON describing a ledger entry.
std::string toLower(std::string str)
Fold ASCII upper case letters to lower case.
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
json::Value doAccountInfo(rpc::JsonContext &context)
STLedgerEntry SLE
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
T size(T... args)
unsigned int apiVersion
Definition Context.h:37
Application & app
Definition Context.h:29
json::Value params
Definition Context.h:51
T substr(T... args)
T value(T... args)