2017-04-03 11:13:51 +02:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use std::cmp::{Ord, PartialOrd, Ordering};
|
|
|
|
use std::ops::Deref;
|
2017-04-08 11:26:16 +02:00
|
|
|
use rustc_serialize::hex::{ToHex, FromHex};
|
2017-04-03 11:13:51 +02:00
|
|
|
use serde::{Serialize, Deserialize, Serializer, Deserializer};
|
|
|
|
use serde::de::{Visitor, Error as SerdeError};
|
|
|
|
use ethkey::{Public, Secret, Signature};
|
2017-04-08 11:26:16 +02:00
|
|
|
use util::{H256, Bytes};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
/// Serializable shadow decryption result.
|
|
|
|
pub struct SerializableDocumentEncryptedKeyShadow {
|
|
|
|
/// Decrypted secret point. It is partially decrypted if shadow decrpytion was requested.
|
|
|
|
pub decrypted_secret: SerializablePublic,
|
|
|
|
/// Shared common point.
|
|
|
|
pub common_point: SerializablePublic,
|
|
|
|
/// If shadow decryption was requested: shadow decryption coefficients, encrypted with requestor public.
|
|
|
|
pub decrypt_shadows: Vec<SerializableBytes>,
|
|
|
|
}
|
|
|
|
|
2017-05-05 15:57:29 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2017-04-08 11:26:16 +02:00
|
|
|
/// Serializable Bytes.
|
2017-05-05 15:57:29 +02:00
|
|
|
pub struct SerializableBytes(pub Bytes);
|
2017-04-08 11:26:16 +02:00
|
|
|
|
|
|
|
impl<T> From<T> for SerializableBytes where Bytes: From<T> {
|
|
|
|
fn from(s: T) -> SerializableBytes {
|
|
|
|
SerializableBytes(s.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Bytes> for SerializableBytes {
|
|
|
|
fn into(self) -> Bytes {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for SerializableBytes {
|
|
|
|
type Target = Bytes;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Bytes {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for SerializableBytes {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
2017-05-05 15:57:29 +02:00
|
|
|
let mut serialized = "0x".to_owned();
|
|
|
|
serialized.push_str(self.0.to_hex().as_ref());
|
|
|
|
serializer.serialize_str(serialized.as_ref())
|
2017-04-08 11:26:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for SerializableBytes {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where D: Deserializer
|
|
|
|
{
|
|
|
|
let s = String::deserialize(deserializer)?;
|
2017-05-05 15:57:29 +02:00
|
|
|
if s.len() >= 2 && &s[0..2] == "0x" && s.len() & 1 == 0 {
|
|
|
|
let data = s[2..].from_hex().map_err(SerdeError::custom)?;
|
|
|
|
Ok(SerializableBytes(data))
|
|
|
|
} else {
|
|
|
|
Err(SerdeError::custom("invalid format"))
|
|
|
|
}
|
2017-04-08 11:26:16 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-03 11:13:51 +02:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
/// Serializable Signature.
|
2017-05-05 15:57:29 +02:00
|
|
|
pub struct SerializableSignature(pub Signature);
|
2017-04-03 11:13:51 +02:00
|
|
|
|
|
|
|
impl<T> From<T> for SerializableSignature where Signature: From<T> {
|
|
|
|
fn from(s: T) -> SerializableSignature {
|
|
|
|
SerializableSignature(s.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Signature> for SerializableSignature {
|
|
|
|
fn into(self) -> Signature {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for SerializableSignature {
|
|
|
|
type Target = Signature;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Signature {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for SerializableSignature {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
2017-05-05 15:57:29 +02:00
|
|
|
let mut serialized = "0x".to_owned();
|
|
|
|
serialized.push_str(self.0.to_hex().as_ref());
|
|
|
|
serializer.serialize_str(serialized.as_ref())
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for SerializableSignature {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer {
|
|
|
|
struct HashVisitor;
|
|
|
|
|
|
|
|
impl Visitor for HashVisitor {
|
|
|
|
type Value = SerializableSignature;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(formatter, "a hex-encoded Signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: SerdeError {
|
2017-05-05 15:57:29 +02:00
|
|
|
if value.len() >= 2 && &value[0..2] == "0x" && value.len() & 1 == 0 {
|
|
|
|
value[2..].parse().map(|s| SerializableSignature(s)).map_err(SerdeError::custom)
|
|
|
|
} else {
|
|
|
|
Err(SerdeError::custom("invalid format"))
|
|
|
|
}
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: SerdeError {
|
|
|
|
self.visit_str(value.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize(HashVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
/// Serializable H256.
|
2017-05-05 15:57:29 +02:00
|
|
|
pub struct SerializableH256(pub H256);
|
2017-04-03 11:13:51 +02:00
|
|
|
|
|
|
|
impl<T> From<T> for SerializableH256 where H256: From<T> {
|
|
|
|
fn from(s: T) -> SerializableH256 {
|
|
|
|
SerializableH256(s.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<H256> for SerializableH256 {
|
|
|
|
fn into(self) -> H256 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for SerializableH256 {
|
|
|
|
type Target = H256;
|
|
|
|
|
|
|
|
fn deref(&self) -> &H256 {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for SerializableH256 {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
2017-05-05 15:57:29 +02:00
|
|
|
let mut serialized = "0x".to_owned();
|
|
|
|
serialized.push_str(self.0.to_hex().as_ref());
|
|
|
|
serializer.serialize_str(serialized.as_ref())
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for SerializableH256 {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer {
|
|
|
|
struct HashVisitor;
|
|
|
|
|
|
|
|
impl Visitor for HashVisitor {
|
|
|
|
type Value = SerializableH256;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(formatter, "a hex-encoded H256")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: SerdeError {
|
2017-05-05 15:57:29 +02:00
|
|
|
if value.len() >= 2 && &value[0..2] == "0x" && value.len() & 1 == 0 {
|
|
|
|
value[2..].parse().map(|s| SerializableH256(s)).map_err(SerdeError::custom)
|
|
|
|
} else {
|
|
|
|
Err(SerdeError::custom("invalid format"))
|
|
|
|
}
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: SerdeError {
|
|
|
|
self.visit_str(value.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize(HashVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
/// Serializable EC scalar/secret key.
|
2017-05-05 15:57:29 +02:00
|
|
|
pub struct SerializableSecret(pub Secret);
|
2017-04-03 11:13:51 +02:00
|
|
|
|
|
|
|
impl<T> From<T> for SerializableSecret where Secret: From<T> {
|
|
|
|
fn from(s: T) -> SerializableSecret {
|
|
|
|
SerializableSecret(s.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Secret> for SerializableSecret {
|
|
|
|
fn into(self) -> Secret {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for SerializableSecret {
|
|
|
|
type Target = Secret;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Secret {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for SerializableSecret {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
2017-05-05 15:57:29 +02:00
|
|
|
let mut serialized = "0x".to_owned();
|
|
|
|
serialized.push_str(self.0.to_hex().as_ref());
|
|
|
|
serializer.serialize_str(serialized.as_ref())
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for SerializableSecret {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer {
|
|
|
|
struct HashVisitor;
|
|
|
|
|
|
|
|
impl Visitor for HashVisitor {
|
|
|
|
type Value = SerializableSecret;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(formatter, "a hex-encoded EC scalar")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: SerdeError {
|
2017-05-05 15:57:29 +02:00
|
|
|
if value.len() >= 2 && &value[0..2] == "0x" && value.len() & 1 == 0 {
|
|
|
|
value[2..].parse().map(|s| SerializableSecret(s)).map_err(SerdeError::custom)
|
|
|
|
} else {
|
|
|
|
Err(SerdeError::custom("invalid format"))
|
|
|
|
}
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: SerdeError {
|
|
|
|
self.visit_str(value.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize(HashVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
/// Serializable EC point/public key.
|
2017-05-05 15:57:29 +02:00
|
|
|
pub struct SerializablePublic(pub Public);
|
2017-04-03 11:13:51 +02:00
|
|
|
|
|
|
|
impl<T> From<T> for SerializablePublic where Public: From<T> {
|
|
|
|
fn from(p: T) -> SerializablePublic {
|
|
|
|
SerializablePublic(p.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Public> for SerializablePublic {
|
|
|
|
fn into(self) -> Public {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for SerializablePublic {
|
|
|
|
type Target = Public;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Public {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for SerializablePublic { }
|
|
|
|
|
|
|
|
impl PartialEq for SerializablePublic {
|
|
|
|
fn eq(&self, other: &SerializablePublic) -> bool {
|
|
|
|
self.0.eq(&other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for SerializablePublic {
|
|
|
|
fn cmp(&self, other: &SerializablePublic) -> Ordering {
|
|
|
|
self.0.cmp(&other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for SerializablePublic {
|
|
|
|
fn partial_cmp(&self, other: &SerializablePublic) -> Option<Ordering> {
|
|
|
|
self.0.partial_cmp(&other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for SerializablePublic {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
2017-05-05 15:57:29 +02:00
|
|
|
let mut serialized = "0x".to_owned();
|
|
|
|
serialized.push_str(self.0.to_hex().as_ref());
|
|
|
|
serializer.serialize_str(serialized.as_ref())
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for SerializablePublic {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer {
|
|
|
|
struct HashVisitor;
|
|
|
|
|
|
|
|
impl Visitor for HashVisitor {
|
|
|
|
type Value = SerializablePublic;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(formatter, "a hex-encoded EC point")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: SerdeError {
|
2017-05-05 15:57:29 +02:00
|
|
|
if value.len() >= 2 && &value[0..2] == "0x" && value.len() & 1 == 0 {
|
|
|
|
value[2..].parse().map(|s| SerializablePublic(s)).map_err(SerdeError::custom)
|
|
|
|
} else {
|
|
|
|
Err(SerdeError::custom("invalid format"))
|
|
|
|
}
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: SerdeError {
|
|
|
|
self.visit_str(value.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize(HashVisitor)
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 15:57:29 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use serde_json;
|
|
|
|
use super::{SerializableBytes, SerializablePublic};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_and_deserialize_bytes() {
|
|
|
|
let bytes = SerializableBytes(vec![1, 2, 3, 4]);
|
|
|
|
let bytes_serialized = serde_json::to_string(&bytes).unwrap();
|
|
|
|
assert_eq!(&bytes_serialized, r#""0x01020304""#);
|
|
|
|
let bytes_deserialized: SerializableBytes = serde_json::from_str(&bytes_serialized).unwrap();
|
|
|
|
assert_eq!(bytes_deserialized, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_and_deserialize_public() {
|
|
|
|
let public = SerializablePublic("cac6c205eb06c8308d65156ff6c862c62b000b8ead121a4455a8ddeff7248128d895692136f240d5d1614dc7cc4147b1bd584bd617e30560bb872064d09ea325".parse().unwrap());
|
|
|
|
let public_serialized = serde_json::to_string(&public).unwrap();
|
|
|
|
assert_eq!(&public_serialized, r#""0xcac6c205eb06c8308d65156ff6c862c62b000b8ead121a4455a8ddeff7248128d895692136f240d5d1614dc7cc4147b1bd584bd617e30560bb872064d09ea325""#);
|
|
|
|
let public_deserialized: SerializablePublic = serde_json::from_str(&public_serialized).unwrap();
|
|
|
|
assert_eq!(public_deserialized, public);
|
|
|
|
}
|
|
|
|
}
|