From f09be04633a54b424ec2fc98efd9715ae057bcd4 Mon Sep 17 00:00:00 2001 From: Demilade Sonuga Date: Tue, 26 Nov 2024 12:39:33 +0100 Subject: [PATCH] Added serde implementations for public keys and signatures --- src/serde_support.rs | 98 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/src/serde_support.rs b/src/serde_support.rs index 1ced0e4..3e4589c 100644 --- a/src/serde_support.rs +++ b/src/serde_support.rs @@ -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( @@ -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", )); } } @@ -46,3 +46,97 @@ impl<'de> Deserialize<'de> for PublicKey { Ok(pubk) } } + +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 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( + &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 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( + &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 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) + } +}