rippled
Loading...
Searching...
No Matches
DepositAuthorized.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2018 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/misc/CredentialHelpers.h>
21#include <xrpld/ledger/ReadView.h>
22#include <xrpld/rpc/Context.h>
23#include <xrpld/rpc/detail/RPCHelpers.h>
24#include <xrpl/protocol/ErrorCodes.h>
25#include <xrpl/protocol/Indexes.h>
26#include <xrpl/protocol/RPCErr.h>
27#include <xrpl/protocol/jss.h>
28
29namespace ripple {
30
31// {
32// source_account : <ident>
33// destination_account : <ident>
34// ledger_hash : <ledger>
35// ledger_index : <ledger_index>
36// credentials : [<credentialID>,...]
37// }
38
41{
42 Json::Value const& params = context.params;
43
44 // Validate source_account.
45 if (!params.isMember(jss::source_account))
46 return RPC::missing_field_error(jss::source_account);
47 if (!params[jss::source_account].isString())
48 return RPC::make_error(
50 RPC::expected_field_message(jss::source_account, "a string"));
51
52 auto srcID = parseBase58<AccountID>(params[jss::source_account].asString());
53 if (!srcID)
55 auto const srcAcct{std::move(srcID.value())};
56
57 // Validate destination_account.
58 if (!params.isMember(jss::destination_account))
59 return RPC::missing_field_error(jss::destination_account);
60 if (!params[jss::destination_account].isString())
61 return RPC::make_error(
63 RPC::expected_field_message(jss::destination_account, "a string"));
64
65 auto dstID =
66 parseBase58<AccountID>(params[jss::destination_account].asString());
67 if (!dstID)
69 auto const dstAcct{std::move(dstID.value())};
70
71 // Validate ledger.
73 Json::Value result = RPC::lookupLedger(ledger, context);
74
75 if (!ledger)
76 return result;
77
78 // If source account is not in the ledger it can't be authorized.
79 if (!ledger->exists(keylet::account(srcAcct)))
80 {
82 return result;
83 }
84
85 // If destination account is not in the ledger you can't deposit to it, eh?
86 auto const sleDest = ledger->read(keylet::account(dstAcct));
87 if (!sleDest)
88 {
90 return result;
91 }
92
93 bool const reqAuth =
94 (sleDest->getFlags() & lsfDepositAuth) && (srcAcct != dstAcct);
95 bool const credentialsPresent = params.isMember(jss::credentials);
96
99 if (credentialsPresent)
100 {
101 auto const& creds(params[jss::credentials]);
102 if (!creds.isArray() || !creds)
103 {
104 return RPC::make_error(
107 jss::credentials,
108 "is non-empty array of CredentialID(hash256)"));
109 }
110 else if (creds.size() > maxCredentialsArraySize)
111 {
112 return RPC::make_error(
115 jss::credentials, "array too long"));
116 }
117
118 lifeExtender.reserve(creds.size());
119 for (auto const& jo : creds)
120 {
121 if (!jo.isString())
122 {
123 return RPC::make_error(
126 jss::credentials, "an array of CredentialID(hash256)"));
127 }
128
129 uint256 credH;
130 auto const credS = jo.asString();
131 if (!credH.parseHex(credS))
132 {
133 return RPC::make_error(
136 jss::credentials, "an array of CredentialID(hash256)"));
137 }
138
140 ledger->read(keylet::credential(credH));
141 if (!sleCred)
142 {
144 rpcBAD_CREDENTIALS, "credentials don't exist", result);
145 return result;
146 }
147
148 if (!(sleCred->getFlags() & lsfAccepted))
149 {
151 rpcBAD_CREDENTIALS, "credentials aren't accepted", result);
152 return result;
153 }
154
156 sleCred, ledger->info().parentCloseTime))
157 {
159 rpcBAD_CREDENTIALS, "credentials are expired", result);
160 return result;
161 }
162
163 if ((*sleCred)[sfSubject] != srcAcct)
164 {
167 "credentials doesn't belong to the root account",
168 result);
169 return result;
170 }
171
172 auto [it, ins] = sorted.emplace(
173 (*sleCred)[sfIssuer], (*sleCred)[sfCredentialType]);
174 if (!ins)
175 {
177 rpcBAD_CREDENTIALS, "duplicates in credentials", result);
178 return result;
179 }
180 lifeExtender.push_back(std::move(sleCred));
181 }
182 }
183
184 // If the two accounts are the same OR if that flag is
185 // not set, then the deposit should be fine.
186 bool depositAuthorized = true;
187 if (reqAuth)
188 depositAuthorized =
189 ledger->exists(keylet::depositPreauth(dstAcct, srcAcct)) ||
190 (credentialsPresent &&
191 ledger->exists(keylet::depositPreauth(dstAcct, sorted)));
192
193 result[jss::source_account] = params[jss::source_account].asString();
194 result[jss::destination_account] =
195 params[jss::destination_account].asString();
196 if (credentialsPresent)
197 result[jss::credentials] = params[jss::credentials];
198
199 result[jss::deposit_authorized] = depositAuthorized;
200 return result;
201}
202
203} // namespace ripple
Represents a JSON value.
Definition: json_value.h:147
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:943
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:502
T emplace(T... args)
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Definition: ErrorCodes.cpp:181
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition: ErrorCodes.h:223
std::string expected_field_message(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:327
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.
Definition: RPCHelpers.cpp:622
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:273
bool checkExpired(std::shared_ptr< SLE const > const &sleCredential, NetClock::time_point const &closed)
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Definition: Indexes.cpp:521
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:160
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Definition: Indexes.cpp:318
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ rpcSRC_ACT_NOT_FOUND
Definition: ErrorCodes.h:122
@ rpcACT_MALFORMED
Definition: ErrorCodes.h:90
@ rpcBAD_CREDENTIALS
Definition: ErrorCodes.h:152
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
@ rpcDST_ACT_NOT_FOUND
Definition: ErrorCodes.h:105
@ lsfDepositAuth
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
Json::Value doDepositAuthorized(RPC::JsonContext &context)
std::size_t constexpr maxCredentialsArraySize
The maximum number of credentials can be passed in array.
Definition: Protocol.h:106
T push_back(T... args)
T reserve(T... args)
Json::Value params
Definition: Context.h:64