-
Notifications
You must be signed in to change notification settings - Fork 1
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
225 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,154 @@ | ||
// 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 bs58; | ||
use dusk_bytes::Serializable; | ||
use serde::de::Error as SerdeError; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use crate::{ | ||
MultisigPublicKey, MultisigSignature, PublicKey, SecretKey, Signature, | ||
}; | ||
|
||
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 decoded = | ||
bs58::decode(&s).into_vec().map_err(SerdeError::custom)?; | ||
let decoded_len = decoded.len(); | ||
let byte_length_str = format!("{}", Self::SIZE); | ||
let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| { | ||
SerdeError::invalid_length(decoded_len, &byte_length_str.as_str()) | ||
})?; | ||
PublicKey::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{err:?}"))) | ||
} | ||
} | ||
|
||
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 decoded = | ||
bs58::decode(&s).into_vec().map_err(SerdeError::custom)?; | ||
let decoded_len = decoded.len(); | ||
let byte_length_str = format!("{}", Self::SIZE); | ||
let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| { | ||
SerdeError::invalid_length(decoded_len, &byte_length_str.as_str()) | ||
})?; | ||
MultisigPublicKey::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{err:?}"))) | ||
} | ||
} | ||
|
||
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 decoded = | ||
bs58::decode(&s).into_vec().map_err(SerdeError::custom)?; | ||
let decoded_len = decoded.len(); | ||
let byte_length_str = format!("{}", Self::SIZE); | ||
let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| { | ||
SerdeError::invalid_length(decoded_len, &byte_length_str.as_str()) | ||
})?; | ||
Signature::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{err:?}"))) | ||
} | ||
} | ||
|
||
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 decoded = | ||
bs58::decode(&s).into_vec().map_err(SerdeError::custom)?; | ||
let decoded_len = decoded.len(); | ||
let byte_length_str = format!("{}", Self::SIZE); | ||
let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| { | ||
SerdeError::invalid_length(decoded_len, &byte_length_str.as_str()) | ||
})?; | ||
MultisigSignature::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{err:?}"))) | ||
} | ||
} | ||
|
||
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 decoded = | ||
bs58::decode(&s).into_vec().map_err(SerdeError::custom)?; | ||
let decoded_len = decoded.len(); | ||
let byte_length_str = format!("{}", Self::SIZE); | ||
let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| { | ||
SerdeError::invalid_length(decoded_len, &byte_length_str.as_str()) | ||
})?; | ||
SecretKey::from_bytes(&bytes) | ||
.map_err(|err| SerdeError::custom(format!("{err:?}"))) | ||
} | ||
} |
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,62 @@ | ||
// 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. | ||
|
||
#![cfg(feature = "serde")] | ||
|
||
use bls12_381_bls::{MultisigPublicKey, PublicKey, SecretKey}; | ||
use rand::rngs::StdRng; | ||
use rand::SeedableRng; | ||
|
||
#[test] | ||
fn public_key() { | ||
let mut rng = StdRng::seed_from_u64(0xbeef); | ||
let pk = PublicKey::from(&SecretKey::random(&mut rng)); | ||
let ser = serde_json::to_string(&pk); | ||
let deser = serde_json::from_str(&ser.unwrap()); | ||
assert_eq!(pk, deser.unwrap()); | ||
} | ||
|
||
#[test] | ||
fn multisig_public_key() { | ||
let mut rng = StdRng::seed_from_u64(0xbeef); | ||
let pk = MultisigPublicKey::aggregate(&[PublicKey::from( | ||
&SecretKey::random(&mut rng), | ||
)]) | ||
.unwrap(); | ||
let ser = serde_json::to_string(&pk); | ||
let deser = serde_json::from_str(&ser.unwrap()); | ||
assert_eq!(pk, deser.unwrap()); | ||
} | ||
|
||
#[test] | ||
fn signature() { | ||
let mut rng = StdRng::seed_from_u64(0xbeef); | ||
let sk = SecretKey::random(&mut rng); | ||
let signature = sk.sign(b"a message"); | ||
let ser = serde_json::to_string(&signature).unwrap(); | ||
let deser = serde_json::from_str(&ser).unwrap(); | ||
assert_eq!(signature, deser); | ||
} | ||
|
||
#[test] | ||
fn multisig_signature() { | ||
let mut rng = StdRng::seed_from_u64(0xbeef); | ||
let sk = SecretKey::random(&mut rng); | ||
let pk = PublicKey::from(&sk); | ||
let signature = sk.sign_multisig(&pk, b"a message"); | ||
let ser = serde_json::to_string(&signature).unwrap(); | ||
let deser = serde_json::from_str(&ser).unwrap(); | ||
assert_eq!(signature, deser); | ||
} | ||
|
||
#[test] | ||
fn secret_key() { | ||
let mut rng = StdRng::seed_from_u64(0xbeef); | ||
let sk = SecretKey::random(&mut rng); | ||
let ser = serde_json::to_string(&sk).unwrap(); | ||
let deser = serde_json::from_str(&ser).unwrap(); | ||
assert_eq!(sk, deser); | ||
} |