Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serde): add serde_with support for serialization #72

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rust-version = "1.81.0"
[features]
default = []
cli = ["dep:clap"]
serde_with = ["dep:serde_with"]

[[bin]]
name = "stellar-strkey"
Expand All @@ -24,8 +25,11 @@ doctest = false
crate-git-revision = "0.0.6"

[dev-dependencies]
proptest = "1.0.0"
proptest ="1.0.0"
serde = { version = "1.0.214", features = ["derive"] }
toml = "0.8.19"

[dependencies]
data-encoding = { version = "2.6.0", default-features = false, features = ["alloc"] }
clap = { version = "4.2.4", default-features = false, features = ["std", "derive", "usage", "help"], optional = true }
serde_with = { version = "3.11.0", optional = true }
16 changes: 16 additions & 0 deletions src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use core::{
};

#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "serde_with",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct PrivateKey(pub [u8; 32]);

impl Debug for PrivateKey {
Expand Down Expand Up @@ -66,6 +70,10 @@ impl FromStr for PrivateKey {
}

#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "serde_with",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct PublicKey(pub [u8; 32]);

impl Debug for PublicKey {
Expand Down Expand Up @@ -121,6 +129,10 @@ impl FromStr for PublicKey {
}

#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "serde_with",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct MuxedAccount {
pub ed25519: [u8; 32],
pub id: u64,
Expand Down Expand Up @@ -192,6 +204,10 @@ impl FromStr for MuxedAccount {
///
/// The payload must not have a size larger than u32::MAX.
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "serde_with",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct SignedPayload {
pub ed25519: [u8; 32],
pub payload: Vec<u8>,
Expand Down
16 changes: 16 additions & 0 deletions src/strkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ use crate::{
};

#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[cfg_attr(
feature = "serde_with",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub enum Strkey {
PublicKeyEd25519(ed25519::PublicKey),
PrivateKeyEd25519(ed25519::PrivateKey),
Expand Down Expand Up @@ -73,6 +77,10 @@ impl FromStr for Strkey {
}

#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "serde_with",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct PreAuthTx(pub [u8; 32]);

impl Debug for PreAuthTx {
Expand Down Expand Up @@ -125,6 +133,10 @@ impl FromStr for PreAuthTx {
}

#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "serde_with",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct HashX(pub [u8; 32]);

impl Debug for HashX {
Expand Down Expand Up @@ -177,6 +189,10 @@ impl FromStr for HashX {
}

#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "serde_with",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct Contract(pub [u8; 32]);

impl Debug for Contract {
Expand Down
18 changes: 17 additions & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,5 +443,21 @@ fn assert_convert_roundtrip(s: &str, strkey: &Strkey) {
let strkey_result = Strkey::from_string(s).unwrap();
assert_eq!(&strkey_result, strkey);
let str_result = format!("{strkey}");
assert_eq!(s, str_result)
assert_eq!(s, str_result);
#[cfg(feature = "serde_with")]
test_serde_with(strkey);
}

#[cfg(feature = "serde_with")]
fn test_serde_with(strkey: &Strkey) {
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct StrKey {
strkey: Strkey,
}
let str_key_s = toml::to_string(&StrKey {
strkey: strkey.clone(),
})
.unwrap();
let str_key: StrKey = toml::from_str(&str_key_s).unwrap();
assert_eq!(strkey, &str_key.strkey);
}
Loading