rippled
multisign.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/HashPrefix.h>
22 #include <ripple/protocol/Sign.h>
23 #include <ripple/protocol/UintTypes.h>
24 #include <ripple/protocol/jss.h>
25 #include <optional>
26 #include <sstream>
27 #include <test/jtx/multisign.h>
28 #include <test/jtx/utility.h>
29 
30 namespace ripple {
31 namespace test {
32 namespace jtx {
33 
36  Account const& account,
37  std::uint32_t quorum,
38  std::vector<signer> const& v)
39 {
40  Json::Value jv;
41  jv[jss::Account] = account.human();
42  jv[jss::TransactionType] = jss::SignerListSet;
43  jv[sfSignerQuorum.getJsonName()] = quorum;
44  auto& ja = jv[sfSignerEntries.getJsonName()];
45  for (std::size_t i = 0; i < v.size(); ++i)
46  {
47  auto const& e = v[i];
48  auto& je = ja[i][sfSignerEntry.getJsonName()];
49  je[jss::Account] = e.account.human();
50  je[sfSignerWeight.getJsonName()] = e.weight;
51  if (e.tag)
52  je[sfWalletLocator.getJsonName()] = to_string(*e.tag);
53  }
54  return jv;
55 }
56 
58 signers(Account const& account, none_t)
59 {
60  Json::Value jv;
61  jv[jss::Account] = account.human();
62  jv[jss::TransactionType] = jss::SignerListSet;
63  jv[sfSignerQuorum.getJsonName()] = 0;
64  return jv;
65 }
66 
67 //------------------------------------------------------------------------------
68 
69 msig::msig(std::vector<msig::Reg> signers_) : signers(std::move(signers_))
70 {
71  // Signatures must be applied in sorted order.
72  std::sort(
73  signers.begin(),
74  signers.end(),
75  [](msig::Reg const& lhs, msig::Reg const& rhs) {
76  return lhs.acct.id() < rhs.acct.id();
77  });
78 }
79 
80 void
81 msig::operator()(Env& env, JTx& jt) const
82 {
83  auto const mySigners = signers;
84  jt.signer = [mySigners, &env](Env&, JTx& jtx) {
85  jtx[sfSigningPubKey.getJsonName()] = "";
87  try
88  {
89  st = parse(jtx.jv);
90  }
91  catch (parse_error const&)
92  {
93  env.test.log << pretty(jtx.jv) << std::endl;
94  Rethrow();
95  }
96  auto& js = jtx[sfSigners.getJsonName()];
97  for (std::size_t i = 0; i < mySigners.size(); ++i)
98  {
99  auto const& e = mySigners[i];
100  auto& jo = js[i][sfSigner.getJsonName()];
101  jo[jss::Account] = e.acct.human();
102  jo[jss::SigningPubKey] = strHex(e.sig.pk().slice());
103 
104  Serializer ss{buildMultiSigningData(*st, e.acct.id())};
105  auto const sig = ripple::sign(
106  *publicKeyType(e.sig.pk().slice()), e.sig.sk(), ss.slice());
108  strHex(Slice{sig.data(), sig.size()});
109  }
110  };
111 }
112 
113 } // namespace jtx
114 } // namespace test
115 } // namespace ripple
sstream
ripple::sfSignerWeight
const SF_UINT16 sfSignerWeight
ripple::test::jtx::parse_error
Thrown when parse fails.
Definition: utility.h:34
ripple::sfSigners
const SField sfSigners
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
std::vector
STL class.
std::vector::size
T size(T... args)
ripple::sfSigningPubKey
const SF_VL sfSigningPubKey
ripple::test::jtx::msig::msig
msig(std::vector< Reg > signers_)
Definition: multisign.cpp:69
ripple::test::jtx::msig::Reg
Definition: multisign.h:66
ripple::test::jtx::none_t
Definition: tags.h:28
ripple::SField::getJsonName
Json::StaticString const & getJsonName() const
Definition: SField.h:187
std::sort
T sort(T... args)
ripple::sfSignerQuorum
const SF_UINT32 sfSignerQuorum
ripple::publicKeyType
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:207
ripple::test::jtx::signers
Json::Value signers(Account const &account, std::uint32_t quorum, std::vector< signer > const &v)
Definition: multisign.cpp:35
ripple::Rethrow
void Rethrow()
Rethrow the exception currently being handled.
Definition: contract.h:48
ripple::test::jtx::JTx
Execution context for applying a JSON transaction.
Definition: JTx.h:42
std::uint32_t
ripple::test::jtx::sig
Set the regular signature on a JTx.
Definition: sig.h:34
ripple::test::jtx::msig::operator()
void operator()(Env &, JTx &jt) const
Definition: multisign.cpp:81
ripple::test::jtx::Env::test
beast::unit_test::suite & test
Definition: Env.h:119
ripple::test::jtx::JTx::signer
std::function< void(Env &, JTx &)> signer
Definition: JTx.h:51
ripple::sfSigner
const SField sfSigner
ripple::sfSignerEntry
const SField sfSignerEntry
ripple::Serializer
Definition: Serializer.h:39
ripple::sfSignerEntries
const SField sfSignerEntries
ripple::test::jtx::msig::signers
std::vector< Reg > signers
Definition: multisign.h:96
ripple::sfTxnSignature
const SF_VL sfTxnSignature
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::endl
T endl(T... args)
ripple::sfWalletLocator
const SF_UINT256 sfWalletLocator
ripple::sign
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &m)
Generate a signature for a message.
Definition: SecretKey.cpp:238
std
STL namespace.
optional
std::size_t
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::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
ripple::test::jtx::parse
STObject parse(Json::Value const &jv)
Convert JSON to STObject.
Definition: utility.cpp:35
ripple::buildMultiSigningData
Serializer buildMultiSigningData(STObject const &obj, AccountID const &signingID)
Return a Serializer suitable for computing a multisigning TxnSignature.
Definition: Sign.cpp:87
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
Json::Value
Represents a JSON value.
Definition: json_value.h:145