diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 1c08082850..d3e59b69ce 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -32,3 +32,4 @@ add_subdirectory(utils) add_subdirectory(offchain) add_subdirectory(parachain) add_subdirectory(dispute_coordinator) +add_subdirectory(key) diff --git a/core/application/CMakeLists.txt b/core/application/CMakeLists.txt index 85a3012494..61a5e127f4 100644 --- a/core/application/CMakeLists.txt +++ b/core/application/CMakeLists.txt @@ -74,6 +74,7 @@ target_link_libraries(application_modes hexutil RapidJSON::rapidjson benchmark::benchmark + kagome_keys ) add_library(recovery_mode diff --git a/core/injector/CMakeLists.txt b/core/injector/CMakeLists.txt index ee180e700b..92f66d158c 100644 --- a/core/injector/CMakeLists.txt +++ b/core/injector/CMakeLists.txt @@ -45,6 +45,7 @@ target_link_libraries(application_injector grandpa_api host_api_factory kagome_benchmarks + kagome_keys log_configurator metadata_api metrics diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index 23c49e26cd..75d20fb98f 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -107,6 +107,7 @@ #include "injector/calculate_genesis_state.hpp" #include "injector/get_peer_keypair.hpp" #include "injector/idle_trie_pruner.hpp" +#include "key/key.hpp" #include "log/configurator.hpp" #include "log/logger.hpp" #include "metrics/impl/exposer_impl.hpp" @@ -1117,6 +1118,10 @@ namespace kagome::injector { .template create>(); } + std::shared_ptr KagomeNodeInjector::injectKey() { + return pimpl_->injector_.template create>(); + } + std::shared_ptr KagomeNodeInjector::injectWatchdog() { return pimpl_->injector_.template create>(); } diff --git a/core/injector/application_injector.hpp b/core/injector/application_injector.hpp index ea68b4ebfe..efa9b1fcca 100644 --- a/core/injector/application_injector.hpp +++ b/core/injector/application_injector.hpp @@ -31,6 +31,7 @@ namespace kagome { class PrecompileWasmMode; class RecoveryMode; class BenchmarkMode; + class Key; } // namespace application::mode namespace authority_discovery { @@ -104,6 +105,10 @@ namespace kagome { class TelemetryService; } + namespace key { + class Key; + } + class Watchdog; } // namespace kagome @@ -163,6 +168,7 @@ namespace kagome::injector { injectPrecompileWasmMode(); std::shared_ptr injectRecoveryMode(); std::shared_ptr injectBlockBenchmark(); + std::shared_ptr injectKey(); protected: std::shared_ptr pimpl_; diff --git a/core/key/CMakeLists.txt b/core/key/CMakeLists.txt new file mode 100644 index 0000000000..7dbd2cf7da --- /dev/null +++ b/core/key/CMakeLists.txt @@ -0,0 +1,3 @@ + +add_library(kagome_keys key.cpp) +target_link_libraries(kagome_keys p2p::p2p) diff --git a/core/key/key.cpp b/core/key/key.cpp new file mode 100644 index 0000000000..2ae61f1c7f --- /dev/null +++ b/core/key/key.cpp @@ -0,0 +1,42 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "key/key.hpp" + +#include +#include "crypto/key_store.hpp" +#include "crypto/random_generator/boost_generator.hpp" + +namespace kagome::key { + + Key::Key( + std::shared_ptr ed_crypto_provider, + std::shared_ptr key_marshaller) + : ed_crypto_provider_{std::move(ed_crypto_provider)}, + key_marshaller_{std::move(key_marshaller)} { + BOOST_ASSERT(ed_crypto_provider_ != nullptr); + BOOST_ASSERT(key_marshaller_ != nullptr); + } + + outcome::result Key::run() { + auto random_generator = std::make_shared(); + + auto seed = kagome::crypto::Ed25519Seed::from( + crypto::SecureCleanGuard{random_generator->randomBytes( + kagome::crypto::Ed25519Seed::size())}) + .value(); + auto keypair = ed_crypto_provider_->generateKeypair(seed, {}).value(); + auto libp2p_key = crypto::ed25519KeyToLibp2pKeypair(keypair); + libp2p::crypto::ProtobufKey protobuf_key{ + key_marshaller_->marshal(libp2p_key.publicKey).value()}; + auto peer_id = libp2p::peer::PeerId::fromPublicKey(protobuf_key); + std::cerr << peer_id.value().toBase58() << std::endl; + std::cout << kagome::common::hex_lower(keypair.secret_key.unsafeBytes()) + << std::endl; + + return outcome::success(); + } +} // namespace kagome::key diff --git a/core/key/key.hpp b/core/key/key.hpp new file mode 100644 index 0000000000..6ea082da18 --- /dev/null +++ b/core/key/key.hpp @@ -0,0 +1,29 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#include +#include "crypto/ed25519_provider.hpp" + +namespace kagome::key { + + class Key { + public: + Key(std::shared_ptr ed_crypto_provider, + std::shared_ptr + key_marshaller); + + outcome::result run(); + + private: + std::shared_ptr ed_crypto_provider_; + std::shared_ptr key_marshaller_; + }; + +} // namespace kagome::key diff --git a/core/utils/CMakeLists.txt b/core/utils/CMakeLists.txt index 14b509701e..9680c54357 100644 --- a/core/utils/CMakeLists.txt +++ b/core/utils/CMakeLists.txt @@ -42,5 +42,5 @@ add_library(kagome-key ) target_link_libraries(kagome-key - ed25519_provider + application_injector ) diff --git a/core/utils/key_main.cpp b/core/utils/key_main.cpp index 92fc096946..00ccdee83d 100644 --- a/core/utils/key_main.cpp +++ b/core/utils/key_main.cpp @@ -3,36 +3,18 @@ * All Rights Reserved * SPDX-License-Identifier: Apache-2.0 */ -#include -#include -#include "crypto/ed25519/ed25519_provider_impl.hpp" -#include "crypto/hasher/hasher_impl.hpp" -#include "crypto/key_store.hpp" -#include "crypto/random_generator/boost_generator.hpp" +#include "application/impl/app_configuration_impl.hpp" +#include "injector/application_injector.hpp" +#include "key/key.hpp" namespace kagome { int key_main(int argc, const char **argv) { if (argc == 2) { if (std::string(argv[1]) == "--generate-node-key") { - auto random_generator = - std::make_shared(); - auto hasher = std::make_shared(); - - auto ed25519_provider = - std::make_shared(hasher); - - auto seed = kagome::crypto::Ed25519Seed::from( - crypto::SecureCleanGuard{random_generator->randomBytes( - kagome::crypto::Ed25519Seed::size())}) - .value(); - auto keypair = ed25519_provider->generateKeypair(seed, {}).value(); - auto libp2p_key = crypto::ed25519KeyToLibp2pKeypair(keypair); - libp2p::crypto::ProtobufKey protobuf_key{ - common::Buffer{libp2p_key.publicKey.data}}; - auto peer_id = libp2p::peer::PeerId::fromPublicKey(protobuf_key); - std::cerr << peer_id.value().toBase58() << std::endl; - std::cout << kagome::common::hex_lower(keypair.secret_key.unsafeBytes()) - << std::endl; + injector::KagomeNodeInjector injector{ + std::make_shared()}; + auto key = injector.injectKey(); + auto res = key->run(); } else if (std::string(argv[1]) == "--help") { std::cerr << "Usage: " << argv[0] << " --generate-node-key" << "\nGenerates a node key and prints the peer ID to stderr " @@ -42,7 +24,7 @@ namespace kagome { std::cerr << "Unknown command: " << argv[1] << std::endl; std::cerr << "Usage: " << argv[0] << " --generate-node-key" << std::endl; - return 2; + return 3; } } else { std::cerr << "Usage: " << argv[0] << " --generate-node-key" << std::endl;