Skip to content

Commit

Permalink
account id
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Oct 21, 2023
1 parent da03071 commit 5581d8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/curr/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//#
//# ## Strkey Types
//# - PublicKey
//# - PublicKey
//# - AccountId
//# - MuxedAccount
//# - MuxedAccountMed25519
//# - SignerKey
Expand All @@ -20,7 +20,7 @@
//# - AssetCode4
//# - AssetCode12
#![cfg(feature = "std")]
use super::{Error, PublicKey, Uint256};
use super::{AccountId, Error, PublicKey, Uint256};

impl From<stellar_strkey::DecodeError> for Error {
fn from(_: stellar_strkey::DecodeError) -> Self {
Expand Down Expand Up @@ -51,3 +51,16 @@ impl core::str::FromStr for PublicKey {
Ok(PublicKey::PublicKeyTypeEd25519(Uint256(k)))
}
}

impl core::fmt::Display for AccountId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.fmt(f)
}
}

impl core::str::FromStr for AccountId {
type Err = Error;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
Ok(AccountId(PublicKey::from_str(s)?))
}
}
20 changes: 19 additions & 1 deletion tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use stellar_xdr::curr as stellar_xdr;
#[cfg(feature = "next")]
use stellar_xdr::next as stellar_xdr;

use stellar_xdr::{PublicKey, Uint256};
use stellar_xdr::{AccountId, PublicKey, Uint256};

#[test]
fn public_key_from_str() {
Expand All @@ -27,3 +27,21 @@ fn public_key_to_string() {
"GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF"
);
}

#[test]
fn account_id_from_str() {
let v = AccountId::from_str("GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF");
assert_eq!(
v,
Ok(AccountId(PublicKey::PublicKeyTypeEd25519(Uint256([0; 32]))))
);
}

#[test]
fn account_id_to_string() {
let s = AccountId(PublicKey::PublicKeyTypeEd25519(Uint256([0; 32]))).to_string();
assert_eq!(
s,
"GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF"
);
}

0 comments on commit 5581d8b

Please sign in to comment.