referrerpolicy=no-referrer-when-downgrade

assets_common/
foreign_creators.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use core::fmt::Debug;
17use frame_support::traits::{
18	ContainsPair, EnsureOrigin, EnsureOriginWithArg, Everything, OriginTrait,
19};
20use pallet_xcm::{EnsureXcm, Origin as XcmOrigin};
21use xcm::latest::Location;
22use xcm_executor::traits::ConvertLocation;
23
24/// `EnsureOriginWithArg` impl for `CreateOrigin` that allows only XCM origins that are locations
25/// containing the class location.
26pub struct ForeignCreators<IsForeign, AccountOf, AccountId, L = Location>(
27	core::marker::PhantomData<(IsForeign, AccountOf, AccountId, L)>,
28);
29impl<
30		IsForeign: ContainsPair<L, L>,
31		AccountOf: ConvertLocation<AccountId>,
32		AccountId: Clone,
33		RuntimeOrigin: From<XcmOrigin> + OriginTrait + Clone + Debug,
34		L: TryFrom<Location> + TryInto<Location> + Clone + Debug,
35	> EnsureOriginWithArg<RuntimeOrigin, L> for ForeignCreators<IsForeign, AccountOf, AccountId, L>
36where
37	for<'a> &'a RuntimeOrigin::PalletsOrigin: TryInto<&'a XcmOrigin>,
38{
39	type Success = AccountId;
40
41	fn try_origin(
42		origin: RuntimeOrigin,
43		asset_location: &L,
44	) -> core::result::Result<Self::Success, RuntimeOrigin> {
45		tracing::trace!(target: "xcm::try_origin", ?origin, ?asset_location, "ForeignCreators");
46		let origin_location = EnsureXcm::<Everything, L>::try_origin(origin.clone())?;
47		if !IsForeign::contains(asset_location, &origin_location) {
48			tracing::trace!(target: "xcm::try_origin", ?asset_location, ?origin_location, "ForeignCreators: no match");
49			return Err(origin)
50		}
51		let latest_location: Location =
52			origin_location.clone().try_into().map_err(|_| origin.clone())?;
53		AccountOf::convert_location(&latest_location).ok_or(origin)
54	}
55
56	#[cfg(feature = "runtime-benchmarks")]
57	fn try_successful_origin(a: &L) -> Result<RuntimeOrigin, ()> {
58		let latest_location: Location = (*a).clone().try_into().map_err(|_| ())?;
59		Ok(pallet_xcm::Origin::Xcm(latest_location).into())
60	}
61}