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  interface::types::PublicKeyHexStringView public_key,
41  const std::optional<interface::types::TLSCertificateType>
42  &tls_certificate = std::nullopt) override {
43  iroha::protocol::Peer peer;
44  peer.set_address(address);
45  std::string_view const &public_key_string{public_key};
46  peer.set_peer_key(public_key_string.data(), public_key_string.size());
47  if (tls_certificate) {
48  peer.set_tls_certificate(*tls_certificate);
49  }
50  auto proto_peer = std::make_unique<Peer>(std::move(peer));
51 
52  auto error = validator_.validatePeer(*proto_peer);
53 
54  return validated<std::unique_ptr<interface::Peer>>(
55  std::move(proto_peer), error);
56  }
57 
59  const interface::types::AccountIdType &account_id,
60  const interface::types::DomainIdType &domain_id,
62  const interface::types::JsonType &jsonData) override {
63  iroha::protocol::Account account;
64  account.set_account_id(account_id);
65  account.set_domain_id(domain_id);
66  account.set_quorum(quorum);
67  account.set_json_data(jsonData);
68 
69  auto proto_account = std::make_unique<Account>(std::move(account));
70 
71  auto error = validator_.validateAccount(*proto_account);
72 
73  return validated<std::unique_ptr<interface::Account>>(
74  std::move(proto_account), error);
75  }
76 
79  const interface::types::AssetIdType &asset_id,
80  const interface::Amount &balance) override {
81  iroha::protocol::AccountAsset account_asset;
82  account_asset.set_account_id(account_id);
83  account_asset.set_asset_id(asset_id);
84  account_asset.set_balance(balance.toStringRepr());
85 
86  auto proto_account_asset =
87  std::make_unique<AccountAsset>(std::move(account_asset));
88 
89  auto error = validator_.validateAccountAsset(*proto_account_asset);
90 
91  return validated<std::unique_ptr<interface::AccountAsset>>(
92  std::move(proto_account_asset), error);
93  }
94 
96  const interface::types::AssetIdType &asset_id,
97  const interface::types::DomainIdType &domain_id,
98  interface::types::PrecisionType precision) override {
99  iroha::protocol::Asset asset;
100  asset.set_asset_id(asset_id);
101  asset.set_domain_id(domain_id);
102  asset.set_precision(precision);
103 
104  auto proto_asset = std::make_unique<Asset>(std::move(asset));
105 
106  auto error = validator_.validateAsset(*proto_asset);
107 
108  return validated<std::unique_ptr<interface::Asset>>(
109  std::move(proto_asset), error);
110  }
111 
113  const interface::types::DomainIdType &domain_id,
114  const interface::types::RoleIdType &default_role) override {
115  iroha::protocol::Domain domain;
116  domain.set_domain_id(domain_id);
117  domain.set_default_role(default_role);
118 
119  auto proto_domain = std::make_unique<Domain>(std::move(domain));
120 
121  auto error = validator_.validateDomain(*proto_domain);
122 
123  return validated<std::unique_ptr<interface::Domain>>(
124  std::move(proto_domain), error);
125  }
126 
128  interface::types::PublicKeyHexStringView key,
129  interface::types::SignedHexStringView signed_data) override {
130  iroha::protocol::Signature signature;
131  std::string_view const &public_key_string{key};
132  signature.set_public_key(public_key_string.data(),
133  public_key_string.size());
134  std::string_view const &signed_string{signed_data};
135  signature.set_signature(signed_string.data(), signed_string.size());
136 
137  auto proto_singature =
138  std::make_unique<Signature>(std::move(signature));
139 
140  auto error = validator_.validateSignatureForm(*proto_singature);
141 
142  return validated<std::unique_ptr<interface::Signature>>(
143  std::move(proto_singature), error);
144  }
145 
146  private:
152  template <typename ReturnValueType>
154  ReturnValueType object,
155  const std::optional<validation::ValidationError> &error) {
156  if (error) {
157  return error.value().toString();
158  }
159  return iroha::expected::makeValue(std::move(object));
160  }
161 
162  Validator validator_;
163  };
164  } // namespace proto
165 } // namespace shared_model
166 
167 #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:95
std::string AddressType
Type of peer address.
Definition: types.hpp:50
std::string DomainIdType
Iroha domain id type.
Definition: types.hpp:56
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:78
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:112
Definition: proto_common_objects_factory.hpp:32
Definition: common_objects_factory.hpp:27
FactoryResult< std::unique_ptr< interface::Peer > > createPeer(const interface::types::AddressType &address, interface::types::PublicKeyHexStringView public_key, const std::optional< interface::types::TLSCertificateType > &tls_certificate=std::nullopt) override
Definition: proto_common_objects_factory.hpp:38
std::string AccountIdType
Type of account id.
Definition: types.hpp:38
FactoryResult< std::unique_ptr< interface::Signature > > createSignature(interface::types::PublicKeyHexStringView key, interface::types::SignedHexStringView signed_data) override
Definition: proto_common_objects_factory.hpp:127
ProtoCommonObjectsFactory(std::shared_ptr< validation::ValidatorsConfig > config)
Definition: proto_common_objects_factory.hpp:34
uint8_t PrecisionType
Type of precision.
Definition: types.hpp:46
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:58
uint16_t QuorumType
Type of Quorum used in transaction and set quorum.
Definition: types.hpp:65
std::string JsonType
Type of JSON data.
Definition: types.hpp:77
std::string RoleIdType
Type of role (i.e admin, user)
Definition: types.hpp:54
std::string toStringRepr() const
Definition: amount.cpp:93
std::string AssetIdType
Type of asset id.
Definition: types.hpp:58