rippled
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 <ripple/ledger/ReadView.h>
21 #include <ripple/net/RPCErr.h>
22 #include <ripple/protocol/ErrorCodes.h>
23 #include <ripple/protocol/Indexes.h>
24 #include <ripple/protocol/jss.h>
25 #include <ripple/rpc/Context.h>
26 #include <ripple/rpc/impl/RPCHelpers.h>
27 
28 namespace ripple {
29 
30 // {
31 // source_account : <ident>
32 // destination_account : <ident>
33 // ledger_hash : <ledger>
34 // ledger_index : <ledger_index>
35 // }
36 
39 {
40  Json::Value const& params = context.params;
41 
42  // Validate source_account.
43  if (!params.isMember(jss::source_account))
44  return RPC::missing_field_error(jss::source_account);
45  if (!params[jss::source_account].isString())
46  return RPC::make_error(
48  RPC::expected_field_message(jss::source_account, "a string"));
49 
50  auto srcID = parseBase58<AccountID>(params[jss::source_account].asString());
51  if (!srcID)
52  return rpcError(rpcACT_MALFORMED);
53  auto const srcAcct{std::move(srcID.value())};
54 
55  // Validate destination_account.
56  if (!params.isMember(jss::destination_account))
57  return RPC::missing_field_error(jss::destination_account);
58  if (!params[jss::destination_account].isString())
59  return RPC::make_error(
61  RPC::expected_field_message(jss::destination_account, "a string"));
62 
63  auto dstID =
64  parseBase58<AccountID>(params[jss::destination_account].asString());
65  if (!dstID)
66  return rpcError(rpcACT_MALFORMED);
67  auto const dstAcct{std::move(dstID.value())};
68 
69  // Validate ledger.
71  Json::Value result = RPC::lookupLedger(ledger, context);
72 
73  if (!ledger)
74  return result;
75 
76  // If source account is not in the ledger it can't be authorized.
77  if (!ledger->exists(keylet::account(srcAcct)))
78  {
80  return result;
81  }
82 
83  // If destination account is not in the ledger you can't deposit to it, eh?
84  auto const sleDest = ledger->read(keylet::account(dstAcct));
85  if (!sleDest)
86  {
88  return result;
89  }
90 
91  // If the two accounts are the same, then the deposit should be fine.
92  bool depositAuthorized{true};
93  if (srcAcct != dstAcct)
94  {
95  // Check destination for the DepositAuth flag. If that flag is
96  // not set then a deposit should be just fine.
97  if (sleDest->getFlags() & lsfDepositAuth)
98  {
99  // See if a preauthorization entry is in the ledger.
100  auto const sleDepositAuth =
101  ledger->read(keylet::depositPreauth(dstAcct, srcAcct));
102  depositAuthorized = static_cast<bool>(sleDepositAuth);
103  }
104  }
105  result[jss::source_account] = params[jss::source_account].asString();
106  result[jss::destination_account] =
107  params[jss::destination_account].asString();
108 
109  result[jss::deposit_authorized] = depositAuthorized;
110  return result;
111 }
112 
113 } // namespace ripple
ripple::RPC::JsonContext
Definition: Context.h:53
std::shared_ptr
STL class.
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
ripple::rpcSRC_ACT_NOT_FOUND
@ rpcSRC_ACT_NOT_FOUND
Definition: ErrorCodes.h:122
ripple::RPC::lookupLedger
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:675
ripple::lsfDepositAuth
@ lsfDepositAuth
Definition: LedgerFormats.h:234
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:262
ripple::RPC::expected_field_message
std::string expected_field_message(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:316
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:133
ripple::ReadView::exists
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::rpcDST_ACT_NOT_FOUND
@ rpcDST_ACT_NOT_FOUND
Definition: ErrorCodes.h:105
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::rpcACT_MALFORMED
@ rpcACT_MALFORMED
Definition: ErrorCodes.h:90
ripple::doDepositAuthorized
Json::Value doDepositAuthorized(RPC::JsonContext &context)
Definition: DepositAuthorized.cpp:38
ripple::keylet::depositPreauth
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Definition: Indexes.cpp:287
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::RPC::inject_error
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition: ErrorCodes.h:212
ripple::RPC::make_error
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Definition: ErrorCodes.cpp:178
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469