1

I am working on a Substrate runtime and would like to convert a string representation of an address, such as "5HYKElof8yaKoyCX9L...GHii4BkrgvKymz34W", into a Substrate AccountId so that I can use it as a key in a storage map.

For reference, I have a storage map defined as follows:

#[pallet::storage]
pub(super) type MyStorage<T: Config> = StorageMap<
    _,
    Blake2_128Concat,
    T::AccountId, // This is where I need the converted Substrate address
    u32,
    OptionQuery,
>;
6

2 Answers 2

1

We usually do not work with ss58 formats in a runtime environment. On some higher level they should be mapped into to your actual account id type. And utility functions for ss58 format, is not available for no std.

Sometimes that format might still look useful in runtime env, for example when you write a migration for a list of accounts, and listing them in [u8; 32] array format does not look nice. In this cases I usually use hex version of addresses (with some hex crate help). And I might provide a test which asserts that list with a list in ss58 format to make a review for such PR simpler.

-1

You can use:

AccountId32::from_str("55HYKElof8yaKoyCX9L...GHii4BkrgvKymz34W")

See this test in the Substrate repository: https://github.com/paritytech/polkadot-sdk/blob/master/substrate/primitives/core/src/crypto.rs#L1420C11-L1420C84

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.