xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
rpc
handlers
orderbook
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
26
namespace
xrpl::rpc
{
27
28
DepositAuthorizedHandler::DepositAuthorizedHandler
(
JsonContext
& context) :
context_
(context)
29
{
30
}
31
32
std::expected<DepositAuthorizedHandler::Output, ::rpc::Status>
33
DepositAuthorizedHandler::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
63
std::set<std::pair<AccountID, Slice>
> sorted;
64
std::vector<SLE::const_pointer>
lifeExtender;
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{
94
RpcBadCredentials
,
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
123
void
124
DepositAuthorizedHandler::writeResult
(
json::Value
& value,
Output
const
& output)
const
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
json::Value
Represents a JSON value.
Definition
json_value.h:121
xrpl::STLedgerEntry::const_pointer
std::shared_ptr< STLedgerEntry const > const_pointer
Definition
STLedgerEntry.h:36
xrpl::rpc::DepositAuthorizedHandler::DepositAuthorizedHandler
DepositAuthorizedHandler(JsonContext &context)
Construct a handler for a single request.
Definition
DepositAuthorized.cpp:28
xrpl::rpc::DepositAuthorizedHandler::process
std::expected< Output, ::rpc::Status > process(Input const &input) const
Process the request.
Definition
DepositAuthorized.cpp:33
xrpl::rpc::DepositAuthorizedHandler::context_
std::reference_wrapper< JsonContext > context_
Definition
DepositAuthorized.h:63
xrpl::rpc::DepositAuthorizedHandler::writeResult
void writeResult(json::Value &value, Output const &output) const
Write the output of process() into the response.
Definition
DepositAuthorized.cpp:124
std::set::emplace
T emplace(T... args)
xrpl::credentials::checkExpired
bool checkExpired(SLE const &sleCredential, NetClock::time_point const &closed)
Definition
CredentialHelpers.cpp:39
xrpl::keylet::depositPreauth
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Definition
Indexes.cpp:386
xrpl::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition
Indexes.cpp:233
xrpl::keylet::credential
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Definition
Indexes.cpp:559
xrpl::rpc
The api_version numbers this server serves.
Definition
ApiVersion.h:26
xrpl::rpc::getLedger
::rpc::Status getLedger(T &ledger, UInt256 const &ledgerHash, Context const &context)
Retrieves a ledger by its hash.
Definition
RPCLedgerHelpers.cpp:240
xrpl::rpc::injectLedgerFields
void injectLedgerFields(ReadView const &ledger, Context const &context, json::Value &result)
Writes the fields that identify a ledger into a result.
Definition
RPCLedgerHelpers.cpp:391
xrpl::RpcDstActNotFound
@ RpcDstActNotFound
Definition
ErrorCodes.h:88
xrpl::RpcBadCredentials
@ RpcBadCredentials
Definition
ErrorCodes.h:135
xrpl::RpcSrcActNotFound
@ RpcSrcActNotFound
Definition
ErrorCodes.h:105
std::optional::has_value
T has_value(T... args)
std::vector::push_back
T push_back(T... args)
std::vector::reserve
T reserve(T... args)
set
xrpl::rpc::DepositAuthorizedHandler::Output
The data a deposit_authorized request produces, written out by writeResult().
Definition
DepositAuthorized.h:31
xrpl::rpc::DepositAuthorizedHandler::Output::ledger
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
Definition
DepositAuthorized.h:32
xrpl::rpc::DepositAuthorizedHandler::Output::depositAuthorized
bool depositAuthorized
Whether the source may deposit.
Definition
DepositAuthorized.h:34
xrpl::rpc::DepositAuthorizedHandler::Output::error
std::optional<::rpc::Status > error
An error written with the ledger fields.
Definition
DepositAuthorized.h:33
xrpl::rpc::JsonContext
Definition
Context.h:41
std::unexpected
T unexpected(T... args)
utility
vector
Generated by
1.17.0