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