substrate_api_client/api/runtime_api/
session_keys.rs

1/*
2   Copyright 2024 Supercomputing Systems AG
3   Licensed under the Apache License, Version 2.0 (the "License");
4   you may not use this file except in compliance with the License.
5   You may obtain a copy of the License at
6	   http://www.apache.org/licenses/LICENSE-2.0
7   Unless required by applicable law or agreed to in writing, software
8   distributed under the License is distributed on an "AS IS" BASIS,
9   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10   See the License for the specific language governing permissions and
11   limitations under the License.
12*/
13
14use super::{RuntimeApi, RuntimeApiClient};
15use crate::{api::Result, rpc::Request};
16use ac_primitives::config::Config;
17#[cfg(all(not(feature = "sync-api"), not(feature = "std")))]
18use alloc::boxed::Box;
19use alloc::{vec, vec::Vec};
20use codec::Encode;
21use sp_core::{crypto::KeyTypeId, Bytes};
22
23#[maybe_async::maybe_async(?Send)]
24pub trait SessionKeysApi: RuntimeApi {
25	type KeyTypeId;
26
27	/// Decode the given public session keys.
28	#[allow(clippy::type_complexity)]
29	async fn decode_session_keys(
30		&self,
31		encoded: Bytes,
32		at_block: Option<Self::Hash>,
33	) -> Result<Option<Vec<(Bytes, Self::KeyTypeId)>>>;
34
35	/// Generate a set of session keys with optionally using the given seed.
36	async fn generate_session_keys(
37		&self,
38		seed: Option<Bytes>,
39		at_block: Option<Self::Hash>,
40	) -> Result<Bytes>;
41}
42
43#[maybe_async::maybe_async(?Send)]
44impl<T, Client> SessionKeysApi for RuntimeApiClient<T, Client>
45where
46	T: Config,
47	Client: Request,
48{
49	type KeyTypeId = KeyTypeId;
50
51	async fn decode_session_keys(
52		&self,
53		encoded: Bytes,
54		at_block: Option<Self::Hash>,
55	) -> Result<Option<Vec<(Bytes, Self::KeyTypeId)>>> {
56		self.runtime_call("SessionKeys_decode_session_keys", vec![encoded.0], at_block)
57			.await
58	}
59
60	async fn generate_session_keys(
61		&self,
62		seed: Option<Bytes>,
63		at_block: Option<Self::Hash>,
64	) -> Result<Bytes> {
65		self.runtime_call("SessionKeys_generate_session_keys", vec![seed.encode()], at_block)
66			.await
67	}
68}