hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
proto_common_objects_factory.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_PROTO_COMMON_OBJECTS_FACTORY_HPP
7 #define IROHA_PROTO_COMMON_OBJECTS_FACTORY_HPP
8 
9 #include <regex>
10 
17 #include "common/result.hpp"
19 #include "primitive.pb.h"
23 
24 namespace shared_model {
25  namespace proto {
31  template <typename Validator>
33  public:
35  std::shared_ptr<validation::ValidatorsConfig> config)
36  : validator_(config) {}
37 
39  const interface::types::AddressType &address,
40  const interface::types::PubkeyType &public_key,
41  const boost::optional<interface::types::TLSCertificateType>
42  &tls_certificate = boost::none) override {
43  iroha::protocol::Peer peer;
44  peer.set_address(address);
45  peer.set_peer_key(public_key.hex());
46  if (tls_certificate) {
47  peer.set_tls_certificate(*tls_certificate);
48  }
49  auto proto_peer = std::make_unique<Peer>(std::move(peer));
50 
51  auto error = validator_.validatePeer(*proto_peer);
52 
53  return validated<std::unique_ptr<interface::Peer>>(
54  std::move(proto_peer), error);
55  }
56 
58  const interface::types::AccountIdType &account_id,
59  const interface::types::DomainIdType &domain_id,
61  const interface::types::JsonType &jsonData) override {
62  iroha::protocol::Account account;
63  account.set_account_id(account_id);
64  account.set_domain_id(domain_id);
65  account.set_quorum(quorum);
66  account.set_json_data(jsonData);
67 
68  auto proto_account = std::make_unique<Account>(std::move(account));
69 
70  auto error = validator_.validateAccount(*proto_account);
71 
72  return validated<std::unique_ptr<interface::Account>>(
73  std::move(proto_account), error);
74  }
75 
78  const interface::types::AssetIdType &asset_id,
79  const interface::Amount &balance) override {
80  iroha::protocol::AccountAsset account_asset;
81  account_asset.set_account_id(account_id);
82  account_asset.set_asset_id(asset_id);
83  account_asset.set_balance(balance.toStringRepr());
84 
85  auto proto_account_asset =
86  std::make_unique<AccountAsset>(std::move(account_asset));
87 
88  auto error = validator_.validateAccountAsset(*proto_account_asset);
89 
90  return validated<std::unique_ptr<interface::AccountAsset>>(
91  std::move(proto_account_asset), error);
92  }
93 
95  const interface::types::AssetIdType &asset_id,
96  const interface::types::DomainIdType &domain_id,
97  interface::types::PrecisionType precision) override {
98  iroha::protocol::Asset asset;
99  asset.set_asset_id(asset_id);
100  asset.set_domain_id(domain_id);
101  asset.set_precision(precision);
102 
103  auto proto_asset = std::make_unique<Asset>(std::move(asset));
104 
105  auto error = validator_.validateAsset(*proto_asset);
106 
107  return validated<std::unique_ptr<interface::Asset>>(
108  std::move(proto_asset), error);
109  }
110 
112  const interface::types::DomainIdType &domain_id,
113  const interface::types::RoleIdType &default_role) override {
114  iroha::protocol::Domain domain;
115  domain.set_domain_id(domain_id);
116  domain.set_default_role(default_role);
117 
118  auto proto_domain = std::make_unique<Domain>(std::move(domain));
119 
120  auto error = validator_.validateDomain(*proto_domain);
121 
122  return validated<std::unique_ptr<interface::Domain>>(
123  std::move(proto_domain), error);
124  }
125 
127  const interface::types::PubkeyType &key,
128  const interface::Signature::SignedType &signed_data) override {
129  iroha::protocol::Signature signature;
130  signature.set_public_key(key.hex());
131  signature.set_signature(signed_data.hex());
132 
133  auto proto_singature =
134  std::make_unique<Signature>(std::move(signature));
135 
136  auto error = validator_.validateSignatureForm(*proto_singature);
137 
138  return validated<std::unique_ptr<interface::Signature>>(
139  std::move(proto_singature), error);
140  }
141 
142  private:
148  template <typename ReturnValueType>
150  ReturnValueType object,
151  const boost::optional<validation::ValidationError> &error) {
152  if (error) {
153  return error.value().toString();
154  }
155  return iroha::expected::makeValue(std::move(object));
156  }
157 
158  Validator validator_;
159  };
160  } // namespace proto
161 } // namespace shared_model
162 
163 #endif // IROHA_PROTO_COMMON_OBJECTS_FACTORY_HPP
FactoryResult< std::unique_ptr< interface::Asset > > createAsset(const interface::types::AssetIdType &asset_id, const interface::types::DomainIdType &domain_id, interface::types::PrecisionType precision) override
Definition: proto_common_objects_factory.hpp:94
std::string AddressType
Type of peer address.
Definition: types.hpp:45
std::string DomainIdType
Iroha domain id type.
Definition: types.hpp:53
Definition: amount.hpp:19
Definition: result_fwd.hpp:27
FactoryResult< std::unique_ptr< interface::AccountAsset > > createAccountAsset(const interface::types::AccountIdType &account_id, const interface::types::AssetIdType &asset_id, const interface::Amount &balance) override
Definition: proto_common_objects_factory.hpp:77
FactoryResult< std::unique_ptr< interface::Domain > > createDomain(const interface::types::DomainIdType &domain_id, const interface::types::RoleIdType &default_role) override
Definition: proto_common_objects_factory.hpp:111
virtual const std::string & hex() const
Definition: blob.cpp:45
Definition: proto_common_objects_factory.hpp:32
Definition: common_objects_factory.hpp:26
std::string AccountIdType
Type of account id.
Definition: types.hpp:39
FactoryResult< std::unique_ptr< interface::Signature > > createSignature(const interface::types::PubkeyType &key, const interface::Signature::SignedType &signed_data) override
Definition: proto_common_objects_factory.hpp:126
ProtoCommonObjectsFactory(std::shared_ptr< validation::ValidatorsConfig > config)
Definition: proto_common_objects_factory.hpp:34
Definition: signed.hpp:17
uint8_t PrecisionType
Type of precision.
Definition: types.hpp:41
Definition: public_key.hpp:16
FactoryResult< std::unique_ptr< interface::Peer > > createPeer(const interface::types::AddressType &address, const interface::types::PubkeyType &public_key, const boost::optional< interface::types::TLSCertificateType > &tls_certificate=boost::none) override
Definition: proto_common_objects_factory.hpp:38
Definition: command_executor.hpp:12
FactoryResult< std::unique_ptr< interface::Account > > createAccount(const interface::types::AccountIdType &account_id, const interface::types::DomainIdType &domain_id, interface::types::QuorumType quorum, const interface::types::JsonType &jsonData) override
Definition: proto_common_objects_factory.hpp:57
uint16_t QuorumType
Type of Quorum used in transaction and set quorum.
Definition: types.hpp:62
std::string JsonType
Type of JSON data.
Definition: types.hpp:74
std::string RoleIdType
Type of role (i.e admin, user)
Definition: types.hpp:51
std::string toStringRepr() const
Definition: amount.cpp:93
std::string AssetIdType
Type of asset id.
Definition: types.hpp:55