Skip to content

Commit

Permalink
Refactor key types (#1829)
Browse files Browse the repository at this point in the history
* refactor: key types
* refactor: separate definition and collection known types
* fix: review issue

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
  • Loading branch information
xDimon authored Oct 9, 2023
1 parent b1ef9b6 commit e55bef8
Show file tree
Hide file tree
Showing 35 changed files with 526 additions and 426 deletions.
4 changes: 2 additions & 2 deletions core/api/service/author/author_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace kagome::api {
* @param public_key The public key in binary
*/
virtual outcome::result<void> insertKey(
crypto::KeyTypeId key_type,
crypto::KeyType key_type,
const gsl::span<const uint8_t> &seed,
const gsl::span<const uint8_t> &public_key) = 0;

Expand Down Expand Up @@ -80,7 +80,7 @@ namespace kagome::api {
*/
virtual outcome::result<bool> hasKey(
const gsl::span<const uint8_t> &public_key,
crypto::KeyTypeId key_type) = 0;
crypto::KeyType key_type) = 0;

/**
* @return collection of pending extrinsics
Expand Down
34 changes: 17 additions & 17 deletions core/api/service/author/impl/author_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
#include "transaction_pool/transaction_pool.hpp"

namespace kagome::api {
const std::vector<crypto::KnownKeyTypeId> kKeyTypes{
crypto::KEY_TYPE_BABE,
crypto::KEY_TYPE_GRAN,
crypto::KEY_TYPE_AUDI,
const std::vector<crypto::KeyType> kKeyTypes{
crypto::KeyTypes::BABE,
crypto::KeyTypes::GRANDPA,
crypto::KeyTypes::AUTHORITY_DISCOVERY,
};

AuthorApiImpl::AuthorApiImpl(sptr<runtime::SessionKeysApi> key_api,
Expand Down Expand Up @@ -62,43 +62,43 @@ namespace kagome::api {
}

outcome::result<void> AuthorApiImpl::insertKey(
crypto::KeyTypeId key_type,
crypto::KeyType key_type_id,
const gsl::span<const uint8_t> &seed,
const gsl::span<const uint8_t> &public_key) {
if (std::find(kKeyTypes.begin(), kKeyTypes.end(), key_type)
if (std::find(kKeyTypes.begin(), kKeyTypes.end(), key_type_id)
== kKeyTypes.end()) {
std::string types;
for (auto &type : kKeyTypes) {
types.append(encodeKeyTypeIdToStr(type));
types.append(crypto::encodeKeyTypeToStr(type));
types.push_back(' ');
}
types.pop_back();
SL_INFO(logger_, "Unsupported key type, only [{}] are accepted", types);
return outcome::failure(crypto::CryptoStoreError::UNSUPPORTED_KEY_TYPE);
};
if (crypto::KEY_TYPE_BABE == key_type
or crypto::KEY_TYPE_AUDI == key_type) {
if (crypto::KeyTypes::BABE == key_type_id
or crypto::KeyTypes::AUTHORITY_DISCOVERY == key_type_id) {
OUTCOME_TRY(seed_typed, crypto::Sr25519Seed::fromSpan(seed));
OUTCOME_TRY(public_key_typed,
crypto::Sr25519PublicKey::fromSpan(public_key));
OUTCOME_TRY(keypair,
store_->generateSr25519Keypair(key_type, seed_typed));
store_->generateSr25519Keypair(key_type_id, seed_typed));
if (public_key_typed != keypair.public_key) {
return outcome::failure(crypto::CryptoStoreError::WRONG_PUBLIC_KEY);
}
}
if (crypto::KEY_TYPE_GRAN == key_type) {
if (crypto::KeyTypes::GRANDPA == key_type_id) {
OUTCOME_TRY(seed_typed, crypto::Ed25519Seed::fromSpan(seed));
OUTCOME_TRY(public_key_typed,
crypto::Ed25519PublicKey::fromSpan(public_key));
OUTCOME_TRY(
keypair,
store_->generateEd25519Keypair(crypto::KEY_TYPE_GRAN, seed_typed));
OUTCOME_TRY(keypair,
store_->generateEd25519Keypair(crypto::KeyTypes::GRANDPA,
seed_typed));
if (public_key_typed != keypair.public_key) {
return outcome::failure(crypto::CryptoStoreError::WRONG_PUBLIC_KEY);
}
}
auto res = key_store_->saveKeyPair(key_type, public_key, seed);
auto res = key_store_->saveKeyPair(key_type_id, public_key, seed);
return res;
}

Expand All @@ -125,7 +125,7 @@ namespace kagome::api {
}
stream >> key;
if (store_->findEd25519Keypair(
crypto::KEY_TYPE_GRAN,
crypto::KeyTypes::GRANDPA,
crypto::Ed25519PublicKey(common::Blob<32>(key)))) {
unsigned count = 1;
while (stream.currentIndex() < keys.size()) {
Expand All @@ -142,7 +142,7 @@ namespace kagome::api {
}

outcome::result<bool> AuthorApiImpl::hasKey(
const gsl::span<const uint8_t> &public_key, crypto::KeyTypeId key_type) {
const gsl::span<const uint8_t> &public_key, crypto::KeyType key_type) {
auto res = key_store_->searchForPhrase(key_type, public_key);
if (not res) {
return res.error();
Expand Down
4 changes: 2 additions & 2 deletions core/api/service/author/impl/author_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace kagome::api {
const primitives::Extrinsic &extrinsic) override;

outcome::result<void> insertKey(
crypto::KeyTypeId key_type,
crypto::KeyType key_type,
const gsl::span<const uint8_t> &seed,
const gsl::span<const uint8_t> &public_key) override;

Expand All @@ -93,7 +93,7 @@ namespace kagome::api {
const gsl::span<const uint8_t> &keys) override;

outcome::result<bool> hasKey(const gsl::span<const uint8_t> &public_key,
crypto::KeyTypeId key_type) override;
crypto::KeyType key_type) override;

outcome::result<std::vector<primitives::Extrinsic>> pendingExtrinsics()
override;
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/author/requests/has_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace kagome::api::author::request {
outcome::result<Return> execute() override {
OUTCOME_TRY(public_key, common::unhexWith0x(getParam<0>()));
return api_->hasKey(gsl::span(public_key.data(), public_key.size()),
crypto::decodeKeyTypeIdFromStr(getParam<1>()));
crypto::decodeKeyTypeFromStr(getParam<1>()));
}

private:
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/author/requests/insert_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace kagome::api::author::request {
outcome::result<Return> execute() override {
OUTCOME_TRY(seed, common::unhexWith0x(getParam<1>()));
OUTCOME_TRY(public_key, common::unhexWith0x(getParam<2>()));
return api_->insertKey(crypto::decodeKeyTypeIdFromStr(getParam<0>()),
return api_->insertKey(crypto::decodeKeyTypeFromStr(getParam<0>()),
gsl::span(seed.data(), seed.size()),
gsl::span(public_key.data(), public_key.size()));
}
Expand Down
2 changes: 1 addition & 1 deletion core/authority_discovery/query/query_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace kagome::authority_discovery {
authority_discovery_api_->authorities(block_tree_->bestBlock().hash));
OUTCOME_TRY(local_keys,
crypto_store_->getSr25519PublicKeys(
crypto::KnownKeyTypeId::KEY_TYPE_AUDI));
crypto::KeyTypes::AUTHORITY_DISCOVERY));
authorities.erase(
std::remove_if(authorities.begin(),
authorities.end(),
Expand Down
10 changes: 5 additions & 5 deletions core/authorship/impl/block_builder_factory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ namespace kagome::authorship {
BOOST_ASSERT(parent_number == parent.number);

auto number = parent.number + 1;
primitives::BlockHeader header;
header.number = number;
header.parent_hash = parent.hash;
header.digest = std::move(inherent_digest);

primitives::BlockHeader header{
.number = number,
.parent_hash = parent.hash,
.digest = std::move(inherent_digest),
};
if (auto res =
r_core_->initialize_block(header, std::move(changes_tracker));
not res) {
Expand Down
30 changes: 15 additions & 15 deletions core/crypto/crypto_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace kagome::crypto {
* @return generated key pair or error
*/
virtual outcome::result<EcdsaKeypair> generateEcdsaKeypair(
KeyTypeId key_type, std::string_view mnemonic_phrase) = 0;
KeyType key_type, std::string_view mnemonic_phrase) = 0;

/**
* @brief generates Ed25519 keypair and stores it in memory
Expand All @@ -49,7 +49,7 @@ namespace kagome::crypto {
* @return generated key pair or error
*/
virtual outcome::result<Ed25519Keypair> generateEd25519Keypair(
KeyTypeId key_type, std::string_view mnemonic_phrase) = 0;
KeyType key_type, std::string_view mnemonic_phrase) = 0;

