Kagome
Polkadot Runtime Engine in C++17
signer.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_PARACHAIN_VALIDATOR_SIGNER_HPP
7 #define KAGOME_PARACHAIN_VALIDATOR_SIGNER_HPP
8 
9 #include <optional>
10 
15 
16 namespace kagome::parachain {
19  private:
20  static auto &toSignable(const crypto::Hasher &, const scale::BitVec &v) {
21  return v;
22  }
23  static auto toSignable(const crypto::Hasher &hasher,
24  const network::CandidateState &v) {
25  constexpr std::array<uint8_t, 4> kMagic{'B', 'K', 'N', 'G'};
26  return std::make_tuple(
27  kMagic, static_cast<uint8_t>(v.which()), candidateHash(hasher, v));
28  }
29 
30  public:
31  SCALE_TIE(2);
32 
34  static outcome::result<SigningContext> make(
35  const std::shared_ptr<runtime::ParachainHost> &parachain_api,
37 
39  template <typename T>
40  auto signable(const crypto::Hasher &hasher, const T &payload) const {
41  auto &&signable = toSignable(hasher, payload);
42  return scale::encode(std::tie(signable, *this)).value();
43  }
44 
49  };
50 
53  public:
55 
56  ValidatorSigner(ValidatorIndex validator_index,
57  SigningContext context,
58  std::shared_ptr<crypto::Sr25519Keypair> keypair,
59  std::shared_ptr<crypto::Hasher> hasher,
60  std::shared_ptr<crypto::Sr25519Provider> sr25519_provider);
61 
63  template <typename T>
64  outcome::result<network::Signed<T>> sign(T payload) const {
65  auto data = context_.signable(*hasher_, payload);
66  OUTCOME_TRY(signature, sr25519_provider_->sign(*keypair_, data));
67  return network::Signed<T>{
68  std::move(payload),
69  validator_index_,
70  signature,
71  };
72  }
73 
75  ValidatorIndex validatorIndex() const;
76 
78  const primitives::BlockHash &relayParent() const;
79 
80  private:
83  std::shared_ptr<crypto::Sr25519Keypair> keypair_;
84  std::shared_ptr<crypto::Hasher> hasher_;
85  std::shared_ptr<crypto::Sr25519Provider> sr25519_provider_;
86  };
87 
90  public:
92  std::shared_ptr<runtime::ParachainHost> parachain_api,
93  std::shared_ptr<crypto::SessionKeys> session_keys,
94  std::shared_ptr<crypto::Hasher> hasher,
95  std::shared_ptr<crypto::Sr25519Provider> sr25519_provider);
96 
98  outcome::result<std::optional<ValidatorSigner>> at(
100 
101  private:
102  std::shared_ptr<runtime::ParachainHost> parachain_api_;
103  std::shared_ptr<crypto::SessionKeys> session_keys_;
104  std::shared_ptr<crypto::Hasher> hasher_;
105  std::shared_ptr<crypto::Sr25519Provider> sr25519_provider_;
106  };
107 } // namespace kagome::parachain
108 
109 #endif // KAGOME_PARACHAIN_VALIDATOR_SIGNER_HPP
network::ValidatorIndex ValidatorIndex
Definition: signer.hpp:54
std::shared_ptr< crypto::Sr25519Keypair > keypair_
Definition: signer.hpp:83
runtime::SessionIndex session_index
Current session index.
Definition: signer.hpp:46
std::shared_ptr< crypto::Sr25519Provider > sr25519_provider_
Definition: signer.hpp:85
Payload signed by validator.
boost::variant< Dummy, CommittedCandidateReceipt, primitives::BlockHash > CandidateState
std::shared_ptr< crypto::Hasher > hasher_
Definition: signer.hpp:104
auto signable(const crypto::Hasher &hasher, const T &payload) const
Make signable message for payload.
Definition: signer.hpp:40
std::shared_ptr< runtime::ParachainHost > parachain_api_
Definition: signer.hpp:102
std::shared_ptr< crypto::SessionKeys > session_keys_
Definition: signer.hpp:103
std::shared_ptr< crypto::Hasher > hasher_
Definition: signer.hpp:84
std::shared_ptr< crypto::Sr25519Provider > sr25519_provider_
Definition: signer.hpp:105
static outcome::result< SigningContext > make(const std::shared_ptr< runtime::ParachainHost > &parachain_api, const primitives::BlockHash &relay_parent)
Make signing context for given block.
Definition: signer.cpp:9
Signs payload with signing context and validator keypair.
Definition: signer.hpp:52
static auto toSignable(const crypto::Hasher &hasher, const network::CandidateState &v)
Definition: signer.hpp:23
outcome::result< network::Signed< T > > sign(T payload) const
Sign payload.
Definition: signer.hpp:64
CandidateHash candidateHash(const crypto::Hasher &hasher, const CandidateReceipt &receipt)
A type returned by runtime with current session index and a parent hash.
Definition: signer.hpp:18
primitives::BlockHash relay_parent
Hash of the parent.
Definition: signer.hpp:48
static auto & toSignable(const crypto::Hasher &, const scale::BitVec &v)
Definition: signer.hpp:20
Creates validator signer.
Definition: signer.hpp:89