Skip to content

Commit

Permalink
Update account derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Jul 24, 2023
1 parent 0f55a29 commit da9ef98
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
2 changes: 1 addition & 1 deletion docs/build/environment/zombienet-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ The following steps will explain how to execute a cross-chain remote call. `Alic

1. Calculate `Alice's` derived account on `Shibuya` when sending instructions from `Shiden`.
```
> ./xcm-tools account32-hash -p 2007 -a 0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d -n kusama
> ./xcm-tools remote-account -p 2007 -a 0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d
5Cvcv8RvSsp6go2pQ8FRXcGLAzNp5eyC8Je7KLHz5zFwuUyT
```

Expand Down
54 changes: 41 additions & 13 deletions docs/xcm/building-with-xcm/xc-remote-transact.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The developer or user must ensure that the destination chain supports the encode

### XCM Sequence

At the moment, remote execution from origins other than parachain accounts are only allowed to be initiated by the `Shibuya` runtime (including RocStar).
At the moment, remote execution from origins other than parachain accounts are only allowed to be initiated by the `Shibuya` runtime (including RocStar) and `Shiden` runtime.

A permissible sequence of instructions will therefore have to start like:
1. `DescendOrigin`
Expand Down Expand Up @@ -52,7 +52,7 @@ Call data can be virtually anything supported by the remote chain - it doesn't m

### Remote Wasm Smart Contract Execution

For all our runtimes supporting Wasm smart sontracts & remote transaction via XCM, users are able to fully utilize contract uploads, instantiation, and most important - calls. No special approach or setup is needed since Wasm smart contracts are native to Astar chains. Sending a remote Wasm smart contract call is the same as executing any other remote transaction.
For all our runtimes supporting Wasm smart contracts & remote transaction via XCM, users are able to fully utilize contract instantiation & calls. No special approach or setup is needed since Wasm smart contracts are native to Astar chains. Sending a remote Wasm smart contract call is the same as executing any other remote transaction.

