Skip to content

Commit

Permalink
feat(serde): add serde_with support for serialization
Browse files Browse the repository at this point in the history
Optionally provide serialization with serde_with. Since the types already provide `Display` and `FromStr` we can use `serde_with` to provide `serde` support. This will allow storing strkeys in config files with their types and not just `String`.
  • Loading branch information
willemneal authored and gitbutler-client committed Nov 4, 2024
1 parent 8f5fb5b commit 64952ae
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
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(s, strkey);
}

#[cfg(feature = "serde_with")]
fn test_serde_with(s: &str, 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);
}

0 comments on commit 64952ae

Please sign in to comment.