Skip to content

Commit

Permalink
Added serde support
Browse files Browse the repository at this point in the history
  • Loading branch information
d-sonuga committed Dec 5, 2024
1 parent 256fa23 commit 035db06
Show file tree
Hide file tree
Showing 5 changed files with 438 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dusk_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
uses: dusk-network/.github/.github/workflows/code-analysis.yml@main
with:
clippy_default: false
clippy_args: --features=rkyv/size_32,zk
clippy_args: --features=rkyv/size_32,zk,serde

dusk_analyzer:
name: Dusk Analyzer
Expand All @@ -26,7 +26,7 @@ jobs:
name: Nightly tests
uses: dusk-network/.github/.github/workflows/run-tests.yml@main
with:
test_flags: --features=zk,alloc
test_flags: --features=zk,alloc,serde

test_nightly_multisig_compiles:
name: Nightly multisig tests compile
Expand Down
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 }
dusk-plonk = { version = "0.20", default-features = false, features = ["alloc"], optional = true }
serde = { version = "1.0", optional = true }
bs58 = { version = "0.4" , optional = true }
serde_json = { version = "1.0", optional = true }

[dev-dependencies]
rkyv = { version = "0.7", default-features = false, features = ["size_32"] }
Expand Down Expand Up @@ -62,6 +65,11 @@ rkyv-impl = [
"rkyv",
"bytecheck",
]
serde = [
"dep:serde",
"bs58",
"serde_json"
]

[[test]]
name = "gadgets"
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ mod signatures;
#[cfg(feature = "zk")]
pub mod gadgets;

#[cfg(feature = "serde")]
mod serde_support;

pub use error::Error;
pub use keys::public::PublicKey;
pub use keys::secret::SecretKey;
Expand Down
330 changes: 330 additions & 0 deletions src/serde_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
// 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 dusk_bytes::Serializable;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

use crate::{
PublicKey, PublicKeyDouble, PublicKeyVarGen, SecretKey, SecretKeyVarGen,
Signature, SignatureDouble, SignatureVarGen,
};

impl Serialize for PublicKey {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for PublicKey {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
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 PublicKey: invalid byte length",
));
}
}
Err(err) => {
return Err(de::Error::custom(format!(
"Failed to deserialize PublicKey: {err:?}"
)))
}
}
let pubk = PublicKey::from_bytes(&bytes).map_err(|err| {
de::Error::custom(format!(
"Failed to deserialize PublicKey: {err:?}"
))
})?;
Ok(pubk)
}
}

impl Serialize for SecretKey {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for SecretKey {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
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 SecretKey: invalid byte length",
));
}
}
Err(err) => {
return Err(de::Error::custom(format!(
"Failed to deserialize SecretKey: {err:?}"
)))
}
}
let sk = SecretKey::from_bytes(&bytes).map_err(|err| {
de::Error::custom(format!(
"Failed to deserialize SecretKey: {err:?}"
))
})?;
Ok(sk)
}
}

impl Serialize for Signature {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for Signature {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
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 Signature: invalid byte length",
));
}
}
Err(err) => {
return Err(de::Error::custom(format!(
"Failed to deserialize Signature: {err:?}"
)))
}
}
let sig = Signature::from_bytes(&bytes).map_err(|err| {
de::Error::custom(format!(
"Failed to deserialize Signature: {err:?}"
))
})?;
Ok(sig)
}
}

impl Serialize for PublicKeyDouble {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for PublicKeyDouble {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
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 PublicKeyDouble: invalid byte length",
));
}
}
Err(err) => {
return Err(de::Error::custom(format!(
"Failed to deserialize PublicKeyDouble: {err:?}"
)))
}
}
let pk = PublicKeyDouble::from_bytes(&bytes).map_err(|err| {
de::Error::custom(format!(
"Failed to deserialize PublicKeyDouble: {err:?}"
))
})?;
Ok(pk)
}
}

impl Serialize for SignatureDouble {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for SignatureDouble {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
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 SignatureDouble: invalid byte length",
));
}
}
Err(err) => {
return Err(de::Error::custom(format!(
"Failed to deserialize SignatureDouble: {err:?}"
)))
}
}
let sig = SignatureDouble::from_bytes(&bytes).map_err(|err| {
de::Error::custom(format!(
"Failed to deserialize SignatureDouble: {err:?}"
))
})?;
Ok(sig)
}
}

impl Serialize for PublicKeyVarGen {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for PublicKeyVarGen {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
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 PublicKeyVarGen: invalid byte length",
));
}
}
Err(err) => {
return Err(de::Error::custom(format!(
"Failed to deserialize PublicKeyVarGen: {err:?}"
)))
}
}
let pk = PublicKeyVarGen::from_bytes(&bytes).map_err(|err| {
de::Error::custom(format!(
"Failed to deserialize PublicKeyVarGen: {err:?}"
))
})?;
Ok(pk)
}
}

impl Serialize for SecretKeyVarGen {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for SecretKeyVarGen {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
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 SecretKeyVarGen: invalid byte length",
));
}
}
Err(err) => {
return Err(de::Error::custom(format!(
"Failed to deserialize SecretKeyVarGen: {err:?}"
)))
}
}
let pk = SecretKeyVarGen::from_bytes(&bytes).map_err(|err| {
de::Error::custom(format!(
"Failed to deserialize SecretKeyVarGen: {err:?}"
))
})?;
Ok(pk)
}
}

impl Serialize for SignatureVarGen {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for SignatureVarGen {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
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 SignatureVarGen: invalid byte length",
));
}
}
Err(err) => {
return Err(de::Error::custom(format!(
"Failed to deserialize SignatureVarGen: {err:?}"
)))
}
}
let sig = SignatureVarGen::from_bytes(&bytes).map_err(|err| {
de::Error::custom(format!(
"Failed to deserialize SignatureVarGen: {err:?}"
))
})?;
Ok(sig)
}
}
Loading

0 comments on commit 035db06

Please sign in to comment.