17 case E::UNSUPPORTED_KEY_TYPE:
18 return "key type is not supported";
19 case E::UNSUPPORTED_CRYPTO_TYPE:
20 return "cryptographic type is not supported";
21 case E::WRONG_SEED_SIZE:
22 return "wrong seed size";
23 case E::KEY_NOT_FOUND:
24 return "key not found";
25 case E::BABE_ALREADY_EXIST:
26 return "BABE key already exists";
27 case E::GRAN_ALREADY_EXIST:
28 return "GRAN key already exists";
29 case E::AUDI_ALREADY_EXIST:
30 return "AUDI key already exists";
31 case E::WRONG_PUBLIC_KEY:
32 return "Public key doesn't match seed";
34 return "Unknown CryptoStoreError code";
40 std::shared_ptr<EcdsaSuite> ecdsa_suite,
41 std::shared_ptr<Ed25519Suite> ed_suite,
42 std::shared_ptr<Sr25519Suite> sr_suite,
43 std::shared_ptr<Bip39Provider> bip39_provider,
44 std::shared_ptr<KeyFileStorage> key_fs)
45 : file_storage_{std::move(key_fs)},
59 KeyTypeId key_type, std::string_view mnemonic_phrase) {
62 .insert(kp.public_key, kp.secret_key);
67 KeyTypeId key_type, std::string_view mnemonic_phrase) {
70 .insert(kp.public_key, kp.secret_key);
75 KeyTypeId key_type, std::string_view mnemonic_phrase) {
78 .insert(kp.public_key, kp.secret_key);
86 .insert(kp.public_key, kp.secret_key);
91 KeyTypeId key_type,
const Ed25519Seed &seed) {
92 OUTCOME_TRY(kp,
ed_suite_->generateKeypair(seed));
94 .insert(kp.public_key, kp.secret_key);
99 KeyTypeId key_type,
const Sr25519Seed &seed) {
100 OUTCOME_TRY(kp,
sr_suite_->generateKeypair(seed));
102 .insert(kp.public_key, kp.secret_key);
103 return std::move(kp);
122 KeyTypeId key_type,
const EcdsaPublicKey &pk)
const {
126 return kp_opt.value();
128 OUTCOME_TRY(seed_bytes,
130 if (not seed_bytes) {
134 auto bytes = seed_bytes.value();
135 if (seed.
size() != bytes.size()) {
138 std::copy(bytes.begin(), bytes.end(), seed.begin());
143 KeyTypeId key_type,
const Ed25519PublicKey &pk)
const {
146 return kp_opt.value();
148 OUTCOME_TRY(seed_bytes,
150 if (not seed_bytes) {
153 OUTCOME_TRY(seed, Ed25519Seed::fromSpan(seed_bytes.value()));
158 KeyTypeId key_type,
const Sr25519PublicKey &pk)
const {
161 return kp_opt.value();
163 OUTCOME_TRY(seed_bytes,
165 if (not seed_bytes) {
168 OUTCOME_TRY(seed, Sr25519Seed::fromSpan(seed_bytes.value()));
172 outcome::result<CryptoStoreImpl::EcdsaKeys>
179 outcome::result<CryptoStoreImpl::Ed25519Keys>
185 outcome::result<CryptoStoreImpl::Sr25519Keys>
194 if (not keys or keys.value().empty()) {
207 if (lookup_res.has_error()
209 OUTCOME_TRY(kp,
ed_suite_->generateRandomKeypair());
211 .insert(kp.public_key, kp.secret_key);
212 OUTCOME_TRY(
file_storage_->saveKeyHexAtPath(kp.secret_key, key_path));
216 if (lookup_res.has_error()) {
217 return lookup_res.error();
219 const auto &contents = lookup_res.value();
220 BOOST_ASSERT(ED25519_SEED_LENGTH == contents.size()
221 or 2 * ED25519_SEED_LENGTH == contents.size());
223 if (ED25519_SEED_LENGTH == contents.size()) {
226 Ed25519Seed::fromSpan(gsl::span(
227 reinterpret_cast<const uint8_t *>(contents.data()),
228 ED25519_SEED_LENGTH)));
230 }
else if (2 * ED25519_SEED_LENGTH == contents.size()) {
231 OUTCOME_TRY(_seed, Ed25519Seed::fromHexWithPrefix(contents));
236 OUTCOME_TRY(kp,
ed_suite_->generateKeypair(seed));
239 .insert(kp.public_key, kp.secret_key);
247 {libp2p::crypto::Key::Type::Ed25519,
248 std::vector<uint8_t>{public_key.cbegin(), public_key.cend()}}};
249 libp2p::crypto::PrivateKey lp2p_private{
250 {libp2p::crypto::Key::Type::Ed25519,
251 std::vector<uint8_t>{secret_key.cbegin(), secret_key.cend()}}};
252 return libp2p::crypto::KeyPair{
253 .publicKey = lp2p_public,
254 .privateKey = lp2p_private,
outcome::result< Ed25519Keypair > findEd25519Keypair(KeyTypeId key_type, const Ed25519PublicKey &pk) const override
searches for key pair
outcome::result< EcdsaKeypair > generateEcdsaKeypair(KeyTypeId key_type, std::string_view mnemonic_phrase) override
generates ecdsa keypair and stores it in memory
outcome::result< Ed25519Keypair > generateEd25519KeypairOnDisk(KeyTypeId key_type) override
generates Ed25519 keypair and stores it on disk
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::optional< libp2p::crypto::KeyPair > getLibp2pKeypair() const override
outcome::result< Sr25519Keys > getSr25519PublicKeys(KeyTypeId key_type) const override
searches for SR25519 keys of specified type
OUTCOME_CPP_DEFINE_CATEGORY(kagome::crypto, CryptoStoreError, e)
std::shared_ptr< KeyFileStorage > file_storage_
outcome::result< EcdsaKeypair > generateEcdsaKeypairOnDisk(KeyTypeId key_type) override
generates ecdsa keypair and stores it on disk
std::shared_ptr< Ed25519Suite > ed_suite_
std::unordered_map< KeyTypeId, KeyCache< Ed25519Suite > > ed_caches_
std::shared_ptr< EcdsaSuite > ecdsa_suite_
outcome::result< EcdsaKeypair > findEcdsaKeypair(KeyTypeId key_type, const EcdsaPublicKey &pk) const override
searches for key pair
outcome::result< Sr25519Keypair > findSr25519Keypair(KeyTypeId key_type, const Sr25519PublicKey &pk) const override
searches for key pair
Ed25519PrivateKey secret_key
outcome::result< Ed25519Keys > getEd25519PublicKeys(KeyTypeId key_type) const override
searches for Ed25519 keys of specified type
libp2p::crypto::KeyPair ed25519KeyToLibp2pKeypair(const Ed25519Keypair &kp)
uint32_t KeyTypeId
Key type identifier.
common::Blob< constants::kGeneralPublicKeySize > PublicKey
gsl::span< const uint8_t > make_span(const rocksdb::Slice &s)
CryptoStoreImpl(std::shared_ptr< EcdsaSuite > ecdsa_suite, std::shared_ptr< Ed25519Suite > ed_suite, std::shared_ptr< Sr25519Suite > sr_suite, std::shared_ptr< Bip39Provider > bip39_provider, std::shared_ptr< KeyFileStorage > key_fs)
KeyCache< Suite > & getCache(std::shared_ptr< Suite > suite, std::unordered_map< KeyTypeId, KeyCache< Suite >> &caches, KeyTypeId type) const
std::shared_ptr< Sr25519Suite > sr_suite_
outcome::result< Sr25519Keypair > generateSr25519KeypairOnDisk(KeyTypeId key_type) override
generates SR25519 keypair and stores it on disk
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
std::unordered_map< KeyTypeId, KeyCache< EcdsaSuite > > ecdsa_caches_
outcome::result< EcdsaKeys > getEcdsaPublicKeys(KeyTypeId key_type) const override
searches for ecdsa keys of specified type
outcome::result< Sr25519Keypair > generateSr25519Keypair(KeyTypeId key_type, std::string_view mnemonic_phrase) override
generates SR25519 keypair and stores it in memory
outcome::result< Ed25519Keypair > generateEd25519Keypair(KeyTypeId key_type, std::string_view mnemonic_phrase) override
generates Ed25519 keypair and stores it in memory
outcome::result< libp2p::crypto::KeyPair > loadLibp2pKeypair(const Path &key_path) const override
static constexpr size_t size()
Ed25519PublicKey public_key
std::shared_ptr< Bip39Provider > bip39_provider_
Logger createLogger(const std::string &tag)