Kagome
Polkadot Runtime Engine in C++17
key_type.cpp
Go to the documentation of this file.
1 
7 
8 #include <unordered_set>
9 
10 #include <boost/endian/arithmetic.hpp>
11 
12 namespace kagome::crypto {
13 
15  static const std::unordered_set<KeyTypeId> supported_types = {
24  };
25 
26  return supported_types.count(k) > 0;
27  }
28 
29  std::string encodeKeyTypeIdToStr(KeyTypeId key_type_id) {
30  const auto *p = reinterpret_cast<const char *>(&key_type_id);
31  std::string res(p, p + sizeof(KeyTypeId));
32  return res;
33  }
34 
35  KeyTypeId decodeKeyTypeIdFromStr(std::string_view str) {
37 
38  if (str.size() == sizeof(KeyTypeId)) {
39  // string's data is aligned as KeyTypeId
40  if (reinterpret_cast<uintptr_t>(str.data())
41  % std::alignment_of_v<KeyTypeId> == 0) {
42  res = *reinterpret_cast<const kagome::crypto::KeyTypeId *>(str.data());
43  } else {
44  memcpy(&res, str.data(), sizeof(KeyTypeId));
45  }
46  }
47 
48  return res;
49  }
50 
51 } // namespace kagome::crypto
52 
54  using Error = kagome::crypto::KeyTypeError;
55  switch (e) {
57  return "key type is not supported";
59  return "key type id is not supported";
60  }
61  return "Unknown KeyTypeError";
62 }
OUTCOME_CPP_DEFINE_CATEGORY(kagome::crypto, KeyTypeError, e)
Definition: key_type.cpp:53
uint32_t KeyTypeId
Key type identifier.
Definition: key_type.hpp:21
std::string encodeKeyTypeIdToStr(KeyTypeId key_type_id)
makes string representation of KeyTypeId
Definition: key_type.cpp:29
bool isSupportedKeyType(KeyTypeId k)
checks whether key type value is supported
Definition: key_type.cpp:14
KeyTypeId decodeKeyTypeIdFromStr(std::string_view str)
restores KeyTypeId from its string representation
Definition: key_type.cpp:35