From 028d3cd0cea102fca435ab90e978804cd7135cd3 Mon Sep 17 00:00:00 2001 From: Demilade Sonuga Date: Wed, 4 Dec 2024 14:03:12 +0100 Subject: [PATCH] Added serde support --- .github/workflows/dusk_ci.yml | 4 +- Cargo.toml | 4 + src/lib.rs | 3 + src/serde_support.rs | 154 ++++++++++++++++++++++++++++++++++ tests/serde.rs | 62 ++++++++++++++ 5 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 src/serde_support.rs create mode 100644 tests/serde.rs diff --git a/.github/workflows/dusk_ci.yml b/.github/workflows/dusk_ci.yml index d2796a6..6c655ab 100644 --- a/.github/workflows/dusk_ci.yml +++ b/.github/workflows/dusk_ci.yml @@ -26,10 +26,10 @@ jobs: name: Nightly std tests uses: dusk-network/.github/.github/workflows/run-tests.yml@main with: - test_flags: --features=rkyv-impl,rkyv/size_16 + test_flags: --features=rkyv-impl,rkyv/size_16,serde test_parallel: name: Nightly std tests parallel uses: dusk-network/.github/.github/workflows/run-tests.yml@main with: - test_flags: --features=parallel,rkyv-impl,rkyv/size_16 + test_flags: --features=parallel,rkyv-impl,rkyv/size_16,serde diff --git a/Cargo.toml b/Cargo.toml index e072e9d..a5b013d 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", optional = true } +bs58 = { version = "0.4" , optional = true } +serde_json = { version = "1.0", 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..a0630cf --- /dev/null +++ b/src/serde_support.rs @@ -0,0 +1,154 @@ +// 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 alloc::format; +use alloc::string::String; + +use bs58; +use dusk_bytes::Serializable; +use serde::de::Error as SerdeError; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use crate::{ + MultisigPublicKey, MultisigSignature, PublicKey, SecretKey, Signature, +}; + +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 decoded = + bs58::decode(&s).into_vec().map_err(SerdeError::custom)?; + let decoded_len = decoded.len(); + let byte_length_str = format!("{}", Self::SIZE); + let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| { + SerdeError::invalid_length(decoded_len, &byte_length_str.as_str()) + })?; + PublicKey::from_bytes(&bytes) + .map_err(|err| SerdeError::custom(format!("{err:?}"))) + } +} + +impl Serialize for MultisigPublicKey { + fn serialize( + &self, + serializer: S, + ) -> Result { + let s = bs58::encode(self.to_bytes()).into_string(); + serializer.serialize_str(&s) + } +} + +impl<'de> Deserialize<'de> for MultisigPublicKey { + fn deserialize>( + deserializer: D, + ) -> Result { + let s = String::deserialize(deserializer)?; + let decoded = + bs58::decode(&s).into_vec().map_err(SerdeError::custom)?; + let decoded_len = decoded.len(); + let byte_length_str = format!("{}", Self::SIZE); + let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| { + SerdeError::invalid_length(decoded_len, &byte_length_str.as_str()) + })?; + MultisigPublicKey::from_bytes(&bytes) + .map_err(|err| SerdeError::custom(format!("{err:?}"))) + } +} + +impl Serialize for Signature { + fn serialize( + &self, + serializer: S, + ) -> Result { + let s = bs58::encode(self.to_bytes()).into_string(); + serializer.serialize_str(&s) + } +} + +impl<'de> Deserialize<'de> for Signature { + fn deserialize>( + deserializer: D, + ) -> Result { + let s = String::deserialize(deserializer)?; + let decoded = + bs58::decode(&s).into_vec().map_err(SerdeError::custom)?; + let decoded_len = decoded.len(); + let byte_length_str = format!("{}", Self::SIZE); + let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| { + SerdeError::invalid_length(decoded_len, &byte_length_str.as_str()) + })?; + Signature::from_bytes(&bytes) + .map_err(|err| SerdeError::custom(format!("{err:?}"))) + } +} + +impl Serialize for MultisigSignature { + fn serialize( + &self, + serializer: S, + ) -> Result { + let s = bs58::encode(self.to_bytes()).into_string(); + serializer.serialize_str(&s) + } +} + +impl<'de> Deserialize<'de> for MultisigSignature { + fn deserialize>( + deserializer: D, + ) -> Result { + let s = String::deserialize(deserializer)?; + let decoded = + bs58::decode(&s).into_vec().map_err(SerdeError::custom)?; + let decoded_len = decoded.len(); + let byte_length_str = format!("{}", Self::SIZE); + let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| { + SerdeError::invalid_length(decoded_len, &byte_length_str.as_str()) + })?; + MultisigSignature::from_bytes(&bytes) + .map_err(|err| SerdeError::custom(format!("{err:?}"))) + } +} + +impl Serialize for SecretKey { + fn serialize( + &self, + serializer: S, + ) -> Result { + let s = bs58::encode(self.to_bytes()).into_string(); + serializer.serialize_str(&s) + } +} + +impl<'de> Deserialize<'de> for SecretKey { + fn deserialize>( + deserializer: D, + ) -> Result { + let s = String::deserialize(deserializer)?; + let decoded = + bs58::decode(&s).into_vec().map_err(SerdeError::custom)?; + let decoded_len = decoded.len(); + let byte_length_str = format!("{}", Self::SIZE); + let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| { + SerdeError::invalid_length(decoded_len, &byte_length_str.as_str()) + })?; + SecretKey::from_bytes(&bytes) + .map_err(|err| SerdeError::custom(format!("{err:?}"))) + } +} diff --git a/tests/serde.rs b/tests/serde.rs new file mode 100644 index 0000000..1826e9c --- /dev/null +++ b/tests/serde.rs @@ -0,0 +1,62 @@ +// 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. + +#![cfg(feature = "serde")] + +use bls12_381_bls::{MultisigPublicKey, PublicKey, SecretKey}; +use rand::rngs::StdRng; +use rand::SeedableRng; + +#[test] +fn public_key() { + let mut rng = StdRng::seed_from_u64(0xbeef); + 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()); +} + +#[test] +fn multisig_public_key() { + let mut rng = StdRng::seed_from_u64(0xbeef); + let pk = MultisigPublicKey::aggregate(&[PublicKey::from( + &SecretKey::random(&mut rng), + )]) + .unwrap(); + let ser = serde_json::to_string(&pk); + let deser = serde_json::from_str(&ser.unwrap()); + assert_eq!(pk, deser.unwrap()); +} + +#[test] +fn signature() { + let mut rng = StdRng::seed_from_u64(0xbeef); + let sk = SecretKey::random(&mut rng); + let signature = sk.sign(b"a message"); + let ser = serde_json::to_string(&signature).unwrap(); + let deser = serde_json::from_str(&ser).unwrap(); + assert_eq!(signature, deser); +} + +#[test] +fn multisig_signature() { + let mut rng = StdRng::seed_from_u64(0xbeef); + let sk = SecretKey::random(&mut rng); + let pk = PublicKey::from(&sk); + let signature = sk.sign_multisig(&pk, b"a message"); + let ser = serde_json::to_string(&signature).unwrap(); + let deser = serde_json::from_str(&ser).unwrap(); + assert_eq!(signature, deser); +} + +#[test] +fn secret_key() { + let mut rng = StdRng::seed_from_u64(0xbeef); + let sk = SecretKey::random(&mut rng); + let ser = serde_json::to_string(&sk).unwrap(); + let deser = serde_json::from_str(&ser).unwrap(); + assert_eq!(sk, deser); +}