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