14 case E::WRONG_KEYFILE_NAME:
15 return "specified file name is not a valid key file";
16 case E::NOT_REGULAR_FILE:
17 return "provided key file is not regular";
18 case E::FAILED_OPEN_FILE:
19 return "failed to open key file for reading";
20 case E::FILE_DOESNT_EXIST:
21 return "key file doesn't exist";
22 case E::INVALID_FILE_FORMAT:
23 return "specified key file is invalid";
24 case E::INCONSISTENT_KEYFILE:
25 return "key file is inconsistent, public key != derived public key";
26 case E::KEYS_PATH_IS_NOT_DIRECTORY:
27 return "specified key storage directory path is not a directory";
28 case E::FAILED_CREATE_KEYS_DIRECTORY:
29 return "failed to create key storage directory";
31 return "unknown KeyFileStorage error";
40 std::unique_ptr<KeyFileStorage> kfs{
new KeyFileStorage(keystore_path)};
41 OUTCOME_TRY(kfs->initialize());
49 outcome::result<std::pair<KeyTypeId, Buffer>>
51 if (file_name.size() < 4) {
55 auto key_type_str = file_name.substr(0, 4);
59 "key type <ascii: {}, hex: {:08x}> is not officially supported",
63 auto public_key_hex = file_name.substr(4);
67 return {key_type, public_key};
71 KeyTypeId key_type, gsl::span<const uint8_t> public_key)
const {
80 gsl::span<const uint8_t> public_key,
81 gsl::span<const uint8_t> seed)
const {
85 "Saving keypair (public: {}) to {}",
88 return outcome::success();
92 boost::system::error_code ec{};
94 if (ec and ec != boost::system::errc::no_such_file_or_directory) {
95 logger_->error(
"Error initializing key storage: {}", ec.message());
96 return outcome::failure(ec);
104 logger_->error(
"Error scanning key storage: {}", ec.message());
105 return outcome::failure(ec);
109 if (not boost::filesystem::create_directories(
keystore_path_, ec)) {
113 logger_->error(
"Error creating keystore dir: {}", ec.message());
114 return outcome::failure(ec);
118 return outcome::success();
122 const Path &file_path)
const {
123 if (!boost::filesystem::exists(file_path)) {
129 file.open(file_path.string(), std::ios::in | std::ios::binary);
130 if (!file.is_open()) {
136 SL_TRACE(
logger_,
"Loaded seed {} from {}", content, file_path.native());
141 gsl::span<const uint8_t> private_key,
144 file.open(path.native(), std::ios::out | std::ios::trunc);
145 if (!file.is_open()) {
150 SL_TRACE(
logger_,
"Saving key to {}", path.native());
151 return outcome::success();
158 boost::system::error_code ec{};
160 std::vector<Buffer> keys;
164 logger_->error(
"Error scanning keystore: {}", ec.message());
167 for (; it != end; ++it) {
168 if (!fs::is_regular_file(*it)) {
175 auto &[id, pk] = info.value();
185 KeyTypeId type, gsl::span<const uint8_t> public_key_bytes)
const {
188 boost::system::error_code ec{};
190 if (not fs::exists(key_path, ec)) {
195 return Buffer{std::move(seed_bytes)};
static outcome::result< SLBuffer > fromHex(std::string_view hex)
Construct SLBuffer from hex string.
Class represents arbitrary (including empty) byte buffer.
outcome::result< void > initialize()
std::string hex_lower(const gsl::span< const uint8_t > bytes) noexcept
Converts bytes to hex representation.
outcome::result< std::pair< KeyTypeId, Buffer > > parseKeyFileName(std::string_view file_name) const
outcome::result< std::vector< uint8_t > > unhexWith0x(std::string_view hex_with_prefix)
Unhex hex-string with 0x in the begining.
OUTCOME_CPP_DEFINE_CATEGORY(kagome::crypto, KeyFileStorage::Error, e)
outcome::result< std::vector< Buffer > > collectPublicKeys(KeyTypeId type) const
outcome::result< void > saveKeyPair(KeyTypeId type, gsl::span< const uint8_t > public_key, gsl::span< const uint8_t > seed) const
uint32_t KeyTypeId
Key type identifier.
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
outcome::result< std::string > loadFileContent(const Path &file_path) const
std::string hex_lower_0x(gsl::span< const uint8_t > bytes) noexcept
Converts bytes to hex representation with prefix 0x.
outcome::result< void > saveKeyHexAtPath(gsl::span< const uint8_t > private_key, const Path &path) const
std::string encodeKeyTypeIdToStr(KeyTypeId key_type_id)
makes string representation of KeyTypeId
Path composeKeyPath(KeyTypeId key_type, gsl::span< const uint8_t > public_key) const
static outcome::result< std::unique_ptr< KeyFileStorage > > createAt(Path keystore_path)
Logger createLogger(const std::string &tag)
outcome::result< std::optional< Buffer > > searchForSeed(KeyTypeId type, gsl::span< const uint8_t > public_key_bytes) const
bool isSupportedKeyType(KeyTypeId k)
checks whether key type value is supported
boost::filesystem::path Path
KeyFileStorage(Path keystore_path)
KeyTypeId decodeKeyTypeIdFromStr(std::string_view str)
restores KeyTypeId from its string representation