From c35425ba850839a946a682f1ac8f8f5ac8ea88cb Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Fri, 20 Oct 2023 23:50:08 -0700 Subject: [PATCH] Add str conversions for PublicKey --- Cargo.lock | 17 +++++++++++++++ Cargo.toml | 1 + src/curr/mod.rs | 2 ++ src/curr/str.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/str.rs | 29 ++++++++++++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 src/curr/str.rs create mode 100644 tests/str.rs diff --git a/Cargo.lock b/Cargo.lock index 687c65a9..afdf9a97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "base32" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23ce669cd6c8588f79e15cf450314f9638f967fc5770ff1c7c1deb0925ea7cfa" + [[package]] name = "base64" version = "0.13.0" @@ -468,6 +474,16 @@ dependencies = [ "syn 2.0.15", ] +[[package]] +name = "stellar-strkey" +version = "0.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0689070126ca7f2effc2c5726584446db52190f0cef043c02eb4040a711c11" +dependencies = [ + "base32", + "thiserror", +] + [[package]] name = "stellar-xdr" version = "20.0.0-rc1" @@ -480,6 +496,7 @@ dependencies = [ "serde", "serde_json", "serde_with", + "stellar-strkey", "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index 7a009d8f..ba942478 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/curr/mod.rs b/src/curr/mod.rs index a36c0299..8ab0180c 100644 --- a/src/curr/mod.rs +++ b/src/curr/mod.rs @@ -1,6 +1,8 @@ mod generated; pub use generated::*; +mod str; + mod scval_conversions; pub use scval_conversions::*; diff --git a/src/curr/str.rs b/src/curr/str.rs new file mode 100644 index 00000000..814a0746 --- /dev/null +++ b/src/curr/str.rs @@ -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 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 { + let stellar_strkey::ed25519::PublicKey(k) = + stellar_strkey::ed25519::PublicKey::from_str(s)?; + Ok(PublicKey::PublicKeyTypeEd25519(Uint256(k))) + } +} diff --git a/tests/str.rs b/tests/str.rs new file mode 100644 index 00000000..74ad469f --- /dev/null +++ b/tests/str.rs @@ -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" + ); +}