Skip to content

src/crypto/crypto_store/key_type.cpp

Namespaces

Name
sgns
sgns::crypto

Functions

Name
bool isSupportedKeyType(KeyTypeId k)
checks whether key type value is supported
std::string decodeKeyTypeId(KeyTypeId param)
makes string representation of KeyTypeId
OUTCOME_CPP_DEFINE_CATEGORY_3(sgns::crypto , KeyTypeError , e )

Functions Documentation

function isSupportedKeyType

bool isSupportedKeyType(
    KeyTypeId k
)

checks whether key type value is supported

Parameters:

  • k key type value

Return: true if supported, false otherwise

function decodeKeyTypeId

std::string decodeKeyTypeId(
    KeyTypeId param
)

makes string representation of KeyTypeId

Parameters:

  • param param key type

Return: string representation of key type value

function OUTCOME_CPP_DEFINE_CATEGORY_3

OUTCOME_CPP_DEFINE_CATEGORY_3(
    sgns::crypto ,
    KeyTypeError ,
    e 
)

Source code

#include "crypto/crypto_store/key_type.hpp"

#include <unordered_set>

#include <boost/endian/arithmetic.hpp>
#include <array>
namespace sgns::crypto {

  bool isSupportedKeyType(KeyTypeId k) {
    static const std::unordered_set<KeyTypeId> supported_types = {
        key_types::kProd,
        key_types::kGran,
        key_types::kAcco,
        key_types::kImon,
        key_types::kAudi,
        key_types::kLp2p};

    return supported_types.count(k) > 0;
  }

  std::string decodeKeyTypeId(KeyTypeId param) {
    constexpr unsigned size = sizeof(KeyTypeId);
    constexpr unsigned bits = size * 8u;
    std::array<char, size> key_type{};

    boost::endian::endian_buffer<boost::endian::order::big, KeyTypeId, bits>
        buf(param);
    for (size_t i = 0; i < size; ++i) {
      // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
      key_type.at(i) = buf.data()[i];
    }

    return std::string(key_type.begin(), key_type.end());
  }
}  // namespace sgns::crypto

OUTCOME_CPP_DEFINE_CATEGORY_3(sgns::crypto, KeyTypeError, e) {
  using Error = sgns::crypto::KeyTypeError;
  switch (e) {
    case Error::UNSUPPORTED_KEY_TYPE:
      return "key type is not supported";
    case Error::UNSUPPORTED_KEY_TYPE_ID:
      return "key type id is not supported";
  }

  return "Unknown KeyTypeError";
}

Updated on 2026-03-04 at 13:10:44 -0800