Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
3 : * http://soramitsu.co.jp
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #ifndef IROHA_UNSIGNED_PROTO_HPP
19 : #define IROHA_UNSIGNED_PROTO_HPP
20 :
21 : #include "backend/protobuf/common_objects/signature.hpp"
22 : #include "backend/protobuf/transaction.hpp"
23 : #include "cryptography/crypto_provider/crypto_signer.hpp"
24 : #include "cryptography/keypair.hpp"
25 : #include "interfaces/common_objects/types.hpp"
26 :
27 : namespace shared_model {
28 : namespace proto {
29 : /**
30 : * Class for holding built but still unsigned objects
31 : * @tparam T - type of object received from builder
32 : *
33 : * NOTE: finish() moves internal object, so calling methods after
34 : * finish() throws an exception
35 : */
36 : template <typename T>
37 : class DEPRECATED UnsignedWrapper {
38 : public:
39 : using ModelType = T;
40 :
41 : /**
42 : * Constructs new unsigned object instance
43 : * @param o - object received from builder
44 : */
45 : explicit UnsignedWrapper(const T &o) : object_(o) {}
46 :
47 : explicit UnsignedWrapper(T &&o) : object_(std::move(o)) {}
48 :
49 : UnsignedWrapper(UnsignedWrapper<T> &&w)
50 : : object_(std::move(w.object_)),
51 : object_finalized_(w.object_finalized_) {
52 : w.object_finalized_ = true;
53 : }
54 :
55 : UnsignedWrapper<T> &operator=(UnsignedWrapper<T> &&w) {
56 : object_ = std::move(w.object_);
57 : object_finalized_ = w.object_finalized_;
58 : w.object_finalized_ = true;
59 :
60 : return *this;
61 : }
62 :
63 : UnsignedWrapper(const UnsignedWrapper<T> &o) = default;
64 : UnsignedWrapper<T> &operator=(const UnsignedWrapper<T> &w) = default;
65 :
66 : /**
67 : * Add signature and retrieve signed result
68 : * @param signature - signature to add
69 : * @return signed object
70 : */
71 : UnsignedWrapper &signAndAddSignature(const crypto::Keypair &keypair) {
72 3478 : auto signedBlob = shared_model::crypto::CryptoSigner<>::sign(
73 3364 : shared_model::crypto::Blob(object_.payload()), keypair);
74 3478 : if (object_finalized_) {
75 0 : throw std::runtime_error("object has already been finalized");
76 : }
77 3477 : object_.addSignature(signedBlob, keypair.publicKey());
78 : // TODO: 05.12.2017 luckychess think about false case
79 : return *this;
80 3477 : }
81 :
82 : /**
83 : * Finishes object building
84 : * @return built signed object
85 : */
86 : T finish() {
87 3463 : if (boost::size(object_.signatures()) == 0) {
88 0 : throw std::invalid_argument("Cannot get object without signatures");
89 : }
90 3463 : if (object_finalized_) {
91 0 : throw std::runtime_error("object has already been finalized");
92 : }
93 :
94 3463 : object_finalized_ = true;
95 3463 : return std::move(object_);
96 0 : }
97 :
98 : interface::types::HashType hash() {
99 : return object_.hash();
100 : }
101 :
102 : template <typename U = T>
103 : std::enable_if_t<
104 : std::is_base_of<shared_model::interface::Transaction, U>::value,
105 : interface::types::HashType>
106 : reducedHash() const {
107 2053 : return object_.reducedHash();
108 : }
109 :
110 : private:
111 : T object_;
112 5516 : bool object_finalized_{false};
113 : };
114 : } // namespace proto
115 : } // namespace shared_model
116 :
117 : #endif // IROHA_UNSIGNED_PROTO_HPP
|