# MultiLocation-based account derivation

**URL:** <https://forum.polkadot.network/t/multilocation-based-account-derivation/436>\
**Category:** Tech Talk\
**Tags:** xcm\
**Created:** [September 19, 2022, 4:27pm UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436 "2022-09-19T16:27:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![girazoki](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@girazoki](https://forum.polkadot.network/u/girazoki)\
**Post date:** [September 19, 2022, 4:27pm UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/1 "2022-09-19T16:27:03Z")

</div>

Currently the XCM configuration defines how to convert multilocations to accounts, usually mapping the parachain multilocation to is sovereign account through the _**[SiblingParachainConvertsVia](https://github.com/paritytech/polkadot/blob/bc0cc9d610a4f5de901ad9a878fe5c6c7b1708c9/xcm/xcm-builder/src/location_conversion.rs#L87)**_ or the _**[ChildParachainConvertsVia](https://github.com/paritytech/polkadot/blob/bc0cc9d610a4f5de901ad9a878fe5c6c7b1708c9/xcm/xcm-builder/src/location_conversion.rs#L66)**_

However, this location conversions obviously fail if the multilocation does not represent a parachain. Therefore, we need to add further conversions if we want to map an origin representing, e.g., an account in a parachain (e.g., _MultiLocation {parents: 1, interior: X2(Parachain, AccountKey32)}_). We have observed the existence of _**[Account32Hash](https://github.com/paritytech/polkadot/blob/bc0cc9d610a4f5de901ad9a878fe5c6c7b1708c9/xcm/xcm-builder/src/location_conversion.rs#L25)**_, which allows you to convert a generic multilocation to an accountId by hashing it.

In Moonbeam, we have already started experiment with these kind of location conversions. Should this become the standard way of implementing such usecase? And although I know this clearly depends on the parachain implementing it, maybe it would make sense to provide a RPC method that performs this conversion to help UI tools?

---

<div class="post-metadata">

**Author:** ![kckyeung](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@kckyeung](https://forum.polkadot.network/u/kckyeung)\
**Post date:** [September 22, 2022, 3:54am UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/2 "2022-09-22T03:54:57Z")

</div>

I suppose I’m still not clear on what use-case that deriving an account is supposed to solve. Aside from `MultiLocation`s coming from the relay chain or parachains, is it not enough to simply extract the `AccountKey32` from a `MultiLocation` containing the `AccountKey32` junction?

---

<div class="post-metadata">

**Author:** ![girazoki](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@girazoki](https://forum.polkadot.network/u/girazoki)\
**Post date:** [September 22, 2022, 8:16am UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/3 "2022-09-22T08:16:25Z")

</div>

The use case we are trying to achieve:

Parachain A sends a message to Parachain B with `DescendOrigin` as first instruction, which would convert the origin from `*MultiLocation {parents: 1, interior: X1(Parachain(para_a_id))}*` to `MultiLocation {parents: 1, interior: X2(Parachain(para_a_id), AccountKey32(para_a_account))}`. This allows parachain a to have “more sovereign accounts” other than the main one.

The problem I see if parachain B extracts `para_a_account` from AccountKey32 is that parachain A would be able to control any account it wants in parachain B (or in any other parachain that uses the same multilocation to account conversion). While hashing the multilocation and converting it into an account ensures isolation. We are guaranteed that if one parachain gets compromised, it can only control its sovereign accounts, but not others

---

<div class="post-metadata">

**Author:** ![girazoki](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@girazoki](https://forum.polkadot.network/u/girazoki)\
**Post date:** [September 22, 2022, 8:18am UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/4 "2022-09-22T08:18:26Z")

</div>

The DescendOrigin as first instruction seems to be what pallet-xcm is doing when using the send extrinsic and the origin is not root: [polkadot/lib.rs at e2da6e5369b0b1a2dd7524bfef03ec269dbc7ec6 · paritytech/polkadot · GitHub](https://github.com/paritytech/polkadot/blob/e2da6e5369b0b1a2dd7524bfef03ec269dbc7ec6/xcm/pallet-xcm/src/lib.rs#L1066)

---

<div class="post-metadata">

**Author:** ![kckyeung](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@kckyeung](https://forum.polkadot.network/u/kckyeung)\
**Post date:** [September 23, 2022, 4:42pm UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/5 "2022-09-23T16:42:31Z")

</div>

> [@girazoki](#):
>
> This allows parachain a to have “more sovereign accounts” other than the main one.

I don’t believe so? Just by reading what the origin of `../Parachain(para_a_id)/AccountId32(para_a_account)` represents, it’s simply a less privileged account than the sovereign account of parachain A (i.e. a less privileged origin than `../Parachain(para_a_id)`).

> [@girazoki](#):
>
> The problem I see if parachain B extracts `para_a_account` from AccountKey32 is that parachain A would be able to control any account it wants in parachain B

Not really. Recall that `DescendOrigin` changes the _origin_, and, if I’m not mistaken, always to a lower privileged one. In other words, `DescendOrigin` won’t ever allow you to attach an `AccountId32` junction to it that contains parachain B’s account ID – it would always be understood as the account ID of parachain A, because parachain is the _origin_ of the XCM.

---

<div class="post-metadata">

**Author:** ![girazoki](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@girazoki](https://forum.polkadot.network/u/girazoki)\
**Post date:** [September 26, 2022, 7:51am UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/6 "2022-09-26T07:51:21Z")

</div>

> [@kckyeung](#):
>
> I don’t believe so? Just by reading what the origin of `../Parachain(para_a_id)/AccountId32(para_a_account)` represents, it’s simply a less privileged account than the sovereign account of parachain A (i.e. a less privileged origin than `../Parachain(para_a_id)`).

Sorry, I used the term “sovereign” because I think by definition “sovereign” referts to an account controlled by one consensus system in another consensus system. But yeah I agree these would “less priviledged accounts”, but I dont yet have a name for them (maybe Multilocation-derived accounts?).

> [@kckyeung](#):
>
> Not really. Recall that `DescendOrigin` changes the _origin_, and, if I’m not mistaken, always to a lower privileged one. In other words, `DescendOrigin` won’t ever allow you to attach an `AccountId32` junction to it that contains parachain B’s account ID – it would always be understood as the account ID of parachain A, because parachain is the _origin_ of the XCM.

Let’s try to go back for a second. Our goal is that a multilocation of the form `../Parachain(para_a_id)/AccountId32(para_a_account)` translates to an account (obviously a less-priviledged account than the sovereign account) in parachain B. This account could be used to dispatch Transact operations, Withdraw tokens from it, etc.

The question here is, how do we transform a multilocation of the form `../Parachain(para_a_id)/AccountId32(para_a_account)` into a parachain B account? Ideally we want to maintain isolation, meaning `../Parachain(para_a_id)/AccountId32(para_a_account)` and `../Parachain(para_c_id)/AccountId32(para_c_account)` can never be mapped the same parachain B account. We believe hashing the multilocation as a whole and then converting it into an accountId32 guarantees uniqueness, while dispatching directly from the accountId32 set in the junction does not.

Is there anything I am missing?

---

<div class="post-metadata">

**Author:** ![crystalin](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.polkadot.network/crystalin/32/343_2.png) [@crystalin](https://forum.polkadot.network/u/crystalin)\
**Post date:** [October 6, 2022, 2:37pm UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/7 "2022-10-06T14:37:57Z")

</div>

@xlc I’d be happy to get your opinion on it too. Is having a standard for managing those multilocation something you would be interested to follow too ?

---

<div class="post-metadata">

**Author:** ![xlc](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.polkadot.network/xlc/32/690_2.png) [@xlc](https://forum.polkadot.network/u/xlc)\
**Post date:** [October 7, 2022, 1:42am UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/8 "2022-10-07T01:42:03Z")

</div>

Yeah I had similar thought about this before. This is a must have feature to allow non-signed-origin to use XCM with all the parachains without chain specific modification.

For example, a common use case is for Polkadot Council to be able to control some assets on another parachain, for whatever reason. Right now the dest parachain have to make some additional configuration to support the council origin from relaychain and eventually map it to an `Account` type to hold the asset. With a standard way to map `MultiLocation` to `Account`, then no additional work required to support it. The source parachain can come up whatever `MultiLocation` as they need, and it just work on all other parachain that supports this standard.

---

<div class="post-metadata">

**Author:** ![Dino](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.polkadot.network/dino/32/564_2.png) [@Dino](https://forum.polkadot.network/u/Dino)\
**Post date:** [February 17, 2023, 10:01am UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/9 "2023-02-17T10:01:28Z")

</div>

Has this discussion continued in some other place perhaps? 🙂

We’ve had the `Account32Hash` conversion enabled for some time on our testnet (Shibuya) but are hesitant to put it on the production network yet since it doesn’t seem like _future-proof_ approach.

With the introduction of **XCM v3** , the derived account will no longer be same as in **v2** I guess.

Function looks like this:

```auto
fn convert_ref(location: impl Borrow<MultiLocation>) -> Result<AccountId, ()> {
	Ok(("multiloc", location.borrow()).using_encoded(blake2_256).into())
}

```

And `Account32Id` has changed between **v2** and **v3** :

- **v2** : `AccountId32 { network: NetworkId, id: [u8; 32] }`
- **v3** : `AccountId32 { network: Option<NetworkId>, id: [u8; 32] }`

---

<div class="post-metadata">

**Author:** ![girazoki](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@girazoki](https://forum.polkadot.network/u/girazoki)\
**Post date:** [February 17, 2023, 10:21am UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/10 "2023-02-17T10:21:52Z")

</div>

Hi Dino, I think it would be OK to just convert from V3 multilocation to V2 multilocation before you do the hashing.

However there is an ongoing new implementation here: [XCM: Remote account converter by mustermeiszer · Pull Request #6662 · paritytech/polkadot · GitHub](https://github.com/paritytech/polkadot/pull/6662), which should make things easier

---

<div class="post-metadata">

**Author:** ![Dino](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.polkadot.network/dino/32/564_2.png) [@Dino](https://forum.polkadot.network/u/Dino)\
**Post date:** [February 17, 2023, 1:25pm UTC](https://forum.polkadot.network/t/multilocation-based-account-derivation/436/11 "2023-02-17T13:25:24Z")

</div>

Hey @girazoki,

> [@girazoki](#):
>
> I think it would be OK to just convert from V3 multilocation to V2 multilocation before you do the hashing.

Agree, it’s not much work to keep it backwards compatible but I’d like us to avoid having to do that in the future.

Wasn’t aware of the PR you linked, thanks a lot!  
Just what I was looking for 🙂
