8 #include <jsonrpc-lean/fault.h> 9 #include <boost/assert.hpp> 10 #include <boost/system/error_code.hpp> 23 #include "scale/scale_decoder_stream.hpp" 28 const std::vector<crypto::KnownKeyTypeId>
kKeyTypes{
40 : keys_api_(
std::move(key_api)),
41 pool_{std::move(pool)},
43 keys_{std::move(keys)},
47 BOOST_ASSERT_MSG(
keys_api_ !=
nullptr,
"session keys api is nullptr");
48 BOOST_ASSERT_MSG(
pool_ !=
nullptr,
"transaction pool is nullptr");
49 BOOST_ASSERT_MSG(
store_ !=
nullptr,
"crypto store is nullptr");
50 BOOST_ASSERT_MSG(
keys_ !=
nullptr,
"session keys store is nullptr");
51 BOOST_ASSERT_MSG(
key_store_ !=
nullptr,
"key store is nullptr");
52 BOOST_ASSERT_MSG(
block_tree_ !=
nullptr,
"block tree is nullptr");
53 BOOST_ASSERT_MSG(
logger_ !=
nullptr,
"logger is nullptr");
57 std::shared_ptr<api::ApiService>
const &api_service) {
58 BOOST_ASSERT(api_service !=
nullptr);
65 return pool_->submitExtrinsic(source, extrinsic);
70 const gsl::span<const uint8_t> &seed,
71 const gsl::span<const uint8_t> &public_key) {
80 SL_INFO(
logger_,
"Unsupported key type, only [{}] are accepted", types);
84 SL_INFO(
logger_,
"Babe key already exists and won't be replaced");
88 SL_INFO(
logger_,
"Grandpa key already exists and won't be replaced");
93 "Authority discovery key already exists and won't be replaced");
98 OUTCOME_TRY(seed_typed, crypto::Sr25519Seed::fromSpan(seed));
99 OUTCOME_TRY(public_key_typed,
100 crypto::Sr25519PublicKey::fromSpan(public_key));
102 store_->generateSr25519Keypair(key_type, seed_typed));
103 if (public_key_typed != keypair.public_key) {
108 OUTCOME_TRY(seed_typed, crypto::Ed25519Seed::fromSpan(seed));
109 OUTCOME_TRY(public_key_typed,
110 crypto::Ed25519PublicKey::fromSpan(public_key));
114 if (public_key_typed != keypair.public_key) {
118 auto res =
key_store_->saveKeyPair(key_type, public_key, seed);
120 keys_->getBabeKeyPair();
121 keys_->getGranKeyPair();
122 keys_->getAudiKeyPair();
127 OUTCOME_TRY(encoded_session_keys,
130 return std::move(encoded_session_keys);
137 const gsl::span<const uint8_t> &keys) {
138 scale::ScaleDecoderStream stream(keys);
139 std::array<uint8_t, 32> key;
140 if (keys.size() < 32 || keys.size() > 32 * 6 || (keys.size() % 32) != 0) {
142 "not valid key sequence, author_hasSessionKeys RPC call expects " 143 "no more than 6 public keys in concatenated string, keys should " 144 "be 32 byte in size");
148 if (
store_->findEd25519Keypair(
152 while (stream.currentIndex() < keys.size()) {
154 if (not
store_->findSr25519Keypair(
167 auto res =
key_store_->searchForSeed(key_type, public_key);
171 return res.value() ?
true :
false;
174 outcome::result<std::vector<primitives::Extrinsic>>
176 auto &pending_txs =
pool_->getPendingTransactions();
178 std::vector<primitives::Extrinsic> result;
179 result.reserve(pending_txs.size());
181 std::transform(pending_txs.begin(),
183 std::back_inserter(result),
184 [](
auto &it) {
return it.second->ext; });
189 outcome::result<std::vector<primitives::Extrinsic>>
191 const std::vector<primitives::ExtrinsicKey> &keys) {
192 BOOST_ASSERT_MSG(
false,
"not implemented");
193 return outcome::failure(boost::system::error_code{});
196 outcome::result<AuthorApi::SubscriptionId>
201 pool_->constructTransaction(TransactionSource::External, extrinsic));
202 OUTCOME_TRY(sub_id, service->subscribeForExtrinsicLifecycle(tx.hash));
207 TransactionSource::External,
209 BOOST_ASSERT(tx_hash == tx.hash);
211 SL_DEBUG(
logger_,
"Subscribe for ex hash={}", tx_hash);
216 throw jsonrpc::InternalErrorFault(
217 "Internal error. Api service not initialized.");
222 return service->unsubscribeFromExtrinsicLifecycle(sub_id);
224 throw jsonrpc::InternalErrorFault(
225 "Internal error. Api service not initialized.");
outcome::result< bool > hasSessionKeys(const gsl::span< const uint8_t > &keys) override
checks if the keystore has private keys for the given session public keys
std::weak_ptr< api::ApiService > api_service_
outcome::result< bool > hasKey(const gsl::span< const uint8_t > &public_key, crypto::KeyTypeId key_type) override
checks if the keystore has private keys for the given public key and key type
outcome::result< common::Hash256 > submitExtrinsic(TransactionSource source, const primitives::Extrinsic &extrinsic) override
validates and sends extrinsic to transaction pool
sptr< runtime::SessionKeysApi > keys_api_
uint32_t KeyTypeId
Key type identifier.
outcome::result< std::vector< primitives::Extrinsic > > pendingExtrinsics() override
sptr< crypto::KeyFileStorage > key_store_
void setApiService(sptr< api::ApiService > const &api_service) override
AuthorApiImpl(sptr< runtime::SessionKeysApi > key_api, sptr< transaction_pool::TransactionPool > pool, sptr< crypto::CryptoStore > store, sptr< crypto::SessionKeys > keys, sptr< crypto::KeyFileStorage > key_store, sptr< blockchain::BlockTree > block_tree)
sptr< transaction_pool::TransactionPool > pool_
outcome::result< SubscriptionId > submitAndWatchExtrinsic(Extrinsic extrinsic) override
constexpr KnownKeyTypeId polkadot_key_order[6]
std::string encodeKeyTypeIdToStr(KeyTypeId key_type_id)
makes string representation of KeyTypeId
sptr< blockchain::BlockTree > block_tree_
primitives::SubscriptionId SubscriptionId
Extrinsic class represents extrinsic.
Logger createLogger(const std::string &tag)
outcome::result< common::Buffer > rotateKeys() override
Generate new session keys and returns the corresponding public keys.
outcome::result< std::vector< primitives::Extrinsic > > removeExtrinsic(const std::vector< primitives::ExtrinsicKey > &keys) override
std::shared_ptr< T > sptr
sptr< crypto::CryptoStore > store_
outcome::result< void > insertKey(crypto::KeyTypeId key_type, const gsl::span< const uint8_t > &seed, const gsl::span< const uint8_t > &public_key) override
insert an anonimous key pair into the keystore
outcome::result< bool > unwatchExtrinsic(SubscriptionId subscription_id) override
sptr< crypto::SessionKeys > keys_
const std::vector< crypto::KnownKeyTypeId > kKeyTypes