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/protocol/ErrorCodes.h>
22 #include <ripple/protocol/Indexes.h>
23 #include <ripple/protocol/jss.h>
24 #include <ripple/rpc/Context.h>
25 #include <ripple/rpc/impl/RPCHelpers.h>
26 
27 namespace ripple {
28 
29 // {
30 // source_account : <ident>
31 // destination_account : <ident>
32 // ledger_hash : <ledger>
33 // ledger_index : <ledger_index>
34 // }
35 
38 {
39  Json::Value const& params = context.params;
40 
41  // Validate source_account.
42  if (!params.isMember(jss::source_account))
43  return RPC::missing_field_error(jss::source_account);
44  if (!params[jss::source_account].isString())
45  return RPC::make_error(
47  RPC::expected_field_message(jss::source_account, "a string"));
48 
49  AccountID srcAcct;
50  {
51  Json::Value const jvAccepted = RPC::accountFromString(
52  srcAcct, params[jss::source_account].asString(), true);
53  if (jvAccepted)
54  return jvAccepted;
55  }
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  AccountID dstAcct;
66  {
67  Json::Value const jvAccepted = RPC::accountFromString(
68  dstAcct, params[jss::destination_account].asString(), true);
69  if (jvAccepted)
70  return jvAccepted;
71  }
72 
73  // Validate ledger.
75  Json::Value result = RPC::lookupLedger(ledger, context);
76 
77  if (!ledger)
78  return result;
79 
80  // If source account is not in the ledger it can't be authorized.
81  if (!ledger->exists(keylet::account(srcAcct)))
82  {
84  return result;
85  }
86 
87  // If destination account is not in the ledger you can't deposit to it, eh?
88  auto const sleDest = ledger->read(keylet::account(dstAcct));
89  if (!sleDest)
90  {
92  return result;
93  }
94 
95  // If the two accounts are the same, then the deposit should be fine.
96  bool depositAuthorized{true};
97  if (srcAcct != dstAcct)
98  {
99  // Check destination for the DepositAuth flag. If that flag is
100  // not set then a deposit should be just fine.
101  if (sleDest->getFlags() & lsfDepositAuth)
102  {
103  // See if a preauthorization entry is in the ledger.
104  auto const sleDepositAuth =
105  ledger->read(keylet::depositPreauth(dstAcct, srcAcct));
106  depositAuthorized = static_cast<bool>(sleDepositAuth);
107  }
108  }
109  result[jss::source_account] = params[jss::source_account].asString();
110  result[jss::destination_account] =
111  params[jss::destination_account].asString();
112 
113  result[jss::deposit_authorized] = depositAuthorized;
114  return result;
115 }
116 
117 } // namespace ripple
ripple::RPC::JsonContext
Definition: Context.h:53
std::shared_ptr
STL class.
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
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:577
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::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:81
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::doDepositAuthorized
Json::Value doDepositAuthorized(RPC::JsonContext &context)
Definition: DepositAuthorized.cpp:37
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::accountFromString
Json::Value accountFromString(AccountID &result, std::string const &strIdent, bool bStrict)
Definition: RPCHelpers.cpp:85
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