From d80dbbe1a1b640a584850a69077607dacb8dd4af Mon Sep 17 00:00:00 2001 From: mustermeiszer Date: Thu, 2 Feb 2023 12:13:13 +0100 Subject: [PATCH 1/9] Draft remote account converter --- xcm/xcm-builder/src/location_conversion.rs | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/xcm/xcm-builder/src/location_conversion.rs b/xcm/xcm-builder/src/location_conversion.rs index a3ced7ba6d90..dd2fd453f81c 100644 --- a/xcm/xcm-builder/src/location_conversion.rs +++ b/xcm/xcm-builder/src/location_conversion.rs @@ -16,12 +16,58 @@ use frame_support::traits::Get; use parity_scale_codec::{Decode, Encode}; +use sp_core::blake2_256; use sp_io::hashing::blake2_256; use sp_runtime::traits::{AccountIdConversion, TrailingZeroInput}; use sp_std::{borrow::Borrow, marker::PhantomData}; use xcm::latest::prelude::*; use xcm_executor::traits::Convert; +// Prefix to increase the entropy of the resulting bytes of the hash +pub const REMOTE_SALT: [u8; 6] = *b"remote"; + +// To be used as a converter in `SovereignSignedViaLocation` +pub struct RemoteAccount(PhantomData<(AccountId)>); +impl + Into<[u8; 32]> + Clone> Convert + for RemoteAccount +{ + fn convert_ref(location: impl Borrow) -> Result { + let entropy = match location.borrow() { + // Used on the relay chain for sending paras that use 32 byte accounts + MultiLocation { + parents: 0, + interior: X2(Parachain(para_id), AccountId32 { id, .. }), + } => (REMOTE_SALT, para_id, id).using_encoded(blake2_256), + // Used on the relay chain for sending paras that use 20 byte accounts + MultiLocation { + parents: 0, + interior: X2(Parachain(para_id), AccountKey20 { key, .. }), + } => (REMOTE_SALT, para_id, key).using_encoded(blake2_256), + // Used on parachain for sending paras that use 32 byte accounts + MultiLocation { + parents: 1, + interior: X2(Parachain(para_id), AccountId32 { id, .. }), + } => (REMOTE_SALT, para_id, id).using_encoded(blake2_256), + // Used on parachain for sending paras that use 20 byte accounts + MultiLocation { + parents: 1, + interior: X2(Parachain(para_id), AccountKey20 { key, .. }), + } => (REMOTE_SALT, para_id, key).using_encoded(blake2_256), + // Used on parachain for sending from the relay-chain + MultiLocation { parents: 1, interior: X2(Here, AccountId32 { id, .. }) } => + (REMOTE_SALT, id).using_encoded(blake2_256), + // No other conversions provided + _ => return Err(()), + }; + + Ok((entropy.into())) + } + + fn reverse_ref(_: impl Borrow) -> Result { + Err(()) + } +} + pub struct Account32Hash(PhantomData<(Network, AccountId)>); impl>, AccountId: From<[u8; 32]> + Into<[u8; 32]> + Clone> Convert for Account32Hash From 1e32ed700b9b8bb28c0800b45fdef335da6aed9b Mon Sep 17 00:00:00 2001 From: mustermeiszer Date: Thu, 2 Feb 2023 17:28:27 +0100 Subject: [PATCH 2/9] Tests and doc comments --- xcm/xcm-builder/src/location_conversion.rs | 279 ++++++++++++++++++++- 1 file changed, 267 insertions(+), 12 deletions(-) diff --git a/xcm/xcm-builder/src/location_conversion.rs b/xcm/xcm-builder/src/location_conversion.rs index dd2fd453f81c..e400e1749d7b 100644 --- a/xcm/xcm-builder/src/location_conversion.rs +++ b/xcm/xcm-builder/src/location_conversion.rs @@ -16,7 +16,6 @@ use frame_support::traits::Get; use parity_scale_codec::{Decode, Encode}; -use sp_core::blake2_256; use sp_io::hashing::blake2_256; use sp_runtime::traits::{AccountIdConversion, TrailingZeroInput}; use sp_std::{borrow::Borrow, marker::PhantomData}; @@ -26,9 +25,15 @@ use xcm_executor::traits::Convert; // Prefix to increase the entropy of the resulting bytes of the hash pub const REMOTE_SALT: [u8; 6] = *b"remote"; -// To be used as a converter in `SovereignSignedViaLocation` -pub struct RemoteAccount(PhantomData<(AccountId)>); -impl + Into<[u8; 32]> + Clone> Convert +/// This converter will for a given `AccountId32`/`AccountKey20` +/// always generate the same "remote" account for a specific +/// sending para-chain. +/// I.e. the user gets the same remote account +/// on every consuming para-chain and relay chain. +/// +/// Can be used as a converter in `SovereignSignedViaLocation` +pub struct RemoteAccount(PhantomData); +impl + Clone> Convert for RemoteAccount { fn convert_ref(location: impl Borrow) -> Result { @@ -37,30 +42,34 @@ impl + Into<[u8; 32]> + Clone> Convert (REMOTE_SALT, para_id, id).using_encoded(blake2_256), + } => RemoteAccount::::from_para_32(para_id, id), + // Used on the relay chain for sending paras that use 20 byte accounts MultiLocation { parents: 0, interior: X2(Parachain(para_id), AccountKey20 { key, .. }), - } => (REMOTE_SALT, para_id, key).using_encoded(blake2_256), + } => RemoteAccount::::from_para_20(para_id, key), + // Used on parachain for sending paras that use 32 byte accounts MultiLocation { parents: 1, interior: X2(Parachain(para_id), AccountId32 { id, .. }), - } => (REMOTE_SALT, para_id, id).using_encoded(blake2_256), + } => RemoteAccount::::from_para_32(para_id, id), + // Used on parachain for sending paras that use 20 byte accounts MultiLocation { parents: 1, interior: X2(Parachain(para_id), AccountKey20 { key, .. }), - } => (REMOTE_SALT, para_id, key).using_encoded(blake2_256), - // Used on parachain for sending from the relay-chain - MultiLocation { parents: 1, interior: X2(Here, AccountId32 { id, .. }) } => - (REMOTE_SALT, id).using_encoded(blake2_256), + } => RemoteAccount::::from_para_20(para_id, key), + + // Used on parachain for sending from the relay chain + MultiLocation { parents: 1, interior: X1(AccountId32 { id, .. }) } => + RemoteAccount::::from_relay_32(id), // No other conversions provided _ => return Err(()), }; - Ok((entropy.into())) + Ok(entropy.into()) } fn reverse_ref(_: impl Borrow) -> Result { @@ -68,6 +77,20 @@ impl + Into<[u8; 32]> + Clone> Convert RemoteAccount { + fn from_para_32(para_id: &u32, id: &[u8; 32]) -> [u8; 32] { + (REMOTE_SALT, para_id, id).using_encoded(blake2_256) + } + + fn from_para_20(para_id: &u32, id: &[u8; 20]) -> [u8; 32] { + (REMOTE_SALT, para_id, id).using_encoded(blake2_256) + } + + fn from_relay_32(id: &[u8; 32]) -> [u8; 32] { + (REMOTE_SALT, id).using_encoded(blake2_256) + } +} + pub struct Account32Hash(PhantomData<(Network, AccountId)>); impl>, AccountId: From<[u8; 32]> + Into<[u8; 32]> + Clone> Convert for Account32Hash @@ -271,4 +294,236 @@ mod tests { let inverted = UniversalLocation::get().invert_target(&input); assert_eq!(inverted, Err(())); } + + #[test] + fn inverter_errors_when_location_is_too_large() { + parameter_types! { + pub UniversalLocation: InteriorMultiLocation = Here; + } + + let input = MultiLocation { parents: 99, interior: X1(Parachain(88)) }; + let inverted = UniversalLocation::get().invert_target(&input); + assert_eq!(inverted, Err(())); + } + + #[test] + fn remote_account_convert_on_para_sending_para_32() { + let mul = MultiLocation { + parents: 1, + interior: X2(Parachain(1), AccountId32 { network: None, id: [0u8; 32] }), + }; + let rem_1 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + + assert_eq!( + [ + 221, 151, 114, 30, 191, 162, 192, 170, 30, 95, 129, 157, 52, 177, 157, 136, 137, + 145, 129, 132, 199, 148, 188, 135, 245, 140, 163, 99, 189, 217, 160, 244 + ], + rem_1 + ); + + let mul = MultiLocation { + parents: 1, + interior: X2( + Parachain(1), + AccountId32 { network: Some(NetworkId::Polkadot), id: [0u8; 32] }, + ), + }; + + assert_eq!(RemoteAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); + + let mul = MultiLocation { + parents: 1, + interior: X2(Parachain(2), AccountId32 { network: None, id: [0u8; 32] }), + }; + let rem_2 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + + assert_eq!( + [ + 146, 61, 202, 186, 203, 8, 50, 95, 225, 247, 103, 152, 224, 178, 32, 43, 34, 78, + 20, 59, 89, 206, 156, 224, 128, 75, 169, 17, 111, 233, 196, 245 + ], + rem_2 + ); + + assert_ne!(rem_1, rem_2); + } + + #[test] + fn remote_account_convert_on_para_sending_para_20() { + let mul = MultiLocation { + parents: 1, + interior: X2(Parachain(1), AccountKey20 { network: None, key: [0u8; 20] }), + }; + let rem_1 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + + assert_eq!( + [ + 82, 158, 157, 51, 146, 108, 167, 74, 51, 248, 120, 39, 30, 248, 58, 104, 176, 75, + 185, 138, 98, 112, 119, 120, 19, 214, 118, 5, 82, 98, 209, 240 + ], + rem_1 + ); + + let mul = MultiLocation { + parents: 1, + interior: X2( + Parachain(1), + AccountKey20 { network: Some(NetworkId::Polkadot), key: [0u8; 20] }, + ), + }; + + assert_eq!(RemoteAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); + + let mul = MultiLocation { + parents: 1, + interior: X2(Parachain(2), AccountKey20 { network: None, key: [0u8; 20] }), + }; + let rem_2 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + + assert_eq!( + [ + 167, 27, 179, 135, 180, 42, 176, 100, 1, 89, 215, 160, 171, 11, 25, 196, 254, 54, + 98, 138, 213, 243, 188, 30, 224, 253, 170, 212, 129, 163, 220, 176 + ], + rem_2 + ); + + assert_ne!(rem_1, rem_2); + } + + #[test] + fn remote_account_convert_on_para_sending_relay() { + let mul = MultiLocation { + parents: 1, + interior: X1(AccountId32 { network: None, id: [0u8; 32] }), + }; + let rem_1 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + + assert_eq!( + [ + 164, 178, 213, 30, 221, 103, 151, 120, 253, 138, 0, 226, 89, 73, 138, 102, 150, 62, + 95, 34, 203, 182, 57, 70, 186, 136, 194, 166, 34, 244, 102, 234 + ], + rem_1 + ); + + let mul = MultiLocation { + parents: 1, + interior: X1(AccountId32 { network: Some(NetworkId::Polkadot), id: [0u8; 32] }), + }; + + assert_eq!(RemoteAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); + + let mul = MultiLocation { + parents: 1, + interior: X1(AccountId32 { network: None, id: [1u8; 32] }), + }; + let rem_2 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + + assert_eq!( + [ + 73, 63, 122, 190, 223, 157, 189, 173, 89, 150, 107, 199, 112, 221, 66, 46, 180, + 228, 130, 199, 10, 171, 148, 247, 159, 146, 206, 29, 215, 49, 242, 75 + ], + rem_2 + ); + + assert_ne!(rem_1, rem_2); + } + + #[test] + fn remote_account_convert_on_relay_sending_para_20() { + let mul = MultiLocation { + parents: 0, + interior: X2(Parachain(1), AccountKey20 { network: None, key: [0u8; 20] }), + }; + let rem_1 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + + assert_eq!( + [ + 82, 158, 157, 51, 146, 108, 167, 74, 51, 248, 120, 39, 30, 248, 58, 104, 176, 75, + 185, 138, 98, 112, 119, 120, 19, 214, 118, 5, 82, 98, 209, 240 + ], + rem_1 + ); + + let mul = MultiLocation { + parents: 1, + interior: X2( + Parachain(1), + AccountKey20 { network: Some(NetworkId::Polkadot), key: [0u8; 20] }, + ), + }; + + assert_eq!(RemoteAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); + + let mul = MultiLocation { + parents: 0, + interior: X2(Parachain(2), AccountKey20 { network: None, key: [0u8; 20] }), + }; + let rem_2 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + + assert_eq!( + [ + 167, 27, 179, 135, 180, 42, 176, 100, 1, 89, 215, 160, 171, 11, 25, 196, 254, 54, + 98, 138, 213, 243, 188, 30, 224, 253, 170, 212, 129, 163, 220, 176 + ], + rem_2 + ); + + assert_ne!(rem_1, rem_2); + } + + #[test] + fn remote_account_convert_on_relay_sending_para_32() { + let mul = MultiLocation { + parents: 0, + interior: X2(Parachain(1), AccountId32 { network: None, id: [0u8; 32] }), + }; + let rem_1 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + + assert_eq!( + [ + 221, 151, 114, 30, 191, 162, 192, 170, 30, 95, 129, 157, 52, 177, 157, 136, 137, + 145, 129, 132, 199, 148, 188, 135, 245, 140, 163, 99, 189, 217, 160, 244 + ], + rem_1 + ); + + let mul = MultiLocation { + parents: 0, + interior: X2( + Parachain(1), + AccountId32 { network: Some(NetworkId::Polkadot), id: [0u8; 32] }, + ), + }; + + assert_eq!(RemoteAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); + + let mul = MultiLocation { + parents: 0, + interior: X2(Parachain(2), AccountId32 { network: None, id: [0u8; 32] }), + }; + let rem_2 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + + assert_eq!( + [ + 146, 61, 202, 186, 203, 8, 50, 95, 225, 247, 103, 152, 224, 178, 32, 43, 34, 78, + 20, 59, 89, 206, 156, 224, 128, 75, 169, 17, 111, 233, 196, 245 + ], + rem_2 + ); + + assert_ne!(rem_1, rem_2); + } + + #[test] + fn remote_account_fails_with_bad_multilocation() { + let mul = MultiLocation { + parents: 1, + interior: X1(AccountKey20 { network: None, key: [0u8; 20] }), + }; + assert!(RemoteAccount::<[u8; 32]>::convert(mul).is_err()); + } } From d2b1b6e564844874f63b10bb35ddbdb03c27a7dd Mon Sep 17 00:00:00 2001 From: mustermeiszer Date: Sun, 5 Feb 2023 23:09:29 +0100 Subject: [PATCH 3/9] Adapt naming, tests fix --- xcm/xcm-builder/src/location_conversion.rs | 118 ++++++++++----------- 1 file changed, 54 insertions(+), 64 deletions(-) diff --git a/xcm/xcm-builder/src/location_conversion.rs b/xcm/xcm-builder/src/location_conversion.rs index e400e1749d7b..2c553a3b96ec 100644 --- a/xcm/xcm-builder/src/location_conversion.rs +++ b/xcm/xcm-builder/src/location_conversion.rs @@ -22,19 +22,19 @@ use sp_std::{borrow::Borrow, marker::PhantomData}; use xcm::latest::prelude::*; use xcm_executor::traits::Convert; -// Prefix to increase the entropy of the resulting bytes of the hash -pub const REMOTE_SALT: [u8; 6] = *b"remote"; +/// Prefix for bytes in account creation of foreign chains +pub const FOREIGN_CHAIN_PREFIX: [u8; 30] = *b"ForeignChainAliasAccountPrefix"; /// This converter will for a given `AccountId32`/`AccountKey20` /// always generate the same "remote" account for a specific -/// sending para-chain. +/// sending chain. /// I.e. the user gets the same remote account /// on every consuming para-chain and relay chain. /// /// Can be used as a converter in `SovereignSignedViaLocation` -pub struct RemoteAccount(PhantomData); +pub struct ForeignChainAliasAccount(PhantomData); impl + Clone> Convert - for RemoteAccount + for ForeignChainAliasAccount { fn convert_ref(location: impl Borrow) -> Result { let entropy = match location.borrow() { @@ -42,29 +42,30 @@ impl + Clone> Convert MultiLocation { parents: 0, interior: X2(Parachain(para_id), AccountId32 { id, .. }), - } => RemoteAccount::::from_para_32(para_id, id), + } => ForeignChainAliasAccount::::from_para_32(para_id, id), // Used on the relay chain for sending paras that use 20 byte accounts MultiLocation { parents: 0, interior: X2(Parachain(para_id), AccountKey20 { key, .. }), - } => RemoteAccount::::from_para_20(para_id, key), + } => ForeignChainAliasAccount::::from_para_20(para_id, key), - // Used on parachain for sending paras that use 32 byte accounts + // Used on para-chain for sending paras that use 32 byte accounts MultiLocation { parents: 1, interior: X2(Parachain(para_id), AccountId32 { id, .. }), - } => RemoteAccount::::from_para_32(para_id, id), + } => ForeignChainAliasAccount::::from_para_32(para_id, id), - // Used on parachain for sending paras that use 20 byte accounts + // Used on para-chain for sending paras that use 20 byte accounts MultiLocation { parents: 1, interior: X2(Parachain(para_id), AccountKey20 { key, .. }), - } => RemoteAccount::::from_para_20(para_id, key), + } => ForeignChainAliasAccount::::from_para_20(para_id, key), - // Used on parachain for sending from the relay chain + // Used on para-chain for sending from the relay chain MultiLocation { parents: 1, interior: X1(AccountId32 { id, .. }) } => - RemoteAccount::::from_relay_32(id), + ForeignChainAliasAccount::::from_relay_32(id), + // No other conversions provided _ => return Err(()), }; @@ -77,17 +78,17 @@ impl + Clone> Convert } } -impl RemoteAccount { +impl ForeignChainAliasAccount { fn from_para_32(para_id: &u32, id: &[u8; 32]) -> [u8; 32] { - (REMOTE_SALT, para_id, id).using_encoded(blake2_256) + (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256) } fn from_para_20(para_id: &u32, id: &[u8; 20]) -> [u8; 32] { - (REMOTE_SALT, para_id, id).using_encoded(blake2_256) + (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256) } fn from_relay_32(id: &[u8; 32]) -> [u8; 32] { - (REMOTE_SALT, id).using_encoded(blake2_256) + (FOREIGN_CHAIN_PREFIX, id).using_encoded(blake2_256) } } @@ -295,29 +296,18 @@ mod tests { assert_eq!(inverted, Err(())); } - #[test] - fn inverter_errors_when_location_is_too_large() { - parameter_types! { - pub UniversalLocation: InteriorMultiLocation = Here; - } - - let input = MultiLocation { parents: 99, interior: X1(Parachain(88)) }; - let inverted = UniversalLocation::get().invert_target(&input); - assert_eq!(inverted, Err(())); - } - #[test] fn remote_account_convert_on_para_sending_para_32() { let mul = MultiLocation { parents: 1, interior: X2(Parachain(1), AccountId32 { network: None, id: [0u8; 32] }), }; - let rem_1 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + let rem_1 = ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(); assert_eq!( [ - 221, 151, 114, 30, 191, 162, 192, 170, 30, 95, 129, 157, 52, 177, 157, 136, 137, - 145, 129, 132, 199, 148, 188, 135, 245, 140, 163, 99, 189, 217, 160, 244 + 99, 200, 141, 87, 123, 89, 117, 64, 130, 31, 1, 205, 203, 222, 3, 213, 96, 214, + 109, 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 ], rem_1 ); @@ -330,18 +320,18 @@ mod tests { ), }; - assert_eq!(RemoteAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); + assert_eq!(ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); let mul = MultiLocation { parents: 1, interior: X2(Parachain(2), AccountId32 { network: None, id: [0u8; 32] }), }; - let rem_2 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + let rem_2 = ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(); assert_eq!( [ - 146, 61, 202, 186, 203, 8, 50, 95, 225, 247, 103, 152, 224, 178, 32, 43, 34, 78, - 20, 59, 89, 206, 156, 224, 128, 75, 169, 17, 111, 233, 196, 245 + 134, 167, 40, 2, 211, 236, 255, 60, 95, 118, 137, 13, 55, 219, 60, 190, 109, 255, + 233, 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 ], rem_2 ); @@ -355,12 +345,12 @@ mod tests { parents: 1, interior: X2(Parachain(1), AccountKey20 { network: None, key: [0u8; 20] }), }; - let rem_1 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + let rem_1 = ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(); assert_eq!( [ - 82, 158, 157, 51, 146, 108, 167, 74, 51, 248, 120, 39, 30, 248, 58, 104, 176, 75, - 185, 138, 98, 112, 119, 120, 19, 214, 118, 5, 82, 98, 209, 240 + 138, 178, 174, 32, 50, 130, 59, 252, 53, 61, 149, 173, 157, 117, 42, 161, 77, 123, + 199, 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 ], rem_1 ); @@ -373,18 +363,18 @@ mod tests { ), }; - assert_eq!(RemoteAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); + assert_eq!(ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); let mul = MultiLocation { parents: 1, interior: X2(Parachain(2), AccountKey20 { network: None, key: [0u8; 20] }), }; - let rem_2 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + let rem_2 = ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(); assert_eq!( [ - 167, 27, 179, 135, 180, 42, 176, 100, 1, 89, 215, 160, 171, 11, 25, 196, 254, 54, - 98, 138, 213, 243, 188, 30, 224, 253, 170, 212, 129, 163, 220, 176 + 71, 235, 74, 190, 35, 18, 98, 222, 79, 110, 131, 222, 110, 187, 70, 157, 157, 61, + 12, 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 ], rem_2 ); @@ -398,12 +388,12 @@ mod tests { parents: 1, interior: X1(AccountId32 { network: None, id: [0u8; 32] }), }; - let rem_1 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + let rem_1 = ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(); assert_eq!( [ - 164, 178, 213, 30, 221, 103, 151, 120, 253, 138, 0, 226, 89, 73, 138, 102, 150, 62, - 95, 34, 203, 182, 57, 70, 186, 136, 194, 166, 34, 244, 102, 234 + 126, 169, 155, 79, 160, 47, 12, 149, 120, 177, 75, 189, 219, 70, 212, 232, 67, 8, + 3, 204, 96, 162, 6, 249, 195, 169, 46, 151, 100, 221, 9, 249 ], rem_1 ); @@ -413,18 +403,18 @@ mod tests { interior: X1(AccountId32 { network: Some(NetworkId::Polkadot), id: [0u8; 32] }), }; - assert_eq!(RemoteAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); + assert_eq!(ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); let mul = MultiLocation { parents: 1, interior: X1(AccountId32 { network: None, id: [1u8; 32] }), }; - let rem_2 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + let rem_2 = ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(); assert_eq!( [ - 73, 63, 122, 190, 223, 157, 189, 173, 89, 150, 107, 199, 112, 221, 66, 46, 180, - 228, 130, 199, 10, 171, 148, 247, 159, 146, 206, 29, 215, 49, 242, 75 + 180, 59, 39, 58, 99, 137, 223, 252, 92, 171, 106, 182, 150, 228, 6, 66, 154, 209, + 41, 100, 2, 187, 51, 247, 231, 43, 84, 150, 13, 159, 236, 153 ], rem_2 ); @@ -438,12 +428,12 @@ mod tests { parents: 0, interior: X2(Parachain(1), AccountKey20 { network: None, key: [0u8; 20] }), }; - let rem_1 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + let rem_1 = ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(); assert_eq!( [ - 82, 158, 157, 51, 146, 108, 167, 74, 51, 248, 120, 39, 30, 248, 58, 104, 176, 75, - 185, 138, 98, 112, 119, 120, 19, 214, 118, 5, 82, 98, 209, 240 + 138, 178, 174, 32, 50, 130, 59, 252, 53, 61, 149, 173, 157, 117, 42, 161, 77, 123, + 199, 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 ], rem_1 ); @@ -456,18 +446,18 @@ mod tests { ), }; - assert_eq!(RemoteAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); + assert_eq!(ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); let mul = MultiLocation { parents: 0, interior: X2(Parachain(2), AccountKey20 { network: None, key: [0u8; 20] }), }; - let rem_2 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + let rem_2 = ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(); assert_eq!( [ - 167, 27, 179, 135, 180, 42, 176, 100, 1, 89, 215, 160, 171, 11, 25, 196, 254, 54, - 98, 138, 213, 243, 188, 30, 224, 253, 170, 212, 129, 163, 220, 176 + 71, 235, 74, 190, 35, 18, 98, 222, 79, 110, 131, 222, 110, 187, 70, 157, 157, 61, + 12, 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 ], rem_2 ); @@ -481,12 +471,12 @@ mod tests { parents: 0, interior: X2(Parachain(1), AccountId32 { network: None, id: [0u8; 32] }), }; - let rem_1 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + let rem_1 = ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(); assert_eq!( [ - 221, 151, 114, 30, 191, 162, 192, 170, 30, 95, 129, 157, 52, 177, 157, 136, 137, - 145, 129, 132, 199, 148, 188, 135, 245, 140, 163, 99, 189, 217, 160, 244 + 99, 200, 141, 87, 123, 89, 117, 64, 130, 31, 1, 205, 203, 222, 3, 213, 96, 214, + 109, 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 ], rem_1 ); @@ -499,18 +489,18 @@ mod tests { ), }; - assert_eq!(RemoteAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); + assert_eq!(ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); let mul = MultiLocation { parents: 0, interior: X2(Parachain(2), AccountId32 { network: None, id: [0u8; 32] }), }; - let rem_2 = RemoteAccount::<[u8; 32]>::convert(mul).unwrap(); + let rem_2 = ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(); assert_eq!( [ - 146, 61, 202, 186, 203, 8, 50, 95, 225, 247, 103, 152, 224, 178, 32, 43, 34, 78, - 20, 59, 89, 206, 156, 224, 128, 75, 169, 17, 111, 233, 196, 245 + 134, 167, 40, 2, 211, 236, 255, 60, 95, 118, 137, 13, 55, 219, 60, 190, 109, 255, + 233, 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 ], rem_2 ); @@ -524,6 +514,6 @@ mod tests { parents: 1, interior: X1(AccountKey20 { network: None, key: [0u8; 20] }), }; - assert!(RemoteAccount::<[u8; 32]>::convert(mul).is_err()); + assert!(ForeignChainAliasAccount::<[u8; 32]>::convert(mul).is_err()); } } From e711e7ea0a777259d976884ebc6a7dd2d333c832 Mon Sep 17 00:00:00 2001 From: mustermeiszer Date: Sun, 5 Feb 2023 23:35:53 +0100 Subject: [PATCH 4/9] Try with prefix in account --- xcm/xcm-builder/src/location_conversion.rs | 92 +++++++++++++++++----- 1 file changed, 71 insertions(+), 21 deletions(-) diff --git a/xcm/xcm-builder/src/location_conversion.rs b/xcm/xcm-builder/src/location_conversion.rs index 2c553a3b96ec..3c87ff4e92a9 100644 --- a/xcm/xcm-builder/src/location_conversion.rs +++ b/xcm/xcm-builder/src/location_conversion.rs @@ -80,15 +80,65 @@ impl + Clone> Convert impl ForeignChainAliasAccount { fn from_para_32(para_id: &u32, id: &[u8; 32]) -> [u8; 32] { - (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256) + const PREFIX: [u8; 8] = *b"ForeignP"; + let mut out = (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256); + + out[0] = PREFIX[0]; + out[1] = PREFIX[1]; + out[2] = PREFIX[2]; + out[3] = PREFIX[3]; + out[4] = PREFIX[4]; + out[5] = PREFIX[5]; + out[6] = PREFIX[6]; + out[7] = PREFIX[7]; + + let id: [u8; 4] = para_id.to_le_bytes(); + + out[8] = id[0]; + out[9] = id[1]; + out[10] = id[2]; + out[11] = id[3]; + + out } fn from_para_20(para_id: &u32, id: &[u8; 20]) -> [u8; 32] { - (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256) + const PREFIX: [u8; 8] = *b"ForeignP"; + let mut out = (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256); + + out[0] = PREFIX[0]; + out[1] = PREFIX[1]; + out[2] = PREFIX[2]; + out[3] = PREFIX[3]; + out[4] = PREFIX[4]; + out[5] = PREFIX[5]; + out[6] = PREFIX[6]; + out[7] = PREFIX[7]; + + let id: [u8; 4] = para_id.to_le_bytes(); + + out[8] = id[0]; + out[9] = id[1]; + out[10] = id[2]; + out[11] = id[3]; + + out } fn from_relay_32(id: &[u8; 32]) -> [u8; 32] { - (FOREIGN_CHAIN_PREFIX, id).using_encoded(blake2_256) + const PREFIX: [u8; 8] = *b"ForeignR"; + let mut out = (FOREIGN_CHAIN_PREFIX, id).using_encoded(blake2_256); + + out[0] = PREFIX[0]; + out[1] = PREFIX[1]; + out[2] = PREFIX[2]; + out[3] = PREFIX[3]; + out[4] = PREFIX[4]; + out[5] = PREFIX[5]; + out[6] = PREFIX[6]; + out[7] = PREFIX[7]; + + out } } @@ -306,8 +356,8 @@ mod tests { assert_eq!( [ - 99, 200, 141, 87, 123, 89, 117, 64, 130, 31, 1, 205, 203, 222, 3, 213, 96, 214, - 109, 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 + 70, 111, 114, 101, 105, 103, 110, 80, 1, 0, 0, 0, 203, 222, 3, 213, 96, 214, 109, + 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 ], rem_1 ); @@ -330,8 +380,8 @@ mod tests { assert_eq!( [ - 134, 167, 40, 2, 211, 236, 255, 60, 95, 118, 137, 13, 55, 219, 60, 190, 109, 255, - 233, 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 + 70, 111, 114, 101, 105, 103, 110, 80, 2, 0, 0, 0, 55, 219, 60, 190, 109, 255, 233, + 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 ], rem_2 ); @@ -349,8 +399,8 @@ mod tests { assert_eq!( [ - 138, 178, 174, 32, 50, 130, 59, 252, 53, 61, 149, 173, 157, 117, 42, 161, 77, 123, - 199, 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 + 70, 111, 114, 101, 105, 103, 110, 80, 1, 0, 0, 0, 157, 117, 42, 161, 77, 123, 199, + 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 ], rem_1 ); @@ -373,8 +423,8 @@ mod tests { assert_eq!( [ - 71, 235, 74, 190, 35, 18, 98, 222, 79, 110, 131, 222, 110, 187, 70, 157, 157, 61, - 12, 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 + 70, 111, 114, 101, 105, 103, 110, 80, 2, 0, 0, 0, 110, 187, 70, 157, 157, 61, 12, + 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 ], rem_2 ); @@ -392,7 +442,7 @@ mod tests { assert_eq!( [ - 126, 169, 155, 79, 160, 47, 12, 149, 120, 177, 75, 189, 219, 70, 212, 232, 67, 8, + 70, 111, 114, 101, 105, 103, 110, 82, 120, 177, 75, 189, 219, 70, 212, 232, 67, 8, 3, 204, 96, 162, 6, 249, 195, 169, 46, 151, 100, 221, 9, 249 ], rem_1 @@ -413,7 +463,7 @@ mod tests { assert_eq!( [ - 180, 59, 39, 58, 99, 137, 223, 252, 92, 171, 106, 182, 150, 228, 6, 66, 154, 209, + 70, 111, 114, 101, 105, 103, 110, 82, 92, 171, 106, 182, 150, 228, 6, 66, 154, 209, 41, 100, 2, 187, 51, 247, 231, 43, 84, 150, 13, 159, 236, 153 ], rem_2 @@ -432,8 +482,8 @@ mod tests { assert_eq!( [ - 138, 178, 174, 32, 50, 130, 59, 252, 53, 61, 149, 173, 157, 117, 42, 161, 77, 123, - 199, 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 + 70, 111, 114, 101, 105, 103, 110, 80, 1, 0, 0, 0, 157, 117, 42, 161, 77, 123, 199, + 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 ], rem_1 ); @@ -456,8 +506,8 @@ mod tests { assert_eq!( [ - 71, 235, 74, 190, 35, 18, 98, 222, 79, 110, 131, 222, 110, 187, 70, 157, 157, 61, - 12, 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 + 70, 111, 114, 101, 105, 103, 110, 80, 2, 0, 0, 0, 110, 187, 70, 157, 157, 61, 12, + 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 ], rem_2 ); @@ -475,8 +525,8 @@ mod tests { assert_eq!( [ - 99, 200, 141, 87, 123, 89, 117, 64, 130, 31, 1, 205, 203, 222, 3, 213, 96, 214, - 109, 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 + 70, 111, 114, 101, 105, 103, 110, 80, 1, 0, 0, 0, 203, 222, 3, 213, 96, 214, 109, + 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 ], rem_1 ); @@ -499,8 +549,8 @@ mod tests { assert_eq!( [ - 134, 167, 40, 2, 211, 236, 255, 60, 95, 118, 137, 13, 55, 219, 60, 190, 109, 255, - 233, 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 + 70, 111, 114, 101, 105, 103, 110, 80, 2, 0, 0, 0, 55, 219, 60, 190, 109, 255, 233, + 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 ], rem_2 ); From 1536ba7868f090b52e04cd5711b5ed2b919d9f9d Mon Sep 17 00:00:00 2001 From: mustermeiszer Date: Mon, 6 Feb 2023 13:25:11 +0100 Subject: [PATCH 5/9] Revert "Try with prefix in account" This reverts commit 461759794cc9f59f768aad0e0899df6dc8cba747. --- xcm/xcm-builder/src/location_conversion.rs | 92 +++++----------------- 1 file changed, 21 insertions(+), 71 deletions(-) diff --git a/xcm/xcm-builder/src/location_conversion.rs b/xcm/xcm-builder/src/location_conversion.rs index 3c87ff4e92a9..2c553a3b96ec 100644 --- a/xcm/xcm-builder/src/location_conversion.rs +++ b/xcm/xcm-builder/src/location_conversion.rs @@ -80,65 +80,15 @@ impl + Clone> Convert impl ForeignChainAliasAccount { fn from_para_32(para_id: &u32, id: &[u8; 32]) -> [u8; 32] { - const PREFIX: [u8; 8] = *b"ForeignP"; - let mut out = (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256); - - out[0] = PREFIX[0]; - out[1] = PREFIX[1]; - out[2] = PREFIX[2]; - out[3] = PREFIX[3]; - out[4] = PREFIX[4]; - out[5] = PREFIX[5]; - out[6] = PREFIX[6]; - out[7] = PREFIX[7]; - - let id: [u8; 4] = para_id.to_le_bytes(); - - out[8] = id[0]; - out[9] = id[1]; - out[10] = id[2]; - out[11] = id[3]; - - out + (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256) } fn from_para_20(para_id: &u32, id: &[u8; 20]) -> [u8; 32] { - const PREFIX: [u8; 8] = *b"ForeignP"; - let mut out = (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256); - - out[0] = PREFIX[0]; - out[1] = PREFIX[1]; - out[2] = PREFIX[2]; - out[3] = PREFIX[3]; - out[4] = PREFIX[4]; - out[5] = PREFIX[5]; - out[6] = PREFIX[6]; - out[7] = PREFIX[7]; - - let id: [u8; 4] = para_id.to_le_bytes(); - - out[8] = id[0]; - out[9] = id[1]; - out[10] = id[2]; - out[11] = id[3]; - - out + (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256) } fn from_relay_32(id: &[u8; 32]) -> [u8; 32] { - const PREFIX: [u8; 8] = *b"ForeignR"; - let mut out = (FOREIGN_CHAIN_PREFIX, id).using_encoded(blake2_256); - - out[0] = PREFIX[0]; - out[1] = PREFIX[1]; - out[2] = PREFIX[2]; - out[3] = PREFIX[3]; - out[4] = PREFIX[4]; - out[5] = PREFIX[5]; - out[6] = PREFIX[6]; - out[7] = PREFIX[7]; - - out + (FOREIGN_CHAIN_PREFIX, id).using_encoded(blake2_256) } } @@ -356,8 +306,8 @@ mod tests { assert_eq!( [ - 70, 111, 114, 101, 105, 103, 110, 80, 1, 0, 0, 0, 203, 222, 3, 213, 96, 214, 109, - 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 + 99, 200, 141, 87, 123, 89, 117, 64, 130, 31, 1, 205, 203, 222, 3, 213, 96, 214, + 109, 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 ], rem_1 ); @@ -380,8 +330,8 @@ mod tests { assert_eq!( [ - 70, 111, 114, 101, 105, 103, 110, 80, 2, 0, 0, 0, 55, 219, 60, 190, 109, 255, 233, - 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 + 134, 167, 40, 2, 211, 236, 255, 60, 95, 118, 137, 13, 55, 219, 60, 190, 109, 255, + 233, 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 ], rem_2 ); @@ -399,8 +349,8 @@ mod tests { assert_eq!( [ - 70, 111, 114, 101, 105, 103, 110, 80, 1, 0, 0, 0, 157, 117, 42, 161, 77, 123, 199, - 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 + 138, 178, 174, 32, 50, 130, 59, 252, 53, 61, 149, 173, 157, 117, 42, 161, 77, 123, + 199, 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 ], rem_1 ); @@ -423,8 +373,8 @@ mod tests { assert_eq!( [ - 70, 111, 114, 101, 105, 103, 110, 80, 2, 0, 0, 0, 110, 187, 70, 157, 157, 61, 12, - 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 + 71, 235, 74, 190, 35, 18, 98, 222, 79, 110, 131, 222, 110, 187, 70, 157, 157, 61, + 12, 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 ], rem_2 ); @@ -442,7 +392,7 @@ mod tests { assert_eq!( [ - 70, 111, 114, 101, 105, 103, 110, 82, 120, 177, 75, 189, 219, 70, 212, 232, 67, 8, + 126, 169, 155, 79, 160, 47, 12, 149, 120, 177, 75, 189, 219, 70, 212, 232, 67, 8, 3, 204, 96, 162, 6, 249, 195, 169, 46, 151, 100, 221, 9, 249 ], rem_1 @@ -463,7 +413,7 @@ mod tests { assert_eq!( [ - 70, 111, 114, 101, 105, 103, 110, 82, 92, 171, 106, 182, 150, 228, 6, 66, 154, 209, + 180, 59, 39, 58, 99, 137, 223, 252, 92, 171, 106, 182, 150, 228, 6, 66, 154, 209, 41, 100, 2, 187, 51, 247, 231, 43, 84, 150, 13, 159, 236, 153 ], rem_2 @@ -482,8 +432,8 @@ mod tests { assert_eq!( [ - 70, 111, 114, 101, 105, 103, 110, 80, 1, 0, 0, 0, 157, 117, 42, 161, 77, 123, 199, - 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 + 138, 178, 174, 32, 50, 130, 59, 252, 53, 61, 149, 173, 157, 117, 42, 161, 77, 123, + 199, 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 ], rem_1 ); @@ -506,8 +456,8 @@ mod tests { assert_eq!( [ - 70, 111, 114, 101, 105, 103, 110, 80, 2, 0, 0, 0, 110, 187, 70, 157, 157, 61, 12, - 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 + 71, 235, 74, 190, 35, 18, 98, 222, 79, 110, 131, 222, 110, 187, 70, 157, 157, 61, + 12, 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 ], rem_2 ); @@ -525,8 +475,8 @@ mod tests { assert_eq!( [ - 70, 111, 114, 101, 105, 103, 110, 80, 1, 0, 0, 0, 203, 222, 3, 213, 96, 214, 109, - 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 + 99, 200, 141, 87, 123, 89, 117, 64, 130, 31, 1, 205, 203, 222, 3, 213, 96, 214, + 109, 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 ], rem_1 ); @@ -549,8 +499,8 @@ mod tests { assert_eq!( [ - 70, 111, 114, 101, 105, 103, 110, 80, 2, 0, 0, 0, 55, 219, 60, 190, 109, 255, 233, - 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 + 134, 167, 40, 2, 211, 236, 255, 60, 95, 118, 137, 13, 55, 219, 60, 190, 109, 255, + 233, 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 ], rem_2 ); From bbe0791b7fe9315eb2cc6ca8f0ed76aa6f6aa023 Mon Sep 17 00:00:00 2001 From: mustermeiszer Date: Tue, 28 Feb 2023 20:19:42 +0100 Subject: [PATCH 6/9] Adapt code and test to review suggestions --- xcm/xcm-builder/src/location_conversion.rs | 59 +++++++++++++--------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/xcm/xcm-builder/src/location_conversion.rs b/xcm/xcm-builder/src/location_conversion.rs index 2c553a3b96ec..abc4a6a72a54 100644 --- a/xcm/xcm-builder/src/location_conversion.rs +++ b/xcm/xcm-builder/src/location_conversion.rs @@ -22,8 +22,17 @@ use sp_std::{borrow::Borrow, marker::PhantomData}; use xcm::latest::prelude::*; use xcm_executor::traits::Convert; -/// Prefix for bytes in account creation of foreign chains -pub const FOREIGN_CHAIN_PREFIX: [u8; 30] = *b"ForeignChainAliasAccountPrefix"; +/// Prefix for generating alias account for accounts coming +/// from chains that use 32 byte long representations. +pub const FOREIGN_CHAIN_PREFIX_PARA_32: [u8; 37] = *b"ForeignChainAliasAccountPrefix_Para32"; + +/// Prefix for generating alias account for accounts coming +/// from chains that use 20 byte long representations. +pub const FOREIGN_CHAIN_PREFIX_PARA_20: [u8; 37] = *b"ForeignChainAliasAccountPrefix_Para20"; + +/// Prefix for generating alias account for accounts coming +/// from the relay chain using 32 byte long representations. +pub const FOREIGN_CHAIN_PREFIX_RELAY: [u8; 36] = *b"ForeignChainAliasAccountPrefix_Relay"; /// This converter will for a given `AccountId32`/`AccountKey20` /// always generate the same "remote" account for a specific @@ -80,15 +89,15 @@ impl + Clone> Convert impl ForeignChainAliasAccount { fn from_para_32(para_id: &u32, id: &[u8; 32]) -> [u8; 32] { - (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256) + (FOREIGN_CHAIN_PREFIX_PARA_32, para_id, id).using_encoded(blake2_256) } fn from_para_20(para_id: &u32, id: &[u8; 20]) -> [u8; 32] { - (FOREIGN_CHAIN_PREFIX, para_id, id).using_encoded(blake2_256) + (FOREIGN_CHAIN_PREFIX_PARA_20, para_id, id).using_encoded(blake2_256) } fn from_relay_32(id: &[u8; 32]) -> [u8; 32] { - (FOREIGN_CHAIN_PREFIX, id).using_encoded(blake2_256) + (FOREIGN_CHAIN_PREFIX_RELAY, id).using_encoded(blake2_256) } } @@ -306,8 +315,8 @@ mod tests { assert_eq!( [ - 99, 200, 141, 87, 123, 89, 117, 64, 130, 31, 1, 205, 203, 222, 3, 213, 96, 214, - 109, 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 + 43, 205, 192, 244, 57, 93, 151, 208, 156, 78, 24, 118, 192, 126, 212, 77, 158, 137, + 98, 101, 12, 20, 129, 113, 190, 51, 32, 217, 39, 113, 109, 6 ], rem_1 ); @@ -330,8 +339,8 @@ mod tests { assert_eq!( [ - 134, 167, 40, 2, 211, 236, 255, 60, 95, 118, 137, 13, 55, 219, 60, 190, 109, 255, - 233, 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 + 2, 64, 114, 208, 208, 93, 134, 10, 11, 103, 214, 240, 226, 215, 103, 4, 54, 15, + 225, 104, 116, 206, 195, 130, 148, 31, 166, 234, 208, 133, 205, 193 ], rem_2 ); @@ -349,8 +358,8 @@ mod tests { assert_eq!( [ - 138, 178, 174, 32, 50, 130, 59, 252, 53, 61, 149, 173, 157, 117, 42, 161, 77, 123, - 199, 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 + 220, 227, 86, 34, 106, 215, 195, 254, 66, 227, 147, 109, 29, 94, 166, 218, 167, 12, + 173, 249, 210, 12, 160, 175, 74, 47, 66, 101, 57, 0, 6, 237 ], rem_1 ); @@ -373,8 +382,8 @@ mod tests { assert_eq!( [ - 71, 235, 74, 190, 35, 18, 98, 222, 79, 110, 131, 222, 110, 187, 70, 157, 157, 61, - 12, 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 + 36, 151, 177, 242, 57, 63, 152, 88, 86, 120, 68, 182, 184, 241, 226, 146, 160, 58, + 117, 88, 68, 252, 215, 207, 123, 47, 230, 70, 226, 128, 93, 95 ], rem_2 ); @@ -392,8 +401,8 @@ mod tests { assert_eq!( [ - 126, 169, 155, 79, 160, 47, 12, 149, 120, 177, 75, 189, 219, 70, 212, 232, 67, 8, - 3, 204, 96, 162, 6, 249, 195, 169, 46, 151, 100, 221, 9, 249 + 94, 196, 87, 187, 9, 80, 218, 5, 197, 191, 254, 209, 97, 71, 157, 179, 89, 177, + 143, 116, 89, 101, 166, 229, 63, 192, 113, 84, 33, 75, 53, 180 ], rem_1 ); @@ -413,8 +422,8 @@ mod tests { assert_eq!( [ - 180, 59, 39, 58, 99, 137, 223, 252, 92, 171, 106, 182, 150, 228, 6, 66, 154, 209, - 41, 100, 2, 187, 51, 247, 231, 43, 84, 150, 13, 159, 236, 153 + 80, 50, 225, 2, 93, 166, 226, 77, 90, 153, 212, 206, 248, 60, 123, 128, 229, 23, + 251, 53, 66, 248, 216, 139, 5, 54, 72, 98, 165, 23, 45, 56 ], rem_2 ); @@ -432,8 +441,8 @@ mod tests { assert_eq!( [ - 138, 178, 174, 32, 50, 130, 59, 252, 53, 61, 149, 173, 157, 117, 42, 161, 77, 123, - 199, 182, 244, 201, 112, 60, 34, 233, 140, 160, 182, 150, 132, 98 + 220, 227, 86, 34, 106, 215, 195, 254, 66, 227, 147, 109, 29, 94, 166, 218, 167, 12, + 173, 249, 210, 12, 160, 175, 74, 47, 66, 101, 57, 0, 6, 237 ], rem_1 ); @@ -456,8 +465,8 @@ mod tests { assert_eq!( [ - 71, 235, 74, 190, 35, 18, 98, 222, 79, 110, 131, 222, 110, 187, 70, 157, 157, 61, - 12, 38, 183, 87, 243, 73, 132, 12, 89, 169, 57, 160, 43, 241 + 36, 151, 177, 242, 57, 63, 152, 88, 86, 120, 68, 182, 184, 241, 226, 146, 160, 58, + 117, 88, 68, 252, 215, 207, 123, 47, 230, 70, 226, 128, 93, 95 ], rem_2 ); @@ -475,8 +484,8 @@ mod tests { assert_eq!( [ - 99, 200, 141, 87, 123, 89, 117, 64, 130, 31, 1, 205, 203, 222, 3, 213, 96, 214, - 109, 250, 209, 25, 219, 76, 147, 227, 249, 57, 153, 100, 151, 161 + 43, 205, 192, 244, 57, 93, 151, 208, 156, 78, 24, 118, 192, 126, 212, 77, 158, 137, + 98, 101, 12, 20, 129, 113, 190, 51, 32, 217, 39, 113, 109, 6 ], rem_1 ); @@ -499,8 +508,8 @@ mod tests { assert_eq!( [ - 134, 167, 40, 2, 211, 236, 255, 60, 95, 118, 137, 13, 55, 219, 60, 190, 109, 255, - 233, 208, 180, 150, 245, 242, 86, 9, 223, 26, 207, 103, 117, 3 + 2, 64, 114, 208, 208, 93, 134, 10, 11, 103, 214, 240, 226, 215, 103, 4, 54, 15, + 225, 104, 116, 206, 195, 130, 148, 31, 166, 234, 208, 133, 205, 193 ], rem_2 ); From 8741b0751dcdba9e0ad586ef313320cf47efb878 Mon Sep 17 00:00:00 2001 From: mustermeiszer Date: Mon, 20 Mar 2023 15:24:00 +0100 Subject: [PATCH 7/9] adapt to take into account parent in conversion --- xcm/xcm-builder/src/location_conversion.rs | 96 +++++++++++++--------- 1 file changed, 55 insertions(+), 41 deletions(-) diff --git a/xcm/xcm-builder/src/location_conversion.rs b/xcm/xcm-builder/src/location_conversion.rs index abc4a6a72a54..10f415576a2d 100644 --- a/xcm/xcm-builder/src/location_conversion.rs +++ b/xcm/xcm-builder/src/location_conversion.rs @@ -41,6 +41,30 @@ pub const FOREIGN_CHAIN_PREFIX_RELAY: [u8; 36] = *b"ForeignChainAliasAccountPref /// on every consuming para-chain and relay chain. /// /// Can be used as a converter in `SovereignSignedViaLocation` +/// +/// Assuming the following network layout. +/// +/// R +/// / \ +/// / \ +/// P1 P2 +/// / \ / \ +/// / \ / \ +/// P1.1 P1.2 P2.1 P2.2 +/// +/// Then a given account A will have the same alias accounts in the +/// same plane. So, it is important which chain account A acts from. +/// E.g. +/// * From P1.2 A will act as +/// * hash(ParaPrefix, A, 1, 1) on P1.2 +/// * hash(ParaPrefix, A, 1, 0) on P1 +/// * From P1 A will act as +/// * hash(RelayPrefix, A, 1) on P1.2 & P1.1 +/// * hash(ParaPrefix, A, 1, 1) on P2 +/// * hash(ParaPrefix, A, 1, 0) on R +/// +/// Note that the alias accounts have overlaps but never on the same +/// chain when the sender comes from different chains. pub struct ForeignChainAliasAccount(PhantomData); impl + Clone> Convert for ForeignChainAliasAccount @@ -51,29 +75,29 @@ impl + Clone> Convert MultiLocation { parents: 0, interior: X2(Parachain(para_id), AccountId32 { id, .. }), - } => ForeignChainAliasAccount::::from_para_32(para_id, id), + } => ForeignChainAliasAccount::::from_para_32(para_id, id, 0), // Used on the relay chain for sending paras that use 20 byte accounts MultiLocation { parents: 0, interior: X2(Parachain(para_id), AccountKey20 { key, .. }), - } => ForeignChainAliasAccount::::from_para_20(para_id, key), + } => ForeignChainAliasAccount::::from_para_20(para_id, key, 0), // Used on para-chain for sending paras that use 32 byte accounts MultiLocation { parents: 1, interior: X2(Parachain(para_id), AccountId32 { id, .. }), - } => ForeignChainAliasAccount::::from_para_32(para_id, id), + } => ForeignChainAliasAccount::::from_para_32(para_id, id, 1), // Used on para-chain for sending paras that use 20 byte accounts MultiLocation { parents: 1, interior: X2(Parachain(para_id), AccountKey20 { key, .. }), - } => ForeignChainAliasAccount::::from_para_20(para_id, key), + } => ForeignChainAliasAccount::::from_para_20(para_id, key, 1), // Used on para-chain for sending from the relay chain MultiLocation { parents: 1, interior: X1(AccountId32 { id, .. }) } => - ForeignChainAliasAccount::::from_relay_32(id), + ForeignChainAliasAccount::::from_relay_32(id, 1), // No other conversions provided _ => return Err(()), @@ -88,16 +112,16 @@ impl + Clone> Convert } impl ForeignChainAliasAccount { - fn from_para_32(para_id: &u32, id: &[u8; 32]) -> [u8; 32] { - (FOREIGN_CHAIN_PREFIX_PARA_32, para_id, id).using_encoded(blake2_256) + fn from_para_32(para_id: &u32, id: &[u8; 32], parents: u8) -> [u8; 32] { + (FOREIGN_CHAIN_PREFIX_PARA_32, para_id, id, parents).using_encoded(blake2_256) } - fn from_para_20(para_id: &u32, id: &[u8; 20]) -> [u8; 32] { - (FOREIGN_CHAIN_PREFIX_PARA_20, para_id, id).using_encoded(blake2_256) + fn from_para_20(para_id: &u32, id: &[u8; 20], parents: u8) -> [u8; 32] { + (FOREIGN_CHAIN_PREFIX_PARA_20, para_id, id, parents).using_encoded(blake2_256) } - fn from_relay_32(id: &[u8; 32]) -> [u8; 32] { - (FOREIGN_CHAIN_PREFIX_RELAY, id).using_encoded(blake2_256) + fn from_relay_32(id: &[u8; 32], parents: u8) -> [u8; 32] { + (FOREIGN_CHAIN_PREFIX_RELAY, id, parents).using_encoded(blake2_256) } } @@ -315,8 +339,8 @@ mod tests { assert_eq!( [ - 43, 205, 192, 244, 57, 93, 151, 208, 156, 78, 24, 118, 192, 126, 212, 77, 158, 137, - 98, 101, 12, 20, 129, 113, 190, 51, 32, 217, 39, 113, 109, 6 + 181, 186, 132, 152, 52, 210, 226, 199, 8, 235, 213, 242, 94, 70, 250, 170, 19, 163, + 196, 102, 245, 14, 172, 184, 2, 148, 108, 87, 230, 163, 204, 32 ], rem_1 ); @@ -339,8 +363,8 @@ mod tests { assert_eq!( [ - 2, 64, 114, 208, 208, 93, 134, 10, 11, 103, 214, 240, 226, 215, 103, 4, 54, 15, - 225, 104, 116, 206, 195, 130, 148, 31, 166, 234, 208, 133, 205, 193 + 183, 188, 66, 169, 82, 250, 45, 30, 142, 119, 184, 55, 177, 64, 53, 114, 12, 147, + 128, 10, 60, 45, 41, 193, 87, 18, 86, 49, 127, 233, 243, 143 ], rem_2 ); @@ -358,8 +382,8 @@ mod tests { assert_eq!( [ - 220, 227, 86, 34, 106, 215, 195, 254, 66, 227, 147, 109, 29, 94, 166, 218, 167, 12, - 173, 249, 210, 12, 160, 175, 74, 47, 66, 101, 57, 0, 6, 237 + 210, 60, 37, 255, 116, 38, 221, 26, 85, 82, 252, 125, 220, 19, 41, 91, 185, 69, + 102, 83, 120, 63, 15, 212, 74, 141, 82, 203, 187, 212, 77, 120 ], rem_1 ); @@ -382,8 +406,8 @@ mod tests { assert_eq!( [ - 36, 151, 177, 242, 57, 63, 152, 88, 86, 120, 68, 182, 184, 241, 226, 146, 160, 58, - 117, 88, 68, 252, 215, 207, 123, 47, 230, 70, 226, 128, 93, 95 + 197, 16, 31, 199, 234, 80, 166, 55, 178, 135, 95, 48, 19, 128, 9, 167, 51, 99, 215, + 147, 94, 171, 28, 157, 29, 107, 240, 22, 10, 104, 99, 186 ], rem_2 ); @@ -401,8 +425,8 @@ mod tests { assert_eq!( [ - 94, 196, 87, 187, 9, 80, 218, 5, 197, 191, 254, 209, 97, 71, 157, 179, 89, 177, - 143, 116, 89, 101, 166, 229, 63, 192, 113, 84, 33, 75, 53, 180 + 227, 12, 152, 241, 220, 53, 26, 27, 1, 167, 167, 214, 61, 161, 255, 96, 56, 16, + 221, 59, 47, 45, 40, 193, 88, 92, 4, 167, 164, 27, 112, 99 ], rem_1 ); @@ -422,8 +446,8 @@ mod tests { assert_eq!( [ - 80, 50, 225, 2, 93, 166, 226, 77, 90, 153, 212, 206, 248, 60, 123, 128, 229, 23, - 251, 53, 66, 248, 216, 139, 5, 54, 72, 98, 165, 23, 45, 56 + 143, 195, 87, 73, 129, 2, 163, 211, 239, 51, 55, 235, 82, 173, 162, 206, 158, 237, + 166, 73, 254, 62, 131, 6, 170, 241, 209, 116, 105, 69, 29, 226 ], rem_2 ); @@ -441,22 +465,12 @@ mod tests { assert_eq!( [ - 220, 227, 86, 34, 106, 215, 195, 254, 66, 227, 147, 109, 29, 94, 166, 218, 167, 12, - 173, 249, 210, 12, 160, 175, 74, 47, 66, 101, 57, 0, 6, 237 + 25, 251, 15, 92, 148, 141, 236, 238, 50, 108, 133, 56, 118, 11, 250, 122, 81, 160, + 104, 160, 97, 200, 210, 49, 208, 142, 64, 144, 24, 110, 246, 101 ], rem_1 ); - let mul = MultiLocation { - parents: 1, - interior: X2( - Parachain(1), - AccountKey20 { network: Some(NetworkId::Polkadot), key: [0u8; 20] }, - ), - }; - - assert_eq!(ForeignChainAliasAccount::<[u8; 32]>::convert(mul).unwrap(), rem_1); - let mul = MultiLocation { parents: 0, interior: X2(Parachain(2), AccountKey20 { network: None, key: [0u8; 20] }), @@ -465,8 +479,8 @@ mod tests { assert_eq!( [ - 36, 151, 177, 242, 57, 63, 152, 88, 86, 120, 68, 182, 184, 241, 226, 146, 160, 58, - 117, 88, 68, 252, 215, 207, 123, 47, 230, 70, 226, 128, 93, 95 + 88, 157, 224, 235, 76, 88, 201, 143, 206, 227, 14, 192, 177, 245, 75, 62, 41, 10, + 107, 182, 61, 57, 239, 112, 43, 151, 58, 111, 150, 153, 234, 189 ], rem_2 ); @@ -484,8 +498,8 @@ mod tests { assert_eq!( [ - 43, 205, 192, 244, 57, 93, 151, 208, 156, 78, 24, 118, 192, 126, 212, 77, 158, 137, - 98, 101, 12, 20, 129, 113, 190, 51, 32, 217, 39, 113, 109, 6 + 45, 120, 232, 0, 226, 49, 106, 48, 65, 181, 184, 147, 224, 235, 198, 152, 183, 156, + 67, 57, 67, 67, 187, 104, 171, 23, 140, 21, 183, 152, 63, 20 ], rem_1 ); @@ -508,8 +522,8 @@ mod tests { assert_eq!( [ - 2, 64, 114, 208, 208, 93, 134, 10, 11, 103, 214, 240, 226, 215, 103, 4, 54, 15, - 225, 104, 116, 206, 195, 130, 148, 31, 166, 234, 208, 133, 205, 193 + 97, 119, 110, 66, 239, 113, 96, 234, 127, 92, 66, 204, 53, 129, 33, 119, 213, 192, + 171, 100, 139, 51, 39, 62, 196, 163, 16, 213, 160, 44, 100, 228 ], rem_2 ); From e3c276ff07cf2487b264cde6c2558f035f046a0d Mon Sep 17 00:00:00 2001 From: mustermeiszer Date: Mon, 20 Mar 2023 16:50:04 +0100 Subject: [PATCH 8/9] fix: docs --- xcm/xcm-builder/src/location_conversion.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/xcm/xcm-builder/src/location_conversion.rs b/xcm/xcm-builder/src/location_conversion.rs index 10f415576a2d..3522f18dc1d0 100644 --- a/xcm/xcm-builder/src/location_conversion.rs +++ b/xcm/xcm-builder/src/location_conversion.rs @@ -42,15 +42,16 @@ pub const FOREIGN_CHAIN_PREFIX_RELAY: [u8; 36] = *b"ForeignChainAliasAccountPref /// /// Can be used as a converter in `SovereignSignedViaLocation` /// +/// ## Example /// Assuming the following network layout. /// -/// R -/// / \ -/// / \ -/// P1 P2 -/// / \ / \ -/// / \ / \ -/// P1.1 P1.2 P2.1 P2.2 +/// R +/// / \ +/// / \ +/// P1 P2 +/// / \ / \ +/// / \ / \ +/// P1.1 P1.2 P2.1 P2.2 /// /// Then a given account A will have the same alias accounts in the /// same plane. So, it is important which chain account A acts from. From 2a5f5b72d2b86f03aec1ae379d2a2dd5c571efa5 Mon Sep 17 00:00:00 2001 From: mustermeiszer Date: Mon, 20 Mar 2023 20:13:26 +0100 Subject: [PATCH 9/9] fix: try doc fix v2 --- xcm/xcm-builder/src/location_conversion.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xcm/xcm-builder/src/location_conversion.rs b/xcm/xcm-builder/src/location_conversion.rs index 3522f18dc1d0..1fc5a2582f5f 100644 --- a/xcm/xcm-builder/src/location_conversion.rs +++ b/xcm/xcm-builder/src/location_conversion.rs @@ -45,6 +45,7 @@ pub const FOREIGN_CHAIN_PREFIX_RELAY: [u8; 36] = *b"ForeignChainAliasAccountPref /// ## Example /// Assuming the following network layout. /// +/// ```notrust /// R /// / \ /// / \ @@ -52,7 +53,7 @@ pub const FOREIGN_CHAIN_PREFIX_RELAY: [u8; 36] = *b"ForeignChainAliasAccountPref /// / \ / \ /// / \ / \ /// P1.1 P1.2 P2.1 P2.2 -/// +/// ``` /// Then a given account A will have the same alias accounts in the /// same plane. So, it is important which chain account A acts from. /// E.g.