Skip to content

Commit

Permalink
impl FromStr for AccountId20 (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
koushiro authored Mar 23, 2023
1 parent 736cef1 commit ce490e2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions primitives/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Self, Self::Err> {
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 {
Expand Down

0 comments on commit ce490e2

Please sign in to comment.