Skip to content

Commit

Permalink
from_str for address
Browse files Browse the repository at this point in the history
  • Loading branch information
bodrych committed Dec 8, 2023
1 parent eafacc9 commit d969d25
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/model/account/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ impl fmt::Debug for Address {
}
}

impl std::str::FromStr for Address {
type Err = Error;

fn from_str(base58string: &str) -> Result<Address> {
Ok(Address {
bytes: Base58::decode(base58string)?,
})
}
}

impl Address {
pub fn from_public_key(chain_id: u8, public_key: &PublicKey) -> Result<Address> {
Ok(Address {
Expand Down Expand Up @@ -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() {
Expand All @@ -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";
Expand Down

0 comments on commit d969d25

Please sign in to comment.