Skip to content

Commit

Permalink
Reinvented to work with injector
Browse files Browse the repository at this point in the history
  • Loading branch information
ErakhtinB committed Dec 9, 2024
1 parent c271f36 commit 792cdf9
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 27 deletions.
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ add_subdirectory(utils)
add_subdirectory(offchain)
add_subdirectory(parachain)
add_subdirectory(dispute_coordinator)
add_subdirectory(key)
1 change: 1 addition & 0 deletions core/application/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ target_link_libraries(application_modes
hexutil
RapidJSON::rapidjson
benchmark::benchmark
kagome_keys
)

add_library(recovery_mode
Expand Down
1 change: 1 addition & 0 deletions core/injector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ target_link_libraries(application_injector
grandpa_api
host_api_factory
kagome_benchmarks
kagome_keys
log_configurator
metadata_api
metrics
Expand Down
5 changes: 5 additions & 0 deletions core/injector/application_injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -1117,6 +1118,10 @@ namespace kagome::injector {
.template create<sptr<benchmark::BlockExecutionBenchmark>>();
}

std::shared_ptr<key::Key> KagomeNodeInjector::injectKey() {
return pimpl_->injector_.template create<sptr<key::Key>>();
}

std::shared_ptr<Watchdog> KagomeNodeInjector::injectWatchdog() {
return pimpl_->injector_.template create<sptr<Watchdog>>();
}
Expand Down
6 changes: 6 additions & 0 deletions core/injector/application_injector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace kagome {
class PrecompileWasmMode;
class RecoveryMode;
class BenchmarkMode;
class Key;
} // namespace application::mode

namespace authority_discovery {
Expand Down Expand Up @@ -104,6 +105,10 @@ namespace kagome {
class TelemetryService;
}

namespace key {
class Key;
}

class Watchdog;
} // namespace kagome

Expand Down Expand Up @@ -163,6 +168,7 @@ namespace kagome::injector {
injectPrecompileWasmMode();
std::shared_ptr<application::mode::RecoveryMode> injectRecoveryMode();
std::shared_ptr<benchmark::BlockExecutionBenchmark> injectBlockBenchmark();
std::shared_ptr<key::Key> injectKey();

protected:
std::shared_ptr<class KagomeNodeInjectorImpl> pimpl_;
Expand Down
3 changes: 3 additions & 0 deletions core/key/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

add_library(kagome_keys key.cpp)
target_link_libraries(kagome_keys p2p::p2p)
42 changes: 42 additions & 0 deletions core/key/key.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#include "key/key.hpp"

#include <libp2p/peer/peer_id.hpp>
#include "crypto/key_store.hpp"
#include "crypto/random_generator/boost_generator.hpp"

namespace kagome::key {

Key::Key(
std::shared_ptr<crypto::Ed25519Provider> ed_crypto_provider,
std::shared_ptr<libp2p::crypto::marshaller::KeyMarshaller> 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<void> Key::run() {
auto random_generator = std::make_shared<crypto::BoostRandomGenerator>();

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
29 changes: 29 additions & 0 deletions core/key/key.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <memory>

#include <libp2p/crypto/key_marshaller.hpp>
#include "crypto/ed25519_provider.hpp"

namespace kagome::key {

class Key {
public:
Key(std::shared_ptr<crypto::Ed25519Provider> ed_crypto_provider,
std::shared_ptr<libp2p::crypto::marshaller::KeyMarshaller>
key_marshaller);

outcome::result<void> run();

private:
std::shared_ptr<crypto::Ed25519Provider> ed_crypto_provider_;
std::shared_ptr<libp2p::crypto::marshaller::KeyMarshaller> key_marshaller_;
};

} // namespace kagome::key
2 changes: 1 addition & 1 deletion core/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ add_library(kagome-key
)

target_link_libraries(kagome-key
ed25519_provider
application_injector
)
34 changes: 8 additions & 26 deletions core/utils/key_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,18 @@
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include <libp2p/crypto/key_marshaller.hpp>
#include <libp2p/peer/peer_id.hpp>
#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<crypto::BoostRandomGenerator>();
auto hasher = std::make_shared<crypto::HasherImpl>();

auto ed25519_provider =
std::make_shared<kagome::crypto::Ed25519ProviderImpl>(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<application::AppConfigurationImpl>()};
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 "
Expand All @@ -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;
Expand Down

0 comments on commit 792cdf9

Please sign in to comment.