-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
366 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
extern crate alloc; | ||
|
||
use alloc::format; | ||
use alloc::string::String; | ||
|
||
use dusk_bytes::Serializable; | ||
use serde::de::Error as SerdeError; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use crate::{ | ||
PublicKey, PublicKeyDouble, PublicKeyVarGen, SecretKey, SecretKeyVarGen, | ||
Signature, SignatureDouble, SignatureVarGen, | ||
}; | ||
|
||
impl Serialize for PublicKey { | ||
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 PublicKey { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
let mut bytes = [0; Self::SIZE]; | ||
let bytes_written = bs58::decode(&s) | ||
.into(&mut bytes) | ||
.map_err(SerdeError::custom)?; | ||
if bytes_written == Self::SIZE { | ||
PublicKey::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{:?}", err))) | ||
} else { | ||
Err(SerdeError::custom( | ||
"Failed to deserialize PublicKey: invalid byte length", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for SecretKey { | ||
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 SecretKey { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
let mut bytes = [0; Self::SIZE]; | ||
let bytes_written = bs58::decode(&s) | ||
.into(&mut bytes) | ||
.map_err(SerdeError::custom)?; | ||
if bytes_written == Self::SIZE { | ||
SecretKey::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{:?}", err))) | ||
} else { | ||
Err(SerdeError::custom( | ||
"Failed to deserialize SecretKey: invalid byte length", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
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 = [0; Self::SIZE]; | ||
let bytes_written = bs58::decode(&s) | ||
.into(&mut bytes) | ||
.map_err(SerdeError::custom)?; | ||
if bytes_written == Self::SIZE { | ||
Signature::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{:?}", err))) | ||
} else { | ||
Err(SerdeError::custom( | ||
"Failed to deserialize Signature: invalid byte length", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for PublicKeyDouble { | ||
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 PublicKeyDouble { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
let mut bytes = [0; Self::SIZE]; | ||
let bytes_written = bs58::decode(&s) | ||
.into(&mut bytes) | ||
.map_err(SerdeError::custom)?; | ||
if bytes_written == Self::SIZE { | ||
PublicKeyDouble::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{:?}", err))) | ||
} else { | ||
Err(SerdeError::custom( | ||
"Failed to deserialize PublicKeyDouble: invalid byte length", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for SignatureDouble { | ||
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 SignatureDouble { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
let mut bytes = [0; Self::SIZE]; | ||
let bytes_written = bs58::decode(&s) | ||
.into(&mut bytes) | ||
.map_err(SerdeError::custom)?; | ||
if bytes_written == Self::SIZE { | ||
SignatureDouble::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{:?}", err))) | ||
} else { | ||
Err(SerdeError::custom( | ||
"Failed to deserialize SignatureDouble: invalid byte length", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for PublicKeyVarGen { | ||
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 PublicKeyVarGen { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
let mut bytes = [0; Self::SIZE]; | ||
let bytes_written = bs58::decode(&s) | ||
.into(&mut bytes) | ||
.map_err(SerdeError::custom)?; | ||
if bytes_written == Self::SIZE { | ||
PublicKeyVarGen::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{:?}", err))) | ||
} else { | ||
Err(SerdeError::custom( | ||
"Failed to deserialize PublicKeyVarGen: invalid byte length", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for SecretKeyVarGen { | ||
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 SecretKeyVarGen { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
let mut bytes = [0; Self::SIZE]; | ||
let bytes_written = bs58::decode(&s) | ||
.into(&mut bytes) | ||
.map_err(SerdeError::custom)?; | ||
if bytes_written == Self::SIZE { | ||
SecretKeyVarGen::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{:?}", err))) | ||
} else { | ||
Err(SerdeError::custom( | ||
"Failed to deserialize SecretKeyVarGen: invalid byte length", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for SignatureVarGen { | ||
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 SignatureVarGen { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
let mut bytes = [0; Self::SIZE]; | ||
let bytes_written = bs58::decode(&s) | ||
.into(&mut bytes) | ||
.map_err(SerdeError::custom)?; | ||
if bytes_written == Self::SIZE { | ||
SignatureVarGen::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{:?}", err))) | ||
} else { | ||
Err(SerdeError::custom( | ||
"Failed to deserialize SignatureVarGen: invalid byte length", | ||
)) | ||
} | ||
} | ||
} |
Oops, something went wrong.