Kagome
Polkadot Runtime Engine in C++17
get_peer_keypair.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_INJECTOR_GET_PEER_KEYPAIR_HPP
7 #define KAGOME_CORE_INJECTOR_GET_PEER_KEYPAIR_HPP
8 
10 #include "common/outcome_throw.hpp"
13 
14 namespace kagome::injector {
15  inline const std::shared_ptr<libp2p::crypto::KeyPair> &get_peer_keypair(
16  const application::AppConfiguration &app_config,
17  const crypto::Ed25519Provider &crypto_provider,
18  crypto::CryptoStore &crypto_store) {
19  static auto initialized =
20  std::optional<std::shared_ptr<libp2p::crypto::KeyPair>>(std::nullopt);
21 
22  if (initialized) {
23  return initialized.value();
24  }
25 
26  auto log = log::createLogger("Injector", "injector");
27 
28  if (app_config.nodeKey()) {
29  log->info("Will use LibP2P keypair from config or 'node-key' CLI arg");
30 
31  auto provided_keypair = crypto_provider.generateKeypair(
32  crypto::Ed25519Seed{app_config.nodeKey().value()});
33  BOOST_ASSERT(provided_keypair.secret_key == app_config.nodeKey().value());
34 
35  auto key_pair = std::make_shared<libp2p::crypto::KeyPair>(
36  crypto::ed25519KeyToLibp2pKeypair(provided_keypair));
37 
38  initialized.emplace(std::move(key_pair));
39  return initialized.value();
40  }
41 
42  if (app_config.nodeKeyFile()) {
43  const auto &path = app_config.nodeKeyFile().value();
44  log->info(
45  "Will use LibP2P keypair from config or 'node-key-file' CLI arg");
46  auto key = crypto_store.loadLibp2pKeypair(path);
47  if (key.has_error()) {
48  log->error("Unable to load user provided key from {}. Error: {}",
49  path,
50  key.error().message());
51  common::raise(key.error());
52  } else {
53  auto key_pair =
54  std::make_shared<libp2p::crypto::KeyPair>(std::move(key.value()));
55  initialized.emplace(std::move(key_pair));
56  return initialized.value();
57  }
58  }
59 
60  if (crypto_store.getLibp2pKeypair().has_value()) {
61  log->info(
62  "Will use LibP2P keypair from config or args (loading from base "
63  "path)");
64 
65  auto stored_keypair = crypto_store.getLibp2pKeypair().value();
66 
67  auto key_pair =
68  std::make_shared<libp2p::crypto::KeyPair>(std::move(stored_keypair));
69 
70  initialized.emplace(std::move(key_pair));
71  return initialized.value();
72  }
73 
74  log->warn(
75  "Can not obtain a libp2p keypair from crypto storage. "
76  "A unique one will be generated");
77 
78  kagome::crypto::Ed25519Keypair generated_keypair;
79  auto save = app_config.shouldSaveNodeKey();
80  if (save) {
81  auto res = crypto_store.generateEd25519KeypairOnDisk(
83  if (res.has_error()) {
84  log->warn("Can't save libp2p keypair: {}", res.error());
85  save = false;
86  } else {
87  generated_keypair = res.value();
88  }
89  }
90  if (not save) {
91  generated_keypair = crypto_provider.generateKeypair();
92  }
93 
94  auto key_pair = std::make_shared<libp2p::crypto::KeyPair>(
95  crypto::ed25519KeyToLibp2pKeypair(generated_keypair));
96 
97  initialized.emplace(std::move(key_pair));
98  return initialized.value();
99  }
100 } // namespace kagome::injector
101 
102 #endif // KAGOME_CORE_INJECTOR_GET_PEER_KEYPAIR_HPP
void raise(T t)
throws outcome::result error as boost exception
virtual std::optional< libp2p::crypto::KeyPair > getLibp2pKeypair() const =0
const std::shared_ptr< libp2p::crypto::KeyPair > & get_peer_keypair(const application::AppConfiguration &app_config, const crypto::Ed25519Provider &crypto_provider, crypto::CryptoStore &crypto_store)
libp2p::crypto::KeyPair ed25519KeyToLibp2pKeypair(const Ed25519Keypair &kp)
virtual const std::optional< crypto::Ed25519PrivateKey > & nodeKey() const =0
virtual outcome::result< libp2p::crypto::KeyPair > loadLibp2pKeypair(const Path &key_path) const =0
virtual Ed25519KeypairAndSeed generateKeypair() const =0
virtual bool shouldSaveNodeKey() const =0
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
virtual outcome::result< Ed25519Keypair > generateEd25519KeypairOnDisk(KeyTypeId key_type)=0
generates Ed25519 keypair and stores it on disk
virtual const std::optional< std::string > & nodeKeyFile() const =0