polkadot_runtime_parachains/origin.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16
17//! Declaration of the parachain specific origin and a pallet that hosts it.
18
19use core::result;
20use polkadot_primitives::Id as ParaId;
21use sp_runtime::traits::BadOrigin;
22
23pub use pallet::*;
24
25/// Ensure that the origin `o` represents a parachain.
26/// Returns `Ok` with the parachain ID that effected the extrinsic or an `Err` otherwise.
27pub fn ensure_parachain<OuterOrigin>(o: OuterOrigin) -> result::Result<ParaId, BadOrigin>
28where
29 OuterOrigin: Into<result::Result<Origin, OuterOrigin>>,
30{
31 match o.into() {
32 Ok(Origin::Parachain(id)) => Ok(id),
33 _ => Err(BadOrigin),
34 }
35}
36
37/// There is no way to register an origin type in `construct_runtime` without a pallet the origin
38/// belongs to.
39///
40/// This module fulfills only the single purpose of housing the `Origin` in `construct_runtime`.
41// ideally, though, the `construct_runtime` should support a free-standing origin.
42#[frame_support::pallet]
43pub mod pallet {
44 use super::*;
45 use frame_support::pallet_prelude::*;
46
47 #[pallet::pallet]
48 pub struct Pallet<T>(_);
49
50 #[pallet::config]
51 pub trait Config: frame_system::Config {}
52
53 /// Origin for the parachains.
54 #[pallet::origin]
55 #[derive(
56 PartialEq,
57 Eq,
58 Clone,
59 Encode,
60 Decode,
61 DecodeWithMemTracking,
62 Debug,
63 scale_info::TypeInfo,
64 MaxEncodedLen,
65 )]
66 pub enum Origin {
67 /// It comes from a parachain.
68 Parachain(ParaId),
69 }
70}
71
72impl From<u32> for Origin {
73 fn from(id: u32) -> Origin {
74 Origin::Parachain(id.into())
75 }
76}