Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor key store #2017

Merged
merged 35 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d8940c7
Working on safe memory heap integration
Harrm Feb 28, 2024
cceeb73
Safe memory storage
Harrm Mar 1, 2024
5fc4177
Comment
Harrm Mar 1, 2024
5890611
Comment
Harrm Mar 1, 2024
c3fde9e
Remove free function for cleanup
Harrm Mar 1, 2024
a51b29b
Make seeds protected too
Harrm Mar 4, 2024
915f966
add logging
Harrm Mar 5, 2024
717154b
Fix common.cpp
Harrm Mar 5, 2024
dae1767
Merge branch 'master' into feature/safe-memory
Harrm Mar 5, 2024
2f3d7d1
Minor fix
Harrm Mar 5, 2024
e4db62c
Merge remote-tracking branch 'origin/feature/safe-memory' into featur…
Harrm Mar 5, 2024
d85e5f9
Fixed some tests
Harrm Mar 7, 2024
e0668a1
Fix bip39 seed initialization
Harrm Mar 11, 2024
3e8c623
Fixes from review
Harrm Mar 11, 2024
d68fec4
Merge branch 'master' of github.com:qdrvm/kagome into feature/safe-me…
Harrm Mar 11, 2024
8903254
Refactor key store in process
Harrm Mar 20, 2024
7f1130b
Refactor key store
Harrm Mar 22, 2024
59f107d
Working on safe memory heap integration
Harrm Feb 28, 2024
7887552
Safe memory storage
Harrm Mar 1, 2024
58867ba
Comment
Harrm Mar 1, 2024
4e4456d
Comment
Harrm Mar 1, 2024
bf4c1d2
Remove free function for cleanup
Harrm Mar 1, 2024
639e09f
Make seeds protected too
Harrm Mar 4, 2024
0b654db
Fixed some tests
Harrm Mar 7, 2024
6592d78
Refactor key store in process
Harrm Mar 20, 2024
1d8d79f
Refactor key store
Harrm Mar 22, 2024
a5dd091
Merge with master
Harrm Mar 25, 2024
60cb78f
Merge branch 'feature/key-file-cache' of github.com:qdrvm/kagome into…
Harrm Mar 25, 2024
00f0f9a
Minor fixes
Harrm Mar 25, 2024
d839ed5
Merge branch 'feature/key-file-cache' of github.com:qdrvm/kagome into…
Harrm Mar 26, 2024
6347bde
Merge branch 'master' into feature/key-file-cache
Harrm Mar 26, 2024
c02ffd2
Fix for apple clang
Harrm Mar 26, 2024
9becbe2
Merge branch 'feature/key-file-cache' of github.com:qdrvm/kagome into…
Harrm Mar 26, 2024
90e3079
Fix secure heap initialization
Harrm Mar 27, 2024
e6bb66c
Update core/api/service/author/requests/has_key.hpp
Harrm Apr 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ target_link_libraries(api
logger
app_state_manager
p2p::p2p_peer_id
crypto_store
key_store
hexutil
scale::scale
storage
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/author/author_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "common/buffer.hpp"
#include "common/buffer_view.hpp"
#include "crypto/common.hpp"
#include "crypto/crypto_store/key_type.hpp"
#include "crypto/key_store/key_type.hpp"
#include "primitives/author_api_primitives.hpp"
#include "primitives/transaction_validity.hpp"

Expand Down
38 changes: 18 additions & 20 deletions core/api/service/author/impl/author_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@

#include "api/service/api_service.hpp"
#include "blockchain/block_tree.hpp"
#include "crypto/crypto_store.hpp"
#include "crypto/crypto_store/crypto_store_impl.hpp"
#include "crypto/crypto_store/crypto_suites.hpp"
#include "crypto/crypto_store/key_file_storage.hpp"
#include "crypto/crypto_store/session_keys.hpp"
#include "crypto/hasher.hpp"
#include "crypto/key_store.hpp"
#include "crypto/key_store/key_file_storage.hpp"
#include "crypto/key_store/session_keys.hpp"
#include "crypto/sr25519_types.hpp"
#include "primitives/transaction.hpp"
#include "runtime/runtime_api/session_keys_api.hpp"
#include "scale/scale_decoder_stream.hpp"
Expand All @@ -34,7 +33,7 @@ namespace kagome::api {

AuthorApiImpl::AuthorApiImpl(sptr<runtime::SessionKeysApi> key_api,
sptr<transaction_pool::TransactionPool> pool,
sptr<crypto::CryptoStore> store,
sptr<crypto::KeyStore> store,
sptr<crypto::SessionKeys> keys,
sptr<crypto::KeyFileStorage> key_store,
LazySPtr<blockchain::BlockTree> block_tree,
Expand Down Expand Up @@ -68,33 +67,33 @@ namespace kagome::api {
== kKeyTypes.end()) {
std::string types;
for (auto &type : kKeyTypes) {
types.append(crypto::encodeKeyTypeToStr(type));
types.append(type.toString());
types.push_back(' ');
}
types.pop_back();
SL_INFO(logger_, "Unsupported key type, only [{}] are accepted", types);
return outcome::failure(crypto::CryptoStoreError::UNSUPPORTED_KEY_TYPE);
return outcome::failure(crypto::KeyStoreError::UNSUPPORTED_KEY_TYPE);
};
if (crypto::KeyTypes::BABE == key_type_id
or crypto::KeyTypes::AUTHORITY_DISCOVERY == key_type_id) {
OUTCOME_TRY(seed_typed, crypto::Sr25519Seed::from(seed));
OUTCOME_TRY(public_key_typed,
crypto::Sr25519PublicKey::fromSpan(public_key));
OUTCOME_TRY(seed_typed, crypto::Sr25519Seed::from(std::move(seed)));
OUTCOME_TRY(keypair,
store_->generateSr25519Keypair(key_type_id, seed_typed));
store_->sr25519().generateKeypair(key_type_id, seed_typed));
if (public_key_typed != keypair.public_key) {
return outcome::failure(crypto::CryptoStoreError::WRONG_PUBLIC_KEY);
return outcome::failure(crypto::KeyStoreError::WRONG_PUBLIC_KEY);
}
}
if (crypto::KeyTypes::GRANDPA == key_type_id) {
OUTCOME_TRY(seed_typed, crypto::Ed25519Seed::from(seed));
OUTCOME_TRY(public_key_typed,
crypto::Ed25519PublicKey::fromSpan(public_key));
OUTCOME_TRY(seed_typed, crypto::Ed25519Seed::from(std::move(seed)));
OUTCOME_TRY(keypair,
store_->generateEd25519Keypair(crypto::KeyTypes::GRANDPA,
seed_typed));
store_->ed25519().generateKeypair(crypto::KeyTypes::GRANDPA,
seed_typed));
if (public_key_typed != keypair.public_key) {
return outcome::failure(crypto::CryptoStoreError::WRONG_PUBLIC_KEY);
return outcome::failure(crypto::KeyStoreError::WRONG_PUBLIC_KEY);
}
}
auto res =
Expand Down Expand Up @@ -123,13 +122,13 @@ namespace kagome::api {
return false;
}
stream >> key;
if (store_->findEd25519Keypair(
if (store_->ed25519().findKeypair(
crypto::KeyTypes::GRANDPA,
crypto::Ed25519PublicKey(common::Blob<32>(key)))) {
unsigned count = 1;
while (stream.currentIndex() < keys.size()) {
stream >> key;
if (not store_->findSr25519Keypair(
if (not store_->sr25519().findKeypair(
crypto::polkadot_key_order[count++],
crypto::Sr25519PublicKey(common::Blob<32>(key)))) {
return false;
Expand All @@ -142,12 +141,11 @@ namespace kagome::api {

outcome::result<bool> AuthorApiImpl::hasKey(const BufferView &public_key,
crypto::KeyType key_type) {
auto res = key_store_->searchForPhrase(key_type, public_key);
auto res = key_store_->searchForKey(key_type, public_key);
if (not res) {
return res.error();
} else {
return res.value() ? true : false;
}
return res.value();
}

outcome::result<std::vector<primitives::Extrinsic>>
Expand Down
6 changes: 3 additions & 3 deletions core/api/service/author/impl/author_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace kagome::blockchain {
class BlockTree;
}
namespace kagome::crypto {
class CryptoStore;
class KeyStore;
class Hasher;
class KeyFileStorage;
class SessionKeys;
Expand Down Expand Up @@ -70,7 +70,7 @@ namespace kagome::api {
*/
AuthorApiImpl(sptr<runtime::SessionKeysApi> key_api,
sptr<transaction_pool::TransactionPool> pool,
sptr<crypto::CryptoStore> store,
sptr<crypto::KeyStore> store,
sptr<crypto::SessionKeys> keys,
sptr<crypto::KeyFileStorage> key_store,
LazySPtr<blockchain::BlockTree> block_tree,
Expand Down Expand Up @@ -108,7 +108,7 @@ namespace kagome::api {
private:
sptr<runtime::SessionKeysApi> keys_api_;
sptr<transaction_pool::TransactionPool> pool_;
sptr<crypto::CryptoStore> store_;
sptr<crypto::KeyStore> store_;
sptr<crypto::SessionKeys> keys_;
sptr<crypto::KeyFileStorage> key_store_;
LazySPtr<api::ApiService> api_service_;
Expand Down
7 changes: 5 additions & 2 deletions core/api/service/author/requests/has_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "api/service/author/author_api.hpp"
#include "api/service/base_request.hpp"
#include "crypto/key_store/key_type.hpp"
#include "outcome/outcome.hpp"

namespace kagome::api::author::request {
Expand All @@ -23,8 +24,10 @@ namespace kagome::api::author::request {

outcome::result<Return> execute() override {
OUTCOME_TRY(public_key, common::unhexWith0x(getParam<0>()));
return api_->hasKey(public_key,
crypto::decodeKeyTypeFromStr(getParam<1>()));
if (auto key_type = crypto::KeyType::fromString(getParam<1>())) {
return api_->hasKey(public_key, *key_type);
}
return crypto::KeyTypeError::UNSUPPORTED_KEY_TYPE;
}

private:
Expand Down
8 changes: 5 additions & 3 deletions core/api/service/author/requests/insert_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "api/service/author/author_api.hpp"
#include "api/service/base_request.hpp"
#include "crypto/common.hpp"
#include "crypto/key_store/key_type.hpp"
#include "outcome/outcome.hpp"

namespace kagome::api::author::request {
Expand All @@ -33,9 +34,10 @@ namespace kagome::api::author::request {
std::string_view{seed_hex.data(), seed_hex.size()},
seed_buf.begin()));
OUTCOME_TRY(public_key, common::unhexWith0x(getParam<2>()));
return api_->insertKey(crypto::decodeKeyTypeFromStr(getParam<0>()),
std::move(seed_buf),
public_key);
if (auto key_type = crypto::KeyType::fromString(getParam<0>())) {
return api_->insertKey(*key_type, std::move(seed_buf), public_key);
}
return crypto::KeyTypeError::UNSUPPORTED_KEY_TYPE;
}

private:
Expand Down
2 changes: 1 addition & 1 deletion core/authority_discovery/publisher/address_publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "application/app_state_manager.hpp"
#include "blockchain/block_tree.hpp"
#include "crypto/crypto_store/session_keys.hpp"
#include "crypto/key_store/session_keys.hpp"
#include "crypto/ed25519_provider.hpp"
#include "crypto/sr25519_provider.hpp"
#include "log/logger.hpp"
Expand Down
6 changes: 3 additions & 3 deletions core/authority_discovery/query/query_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace kagome::authority_discovery {
std::shared_ptr<application::AppStateManager> app_state_manager,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::AuthorityDiscoveryApi> authority_discovery_api,
std::shared_ptr<crypto::CryptoStore> crypto_store,
std::shared_ptr<crypto::KeyStore> key_store,
std::shared_ptr<crypto::Sr25519Provider> sr_crypto_provider,
std::shared_ptr<libp2p::crypto::CryptoProvider> libp2p_crypto_provider,
std::shared_ptr<libp2p::crypto::marshaller::KeyMarshaller> key_marshaller,
Expand All @@ -45,7 +45,7 @@ namespace kagome::authority_discovery {
std::shared_ptr<libp2p::basic::Scheduler> scheduler)
: block_tree_{std::move(block_tree)},
authority_discovery_api_{std::move(authority_discovery_api)},
crypto_store_{std::move(crypto_store)},
key_store_{std::move(key_store)},
sr_crypto_provider_{std::move(sr_crypto_provider)},
libp2p_crypto_provider_{std::move(libp2p_crypto_provider)},
key_marshaller_{std::move(key_marshaller)},
Expand Down Expand Up @@ -101,7 +101,7 @@ namespace kagome::authority_discovery {
authorities,
authority_discovery_api_->authorities(block_tree_->bestBlock().hash));
OUTCOME_TRY(local_keys,
crypto_store_->getSr25519PublicKeys(
key_store_->sr25519().getPublicKeys(
crypto::KeyTypes::AUTHORITY_DISCOVERY));
auto has = [](const std::vector<primitives::AuthorityDiscoveryId> &keys,
const primitives::AuthorityDiscoveryId &key) {
Expand Down
6 changes: 3 additions & 3 deletions core/authority_discovery/query/query_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "application/app_state_manager.hpp"
#include "authority_discovery/interval.hpp"
#include "blockchain/block_tree.hpp"
#include "crypto/crypto_store.hpp"
#include "crypto/key_store.hpp"
#include "crypto/sr25519_provider.hpp"
#include "log/logger.hpp"
#include "runtime/runtime_api/authority_discovery_api.hpp"
Expand All @@ -38,7 +38,7 @@ namespace kagome::authority_discovery {
std::shared_ptr<application::AppStateManager> app_state_manager,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::AuthorityDiscoveryApi> authority_discovery_api,
std::shared_ptr<crypto::CryptoStore> crypto_store,
std::shared_ptr<crypto::KeyStore> key_store,
std::shared_ptr<crypto::Sr25519Provider> sr_crypto_provider,
std::shared_ptr<libp2p::crypto::CryptoProvider> libp2p_crypto_provider,
std::shared_ptr<libp2p::crypto::marshaller::KeyMarshaller>
Expand All @@ -64,7 +64,7 @@ namespace kagome::authority_discovery {

std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<runtime::AuthorityDiscoveryApi> authority_discovery_api_;
std::shared_ptr<crypto::CryptoStore> crypto_store_;
std::shared_ptr<crypto::KeyStore> key_store_;
std::shared_ptr<crypto::Sr25519Provider> sr_crypto_provider_;
std::shared_ptr<libp2p::crypto::CryptoProvider> libp2p_crypto_provider_;
std::shared_ptr<libp2p::crypto::marshaller::KeyMarshaller> key_marshaller_;
Expand Down
6 changes: 6 additions & 0 deletions core/common/bytestr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include <string>
#include <string_view>
#include <vector>

Expand All @@ -19,6 +20,11 @@ namespace kagome {
return {reinterpret_cast<const uint8_t *>(s.data()), s.size()};
}

inline std::span<uint8_t> str2byte(std::span<char> s) {
// NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
return {reinterpret_cast<uint8_t *>(s.data()), s.size()};
}

inline std::string_view byte2str(const common::BufferView &s) {
// NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
return {reinterpret_cast<const char *>(s.data()), s.size()};
Expand Down
75 changes: 75 additions & 0 deletions core/common/optref.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <type_traits>

namespace kagome {
template <typename T>
class OptRef {
public:
OptRef() : data{nullptr} {}
OptRef(T &data) : data{&data} {}
OptRef(T &&) = delete;
OptRef(std::nullopt_t) : data{nullptr} {}

OptRef(const OptRef &) = default;

OptRef &operator=(const OptRef &) = default;

T &operator*() {
BOOST_ASSERT(data);
return *data;
}

const T &operator*() const {
BOOST_ASSERT(data);
return *data;
}

T *operator->() {
BOOST_ASSERT(data);
return data;
}

const T *operator->() const {
BOOST_ASSERT(data);
return data;
}

T &value() {
BOOST_ASSERT(data);
return *data;
}

const T &value() const {
BOOST_ASSERT(data);
return *data;
}

explicit operator bool() const noexcept {
return data != nullptr;
}

bool operator!() const noexcept {
return data == nullptr;
}

bool has_value() const noexcept {
return data != nullptr;
}

bool operator==(const OptRef<T> &) const = default;

bool operator==(const T &other) const {
return has_value() && (*data == other);
}

private:
T *data;
};
} // namespace kagome
2 changes: 1 addition & 1 deletion core/consensus/babe/impl/babe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "consensus/timeline/impl/slot_leadership_error.hpp"
#include "consensus/timeline/slots_util.hpp"
#include "crypto/blake2/blake2b.h"
#include "crypto/crypto_store/session_keys.hpp"
#include "crypto/key_store/session_keys.hpp"
#include "crypto/sr25519_provider.hpp"
#include "dispute_coordinator/dispute_coordinator.hpp"
#include "metrics/histogram_timer.hpp"
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/babe/impl/babe_lottery_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "consensus/babe/babe_config_repository.hpp"
#include "consensus/babe/impl/prepare_transcript.hpp"
#include "consensus/babe/impl/threshold_util.hpp"
#include "crypto/crypto_store/session_keys.hpp"
#include "crypto/key_store/session_keys.hpp"
#include "crypto/hasher.hpp"
#include "crypto/vrf_provider.hpp"

Expand Down
2 changes: 1 addition & 1 deletion core/consensus/beefy/impl/beefy_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "consensus/beefy/impl/beefy_thread_pool.hpp"
#include "consensus/beefy/sig.hpp"
#include "consensus/timeline/timeline.hpp"
#include "crypto/crypto_store/session_keys.hpp"
#include "crypto/key_store/session_keys.hpp"
#include "metrics/histogram_timer.hpp"
#include "network/impl/protocols/beefy_protocol_impl.hpp"
#include "runtime/common/runtime_execution_error.hpp"
Expand Down
Loading
Loading