sp_session/
runtime_api.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use alloc::vec::Vec;
19use codec::{Decode, Encode};
20pub use sp_core::crypto::KeyTypeId;
21use sp_runtime::traits::GeneratedSessionKeys;
22
23/// Opaque [`GeneratedSessionKeys`].
24#[derive(Debug, Default, Decode, Encode, scale_info::TypeInfo)]
25pub struct OpaqueGeneratedSessionKeys {
26	/// The public session keys.
27	pub keys: Vec<u8>,
28	/// The proof proving the ownership of the public session keys for some owner.
29	pub proof: Vec<u8>,
30}
31
32impl<K: Encode, P: Encode> From<GeneratedSessionKeys<K, P>> for OpaqueGeneratedSessionKeys {
33	fn from(value: GeneratedSessionKeys<K, P>) -> Self {
34		Self { keys: value.keys.encode(), proof: value.proof.encode() }
35	}
36}
37
38sp_api::decl_runtime_apis! {
39	/// Session keys runtime api.
40	#[api_version(2)]
41	pub trait SessionKeys {
42		/// Generate a set of session keys with optionally using the given seed.
43		/// The keys should be stored within the keystore exposed via runtime
44		/// externalities.
45		///
46		/// The seed needs to be a valid `utf8` string.
47		///
48		/// Returns the concatenated SCALE encoded public keys.
49		fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> OpaqueGeneratedSessionKeys;
50
51		#[changed_in(2)]
52		fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8>;
53
54		/// Decode the given public session keys.
55		///
56		/// Returns the list of public raw public keys + key type.
57		fn decode_session_keys(encoded: Vec<u8>) -> Option<Vec<(Vec<u8>, KeyTypeId)>>;
58	}
59}