/**
* @brief generates SR25519 keypair and stores it in memory
Expand All @@ -58,7 +58,7 @@ namespace kagome::crypto {
* @return generated key pair or error
*/
virtual outcome::result<Sr25519Keypair> generateSr25519Keypair(
KeyTypeId key_type, std::string_view mnemonic_phrase) = 0;
KeyType key_type, std::string_view mnemonic_phrase) = 0;

/**
* @brief generates ecdsa keypair and stores it in memory
Expand All @@ -67,7 +67,7 @@ namespace kagome::crypto {
* @return generated key pair
*/
virtual outcome::result<EcdsaKeypair> generateEcdsaKeypair(
KeyTypeId key_type, const EcdsaSeed &seed) = 0;
KeyType key_type, const EcdsaSeed &seed) = 0;

/**
* @brief generates Ed25519 keypair and stores it in memory
Expand All @@ -76,7 +76,7 @@ namespace kagome::crypto {
* @return generated key pair
*/
virtual outcome::result<Ed25519Keypair> generateEd25519Keypair(
KeyTypeId key_type, const Ed25519Seed &seed) = 0;
KeyType key_type, const Ed25519Seed &seed) = 0;

/**
* @brief generates SR25519 keypair and stores it in memory
Expand All @@ -85,31 +85,31 @@ namespace kagome::crypto {
* @return generated key
*/
virtual outcome::result<Sr25519Keypair> generateSr25519Keypair(
KeyTypeId key_type, const Sr25519Seed &seed) = 0;
KeyType key_type, const Sr25519Seed &seed) = 0;

