xrpld
Loading...
Searching...
No Matches
utility.cpp
1#include <test/jtx/utility.h>
2
3#include <test/jtx/Account.h>
4
5#include <xrpld/rpc/RPCCall.h>
6
7#include <xrpl/basics/contract.h>
8#include <xrpl/basics/strHex.h>
9#include <xrpl/beast/utility/Journal.h>
10#include <xrpl/json/json_value.h>
11#include <xrpl/ledger/ReadView.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/ErrorCodes.h>
14#include <xrpl/protocol/HashPrefix.h>
15#include <xrpl/protocol/Indexes.h>
16#include <xrpl/protocol/Protocol.h>
17#include <xrpl/protocol/SField.h>
18#include <xrpl/protocol/STObject.h>
19#include <xrpl/protocol/STParsedJSON.h>
20#include <xrpl/protocol/SecretKey.h>
21#include <xrpl/protocol/Serializer.h>
22#include <xrpl/protocol/XRPAmount.h>
23#include <xrpl/protocol/jss.h>
24
25#include <utility>
26#include <vector>
27
28namespace xrpl::test::jtx {
29
30STObject
32{
33 STParsedJSONObject p("tx_json", jv);
34 if (!p.object)
36 return std::move(*p.object);
37}
38
39void
40sign(json::Value& jv, Account const& account, json::Value& sigObject)
41{
42 sigObject[jss::SigningPubKey] = strHex(account.pk().slice());
43 Serializer ss;
46 auto const sig = xrpl::sign(account.pk(), account.sk(), ss.slice());
47 sigObject[jss::TxnSignature] = strHex(Slice{sig.data(), sig.size()});
48}
49
50void
51sign(json::Value& jv, Account const& account)
52{
53 sign(jv, account, jv);
54}
55
56void
57fillFee(json::Value& jv, ReadView const& view)
58{
59 if (jv.isMember(jss::Fee))
60 return;
61
62 auto const base = view.fees().base;
63
64 // For confidential transactions, the fee is higher because confidential
65 // transaction processing is more expensive.
66 auto const txType = jv[jss::TransactionType].asString();
67 if (txType == jss::ConfidentialMPTConvert || txType == jss::ConfidentialMPTConvertBack ||
68 txType == jss::ConfidentialMPTSend || txType == jss::ConfidentialMPTMergeInbox ||
69 txType == jss::ConfidentialMPTClawback)
70 {
71 jv[jss::Fee] = to_string(base * (kConfidentialFeeMultiplier + 1));
72 }
73 else
74 {
75 jv[jss::Fee] = to_string(base);
76 }
77}
78
79void
80fillSeq(json::Value& jv, ReadView const& view)
81{
82 if (jv.isMember(jss::Sequence))
83 return;
84 auto const account = parseBase58<AccountID>(jv[jss::Account].asString());
85 if (!account)
86 Throw<ParseError>("unexpected invalid Account");
87 auto const ar = view.read(keylet::account(*account));
88 if (!ar)
89 Throw<ParseError>("unexpected missing account root");
90 jv[jss::Sequence] = ar->getFieldU32(sfSequence);
91}
92
94cmdToJSONRPC(std::vector<std::string> const& args, beast::Journal j, unsigned int apiVersion)
95{
97 auto const paramsObj = rpcCmdToJson(args, jv, apiVersion, j);
98
99 // Re-use jv to return our formatted result.
100 jv.clear();
101
102 // Allow parser to rewrite method.
103 jv[jss::method] = paramsObj.isMember(jss::method) ? paramsObj[jss::method].asString() : args[0];
104
105 // If paramsObj is not empty, put it in a [params] array.
106 if (paramsObj.begin() != paramsObj.end())
107 {
108 auto& paramsArray = jv[jss::params] = json::ValueType::Array;
109 paramsArray.append(paramsObj);
110 }
111 if (paramsObj.isMember(jss::jsonrpc))
112 jv[jss::jsonrpc] = paramsObj[jss::jsonrpc];
113 if (paramsObj.isMember(jss::ripplerpc))
114 jv[jss::ripplerpc] = paramsObj[jss::ripplerpc];
115 if (paramsObj.isMember(jss::id))
116 jv[jss::id] = paramsObj[jss::id];
117 return jv;
118}
119} // namespace xrpl::test::jtx
A generic endpoint for log messages.
Definition Journal.h:38
Represents a JSON value.
Definition json_value.h:130
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
void clear()
Remove all object members and array elements.
A view into a ledger.
Definition ReadView.h:31
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
void addWithoutSigningFields(Serializer &s) const
Definition STObject.h:985
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:44
An immutable linear range of bytes.
Definition Slice.h:26
Immutable cryptographic account descriptor.
Definition jtx/Account.h:17
@ Array
array value (ordered list)
Definition json_value.h:25
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
void fillFee(json::Value &jv, ReadView const &view)
Set the fee automatically.
Definition utility.cpp:57
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:40
STObject parse(json::Value const &jv)
Convert JSON to STObject.
Definition utility.cpp:31
json::Value cmdToJSONRPC(std::vector< std::string > const &args, beast::Journal j, unsigned int apiVersion)
Given an xrpld unit test rpc command, return the corresponding JSON.
Definition utility.cpp:94
void fillSeq(json::Value &jv, ReadView const &view)
Set the sequence number automatically.
Definition utility.cpp:80
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
constexpr std::uint32_t kConfidentialFeeMultiplier
Extra base fee multiplier charged to confidential MPT transactions.
Definition Protocol.h:364
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
json::Value rpcCmdToJson(std::vector< std::string > const &args, json::Value &retParams, unsigned int apiVersion, beast::Journal j)
Definition RPCCall.cpp:1621
std::string rpcErrorString(json::Value const &jv)
Returns a single string with the contents of an RPC error.
@ TxSign
inner transaction to sign
Definition HashPrefix.h:51
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
XRPAmount base
Cost of a reference transaction in drops.