You can prepare the call within the `polkadot.js portal`(see image below), or custom code using the [polkadot-js/api-contract](https://github.com/polkadot-js/api/tree/46076c5595ab62e960a1097611a3e150bfa942f2/packages/api-contract) TypeScript library.

Expand All @@ -65,25 +65,49 @@ Until then, developers will need to rely on the aforementioned library, or the `
## Derived Remote Accounts

When executing a remote transaction, the remote chain will derive a new address based on the sender's multilocation.
The way this address is derived is determined by the chain itself.
The way this address is derived is determined by the chain itself so this documentation cannot guarantee how any other chain but Astar-based chains.
However, we

For Astar runtimes, a generalized approach, aligned with `Polkadot` and `Kusama` is used. A tuple like `("multiloc", sender_multilocation)` is SCALE encoded and hashed using the `Blake2_256` hasher, with the output being the derived address.

For example, let's assume `Alice` is sending an XCM sequence from `Polkadot` to `Astar`.
For Astar-based runtimes, a generalized approach, aligned with `Polkadot` and `Kusama` is used.
Multilocation is SCALE-encoded, step-by-step, until finally it is hashed using the `Blake2_256`, with the output being the derived address.

| Name | Value |
| ----------- | ----------- |
| Alice's Address in Polkadot | 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5 |
| Alice's Public Key | 0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d |
| Alice's MultiLoc in Astar | { parents: 1, interior: AccountId32 {network: NetworkId::Polkadot, id: 0xd4359...a27d } } |
| Alice's Derived Account in Astar | 5EbMT4dyi1ohgYVwLUvWAoCZCznHMT1Dk5B7GAaGJGcAMRaG |
| Alice's Multiloc in Astar | { parents: 1, interior: AccountId32 {network: NetworkId::Polkadot, id: 0xd4359...a27d } } |

---

For example, let's assume `Alice` is sending an XCM sequence from `AssetHub` to `Astar`.
Sibling parachain's account is derived using a simple approach:

`blake2_256(("SiblingChain", compact_parachain_id, b"AccountId32", public_key).encode())`

| Name | Value |
| ----------- | ----------- |
| Alice's MultiLoc in Astar | { parents: 1, interior: X2(Parachain(1000), AccountId32 {network: NetworkId::Polkadot, id: 0xd4359...a27d }) } |
| Alice's Derived Account | 0x88275533b5d43292c86d05985c3a6e226fee2baeddb4f3b90e30a70bec4d7bff |

Similarly, in case `Alice` is sending XCM from a chain that uses **H160** address format:

`blake2_256(("SiblingChain", compact_parachain_id, b"AccountKey20", public_key).encode())`

---

In case `Alice` is sending XCM from `Polkadot`, the encoded data changes a bit and would be like:

`blake2_256((b"ParentChain", b"AccountId32", public key).encode())`

| Name | Value |
| ----------- | ----------- |
| Alice's Multiloc in Astar | { parents: 1, interior: X1(AccountId32 {network: NetworkId::Polkadot, id: 0xd4359...a27d }) } |
| Alice's Derived Account | 0x7dcb1027ecb97011ebe79ca233def50d1f216eb05d76367c8984f67ccc5d2dd1 |

---

You can use the `xcm-tools` binary to generate the derived address, based on your needs.

:::caution
Be aware that derived accounts have been changed with the introduction of **XCM v3**.
This is because `AccountId32` type's `network` parameter has become an `Option<NetworkId>`.
:::

## Remotely Transact via EVM Smart Contracts

Expand Down Expand Up @@ -117,7 +141,11 @@ For example, let's assume you have a contract deployed on Shibuya and are callin
| Contract Derived SS58 Address | `agn53DdEuRgQsvgxqj5M1AecxB6LpbXT7T1R1hjVcoEBR6M` |
| SS58 Address Public Key | `0xd219fe1b02545c7dd7e718b1530b4e32b23288351f61e5975c7dc49b004ff119` |
| Caller Multilocation | `{ parents: 1, interior: X2 ( Parachain(2000), AccountId32 {network: NetworkId::Any, id: 0xd219f...f119 } ) }` |
| Derived Account32Hash Address | `5FrhDFydxUwbWyXT1XDBhRUUYpQtiJJ6skB6n2XV4NubC9fP` |
| Derived Remote Address | `5FrhDFydxUwbWyXT1XDBhRUUYpQtiJJ6skB6n2XV4NubC9fP` |

:::note
The remote address derivation scheme has changed after writing the initial example but the whole process is otherwise still valid and correct.
:::

This means that the instructions like `WithdrawAsset` and `Transact` will be executed as if origin was address `5FrhDFydxUwbWyXT1XDBhRUUYpQtiJJ6skB6n2XV4NubC9fP`.

Expand Down
37 changes: 12 additions & 25 deletions docs/xcm/integration/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ We've prepared an xcm-tools crate which provides an easy way for users to find a
For finding a parachain's sovereign account address on the Relay Chain, use the following command:

```bash
xcm-tools parachain-account 2006
./xcm-tools sovereign-account 2006
5Ec4AhPW97z4ZyYkd3mYkJrSeZWcwVv4wiANES2QrJi1x17F
```

Replace **2006** with the parachain Id you require.

For calculating a sibling parachain's sovereign account address, use the following command:
```bash
xcm-tools parachain-account -s 2006
./xcm-tools sovereign-account -s 2006
5Eg2fntKDrAxhaGuB3idrxCFu3BveuyB1MooVPYuj2jaoSsw
```

Expand All @@ -38,44 +38,31 @@ Replace 2006 with the parachain Id you require.

For calculating an XC20 EVM address, use the following command:
```bash
xcm-tools asset-id 42424242424242
./xcm-tools asset-id 42424242424242
pallet_assets: 42424242424242
EVM XC20: 0xffffffff000000000000000000002695a9e649b2
```

You can also input a standard asset Id (as seen by pallet-assets), and it will output the H160 address of that asset.

## Account32Hash
## Remote Account

For calculating the `Account32Hash`, see ([here](https://github.com/paritytech/polkadot/blob/master/xcm/xcm-builder/src/location_conversion.rs#L25)), we have provide a dedicated command. However, the possible `MultiLocation`s format is limited to:
For calculating the remote account, see ([here](https://github.com/paritytech/polkadot/blob/master/xcm/xcm-builder/src/location_conversion.rs#L25)), we have provide a dedicated command. However, the possible `MultiLocation`s format is limited to:

1. `{ parents: 1, interior: X1(AccountId32{ network: <network_id>, id: 0x<id>}) }`
2. `{ parents: 1, interior: X2(Parachain(<parachain_id>), AccountId32{ network: <network_id>, id: 0x<id>}) }`
1. `{ parents: 1, interior: X1(AccountId32{ network: _, id: 0x<id>}) }`
2. `{ parents: 1, interior: X2(Parachain(<parachain_id>), AccountId32{ network: _, id: 0x<id>}) }`
2. `{ parents: 1, interior: X2(Parachain(<parachain_id>), AccountKey20{ network: _, key: 0x<id>}) }`

For the first case, use the following command:
```bash
xcm-tools account32-hash -a 0x84746218b9858233f45f99d742aa3ea2f31aeb5a525938f240fdee3000000000
5EKFVedcKtGBPytwgj9sbCTmjSr8wJTmhHuuWTz9RhMRrv1h
./xcm-tools remote-account -a 0x84746218b9858233f45f99d742aa3ea2f31aeb5a525938f240fdee3000000000
5H2dw5K45MfT4dwB7u924MYFASzGoWvACzKuMo3TdgJRkg2R
```
The value under `-a` is a SS58 public key.

For the second case, use the following command:
```bash
xcm-tools account32-hash -p 1000 -a 0x84746218b9858233f45f99d742aa3ea2f31aeb5a525938f240fdee3000000000
5G9fvUEXj93MBeaAzzXQh4RpsTygPssbrVcpuWCzE9u8K4KD
./xcm-tools remote-account -p 1000 -a 0x84746218b9858233f45f99d742aa3ea2f31aeb5a525938f240fdee3000000000
5FkWm28hUM7XC9qvrS3w4RP38wCgajfvFpqyfjeTSVxShdzC
```
The value under `-p` is parachain Id, while `-a` is again the public key.


Supported `network_id` or `-n` values are either `any`, `kusama` or `polkadot`.
This is set by the chain from which the XCM sequence originated.
For our runtimes:
* In case of `Astar`, use `polkadot`
* In case of `Shiden`, use `kusama`
* In case of `Shibuya` (or `RocStar`) use `any`

:::note
By default, if no value is specified, `any` will be used.
Make sure to set the correct network Id based on the chain you're using,
otherwise the address generated by the tool won't match the one generated on-chain.
:::

0 comments on commit da9ef98

Please sign in to comment.