Allow normal accounts to execute XCM for withdraw+teleport (#2953)

* Allow normal accounts to execute XCM for withdraw+teleport

* Bump Substrate
This commit is contained in:
Gavin Wood
2021-04-28 23:13:19 +02:00
committed by GitHub
parent c0bf7e3d8c
commit f36d8efb3d
4 changed files with 215 additions and 157 deletions
+53 -4
View File
@@ -86,7 +86,7 @@ pub use pallet_balances::Call as BalancesCall;
use polkadot_parachain::primitives::Id as ParaId;
use xcm::v0::{MultiLocation, NetworkId, BodyId};
use xcm::v0::{Xcm, MultiLocation, NetworkId, BodyId};
use xcm_executor::XcmExecutor;
use xcm_builder::{
AccountId32Aliases, ChildParachainConvertsVia, SovereignSignedViaLocation,
@@ -619,7 +619,7 @@ pub type TrustedTeleporters = (
);
parameter_types! {
pub AllowUnpaidFrom: Vec<MultiLocation> =
pub AllowUnpaidFrom: Vec<MultiLocation> =
vec![
X1(Parachain(100)),
X1(Parachain(110)),
@@ -664,12 +664,61 @@ pub type LocalOriginToLocation = (
SignedToAccountId32<Origin, AccountId, RococoNetwork>,
);
pub struct OnlyWithdrawTeleportForAccounts;
impl frame_support::traits::Contains<(MultiLocation, Xcm<Call>)> for OnlyWithdrawTeleportForAccounts {
fn contains((ref origin, ref msg): &(MultiLocation, Xcm<Call>)) -> bool {
use xcm::v0::{
Xcm::WithdrawAsset, Order::{BuyExecution, InitiateTeleport, DepositAsset},
MultiAsset::{All, ConcreteFungible}, Junction::AccountId32,
};
match origin {
// Root is allowed to execute anything.
Null => true,
X1(AccountId32 { .. }) => {
// An account ID trying to send a message. We ensure that it's sensible.
// This checks that it's of the form:
// WithdrawAsset {
// assets: [ ConcreteFungible { id: Null } ],
// effects: [ BuyExecution, InitiateTeleport {
// assets: All,
// dest: Parachain,
// effects: [ BuyExecution, DepositAssets {
// assets: All,
// dest: AccountId32,
// } ]
// } ]
// }
matches!(msg, WithdrawAsset { ref assets, ref effects }
if assets.len() == 1
&& matches!(assets[0], ConcreteFungible { id: Null, .. })
&& effects.len() == 2
&& matches!(effects[0], BuyExecution { .. })
&& matches!(effects[1], InitiateTeleport { ref assets, dest: X1(Parachain(..)), ref effects }
if assets.len() == 1
&& matches!(assets[0], All)
&& effects.len() == 2
&& matches!(effects[0], BuyExecution { .. })
&& matches!(effects[1], DepositAsset { ref assets, dest: X1(AccountId32{..}) }
if assets.len() == 1
&& matches!(assets[0], All)
)
)
)
}
// Nobody else is allowed to execute anything.
_ => false,
}
}
}
impl pallet_xcm::Config for Runtime {
type Event = Event;
type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<Origin, LocalOriginToLocation>;
type XcmRouter = XcmRouter;
// Right now nobody but root is allowed to dispatch local XCM messages.
type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<Origin, ()>;
// Anyone can execute XCM messages locally...
type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<Origin, LocalOriginToLocation>;
// ...but they must match our filter, which requires them to be a simple withdraw + teleport.
type XcmExecuteFilter = OnlyWithdrawTeleportForAccounts;
type XcmExecutor = XcmExecutor<XcmConfig>;
}