From ce490e2b3a6ba00b97200dbaad100e890f7a2d9d Mon Sep 17 00:00:00 2001 From: Qinxuan Chen Date: Thu, 23 Mar 2023 17:37:31 +0800 Subject: [PATCH] impl FromStr for AccountId20 (#1028) --- primitives/account/src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/primitives/account/src/lib.rs b/primitives/account/src/lib.rs index 820666caba..d1be0e8e29 100644 --- a/primitives/account/src/lib.rs +++ b/primitives/account/src/lib.rs @@ -19,6 +19,7 @@ use scale_codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; +// Substrate use sp_core::{ecdsa, H160, H256}; use sp_io::hashing::keccak_256; @@ -32,6 +33,17 @@ pub struct AccountId20(pub [u8; 20]); #[cfg(feature = "std")] impl_serde::impl_fixed_hash_serde!(AccountId20, 20); +#[cfg(feature = "std")] +impl std::str::FromStr for AccountId20 { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + H160::from_str(s) + .map(Into::into) + .map_err(|_| "invalid hex address.") + } +} + #[cfg(feature = "std")] impl std::fmt::Display for AccountId20 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {