xrpld
Loading...
Searching...
No Matches
DepositAuthorized.cpp
1#include <xrpld/rpc/handlers/orderbook/DepositAuthorized.h>
2
3#include <xrpld/rpc/Context.h>
4#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
5#include <xrpld/rpc/detail/SpecBridge.hpp>
6
7#include <xrpl/basics/Slice.h>
8#include <xrpl/json/json_value.h>
9#include <xrpl/ledger/ReadView.h>
10#include <xrpl/ledger/helpers/CredentialHelpers.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/ErrorCodes.h>
13#include <xrpl/protocol/Indexes.h>
14#include <xrpl/protocol/LedgerFormats.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STLedgerEntry.h>
17#include <xrpl/protocol/jss.h>
18
19#include <rpcspec/Errors.hpp>
20
21#include <expected>
22#include <set>
23#include <utility>
24#include <vector>
25
26namespace xrpl::rpc {
27
31
32std::expected<DepositAuthorizedHandler::Output, ::rpc::Status>
33DepositAuthorizedHandler::process(Input const& input) const
34{
35 Output output;
36 if (auto const status = getLedger(output.ledger, input.ledger, context_.get());
37 status != ::rpc::Status::kOK)
38 {
39 return std::unexpected{status};
40 }
41 auto const& ledger = *output.ledger;
42
43 auto const& srcAccount = input.sourceAccount;
44 auto const& dstAccount = input.destinationAccount;
45
46 // If source account is not in the ledger it can't be authorized.
47 if (not ledger.exists(keylet::account(srcAccount)))
48 {
49 output.error = RpcSrcActNotFound;
50 return output;
51 }
52
53 // If destination account is not in the ledger you can't deposit to it, eh?
54 auto const sleDest = ledger.read(keylet::account(dstAccount));
55 if (!sleDest)
56 {
57 output.error = RpcDstActNotFound;
58 return output;
59 }
60
61 bool const reqAuth = sleDest->isFlag(lsfDepositAuth) && (srcAccount != dstAccount);
62
65 if (input.credentials.has_value())
66 {
67 lifeExtender.reserve(input.credentials->size());
68 for (auto const& credH : *input.credentials)
69 {
70 // A zero key names an entry that cannot exist
71 SLE::const_pointer sleCred =
72 credH.isZero() ? nullptr : ledger.read(keylet::credential(credH));
73 if (!sleCred)
74 {
75 output.error = ::rpc::Status{RpcBadCredentials, "credentials don't exist"};
76 return output;
77 }
78
79 if (!sleCred->isFlag(lsfAccepted))
80 {
81 output.error = ::rpc::Status{RpcBadCredentials, "credentials aren't accepted"};
82 return output;
83 }
84
85 if (credentials::checkExpired(*sleCred, ledger.header().parentCloseTime))
86 {
87 output.error = ::rpc::Status{RpcBadCredentials, "credentials are expired"};
88 return output;
89 }
90
91 if ((*sleCred)[sfSubject] != srcAccount)
92 {
93 output.error = ::rpc::Status{
95 "credentials doesn't belong to the root account",
96 };
97 return output;
98 }
99
100 auto [it, ins] = sorted.emplace((*sleCred)[sfIssuer], (*sleCred)[sfCredentialType]);
101 if (!ins)
102 {
103 output.error = ::rpc::Status{RpcBadCredentials, "duplicates in credentials"};
104 return output;
105 }
106 lifeExtender.push_back(std::move(sleCred));
107 }
108 }
109
110 // If the two accounts are the same OR if that flag is
111 // not set, then the deposit should be fine.
112 output.depositAuthorized = true;
113 if (reqAuth)
114 {
115 output.depositAuthorized = ledger.exists(keylet::depositPreauth(dstAccount, srcAccount)) or
116 (input.credentials.has_value() and
117 ledger.exists(keylet::depositPreauth(dstAccount, sorted)));
118 }
119
120 return output;
121}
122
123void
125{
126 auto const& params = context_.get().params;
127
128 injectLedgerFields(*output.ledger, context_.get(), value);
129
130 if (output.error.has_value())
131 {
132 injectSpecError(value, *output.error);
133 return;
134 }
135
136 value[jss::source_account] = params[jss::source_account].asString();
137 value[jss::destination_account] = params[jss::destination_account].asString();
138 if (params.isMember(jss::credentials))
139 value[jss::credentials] = params[jss::credentials];
140
141 value[jss::deposit_authorized] = output.depositAuthorized;
142}
143
144} // namespace xrpl::rpc
Represents a JSON value.
Definition json_value.h:121
std::shared_ptr< STLedgerEntry const > const_pointer
DepositAuthorizedHandler(JsonContext &context)
Construct a handler for a single request.
std::expected< Output, ::rpc::Status > process(Input const &input) const
Process the request.
std::reference_wrapper< JsonContext > context_
void writeResult(json::Value &value, Output const &output) const
Write the output of process() into the response.
T emplace(T... args)
bool checkExpired(SLE const &sleCredential, NetClock::time_point const &closed)
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Definition Indexes.cpp:386
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Definition Indexes.cpp:559
The api_version numbers this server serves.
Definition ApiVersion.h:26
::rpc::Status getLedger(T &ledger, UInt256 const &ledgerHash, Context const &context)
Retrieves a ledger by its hash.
void injectLedgerFields(ReadView const &ledger, Context const &context, json::Value &result)
Writes the fields that identify a ledger into a result.
@ RpcDstActNotFound
Definition ErrorCodes.h:88
@ RpcBadCredentials
Definition ErrorCodes.h:135
@ RpcSrcActNotFound
Definition ErrorCodes.h:105
T has_value(T... args)
T push_back(T... args)
T reserve(T... args)
The data a deposit_authorized request produces, written out by writeResult().
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
bool depositAuthorized
Whether the source may deposit.
std::optional<::rpc::Status > error
An error written with the ledger fields.
T unexpected(T... args)