rippled
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 <ripple/basics/contract.h>
21 #include <ripple/protocol/ErrorCodes.h>
22 #include <ripple/protocol/HashPrefix.h>
23 #include <ripple/protocol/Indexes.h>
24 #include <ripple/protocol/STParsedJSON.h>
25 #include <ripple/protocol/UintTypes.h>
26 #include <ripple/protocol/jss.h>
27 #include <cstring>
28 #include <test/jtx/utility.h>
29 
30 namespace ripple {
31 namespace test {
32 namespace jtx {
33 
34 STObject
35 parse(Json::Value const& jv)
36 {
37  STParsedJSONObject p("tx_json", jv);
38  if (!p.object)
39  Throw<parse_error>(rpcErrorString(p.error));
40  return std::move(*p.object);
41 }
42 
43 void
44 sign(Json::Value& jv, Account const& account)
45 {
46  jv[jss::SigningPubKey] = strHex(account.pk().slice());
47  Serializer ss;
50  auto const sig = ripple::sign(account.pk(), account.sk(), ss.slice());
51  jv[jss::TxnSignature] = strHex(Slice{sig.data(), sig.size()});
52 }
53 
54 void
55 fill_fee(Json::Value& jv, ReadView const& view)
56 {
57  if (jv.isMember(jss::Fee))
58  return;
59  jv[jss::Fee] = to_string(view.fees().base);
60 }
61 
62 void
63 fill_seq(Json::Value& jv, ReadView const& view)
64 {
65  if (jv.isMember(jss::Sequence))
66  return;
67  auto const account = parseBase58<AccountID>(jv[jss::Account].asString());
68  if (!account)
69  Throw<parse_error>("unexpected invalid Account");
70  auto const ar = view.read(keylet::account(*account));
71  if (!ar)
72  Throw<parse_error>("unexpected missing account root");
73  jv[jss::Sequence] = ar->getFieldU32(sfSequence);
74 }
75 
76 } // namespace jtx
77 } // namespace test
78 } // namespace ripple
cstring
ripple::rpcErrorString
std::string rpcErrorString(Json::Value const &jv)
Returns a single string with the contents of an RPC error.
Definition: ErrorCodes.cpp:210
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::sfSequence
const SF_UINT32 sfSequence
ripple::ReadView::fees
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
ripple::test::jtx::fill_seq
void fill_seq(Json::Value &jv, ReadView const &view)
Set the sequence number automatically.
Definition: utility.cpp:63
ripple::STParsedJSONObject
Holds the serialized result of parsing an input JSON object.
Definition: STParsedJSON.h:31
ripple::test::jtx::sign
void sign(Json::Value &jv, Account const &account)
Sign automatically.
Definition: utility.cpp:44
ripple::STParsedJSONObject::object
std::optional< STObject > object
The STObject if the parse was successful.
Definition: STParsedJSON.h:50
ripple::STParsedJSONObject::error
Json::Value error
On failure, an appropriate set of error values.
Definition: STParsedJSON.h:53
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:133
ripple::test::jtx::fill_fee
void fill_fee(Json::Value &jv, ReadView const &view)
Set the fee automatically.
Definition: utility.cpp:55
ripple::Serializer::slice
Slice slice() const noexcept
Definition: Serializer.h:63
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
ripple::test::jtx::sig
Set the regular signature on a JTx.
Definition: sig.h:34
ripple::STObject::addWithoutSigningFields
void addWithoutSigningFields(Serializer &s) const
Definition: STObject.h:889
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::Serializer
Definition: Serializer.h:39
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::sign
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &m)
Generate a signature for a message.
Definition: SecretKey.cpp:238
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::Serializer::add32
int add32(std::uint32_t i)
Definition: Serializer.cpp:38
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
ripple::HashPrefix::txSign
@ txSign
inner transaction to sign
ripple::test::jtx::parse
STObject parse(Json::Value const &jv)
Convert JSON to STObject.
Definition: utility.cpp:35
ripple::Fees::base
XRPAmount base
Definition: ReadView.h:51
Json::Value
Represents a JSON value.
Definition: json_value.h:145