From d969d25e4583fb56d554ffbd4eccd4b480affccf Mon Sep 17 00:00:00 2001 From: Artem Badrtdinov Date: Fri, 8 Dec 2023 18:18:23 +0500 Subject: [PATCH] from_str for address --- src/model/account/address.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/model/account/address.rs b/src/model/account/address.rs index e284ef3..39f4e94 100644 --- a/src/model/account/address.rs +++ b/src/model/account/address.rs @@ -17,6 +17,16 @@ impl fmt::Debug for Address { } } +impl std::str::FromStr for Address { + type Err = Error; + + fn from_str(base58string: &str) -> Result
{ + Ok(Address { + bytes: Base58::decode(base58string)?, + }) + } +} + impl Address { pub fn from_public_key(chain_id: u8, public_key: &PublicKey) -> Result
{ Ok(Address { @@ -72,6 +82,7 @@ mod tests { use crate::model::{ByteString, ChainId}; use serde_json::Value; use std::borrow::Borrow; + use std::str::FromStr; #[test] fn test_address_from_public_key() { @@ -97,6 +108,13 @@ mod tests { assert_eq!(expected_address, address.encoded()) } + #[test] + fn test_address_std_from_str() { + let expected_address = "3MtQQX9NwYH5URGGcS2e6ptEgV7wTFesaRW"; + let address = Address::from_str(expected_address).expect("failed to get address from string"); + assert_eq!(expected_address, address.encoded()) + } + #[test] fn test_address_from_json() -> Result<()> { let expected_address = "3MtQQX9NwYH5URGGcS2e6ptEgV7wTFesaRW";