diff --git a/src/serde_support.rs b/src/serde_support.rs index 1ced0e4..e03dce7 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,99 @@ 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) + } +}