hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
unsigned_proto.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_UNSIGNED_PROTO_HPP
7 #define IROHA_UNSIGNED_PROTO_HPP
8 
12 #include "cryptography/keypair.hpp"
14 
15 namespace shared_model {
16  namespace proto {
24  template <typename T>
25  class /*[[deprecated]]*/ UnsignedWrapper {
26  public:
27  using ModelType = T;
28 
33  explicit UnsignedWrapper(const T &o) : object_(o) {}
34 
35  explicit UnsignedWrapper(T && o) : object_(std::move(o)) {}
36 
38  : object_(std::move(w.object_)),
39  object_finalized_(w.object_finalized_) {
40  w.object_finalized_ = true;
41  }
42 
44  object_ = std::move(w.object_);
45  object_finalized_ = w.object_finalized_;
46  w.object_finalized_ = true;
47 
48  return *this;
49  }
50 
51  UnsignedWrapper(const UnsignedWrapper<T> &o) = default;
52  UnsignedWrapper<T> &operator=(const UnsignedWrapper<T> &w) = default;
53 
60  auto signature_hex = shared_model::crypto::CryptoSigner::sign(
61  shared_model::crypto::Blob(object_.payload()), keypair);
62  if (object_finalized_) {
63  throw std::runtime_error("object has already been finalized");
64  }
65  using namespace shared_model::interface::types;
66  object_.addSignature(SignedHexStringView{signature_hex},
67  PublicKeyHexStringView{keypair.publicKey()});
68  // TODO: 05.12.2017 luckychess think about false case
69  return *this;
70  }
71 
76  T finish() {
77  if (boost::size(object_.signatures()) == 0) {
78  throw std::invalid_argument("Cannot get object without signatures");
79  }
80  if (object_finalized_) {
81  throw std::runtime_error("object has already been finalized");
82  }
83 
84  object_finalized_ = true;
85  return std::move(object_);
86  }
87 
89  return object_.hash();
90  }
91 
92  template <typename U = T>
93  std::enable_if_t<
94  std::is_base_of<shared_model::interface::Transaction, U>::value,
96  reducedHash() const {
97  return object_.reducedHash();
98  }
99 
100  private:
101  T object_;
102  bool object_finalized_{false};
103  };
104  } // namespace proto
105 } // namespace shared_model
106 
107 #endif // IROHA_UNSIGNED_PROTO_HPP
std::string const & publicKey() const
Definition: keypair.cpp:15
std::enable_if_t< std::is_base_of< shared_model::interface::Transaction, U >::value, interface::types::HashType > reducedHash() const
Definition: unsigned_proto.hpp:96
UnsignedWrapper(T &&o)
Definition: unsigned_proto.hpp:35
Definition: hash.hpp:18
Definition: blob.hpp:27
T ModelType
Definition: unsigned_proto.hpp:27
Definition: keypair.hpp:19
static std::string sign(const Blob &blob, const Keypair &keypair)
Definition: crypto_signer.cpp:29
Definition: round.cpp:51
UnsignedWrapper< T > & operator=(UnsignedWrapper< T > &&w)
Definition: unsigned_proto.hpp:43
Definition: byte_range.hpp:14
UnsignedWrapper(const T &o)
Definition: unsigned_proto.hpp:33
Definition: unsigned_proto.hpp:25
UnsignedWrapper(UnsignedWrapper< T > &&w)
Definition: unsigned_proto.hpp:37
interface::types::HashType hash()
Definition: unsigned_proto.hpp:88
Definition: command_executor.hpp:13
T finish()
Definition: unsigned_proto.hpp:76
UnsignedWrapper & signAndAddSignature(const crypto::Keypair &keypair)
Definition: unsigned_proto.hpp:59