Skip to content

Commit

Permalink
Add str conversions for PublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Oct 21, 2023
1 parent 313b59d commit c35425b
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ doctest = false
crate-git-revision = "0.0.6"

[dependencies]
stellar-strkey = { version = "0.0.7" }
base64 = { version = "0.13.0", optional = true }
serde = { version = "1.0.139", features = ["derive"], optional = true }
serde_with = { version = "3.0.0", optional = true }
Expand Down
2 changes: 2 additions & 0 deletions src/curr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod generated;
pub use generated::*;

mod str;

mod scval_conversions;
pub use scval_conversions::*;

Expand Down
55 changes: 55 additions & 0 deletions src/curr/str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#![cfg(feature = "std")]

/// Custom string representations of the following types, also used for JSON
/// formatting.
///
/// ## Strkey Types
/// - PublicKey
/// - PublicKey
/// - MuxedAccount
/// - MuxedAccountMed25519
/// - SignerKey
/// - SignerKeyEd25519SignedPayload
/// - NodeId
///
/// ## Asset Types
/// - Asset
/// - AlphaNum4
/// - AlphaNum12
///
/// ## ASCII Types
/// - AssetCode
/// - AssetCode4
/// - AssetCode12
use super::{Error, PublicKey, Uint256};

impl From<stellar_strkey::DecodeError> for Error {
fn from(_: stellar_strkey::DecodeError) -> Self {
// TODO: Add error type for strkeys.
Error::Invalid
}
}

impl core::fmt::Display for PublicKey {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
PublicKey::PublicKeyTypeEd25519(k) => {
let k = stellar_strkey::ed25519::PublicKey::from_payload(&k.0)
.map_err(|_| std::fmt::Error)?;
let s = k.to_string();
f.write_str(&s)?;
}
}
Ok(())
}
}

#[cfg(feature = "alloc")]
impl core::str::FromStr for PublicKey {
type Err = Error;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
let stellar_strkey::ed25519::PublicKey(k) =
stellar_strkey::ed25519::PublicKey::from_str(s)?;
Ok(PublicKey::PublicKeyTypeEd25519(Uint256(k)))
}
}
29 changes: 29 additions & 0 deletions tests/str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![cfg(all(
any(feature = "curr", feature = "next"),
not(all(feature = "curr", feature = "next"))
))]
#![cfg(feature = "std")]

use std::str::FromStr;

#[cfg(feature = "curr")]
use stellar_xdr::curr as stellar_xdr;
#[cfg(feature = "next")]
use stellar_xdr::next as stellar_xdr;

use stellar_xdr::{PublicKey, Uint256};

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

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

0 comments on commit c35425b

Please sign in to comment.