rippled
Loading...
Searching...
No Matches
utility.cpp
1#include <test/jtx/utility.h>
2
3#include <xrpld/rpc/RPCCall.h>
4
5#include <xrpl/basics/contract.h>
6#include <xrpl/json/Object.h>
7#include <xrpl/protocol/ErrorCodes.h>
8#include <xrpl/protocol/HashPrefix.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/STParsedJSON.h>
11#include <xrpl/protocol/UintTypes.h>
12#include <xrpl/protocol/jss.h>
13
14namespace ripple {
15namespace test {
16namespace jtx {
17
18STObject
20{
21 STParsedJSONObject p("tx_json", jv);
22 if (!p.object)
23 Throw<parse_error>(rpcErrorString(p.error));
24 return std::move(*p.object);
25}
26
27void
28sign(Json::Value& jv, Account const& account, Json::Value& sigObject)
29{
30 sigObject[jss::SigningPubKey] = strHex(account.pk().slice());
31 Serializer ss;
34 auto const sig = ripple::sign(account.pk(), account.sk(), ss.slice());
35 sigObject[jss::TxnSignature] = strHex(Slice{sig.data(), sig.size()});
36}
37
38void
39sign(Json::Value& jv, Account const& account)
40{
41 sign(jv, account, jv);
42}
43
44void
45fill_fee(Json::Value& jv, ReadView const& view)
46{
47 if (jv.isMember(jss::Fee))
48 return;
49 jv[jss::Fee] = to_string(view.fees().base);
50}
51
52void
53fill_seq(Json::Value& jv, ReadView const& view)
54{
55 if (jv.isMember(jss::Sequence))
56 return;
57 auto const account = parseBase58<AccountID>(jv[jss::Account].asString());
58 if (!account)
59 Throw<parse_error>("unexpected invalid Account");
60 auto const ar = view.read(keylet::account(*account));
61 if (!ar)
62 Throw<parse_error>("unexpected missing account root");
63 jv[jss::Sequence] = ar->getFieldU32(sfSequence);
64}
65
68 std::vector<std::string> const& args,
70 unsigned int apiVersion)
71{
73 auto const paramsObj = rpcCmdToJson(args, jv, apiVersion, j);
74
75 // Re-use jv to return our formatted result.
76 jv.clear();
77
78 // Allow parser to rewrite method.
79 jv[jss::method] = paramsObj.isMember(jss::method)
80 ? paramsObj[jss::method].asString()
81 : args[0];
82
83 // If paramsObj is not empty, put it in a [params] array.
84 if (paramsObj.begin() != paramsObj.end())
85 {
86 auto& paramsArray = Json::setArray(jv, jss::params);
87 paramsArray.append(paramsObj);
88 }
89 if (paramsObj.isMember(jss::jsonrpc))
90 jv[jss::jsonrpc] = paramsObj[jss::jsonrpc];
91 if (paramsObj.isMember(jss::ripplerpc))
92 jv[jss::ripplerpc] = paramsObj[jss::ripplerpc];
93 if (paramsObj.isMember(jss::id))
94 jv[jss::id] = paramsObj[jss::id];
95 return jv;
96}
97
98} // namespace jtx
99} // namespace test
100} // namespace ripple
Represents a JSON value.
Definition json_value.h:130
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:41
A view into a ledger.
Definition ReadView.h:32
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:944
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:47
An immutable linear range of bytes.
Definition Slice.h:27
Immutable cryptographic account descriptor.
Definition Account.h:20
Set the regular signature on a JTx.
Definition sig.h:16
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
Json::Value & setArray(Json::Value &, Json::StaticString const &key)
Add a new subarray at a named key in a Json object.
Definition Object.h:396
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
void fill_seq(Json::Value &jv, ReadView const &view)
Set the sequence number automatically.
Definition utility.cpp:53
void sign(Json::Value &jv, Account const &account, Json::Value &sigObject)
Sign automatically into a specific Json field of the jv object.
Definition utility.cpp:28
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:67
STObject parse(Json::Value const &jv)
Convert JSON to STObject.
Definition utility.cpp:19
void fill_fee(Json::Value &jv, ReadView const &view)
Set the fee automatically.
Definition utility.cpp:45
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
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:11
Json::Value rpcCmdToJson(std::vector< std::string > const &args, Json::Value &retParams, unsigned int apiVersion, beast::Journal j)
Definition RPCCall.cpp:1432
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
@ txSign
inner transaction to sign
XRPAmount base