/**
* @brief generates ecdsa keypair and stores it on disk
* @param key_type key type identifier
* @return generated key pair or error
*/
virtual outcome::result<EcdsaKeypair> generateEcdsaKeypairOnDisk(
KeyTypeId key_type) = 0;
KeyType key_type) = 0;

/**
* @brief generates Ed25519 keypair and stores it on disk
* @param key_type key type identifier
* @return generated key pair or error
*/
virtual outcome::result<Ed25519Keypair> generateEd25519KeypairOnDisk(
KeyTypeId key_type) = 0;
KeyType key_type) = 0;

/**
* @brief generates SR25519 keypair and stores it on disk
* @param key_type key type identifier
* @return generated key pair or error
*/
virtual outcome::result<Sr25519Keypair> generateSr25519KeypairOnDisk(
KeyTypeId key_type) = 0;
KeyType key_type) = 0;

/**
* @brief searches for key pair
Expand All @@ -118,7 +118,7 @@ namespace kagome::crypto {
* @return found key pair if exists
*/
virtual outcome::result<EcdsaKeypair> findEcdsaKeypair(
KeyTypeId key_type, const EcdsaPublicKey &pk) const = 0;
KeyType key_type, const EcdsaPublicKey &pk) const = 0;

/**
* @brief searches for key pair
Expand All @@ -127,7 +127,7 @@ namespace kagome::crypto {
* @return found key pair if exists
*/
virtual outcome::result<Ed25519Keypair> findEd25519Keypair(
KeyTypeId key_type, const Ed25519PublicKey &pk) const = 0;
KeyType key_type, const Ed25519PublicKey &pk) const = 0;

/**
* @brief searches for key pair
Expand All @@ -136,31 +136,31 @@ namespace kagome::crypto {
* @return found key pair if exists
*/
virtual outcome::result<Sr25519Keypair> findSr25519Keypair(
KeyTypeId key_type, const Sr25519PublicKey &pk) const = 0;
KeyType key_type, const Sr25519PublicKey &pk) const = 0;

/**
* @brief searches for ecdsa keys of specified type
* @param key_type key type identifier to look for
* @return vector of found public keys
*/
virtual outcome::result<EcdsaKeys> getEcdsaPublicKeys(
KeyTypeId key_type) const = 0;
KeyType key_type) const = 0;

/**
* @brief searches for Ed25519 keys of specified type
* @param key_type key type identifier to look for
* @return vector of found public keys
*/
virtual outcome::result<Ed25519Keys> getEd25519PublicKeys(
KeyTypeId key_type) const = 0;
KeyType key_type) const = 0;

/**
* @brief searches for SR25519 keys of specified type
* @param key_type key type identifier to look for
* @return vector of found public keys
*/
virtual outcome::result<Sr25519Keys> getSr25519PublicKeys(
KeyTypeId key_type) const = 0;
KeyType key_type) const = 0;

/**
* Acquires the key from user-provided path or generates and saves the
Expand Down
Loading

0 comments on commit e55bef8

Please sign in to comment.