Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The right way to use Multiaddress #225

Closed
boundless-forest opened this issue Aug 16, 2021 · 3 comments
Closed

The right way to use Multiaddress #225

boundless-forest opened this issue Aug 16, 2021 · 3 comments

Comments

@boundless-forest
Copy link

boundless-forest commented Aug 16, 2021

Although paritytech/substrate#7380 has been merged for a long time, I found there are few docs that talks about how to use this feature. Some questions I have:

  1. Is this feature fully developed or remaining some works to do?
  2. Are there any demos or docs that show how it integrates with evm pallet?

@shawntabrizi Maybe you can help me.

@nuke-web3
Copy link
Contributor

I see in the present Frontier template the Address is set:

pub type Address = sp_runtime::MultiAddress<AccountId, ()>;

and in the EVM pallet config:

type AddressMapping = HashedAddressMapping<BlakeTwo256>;

Are you looking to convert the latter to use the MultiAddress type? If so, this could look much closer to the https://docs.moonbeam.network/learn/unified-accounts/ structure for users, where the conversions/mappings could be done "under the hood" for users.

@shawntabrizi
Copy link
Member

MultiAddress is just an encoding format.

It means that you can use the first two bytes of the encoded MultiAddress to determine which of the address types it actually is.

To use it, you basically just stick a match statement wherever you use this type.

like this:

impl<T: Config> StaticLookup for Module<T>
where
	MultiAddress<T::AccountId, T::AccountIndex>: Codec,
{
	type Source = MultiAddress<T::AccountId, T::AccountIndex>;
	type Target = T::AccountId;

	fn lookup(a: Self::Source) -> Result<Self::Target, LookupError> {
		match a {
			MultiAddress::Id(id) => Ok(id),
			MultiAddress::Address32(hash) => {
				Lookup::<T>::get(hash).ok_or(LookupError)
			},
			_ => Err(LookupError),
		}
	}

	fn unlookup(a: Self::Target) -> Self::Source {
		MultiAddress::Id(a)
	}
}

So in any pallet where you use T::AccountId as an input, you should instead use <T::Lookup as StaticLookup>::Source and then resolve the address format with T::Lookup::lookup(who)?.

Stuff like that.

@nuke-web3
Copy link
Contributor

nuke-web3 commented Mar 19, 2022

https://substrate.stackexchange.com/ could be a good place to resurface some of this for some rep 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants