Skip to content

Commit

Permalink
Added serde implementations for public keys and signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
d-sonuga committed Nov 26, 2024
1 parent 44d0afe commit f09be04
Showing 1 changed file with 96 additions and 2 deletions.
98 changes: 96 additions & 2 deletions src/serde_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bs58;
use dusk_bytes::Serializable;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

use crate::keys::public::PublicKey;
use crate::{MultisigPublicKey, MultisigSignature, PublicKey, Signature};

impl Serialize for PublicKey {
fn serialize<S: Serializer>(
Expand All @@ -35,7 +35,7 @@ impl<'de> Deserialize<'de> for PublicKey {
Ok(n) => {
if n != Self::SIZE {
return Err(de::Error::custom(
"failed to deserialize AccountPublicKey",
"failed to deserialize PublicKey",
));
}
}
Expand All @@ -46,3 +46,97 @@ impl<'de> Deserialize<'de> for PublicKey {
Ok(pubk)
}
}

impl Serialize for MultisigPublicKey {
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 MultisigPublicKey {
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 MultisigPublicKey",
));
}
}
Err(err) => return Err(de::Error::custom(format!("{err:?}"))),
}
let pubk = MultisigPublicKey::from_bytes(&bytes)
.map_err(|err| de::Error::custom(format!("{err:?}")))?;
Ok(pubk)
}
}

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",
));
}
}
Err(err) => return Err(de::Error::custom(format!("{err:?}"))),
}
let pubk = Signature::from_bytes(&bytes)
.map_err(|err| de::Error::custom(format!("{err:?}")))?;
Ok(pubk)
}
}


impl Serialize for MultisigSignature {
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 MultisigSignature {
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 MultisigSignature",
));
}
}
Err(err) => return Err(de::Error::custom(format!("{err:?}"))),
}
let pubk = MultisigSignature::from_bytes(&bytes)
.map_err(|err| de::Error::custom(format!("{err:?}")))?;
Ok(pubk)
}
}

0 comments on commit f09be04

Please sign in to comment.