rippled
utility.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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/basics/contract.h>
21 #include <ripple/json/Object.h>
22 #include <ripple/net/RPCCall.h>
23 #include <ripple/protocol/ErrorCodes.h>
24 #include <ripple/protocol/HashPrefix.h>
25 #include <ripple/protocol/Indexes.h>
26 #include <ripple/protocol/STParsedJSON.h>
27 #include <ripple/protocol/UintTypes.h>
28 #include <ripple/protocol/jss.h>
29 #include <cstring>
30 #include <test/jtx/utility.h>
31 
32 namespace ripple {
33 namespace test {
34 namespace jtx {
35 
36 STObject
37 parse(Json::Value const& jv)
38 {
39  STParsedJSONObject p("tx_json", jv);
40  if (!p.object)
41  Throw<parse_error>(rpcErrorString(p.error));
42  return std::move(*p.object);
43 }
44 
45 void
46 sign(Json::Value& jv, Account const& account)
47 {
48  jv[jss::SigningPubKey] = strHex(account.pk().slice());
49  Serializer ss;
52  auto const sig = ripple::sign(account.pk(), account.sk(), ss.slice());
53  jv[jss::TxnSignature] = strHex(Slice{sig.data(), sig.size()});
54 }
55 
56 void
57 fill_fee(Json::Value& jv, ReadView const& view)
58 {
59  if (jv.isMember(jss::Fee))
60  return;
61  jv[jss::Fee] = to_string(view.fees().base);
62 }
63 
64 void
65 fill_seq(Json::Value& jv, ReadView const& view)
66 {
67  if (jv.isMember(jss::Sequence))
68  return;
69  auto const account = parseBase58<AccountID>(jv[jss::Account].asString());
70  if (!account)
71  Throw<parse_error>("unexpected invalid Account");
72  auto const ar = view.read(keylet::account(*account));
73  if (!ar)
74  Throw<parse_error>("unexpected missing account root");
75  jv[jss::Sequence] = ar->getFieldU32(sfSequence);
76 }
77 
80  std::vector<std::string> const& args,
82  unsigned int apiVersion)
83 {
85  auto const paramsObj = rpcCmdToJson(args, jv, apiVersion, j);
86 
87  // Re-use jv to return our formatted result.
88  jv.clear();
89 
90  // Allow parser to rewrite method.
91  jv[jss::method] = paramsObj.isMember(jss::method)
92  ? paramsObj[jss::method].asString()
93  : args[0];
94 
95  // If paramsObj is not empty, put it in a [params] array.
96  if (paramsObj.begin() != paramsObj.end())
97  {
98  auto& paramsArray = Json::setArray(jv, jss::params);
99  paramsArray.append(paramsObj);
100  }
101  if (paramsObj.isMember(jss::jsonrpc))
102  jv[jss::jsonrpc] = paramsObj[jss::jsonrpc];
103  if (paramsObj.isMember(jss::ripplerpc))
104  jv[jss::ripplerpc] = paramsObj[jss::ripplerpc];
105  if (paramsObj.isMember(jss::id))
106  jv[jss::id] = paramsObj[jss::id];
107  return jv;
108 }
109 
110 } // namespace jtx
111 } // namespace test
112 } // namespace ripple
cstring
ripple::rpcErrorString
std::string rpcErrorString(Json::Value const &jv)
Returns a single string with the contents of an RPC error.
Definition: ErrorCodes.cpp:212
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::sfSequence
const SF_UINT32 sfSequence
std::vector< std::string >
ripple::ReadView::fees
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
ripple::rpcCmdToJson
Json::Value rpcCmdToJson(std::vector< std::string > const &args, Json::Value &retParams, unsigned int apiVersion, beast::Journal j)
Definition: RPCCall.cpp:1476
ripple::test::jtx::fill_seq
void fill_seq(Json::Value &jv, ReadView const &view)
Set the sequence number automatically.
Definition: utility.cpp:65
ripple::STParsedJSONObject
Holds the serialized result of parsing an input JSON object.
Definition: STParsedJSON.h:31
ripple::test::jtx::sign
void sign(Json::Value &jv, Account const &account)
Sign automatically.
Definition: utility.cpp:46
ripple::STParsedJSONObject::object
std::optional< STObject > object
The STObject if the parse was successful.
Definition: STParsedJSON.h:50
ripple::STParsedJSONObject::error
Json::Value error
On failure, an appropriate set of error values.
Definition: STParsedJSON.h:53
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:142
ripple::test::jtx::fill_fee
void fill_fee(Json::Value &jv, ReadView const &view)
Set the fee automatically.
Definition: utility.cpp:57
ripple::test::jtx::cmdToJSONRPC
Json::Value cmdToJSONRPC(std::vector< std::string > const &args, beast::Journal j, unsigned int apiVersion)
Given a rippled unit test rpc command, return the corresponding JSON.
Definition: utility.cpp:79
ripple::Serializer::slice
Slice slice() const noexcept
Definition: Serializer.h:64
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::test::jtx::sig
Set the regular signature on a JTx.
Definition: sig.h:34
ripple::STObject::addWithoutSigningFields
void addWithoutSigningFields(Serializer &s) const
Definition: STObject.h:893
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
Json::setArray
Json::Value & setArray(Json::Value &, Json::StaticString const &key)
Add a new subarray at a named key in a Json object.
Definition: Object.h:414
ripple::Serializer
Definition: Serializer.h:40
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:54
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
Json::Value::clear
void clear()
Remove all object members and array elements.
Definition: json_value.cpp:753
ripple::sign
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &m)
Generate a signature for a message.
Definition: SecretKey.cpp:238
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::Serializer::add32
int add32(std::uint32_t i)
Definition: Serializer.cpp:38
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
ripple::HashPrefix::txSign
@ txSign
inner transaction to sign
ripple::test::jtx::parse
STObject parse(Json::Value const &jv)
Convert JSON to STObject.
Definition: utility.cpp:37
ripple::Fees::base
XRPAmount base
Definition: protocol/Fees.h:34
Json::Value
Represents a JSON value.
Definition: json_value.h:145