6 #ifndef KAGOME_CRYPTO_STORE_IMPL_HPP 7 #define KAGOME_CRYPTO_STORE_IMPL_HPP 9 #include <unordered_set> 11 #include <boost/filesystem.hpp> 12 #include <boost/variant.hpp> 47 std::shared_ptr<Ed25519Suite> ed_suite,
48 std::shared_ptr<Sr25519Suite> sr_suite,
49 std::shared_ptr<Bip39Provider> bip39_provider,
50 std::shared_ptr<KeyFileStorage> key_fs);
52 outcome::result<EcdsaKeypair> generateEcdsaKeypair(
53 KeyTypeId key_type, std::string_view mnemonic_phrase)
override;
55 outcome::result<Ed25519Keypair> generateEd25519Keypair(
56 KeyTypeId key_type, std::string_view mnemonic_phrase)
override;
58 outcome::result<Sr25519Keypair> generateSr25519Keypair(
59 KeyTypeId key_type, std::string_view mnemonic_phrase)
override;
61 outcome::result<EcdsaKeypair> generateEcdsaKeypair(
64 outcome::result<Ed25519Keypair> generateEd25519Keypair(
65 KeyTypeId key_type,
const Ed25519Seed &seed)
override;
67 outcome::result<Sr25519Keypair> generateSr25519Keypair(
68 KeyTypeId key_type,
const Sr25519Seed &seed)
override;
70 outcome::result<EcdsaKeypair> generateEcdsaKeypairOnDisk(
73 outcome::result<Ed25519Keypair> generateEd25519KeypairOnDisk(
76 outcome::result<Sr25519Keypair> generateSr25519KeypairOnDisk(
79 outcome::result<EcdsaKeypair> findEcdsaKeypair(
80 KeyTypeId key_type,
const EcdsaPublicKey &pk)
const override;
82 outcome::result<Ed25519Keypair> findEd25519Keypair(
83 KeyTypeId key_type,
const Ed25519PublicKey &pk)
const override;
85 outcome::result<Sr25519Keypair> findSr25519Keypair(
86 KeyTypeId key_type,
const Sr25519PublicKey &pk)
const override;
88 outcome::result<EcdsaKeys> getEcdsaPublicKeys(
91 outcome::result<Ed25519Keys> getEd25519PublicKeys(
94 outcome::result<Sr25519Keys> getSr25519PublicKeys(
97 std::optional<libp2p::crypto::KeyPair> getLibp2pKeypair()
const override;
99 outcome::result<libp2p::crypto::KeyPair> loadLibp2pKeypair(
100 const Path &key_path)
const override;
103 template <
typename CryptoSuite>
104 outcome::result<std::vector<typename CryptoSuite::PublicKey>>
getPublicKeys(
109 OUTCOME_TRY(keys, file_storage_->collectPublicKeys(key_type));
111 std::vector<typename CryptoSuite::PublicKey> res;
112 res.reserve(keys.size());
113 for (
auto &key : keys) {
115 auto erased = cached_keys.erase(pk);
119 res.emplace_back(std::move(pk));
126 OUTCOME_TRY(seed_bytes, file_storage_->searchForSeed(key_type, key));
129 "The public key has just been scanned, its file has to exist");
130 if (not seed_bytes) {
131 logger_->error(
"Error reading key seed from key file storage");
134 auto seed_res = suite.
toSeed(seed_bytes.value());
140 SL_TRACE(logger_,
"Loaded key {}", pk.toHex());
144 SL_TRACE(logger_,
"Key is correct {}", pk.toHex());
145 res.emplace_back(std::move(pk));
150 cached_keys.begin(), cached_keys.end(), std::back_inserter(res));
154 template <
typename CryptoSuite>
156 std::string_view mnemonic_phrase,
const CryptoSuite &suite) {
161 OUTCOME_TRY(bip_seed, bip39_provider_->generateSeed(mnemonic_phrase));
162 if (bip_seed.size() < Seed::size()) {
166 OUTCOME_TRY(seed, Seed::fromSpan(seed_span));
170 template <
typename CryptoSuite>
173 const std::shared_ptr<CryptoSuite> &suite,
175 OUTCOME_TRY(kp, suite->generateRandomKeypair());
177 OUTCOME_TRY(file_storage_->saveKeyPair(key_type, kp.
public_key, kp.seed));
178 return std::move(kp);
181 template <
typename Suite>
183 std::shared_ptr<Suite> suite,
186 auto it = caches.find(type);
187 if (it == caches.end()) {
188 auto &&[new_it, success] = caches.insert({type,
KeyCache{type, suite}});
189 BOOST_ASSERT(success);
196 mutable std::unordered_map<KeyTypeId, KeyCache<Ed25519Suite>>
ed_caches_;
197 mutable std::unordered_map<KeyTypeId, KeyCache<Sr25519Suite>>
sr_caches_;
210 #endif // KAGOME_CRYPTO_STORE_IMPL_HPP
static const DevMnemonicPhrase & get()
boost::filesystem::path Path
std::unordered_map< KeyTypeId, KeyCache< Sr25519Suite > > sr_caches_
outcome::result< typename CryptoSuite::Keypair > generateKeypair(std::string_view mnemonic_phrase, const CryptoSuite &suite)
std::shared_ptr< KeyFileStorage > file_storage_
std::shared_ptr< Ed25519Suite > ed_suite_
std::unordered_map< KeyTypeId, KeyCache< Ed25519Suite > > ed_caches_
std::shared_ptr< EcdsaSuite > ecdsa_suite_
virtual outcome::result< Seed > toSeed(gsl::span< const uint8_t > bytes) const noexcept=0
Ed25519PrivateKey secret_key
libp2p::crypto::KeyPair ed25519KeyToLibp2pKeypair(const Ed25519Keypair &kp)
uint32_t KeyTypeId
Key type identifier.
virtual outcome::result< PublicKey > toPublicKey(gsl::span< const uint8_t > bytes) const noexcept=0
gsl::span< const uint8_t > make_span(const rocksdb::Slice &s)
KeyCache< Suite > & getCache(std::shared_ptr< Suite > suite, std::unordered_map< KeyTypeId, KeyCache< Suite >> &caches, KeyTypeId type) const
std::shared_ptr< Sr25519Suite > sr_suite_
std::shared_ptr< soralog::Logger > Logger
outcome::result< typename CryptoSuite::Keypair > generateKeypairOnDisk(KeyTypeId key_type, const std::shared_ptr< CryptoSuite > &suite, std::unordered_map< KeyTypeId, KeyCache< CryptoSuite >> &caches)
outcome::result< std::vector< typename CryptoSuite::PublicKey > > getPublicKeys(KeyTypeId key_type, const KeyCache< CryptoSuite > &cache, const CryptoSuite &suite) const
OUTCOME_HPP_DECLARE_ERROR(kagome::crypto, CryptoStoreError)
std::unordered_map< KeyTypeId, KeyCache< EcdsaSuite > > ecdsa_caches_
Ed25519PublicKey public_key
std::shared_ptr< Bip39Provider > bip39_provider_
virtual outcome::result< Keypair > generateKeypair(const Seed &seed) const noexcept=0
virtual std::pair< PublicKey, PrivateKey > decomposeKeypair(const Keypair &kp) const noexcept=0
std::unordered_set< PublicKey > getPublicKeys() const