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/protocol/ErrorCodes.h>
7#include <xrpl/protocol/HashPrefix.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/STParsedJSON.h>
10#include <xrpl/protocol/UintTypes.h>
11#include <xrpl/protocol/jss.h>
12
13namespace xrpl {
14namespace test {
15namespace jtx {
16
17STObject
19{
20 STParsedJSONObject p("tx_json", jv);
21 if (!p.object)
22 Throw<parse_error>(rpcErrorString(p.error));
23 return std::move(*p.object);
24}
25
26void
27sign(Json::Value& jv, Account const& account, Json::Value& sigObject)
28{
29 sigObject[jss::SigningPubKey] = strHex(account.pk().slice());
30 Serializer ss;
33 auto const sig = xrpl::sign(account.pk(), account.sk(), ss.slice());
34 sigObject[jss::TxnSignature] = strHex(Slice{sig.data(), sig.size()});
35}
36
37void
38sign(Json::Value& jv, Account const& account)
39{
40 sign(jv, account, jv);
41}
42
43void
44fill_fee(Json::Value& jv, ReadView const& view)
45{
46 if (jv.isMember(jss::Fee))
47 return;
48 jv[jss::Fee] = to_string(view.fees().base);
49}
50
51void
52fill_seq(Json::Value& jv, ReadView const& view)
53{
54 if (jv.isMember(jss::Sequence))
55 return;
56 auto const account = parseBase58<AccountID>(jv[jss::Account].asString());
57 if (!account)
58 Throw<parse_error>("unexpected invalid Account");
59 auto const ar = view.read(keylet::account(*account));
60 if (!ar)
61 Throw<parse_error>("unexpected missing account root");
62 jv[jss::Sequence] = ar->getFieldU32(sfSequence);
63}
64
67 std::vector<std::string> const& args,
69 unsigned int apiVersion)
70{
72 auto const paramsObj = rpcCmdToJson(args, jv, apiVersion, j);
73
74 // Re-use jv to return our formatted result.
75 jv.clear();
76
77 // Allow parser to rewrite method.
78 jv[jss::method] = paramsObj.isMember(jss::method)
79 ? paramsObj[jss::method].asString()
80 : args[0];
81
82 // If paramsObj is not empty, put it in a [params] array.
83 if (paramsObj.begin() != paramsObj.end())
84 {
85 auto& paramsArray = jv[jss::params] = Json::arrayValue;
86 paramsArray.append(paramsObj);
87 }
88 if (paramsObj.isMember(jss::jsonrpc))
89 jv[jss::jsonrpc] = paramsObj[jss::jsonrpc];
90 if (paramsObj.isMember(jss::ripplerpc))
91 jv[jss::ripplerpc] = paramsObj[jss::ripplerpc];
92 if (paramsObj.isMember(jss::id))
93 jv[jss::id] = paramsObj[jss::id];
94 return jv;
95}
96
97} // namespace jtx
98} // namespace test
99} // namespace xrpl
Represents a JSON value.
Definition json_value.h:131
Value & append(Value const &value)
Append value to array at the end.
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 Fees const & fees() const =0
Returns the fees for the base ledger.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
void addWithoutSigningFields(Serializer &s) const
Definition STObject.h:971
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
@ arrayValue
array value (ordered list)
Definition json_value.h:26
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:27
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:166
void fill_seq(Json::Value &jv, ReadView const &view)
Set the sequence number automatically.
Definition utility.cpp:52
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:66
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:27
STObject parse(Json::Value const &jv)
Convert JSON to STObject.
Definition utility.cpp:18
void fill_fee(Json::Value &jv, ReadView const &view)
Set the fee automatically.
Definition utility.cpp:44
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:11
std::string rpcErrorString(Json::Value const &jv)
Returns a single string with the contents of an RPC error.
@ txSign
inner transaction to sign
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
Json::Value rpcCmdToJson(std::vector< std::string > const &args, Json::Value &retParams, unsigned int apiVersion, beast::Journal j)
Definition RPCCall.cpp:1448
XRPAmount base