0

Can somebody please guide me how to convert public hex key (which is in String format) to T::AccountId?

I have an extrinsic, which is accepting public key 0x307832363438323861303334393166336234666530316165643XXXXXXXXXX in String.

I have to decode it into AccountId. Can somebody please help me to fix this?

1 Answer 1

1
// pallet/lib.rs
trait Config: frame_system::Config {
  type AccountIdFromHex: AccountIdFromHex<AccountId = Self::AccountId>;
  ..
}

trait AccountIdFromHex {
  type AccountId;

  fn try_from_hex(hex: Vec<u8>) -> Result<Self::AccountId>;
}

// runtime/lib.rs
impl Config for Runtime {
  type AccountIdFromHex = Converter;
  ..
}

struct Converter;
impl AccountIdFromHex for Converter {
  type AccountId = AccountId32;

  fn from_hex(hex: Vec<u8>) -> Result<Self::AccountId> {
    array_bytes::vec_n_into::<_, _, 32>(hex).map_err(..)
  }
}

Tips:


If lost the generic ability for your pallet is acceptable. Simply do:

trait Config: frame_system::Config<AccountId = AccountId32> {
  ..
}

#[pallet::call]
fn your_extrinsic(hex: Vec<u8>) -> Result<..> {
  let account_id = array_bytes::vec_n_into::<_, T::AccountId, 32>(hex).map_err(..)?;

  Ok(())
}

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.