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 e34731a
Showing 1 changed file with 98 additions and 2 deletions.
100 changes: 98 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,99 @@ 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 e34731a

Please sign in to comment.