staging_xcm_builder/origin_aliases.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//! Implementation for `ContainsPair<Location, Location>`.
18
19use core::marker::PhantomData;
20use frame_support::traits::{Contains, ContainsPair, Get};
21use xcm::latest::prelude::*;
22
23/// Alias a Foreign `AccountId32` with a local `AccountId32` if the foreign `AccountId32` matches
24/// the `Prefix` pattern.
25///
26/// Requires that the prefixed origin `AccountId32` matches the target `AccountId32`.
27pub struct AliasForeignAccountId32<Prefix>(PhantomData<Prefix>);
28impl<Prefix: Contains<Location>> ContainsPair<Location, Location>
29 for AliasForeignAccountId32<Prefix>
30{
31 fn contains(origin: &Location, target: &Location) -> bool {
32 if let (prefix, Some(account_id @ AccountId32 { .. })) =
33 origin.clone().split_last_interior()
34 {
35 return Prefix::contains(&prefix) &&
36 *target == Location { parents: 0, interior: [account_id].into() }
37 }
38 false
39 }
40}
41
42/// Alias a descendant location of the original origin.
43pub struct AliasChildLocation;
44impl ContainsPair<Location, Location> for AliasChildLocation {
45 fn contains(origin: &Location, target: &Location) -> bool {
46 return target.starts_with(origin)
47 }
48}
49
50/// Alias a location if it passes `Filter` and the original origin is root of `Origin`.
51///
52/// This can be used to allow (trusted) system chains root to alias into other locations.
53/// **Warning**: do not use with untrusted `Origin` chains.
54pub struct AliasOriginRootUsingFilter<Origin, Filter>(PhantomData<(Origin, Filter)>);
55impl<Origin, Filter> ContainsPair<Location, Location> for AliasOriginRootUsingFilter<Origin, Filter>
56where
57 Origin: Get<Location>,
58 Filter: Contains<Location>,
59{
60 fn contains(origin: &Location, target: &Location) -> bool {
61 // check that `origin` is a root location
62 match origin.unpack() {
63 (1, [Parachain(_)]) |
64 (2, [GlobalConsensus(_)]) |
65 (2, [GlobalConsensus(_), Parachain(_)]) => (),
66 _ => return false,
67 };
68 // check that `origin` matches `Origin` and `target` matches `Filter`
69 return Origin::get().eq(origin) && Filter::contains(target)
70 }
71}