Kagome
Polkadot Runtime Engine in C++17
get_keys.cpp
Go to the documentation of this file.
1 
7 
8 #include "scale/scale.hpp"
9 
11 
12  outcome::result<void> GetKeys::init(
13  const jsonrpc::Request::Parameters &params) {
14  // getKeys(childKey, prefix, [opt]at)
15 
16  if (params.size() > 3 or params.size() < 2) {
17  throw jsonrpc::InvalidParametersFault("Incorrect number of params");
18  }
19  // childKey
20  auto &param0 = params[0];
21 
22  if (not param0.IsString() or param0.IsNil()) {
23  throw jsonrpc::InvalidParametersFault(
24  "Parameter '[child_storage_key]' must be a hex string");
25  }
26 
27  OUTCOME_TRY(child_storage_key, common::unhexWith0x(param0.AsString()));
28  child_storage_key_ = common::Buffer(std::move(child_storage_key));
29 
30  auto &param1 = params[1];
31 
32  if (not param1.IsString() and not param1.IsNil()) {
33  throw jsonrpc::InvalidParametersFault(
34  "Parameter '[prefix]' must be a hex string");
35  }
36 
37  if (param1.IsNil()) {
38  prefix_ = std::nullopt;
39  } else if (param1.IsString()) {
40  OUTCOME_TRY(key, common::unhexWith0x(param1.AsString()));
41  prefix_ = common::Buffer(std::move(key));
42  } else {
43  throw jsonrpc::InvalidParametersFault(
44  "Parameter '[prefix]' must be a hex string");
45  }
46 
47  if (params.size() == 2) {
48  return outcome::success();
49  }
50 
51  // process at param
52  if (not params[2].IsString()) {
53  throw jsonrpc::InvalidParametersFault(
54  "Parameter '[at]' must be a hex string representation of an encoded "
55  "optional byte sequence");
56  }
57  OUTCOME_TRY(at_span, common::unhexWith0x(params[2].AsString()));
58  OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
59  at_ = at;
60 
61  return outcome::success();
62  }
63 
64  outcome::result<std::vector<common::Buffer>> GetKeys::execute() {
65  return api_->getKeys(child_storage_key_, prefix_, at_);
66  }
67 } // namespace kagome::api::child_state::request
std::shared_ptr< ChildStateApi > api_
Definition: get_keys.hpp:41
outcome::result< std::vector< uint8_t > > unhexWith0x(std::string_view hex_with_prefix)
Unhex hex-string with 0x in the begining.
Definition: hexutil.cpp:89
std::optional< common::Buffer > prefix_
Definition: get_keys.hpp:43
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
outcome::result< void > init(const jsonrpc::Request::Parameters &params)
Definition: get_keys.cpp:12
static outcome::result< Blob< size_ > > fromSpan(const gsl::span< const uint8_t > &span)
Definition: blob.hpp:208
std::optional< primitives::BlockHash > at_
Definition: get_keys.hpp:44
outcome::result< std::vector< common::Buffer > > execute()
Definition: get_keys.cpp:64