5

Given a hex representation: 0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d, we can get the AccountId it represents using keyring.encodeAddress() using JavaScript. However, what is the corresponding function in Rust?

AccountId is the address of the Substrate user's address. For eg, 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY is the account id of Alice, from the Substrate's dev chain.

Aviral Srivastava
  • 4,058
  • 8
  • 29
  • 81
  • Anyway look at [hex](https://crates.io/crates/hex) – Stargateur Apr 02 '20 at 03:58
  • @Stargateur I used the keywords known in the Substrate world and hence didn't feel the need to elaborate on it. Anyways, I have added the relevant hyperlink. Is it clear now? – Aviral Srivastava Apr 02 '20 at 11:18
  • @Stargateur you suggestion of `hex` won't work, getting the error of "Odd length". This hex value is scale coded. I need to decode it in that manner. Can you help me with that? Also, if the question is clear now, can you remove the downvote so that it can reach more audience? – Aviral Srivastava Apr 02 '20 at 11:30
  • The hex address does not have any special encoding that would stop it from turning into bytes. Odd length error may be related to keeping `0x` at the front, or forgetting to copy all the characters in the string. – Shawn Tabrizi Apr 02 '20 at 12:03
  • 1
    "I used the keywords known in the Substrate world and hence didn't feel the need to elaborate on it." not every one know substrate in fact I expect very few people know it, whereas the number of people knowing rust is much higher. You will increase the quality and the chance of getting a good answer if you describe what you want/need explicitly, then people who only know rust could make a good answer. Because for example, I will not read all the substate doc just to answer you. Your question is still unclean cause we still don't know how your hex value work. your example come from nowhere – Stargateur Apr 02 '20 at 12:51
  • @Stargateur yes, you are right. Apologies. – Aviral Srivastava Apr 02 '20 at 14:04

1 Answers1

4

Within rust, you should not really start with the hex representation, you want to work with bytes.

But assuming you have hex, you can convert a hex string to AccountId bytes using the hex_literal::hex macro:

let account: AccountId32 = hex_literal::hex!["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"].into(),

Note that 0x is omitted from the hex literal.

Now you should have [u8; 32] wrapped in the AccountId32 identity struct.

From there, you can simply do the same logic as done in the implementation for Display for AccountId32:

#[cfg(feature = "std")]
impl std::fmt::Display for AccountId32 {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.to_ss58check())
    }
}

Basically the Address is the ss58 encoded version of the Account Id bytes.

The ss58 codec library can be found here: https://substrate.dev/rustdocs/master/sp_core/crypto/trait.Ss58Codec.html

Shawn Tabrizi
  • 12,206
  • 1
  • 38
  • 69
  • Can't I use the Display directly as `account` is `AccountId32` type for which `Display` is already implemented? – Aviral Srivastava Apr 02 '20 at 12:21
  • Yes, you could directly do `println!("{}", account)` I think – Shawn Tabrizi Apr 02 '20 at 12:31
  • `let account: AccountId32 = hex_literal::hex!["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"].into(); let ss58_account = account.to_ss58check();` I got an error:`method not found in AccountId32` I go to the definition, only find `#[cfg(feature = "std")] impl Ss58Codec for AccountId32 {}`. Does `Ss58Codec` only work under `std`? – gfan Jan 14 '22 at 10:00