-
Notifications
You must be signed in to change notification settings - Fork 40
Save lp2p key #1301
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
Merged
Merged
Save lp2p key #1301
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5d388db
save lp2p key
turuslan e9448ff
Merge branch 'bitfield' into save-lp2p-key-1072
turuslan 885e969
don't save node key by default
turuslan 12c5370
rename
turuslan 39f9850
injector const
turuslan 0f45076
split some code from injector
turuslan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef KAGOME_CORE_INJECTOR_GET_PEER_KEYPAIR_HPP | ||
#define KAGOME_CORE_INJECTOR_GET_PEER_KEYPAIR_HPP | ||
|
||
#include "application/app_configuration.hpp" | ||
#include "common/outcome_throw.hpp" | ||
#include "crypto/crypto_store/crypto_store_impl.hpp" | ||
#include "crypto/ed25519_provider.hpp" | ||
|
||
namespace kagome::injector { | ||
inline const std::shared_ptr<libp2p::crypto::KeyPair> &get_peer_keypair( | ||
const application::AppConfiguration &app_config, | ||
const crypto::Ed25519Provider &crypto_provider, | ||
crypto::CryptoStore &crypto_store) { | ||
static auto initialized = | ||
std::optional<std::shared_ptr<libp2p::crypto::KeyPair>>(std::nullopt); | ||
|
||
if (initialized) { | ||
return initialized.value(); | ||
} | ||
|
||
auto log = log::createLogger("Injector", "injector"); | ||
|
||
if (app_config.nodeKey()) { | ||
log->info("Will use LibP2P keypair from config or 'node-key' CLI arg"); | ||
|
||
auto provided_keypair = | ||
crypto_provider.generateKeypair(app_config.nodeKey().value()); | ||
BOOST_ASSERT(provided_keypair.secret_key == app_config.nodeKey().value()); | ||
|
||
auto key_pair = std::make_shared<libp2p::crypto::KeyPair>( | ||
crypto::ed25519KeyToLibp2pKeypair(provided_keypair)); | ||
|
||
initialized.emplace(std::move(key_pair)); | ||
return initialized.value(); | ||
} | ||
|
||
if (app_config.nodeKeyFile()) { | ||
const auto &path = app_config.nodeKeyFile().value(); | ||
log->info( | ||
"Will use LibP2P keypair from config or 'node-key-file' CLI arg"); | ||
auto key = crypto_store.loadLibp2pKeypair(path); | ||
if (key.has_error()) { | ||
log->error("Unable to load user provided key from {}. Error: {}", | ||
path, | ||
key.error().message()); | ||
common::raise(key.error()); | ||
} else { | ||
auto key_pair = | ||
std::make_shared<libp2p::crypto::KeyPair>(std::move(key.value())); | ||
initialized.emplace(std::move(key_pair)); | ||
return initialized.value(); | ||
} | ||
} | ||
|
||
if (crypto_store.getLibp2pKeypair().has_value()) { | ||
log->info( | ||
"Will use LibP2P keypair from config or args (loading from base " | ||
"path)"); | ||
|
||
auto stored_keypair = crypto_store.getLibp2pKeypair().value(); | ||
|
||
auto key_pair = | ||
std::make_shared<libp2p::crypto::KeyPair>(std::move(stored_keypair)); | ||
|
||
initialized.emplace(std::move(key_pair)); | ||
return initialized.value(); | ||
} | ||
|
||
log->warn( | ||
"Can not obtain a libp2p keypair from crypto storage. " | ||
"A unique one will be generated"); | ||
|
||
kagome::crypto::Ed25519Keypair generated_keypair; | ||
auto save = app_config.shouldSaveNodeKey(); | ||
if (save) { | ||
auto res = crypto_store.generateEd25519KeypairOnDisk( | ||
crypto::KnownKeyTypeId::KEY_TYPE_LP2P); | ||
if (res.has_error()) { | ||
log->warn("Can't save libp2p keypair: {}", res.error()); | ||
save = false; | ||
} else { | ||
generated_keypair = res.value(); | ||
} | ||
} | ||
if (not save) { | ||
generated_keypair = crypto_provider.generateKeypair(); | ||
} | ||
|
||
auto key_pair = std::make_shared<libp2p::crypto::KeyPair>( | ||
crypto::ed25519KeyToLibp2pKeypair(generated_keypair)); | ||
|
||
initialized.emplace(std::move(key_pair)); | ||
return initialized.value(); | ||
} | ||
} // namespace kagome::injector | ||
|
||
#endif // KAGOME_CORE_INJECTOR_GET_PEER_KEYPAIR_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.