Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

XCM Account Derivation #3505

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use xcm_builder::{
AccountId32Aliases, ChildParachainConvertsVia, SovereignSignedViaLocation, CurrencyAdapter as XcmCurrencyAdapter,
ChildParachainAsNative, SignedAccountId32AsNative, ChildSystemParachainAsSuperuser, LocationInverter,
IsConcrete, FixedWeightBounds, TakeWeightCredit, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom,
IsChildSystemParachain, UsingComponents, BackingToPlurality, SignedToAccountId32,
IsChildSystemParachain, UsingComponents, BackingToPlurality, SignedToAccountId32, DerivedParachainAccountId32,
};
use xcm_executor::XcmExecutor;
use sp_arithmetic::Perquintill;
Expand Down Expand Up @@ -1222,6 +1222,8 @@ pub type SovereignAccountOf = (
ChildParachainConvertsVia<ParaId, AccountId>,
// We can directly alias an `AccountId32` into a local account.
AccountId32Aliases<KusamaNetwork, AccountId>,
// We can generate local account ids for accounts originating on parachains.
DerivedParachainAccountId32<KusamaNetwork, AccountId>,
);

/// Our asset transactor. This is what allows us to interest with the runtime facilities from the point of
Expand Down
4 changes: 3 additions & 1 deletion runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ use xcm_builder::{
AccountId32Aliases, ChildParachainConvertsVia, SovereignSignedViaLocation,
CurrencyAdapter as XcmCurrencyAdapter, ChildParachainAsNative, SignedAccountId32AsNative,
ChildSystemParachainAsSuperuser, LocationInverter, IsConcrete, FixedWeightBounds,
BackingToPlurality, SignedToAccountId32, UsingComponents,
BackingToPlurality, SignedToAccountId32, UsingComponents, DerivedParachainAccountId32,
};
use constants::{time::*, currency::*, fee::*};
use frame_support::traits::InstanceFilter;
Expand Down Expand Up @@ -608,6 +608,8 @@ parameter_types! {
pub type SovereignAccountOf = (
ChildParachainConvertsVia<ParaId, AccountId>,
AccountId32Aliases<RococoNetwork, AccountId>,
// We can generate local account ids for accounts originating on parachains.
DerivedParachainAccountId32<RococoNetwork, AccountId>,
);

pub type LocalAssetTransactor =
Expand Down
4 changes: 3 additions & 1 deletion runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use xcm_builder::{
AccountId32Aliases, ChildParachainConvertsVia, SovereignSignedViaLocation, CurrencyAdapter as XcmCurrencyAdapter,
ChildParachainAsNative, SignedAccountId32AsNative, ChildSystemParachainAsSuperuser, LocationInverter, IsConcrete,
FixedWeightBounds, TakeWeightCredit, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom,
IsChildSystemParachain, UsingComponents, SignedToAccountId32,
IsChildSystemParachain, UsingComponents, SignedToAccountId32, DerivedParachainAccountId32,
};

use sp_runtime::{
Expand Down Expand Up @@ -880,6 +880,8 @@ parameter_types! {
pub type LocationConverter = (
ChildParachainConvertsVia<ParaId, AccountId>,
AccountId32Aliases<WestendNetwork, AccountId>,
// We can generate local account ids for accounts originating on parachains.
DerivedParachainAccountId32<WestendNetwork, AccountId>,
);

pub type LocalAssetTransactor =
Expand Down
2 changes: 1 addition & 1 deletion xcm/xcm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod tests;
mod location_conversion;
pub use location_conversion::{
Account32Hash, ParentIsDefault, ChildParachainConvertsVia, SiblingParachainConvertsVia, AccountId32Aliases,
AccountKey20Aliases, LocationInverter,
AccountKey20Aliases, LocationInverter, DerivedParachainAccountId32,
};

mod origin_conversion;
Expand Down
36 changes: 36 additions & 0 deletions xcm/xcm-builder/src/location_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl<
}
}

/// Extracts the `AccountKey20` from the passed `location` if the network matches.
pub struct AccountKey20Aliases<Network, AccountId>(PhantomData<(Network, AccountId)>);
impl<
Network: Get<NetworkId>,
Expand All @@ -143,6 +144,41 @@ impl<
}
}

/// Generates a unique `AccountId` for a given parachain + account combination.
///
/// Used for giving parachains control over some accounts on the interpreting chain.
///
/// Note: Not reversible.
pub struct DerivedParachainAccountId32<Network, AccountId>(PhantomData<(Network, AccountId)>);
impl<
Network: Get<NetworkId>,
AccountId: From<[u8; 32]> + Into<[u8; 32]> + Clone,
> Convert<MultiLocation, AccountId> for DerivedParachainAccountId32<Network, AccountId> {
fn convert(location: MultiLocation) -> Result<AccountId, MultiLocation> {
use Junction::*;
use MultiLocation::*;
let id = match location {
X2(Parachain(para_id), AccountId32 { id, network })
if network == NetworkId::Any || &network == &Network::get() =>
{
(para_id, id).using_encoded(blake2_256)
Copy link
Member

@gavofyork gavofyork Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have a disambiguating prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like generate a 28byte array and prepend a u32 with the para id?

},
X2(Parachain(para_id), AccountKey20 { key, network })
if network == NetworkId::Any || &network == &Network::get() =>
{
(para_id, key).using_encoded(blake2_256)
},
l => return Err(l),
};
Ok(id.into())
}

// reverse conversion is not implemented
fn reverse(who: AccountId) -> Result<MultiLocation, AccountId> {
Err(who)
}
}

/// Simple location inverter; give it this location's ancestry and it'll figure out the inverted
/// location.
///
Expand Down