Kagome
Polkadot Runtime Engine in C++17
sr25519_provider_impl.cpp
Go to the documentation of this file.
1 
7 
9 #include "libp2p/crypto/random_generator.hpp"
10 
11 namespace kagome::crypto {
12  Sr25519ProviderImpl::Sr25519ProviderImpl(std::shared_ptr<CSPRNG> generator)
13  : generator_(std::move(generator)) {
14  BOOST_ASSERT(generator_ != nullptr);
15  }
16 
18  Sr25519Seed seed;
19  generator_->fillRandomly(seed);
20  return Sr25519KeypairAndSeed{generateKeypair(seed), seed};
21  }
22 
24  const Sr25519Seed &seed) const {
25  std::array<uint8_t, constants::sr25519::KEYPAIR_SIZE> kp{};
26  sr25519_keypair_from_seed(kp.data(), seed.data());
27 
28  Sr25519Keypair keypair;
29  std::copy(kp.begin(),
31  keypair.secret_key.begin());
32  std::copy(kp.begin() + constants::sr25519::SECRET_SIZE,
35  keypair.public_key.begin());
36  return keypair;
37  }
38 
39  outcome::result<Sr25519Signature> Sr25519ProviderImpl::sign(
40  const Sr25519Keypair &keypair, gsl::span<const uint8_t> message) const {
41  Sr25519Signature signature{};
42 
43  try {
44  sr25519_sign(signature.data(),
45  keypair.public_key.data(),
46  keypair.secret_key.data(),
47  message.data(),
48  message.size());
49  } catch (...) {
51  }
52 
53  return signature;
54  }
55 
57  const Sr25519Signature &signature,
58  gsl::span<const uint8_t> message,
59  const Sr25519PublicKey &public_key) const {
60  bool result = false;
61  try {
62  result = sr25519_verify_deprecated(
63  signature.data(), message.data(), message.size(), public_key.data());
64  } catch (...) {
66  }
67  return result;
68  }
69 
70  outcome::result<bool> Sr25519ProviderImpl::verify(
71  const Sr25519Signature &signature,
72  gsl::span<const uint8_t> message,
73  const Sr25519PublicKey &public_key) const {
74  bool result = false;
75  try {
76  result = sr25519_verify(
77  signature.data(), message.data(), message.size(), public_key.data());
78  } catch (...) {
80  }
81  return outcome::success(result);
82  }
83 } // namespace kagome::crypto
84 
87  switch (e) {
89  return "failed to sign message, unknown error occured";
91  return "failed to verify message, unknown error occured";
92  }
93  return "unknown Sr25519ProviderError";
94 }
Sr25519ProviderImpl(std::shared_ptr< CSPRNG > generator)
STL namespace.
Sr25519KeypairAndSeed generateKeypair() const override
outcome::result< Sr25519Signature > sign(const Sr25519Keypair &keypair, gsl::span< const uint8_t > message) const override
outcome::result< bool > verify(const Sr25519Signature &signature, gsl::span< const uint8_t > message, const Sr25519PublicKey &public_key) const override
OUTCOME_CPP_DEFINE_CATEGORY(kagome::crypto, Sr25519ProviderError, e)
outcome::result< bool > verify_deprecated(const Sr25519Signature &signature, gsl::span< const uint8_t > message, const Sr25519PublicKey &public_key) const override