From 3af5a7084da77cf5b6559b0594b14680460500fc Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Fri, 28 Oct 2022 13:16:38 +0100 Subject: [PATCH] radicle-crypto: add Serialize and Deserialize for Signature Add Serialize and Deserialize for Signature using the the `into` and `try_from` serde attributes. To achieve this `From` and `TryFrom` implementations were added between `Signature` and `String`. Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett --- radicle-crypto/src/lib.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/radicle-crypto/src/lib.rs b/radicle-crypto/src/lib.rs index f9a6b392..b6043fbf 100644 --- a/radicle-crypto/src/lib.rs +++ b/radicle-crypto/src/lib.rs @@ -63,7 +63,8 @@ where } /// Cryptographic signature. -#[derive(PartialEq, Eq, Copy, Clone)] +#[derive(PartialEq, Eq, Copy, Clone, Serialize, Deserialize)] +#[serde(into = "String", try_from = "String")] pub struct Signature(pub ed25519::Signature); impl fmt::Display for Signature { @@ -126,6 +127,20 @@ impl TryFrom<&[u8]> for Signature { } } +impl From for String { + fn from(s: Signature) -> Self { + s.to_string() + } +} + +impl TryFrom for Signature { + type Error = SignatureError; + + fn try_from(s: String) -> Result { + Self::from_str(&s) + } +} + /// The public/verification key. #[derive(Serialize, Deserialize, Eq, Copy, Clone)] #[serde(into = "String", try_from = "String")]