Add serde for Signature types (#4109)

* refactor: Added `from_slice()` method to ECDSA signatures

* doc: Modified ECDSA signature docstring to note Recovery ID

* feat: Implemented serde for Signature types

Note: using hexstring encoding

* feat: Automatically derive serde for MultiSignature

* refactor: Convert hex bytes using try_from instead of from_slice

Avoids panicking in critical code

Implemented from Peer Review

* clean: spaces -> tabs

* test: Added tests for Signature serialization

Added dependency on serde_json for testing purposes
This commit is contained in:
Bryant Eisenbach
2019-11-18 15:00:47 -05:00
committed by Bastian Köcher
parent 621c02440a
commit 566328cd3e
6 changed files with 157 additions and 17 deletions
+51 -8
View File
@@ -26,6 +26,8 @@ use codec::{Encode, Decode};
#[cfg(feature = "full_crypto")]
use blake2_rfc;
#[cfg(feature = "full_crypto")]
use core::convert::TryFrom;
#[cfg(feature = "std")]
use substrate_bip39::seed_from_entropy;
#[cfg(feature = "std")]
@@ -85,11 +87,11 @@ impl AsMut<[u8]> for Public {
}
impl Deref for Public {
type Target = [u8];
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.0
}
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl rstd::convert::TryFrom<&[u8]> for Public {
@@ -127,11 +129,11 @@ impl From<Public> for H256 {
#[cfg(feature = "std")]
impl std::str::FromStr for Public {
type Err = crate::crypto::PublicError;
type Err = crate::crypto::PublicError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_ss58check(s)
}
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_ss58check(s)
}
}
impl UncheckedFrom<[u8; 32]> for Public {
@@ -199,6 +201,23 @@ impl rstd::convert::TryFrom<&[u8]> for Signature {
}
}
#[cfg(feature = "std")]
impl Serialize for Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
serializer.serialize_str(&hex::encode(self))
}
}
#[cfg(feature = "std")]
impl<'de> Deserialize<'de> for Signature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
let signature_hex = hex::decode(&String::deserialize(deserializer)?)
.map_err(|e| de::Error::custom(format!("{:?}", e)))?;
Ok(Signature::try_from(signature_hex.as_ref())
.map_err(|e| de::Error::custom(format!("{:?}", e)))?)
}
}
impl Clone for Signature {
fn clone(&self) -> Self {
let mut r = [0u8; 64];
@@ -531,6 +550,7 @@ mod test {
use super::*;
use hex_literal::hex;
use crate::crypto::DEV_PHRASE;
use serde_json;
#[test]
fn default_phrase_should_be_used() {
@@ -643,4 +663,27 @@ mod test {
let cmp = Public::from_ss58check(&s).unwrap();
assert_eq!(cmp, public);
}
#[test]
fn signature_serialization_works() {
let pair = Pair::from_seed(b"12345678901234567890123456789012");
let message = b"Something important";
let signature = pair.sign(&message[..]);
let serialized_signature = serde_json::to_string(&signature).unwrap();
// Signature is 64 bytes, so 128 chars + 2 quote chars
assert_eq!(serialized_signature.len(), 130);
let signature = serde_json::from_str(&serialized_signature).unwrap();
assert!(Pair::verify(&signature, &message[..], &pair.public()));
}
#[test]
fn signature_serialization_doesnt_panic() {
fn deserialize_signature(text: &str) -> Result<Signature, serde_json::error::Error> {
Ok(serde_json::from_str(text)?)
}
assert!(deserialize_signature("Not valid json.").is_err());
assert!(deserialize_signature("\"Not an actual signature.\"").is_err());
// Poorly-sized
assert!(deserialize_signature("\"abc123\"").is_err());
}
}