From d044f646bf285991069ffecea0c4a82cc0d1c4d8 Mon Sep 17 00:00:00 2001 From: John Smith Date: Thu, 29 Jun 2023 20:49:15 -0400 Subject: [PATCH] fix serialize for keypair --- veilid-core/src/crypto/mod.rs | 3 +-- .../crypto/{ => types}/byte_array_types.rs | 0 veilid-core/src/crypto/types/keypair.rs | 26 +++++++++++++++++-- veilid-core/src/crypto/types/mod.rs | 2 ++ veilid-python/veilid/json_api.py | 2 ++ 5 files changed, 29 insertions(+), 4 deletions(-) rename veilid-core/src/crypto/{ => types}/byte_array_types.rs (100%) diff --git a/veilid-core/src/crypto/mod.rs b/veilid-core/src/crypto/mod.rs index 2ab7e410..07c02a37 100644 --- a/veilid-core/src/crypto/mod.rs +++ b/veilid-core/src/crypto/mod.rs @@ -1,5 +1,4 @@ mod blake3digest512; -mod byte_array_types; mod dh_cache; mod envelope; mod receipt; @@ -13,7 +12,7 @@ pub mod tests; pub mod vld0; pub use blake3digest512::*; -pub use byte_array_types::*; + pub use crypto_system::*; pub use dh_cache::*; pub use envelope::*; diff --git a/veilid-core/src/crypto/byte_array_types.rs b/veilid-core/src/crypto/types/byte_array_types.rs similarity index 100% rename from veilid-core/src/crypto/byte_array_types.rs rename to veilid-core/src/crypto/types/byte_array_types.rs diff --git a/veilid-core/src/crypto/types/keypair.rs b/veilid-core/src/crypto/types/keypair.rs index fc53af44..d17a12a0 100644 --- a/veilid-core/src/crypto/types/keypair.rs +++ b/veilid-core/src/crypto/types/keypair.rs @@ -3,8 +3,7 @@ use super::*; #[derive( Clone, Copy, - Serialize, - Deserialize, + Default, PartialOrd, Ord, PartialEq, @@ -87,3 +86,26 @@ impl TryFrom<&str> for KeyPair { Self::try_decode(value) } } + +impl serde::Serialize for KeyPair { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let s = self.encode(); + serde::Serialize::serialize(&s, serializer) + } +} + +impl<'de> serde::Deserialize<'de> for KeyPair { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let s = ::deserialize(deserializer)?; + if s == "" { + return Ok(KeyPair::default()); + } + KeyPair::try_decode(s.as_str()).map_err(serde::de::Error::custom) + } +} diff --git a/veilid-core/src/crypto/types/mod.rs b/veilid-core/src/crypto/types/mod.rs index 75d94d18..af03453d 100644 --- a/veilid-core/src/crypto/types/mod.rs +++ b/veilid-core/src/crypto/types/mod.rs @@ -41,10 +41,12 @@ pub fn common_crypto_kinds(a: &[CryptoKind], b: &[CryptoKind]) -> Vec