Skip to content

Commit

Permalink
Merge branch 'master' into fixup/era_points
Browse files Browse the repository at this point in the history
  • Loading branch information
iceseer authored Dec 11, 2024
2 parents fb431d8 + 97ef53f commit 0cf4151
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 1 deletion.
8 changes: 8 additions & 0 deletions core/application/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ target_link_libraries(recovery_mode
logger
)

add_library(kagome_key
modes/key_main.cpp
)

target_link_libraries(kagome_key
application_injector
)

add_library(kagome_application
impl/kagome_application_impl.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion core/application/impl/app_configuration_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ namespace kagome::application {

if (vm.count("help") > 0) {
std::cout
<< "Available subcommands: storage-explorer db-editor benchmark\n";
<< "Available subcommands: storage-explorer db-editor benchmark key\n";
std::cout << desc << '\n';
return false;
}
Expand Down
55 changes: 55 additions & 0 deletions core/application/modes/key.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <memory>

#include <libp2p/crypto/key_marshaller.hpp>
#include <libp2p/peer/peer_id.hpp>
#include "crypto/ed25519_provider.hpp"
#include "crypto/key_store.hpp"
#include "crypto/random_generator/boost_generator.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)
: 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> run() {
auto random_generator = std::make_unique<crypto::BoostRandomGenerator>();
OUTCOME_TRY(
seed,
crypto::Ed25519Seed::from(crypto::SecureCleanGuard{
random_generator->randomBytes(crypto::Ed25519Seed::size())}));
OUTCOME_TRY(keypair, ed_crypto_provider_->generateKeypair(seed, {}));
const auto libp2p_key = crypto::ed25519KeyToLibp2pKeypair(keypair);
const libp2p::crypto::ProtobufKey protobuf_key{
key_marshaller_->marshal(libp2p_key.publicKey).value()};
auto peer_id = libp2p::peer::PeerId::fromPublicKey(protobuf_key);
if (not peer_id) {
return peer_id.error();
}
std::cerr << peer_id.value().toBase58() << "\n";
std::cout << kagome::common::hex_lower(keypair.secret_key.unsafeBytes())
<< "\n";

return outcome::success();
}

private:
std::shared_ptr<crypto::Ed25519Provider> ed_crypto_provider_;
std::shared_ptr<libp2p::crypto::marshaller::KeyMarshaller> key_marshaller_;
};
} // namespace kagome::key
41 changes: 41 additions & 0 deletions core/application/modes/key_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include "application/impl/app_configuration_impl.hpp"
#include "injector/application_injector.hpp"
#include "key.hpp"

namespace kagome {
int key_main(int argc, const char **argv) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
const std::string_view key_command = argv[0];
if (argc == 2) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
const std::string_view command = argv[1];
if (command == "--generate-node-key") {
auto injector = std::make_unique<injector::KagomeNodeInjector>(
std::make_shared<application::AppConfigurationImpl>());
auto key = injector->injectKey();
if (auto res = key->run(); not res) {
std::cerr << "Error: " << res.error().message() << "\n";
return 2;
}
} else if (command == "--help") {
std::cerr << "Usage: " << key_command << " --generate-node-key"
<< "\nGenerates a node key and prints the peer ID to stderr "
"and the secret key to stdout.\n";
} else {
std::cerr << "Unknown command: " << command << "\n";
std::cerr << "Usage: " << key_command << " --generate-node-key\n";
return 3;
}
} else {
std::cerr << "Usage: " << key_command << " --generate-node-key\n";
return 1;
}
return 0;
}

} // namespace kagome
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_key
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 @@ -50,6 +50,7 @@
#include "application/modes/precompile_wasm.hpp"
#include "application/modes/print_chain_info_mode.hpp"
#include "application/modes/recovery_mode.hpp"
#include "application/modes/key.hpp"
#include "authority_discovery/publisher/address_publisher.hpp"
#include "authority_discovery/query/audi_store_impl.hpp"
#include "authority_discovery/query/query_impl.hpp"
Expand Down Expand Up @@ -1119,6 +1120,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
1 change: 1 addition & 0 deletions node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ target_link_libraries(kagome
kagome-db-editor
storage_explorer
filesystem
kagome_key
)
if (BACKWARD)
add_backward(kagome)
Expand Down
5 changes: 5 additions & 0 deletions node/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ int db_editor_main(int argc, const char **argv);

namespace kagome {
int benchmark_main(int argc, const char **argv);
int key_main(int argc, const char **argv);
}

namespace {
Expand Down Expand Up @@ -184,6 +185,10 @@ int main(int argc, const char **argv, const char **env) {
exit_code = kagome::benchmark_main(argc - 1, argv + 1);
}

else if (name == "key") {
exit_code = kagome::key_main(argc - 1, argv + 1);
}

else if (name.substr(0, 1) == "-") {
// The first argument isn't subcommand, run as node
exit_code = run_node(argc, argv);
Expand Down

0 comments on commit 0cf4151

Please sign in to comment.