// 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(())
}