From 81a160001012ec09de3a717889501a4961dae3ea Mon Sep 17 00:00:00 2001 From: Demilade Sonuga Date: Mon, 25 Nov 2024 17:32:30 +0100 Subject: [PATCH] Added serde feature to enable serde `Serialize` and `Deserialize` for execution-core --- Cargo.toml | 4 ++++ src/lib.rs | 3 +++ src/serde_support.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++ tests/keys.rs | 10 ++++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/serde_support.rs diff --git a/Cargo.toml b/Cargo.toml index e072e9d..5daa846 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,9 @@ zeroize = { version = "1", default-features = false, features = ["derive"] } rkyv = { version = "0.7", optional = true, default-features = false } bytecheck = { version = "0.6", optional = true, default-features = false } rayon = { version = "1.8", optional = true } +serde = { version = "1.0.210", optional = true } +bs58 = { version = "0.4.0" , optional = true } +serde_json = { version = "1.0.128", optional = true } [dev-dependencies] rand = "0.8" @@ -34,3 +37,4 @@ rkyv-impl = [ "bytecheck", ] parallel = ["dep:rayon"] +serde = ["dep:serde", "bs58", "serde_json"] diff --git a/src/lib.rs b/src/lib.rs index f71e1f8..89c77c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,9 @@ pub use keys::{ }; pub use signatures::{MultisigSignature, Signature}; +#[cfg(feature = "serde")] +mod serde_support; + #[cfg(feature = "rkyv-impl")] pub use crate::keys::{ public::{ diff --git a/src/serde_support.rs b/src/serde_support.rs new file mode 100644 index 0000000..13603ae --- /dev/null +++ b/src/serde_support.rs @@ -0,0 +1,46 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// +// Copyright (c) DUSK NETWORK. All rights reserved. + +extern crate alloc; + +use crate::keys::public::PublicKey; +use alloc::format; +use alloc::string::String; +use bs58; +use dusk_bytes::Serializable; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +impl Serialize for PublicKey { + fn serialize( + &self, + serializer: S, + ) -> Result { + let s = bs58::encode(self.to_bytes()).into_string(); + serializer.serialize_str(&s) + } +} + +impl<'de> Deserialize<'de> for PublicKey { + fn deserialize>( + deserializer: D, + ) -> Result { + let s = String::deserialize(deserializer)?; + let mut bytes: [u8; Self::SIZE] = [0; Self::SIZE]; + match bs58::decode(&s).into(&mut bytes) { + Ok(n) => { + if n != Self::SIZE { + return Err(de::Error::custom( + "failed to deserialize AccountPublicKey", + )); + } + } + Err(err) => return Err(de::Error::custom(format!("{err:?}"))), + } + let pubk = PublicKey::from_bytes(&bytes) + .map_err(|err| de::Error::custom(format!("{err:?}")))?; + Ok(pubk) + } +} diff --git a/tests/keys.rs b/tests/keys.rs index 99ee52a..9f7ab81 100644 --- a/tests/keys.rs +++ b/tests/keys.rs @@ -59,3 +59,13 @@ fn apk_identity_fails() { Error::InvalidPoint ); } + +#[cfg(feature = "serde")] +#[test] +fn pk_serde() { + let mut rng = StdRng::seed_from_u64(42); + let pk = PublicKey::from(&SecretKey::random(&mut rng)); + let ser = serde_json::to_string(&pk); + let deser = serde_json::from_str(&ser.unwrap()); + assert_eq!(pk, deser.unwrap()); +}