mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
Add hex serialization functions (#2436)
* Change slate seralization to hex * rustfmt * Comment out serde directives Move serialization functions into a seperate module * Remove commented code
This commit is contained in:
parent
329d243ccd
commit
a523f82820
5 changed files with 180 additions and 3 deletions
|
@ -30,6 +30,7 @@ use grin_util as util;
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
extern crate serde;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
use failure;
|
use failure;
|
||||||
|
|
|
@ -26,6 +26,7 @@ pub mod build;
|
||||||
mod error;
|
mod error;
|
||||||
pub mod proof;
|
pub mod proof;
|
||||||
pub mod reward;
|
pub mod reward;
|
||||||
|
pub mod serialization;
|
||||||
pub mod slate;
|
pub mod slate;
|
||||||
|
|
||||||
use crate::consensus;
|
use crate::consensus;
|
||||||
|
|
175
core/src/libtx/serialization.rs
Normal file
175
core/src/libtx/serialization.rs
Normal file
|
@ -0,0 +1,175 @@
|
||||||
|
// Copyright 2018 The Grin Developers
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
//! Sane serialization & deserialization of cryptographic structs into hex
|
||||||
|
|
||||||
|
use crate::keychain::BlindingFactor;
|
||||||
|
use crate::serde::{Deserialize, Deserializer, Serializer};
|
||||||
|
use crate::util::secp::pedersen::{Commitment, RangeProof};
|
||||||
|
use crate::util::{from_hex, to_hex};
|
||||||
|
|
||||||
|
/// Serializes a secp PublicKey to and from hex
|
||||||
|
pub mod pubkey_serde {
|
||||||
|
use crate::serde::{Deserialize, Deserializer, Serializer};
|
||||||
|
use crate::util::secp::key::PublicKey;
|
||||||
|
use crate::util::{from_hex, static_secp_instance, to_hex};
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn serialize<S>(key: &PublicKey, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
let static_secp = static_secp_instance();
|
||||||
|
let static_secp = static_secp.lock();
|
||||||
|
serializer.serialize_str(&to_hex(key.serialize_vec(&static_secp, false).to_vec()))
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn deserialize<'de, D>(deserializer: D) -> Result<PublicKey, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
use serde::de::Error;
|
||||||
|
let static_secp = static_secp_instance();
|
||||||
|
let static_secp = static_secp.lock();
|
||||||
|
String::deserialize(deserializer)
|
||||||
|
.and_then(|string| from_hex(string).map_err(|err| Error::custom(err.to_string())))
|
||||||
|
.and_then(|bytes: Vec<u8>| {
|
||||||
|
PublicKey::from_slice(&static_secp, &bytes)
|
||||||
|
.map_err(|err| Error::custom(err.to_string()))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Serializes an Option<secp::Signature> to and from hex
|
||||||
|
pub mod option_sig_serde {
|
||||||
|
use crate::serde::{Deserialize, Deserializer, Serializer};
|
||||||
|
use crate::util::secp;
|
||||||
|
use crate::util::static_secp_instance;
|
||||||
|
use crate::util::{from_hex, to_hex};
|
||||||
|
use serde::de::Error;
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn serialize<S>(sig: &Option<secp::Signature>, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
match sig {
|
||||||
|
Some(sig) => {
|
||||||
|
let static_secp = static_secp_instance();
|
||||||
|
let static_secp = static_secp.lock();
|
||||||
|
serializer.serialize_str(&to_hex(sig.serialize_der(&static_secp)))
|
||||||
|
}
|
||||||
|
None => serializer.serialize_none(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<secp::Signature>, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let static_secp = static_secp_instance();
|
||||||
|
let static_secp = static_secp.lock();
|
||||||
|
|
||||||
|
Option::<&str>::deserialize(deserializer).and_then(|res| match res {
|
||||||
|
Some(string) => from_hex(string.to_string())
|
||||||
|
.map_err(|err| Error::custom(err.to_string()))
|
||||||
|
.and_then(|bytes: Vec<u8>| {
|
||||||
|
secp::Signature::from_der(&static_secp, &bytes)
|
||||||
|
.map(|val| Some(val))
|
||||||
|
.map_err(|err| Error::custom(err.to_string()))
|
||||||
|
}),
|
||||||
|
None => Ok(None),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Serializes a secp::Signature to and from hex
|
||||||
|
pub mod sig_serde {
|
||||||
|
use crate::serde::{Deserialize, Deserializer, Serializer};
|
||||||
|
use crate::util::secp;
|
||||||
|
use crate::util::static_secp_instance;
|
||||||
|
use crate::util::{from_hex, to_hex};
|
||||||
|
use serde::de::Error;
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn serialize<S>(sig: &secp::Signature, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
let static_secp = static_secp_instance();
|
||||||
|
let static_secp = static_secp.lock();
|
||||||
|
serializer.serialize_str(&to_hex(sig.serialize_der(&static_secp)))
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn deserialize<'de, D>(deserializer: D) -> Result<secp::Signature, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let static_secp = static_secp_instance();
|
||||||
|
let static_secp = static_secp.lock();
|
||||||
|
String::deserialize(deserializer)
|
||||||
|
.and_then(|string| from_hex(string).map_err(|err| Error::custom(err.to_string())))
|
||||||
|
.and_then(|bytes: Vec<u8>| {
|
||||||
|
secp::Signature::from_der(&static_secp, &bytes)
|
||||||
|
.map_err(|err| Error::custom(err.to_string()))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a BlindingFactor from a hex string
|
||||||
|
pub fn blind_from_hex<'de, D>(deserializer: D) -> Result<BlindingFactor, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
use serde::de::Error;
|
||||||
|
String::deserialize(deserializer).and_then(|string| {
|
||||||
|
BlindingFactor::from_hex(&string).map_err(|err| Error::custom(err.to_string()))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a RangeProof from a hex string
|
||||||
|
pub fn rangeproof_from_hex<'de, D>(deserializer: D) -> Result<RangeProof, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
use serde::de::{Error, IntoDeserializer};
|
||||||
|
|
||||||
|
let val = String::deserialize(deserializer)
|
||||||
|
.and_then(|string| from_hex(string).map_err(|err| Error::custom(err.to_string())))?;
|
||||||
|
RangeProof::deserialize(val.into_deserializer())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a Pedersen Commitment from a hex string
|
||||||
|
pub fn commitment_from_hex<'de, D>(deserializer: D) -> Result<Commitment, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
use serde::de::Error;
|
||||||
|
String::deserialize(deserializer)
|
||||||
|
.and_then(|string| from_hex(string).map_err(|err| Error::custom(err.to_string())))
|
||||||
|
.and_then(|bytes: Vec<u8>| Ok(Commitment::from_vec(bytes.to_vec())))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Seralizes a byte string into hex
|
||||||
|
pub fn as_hex<T, S>(bytes: T, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
T: AsRef<[u8]>,
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_str(&to_hex(bytes.as_ref().to_vec()))
|
||||||
|
}
|
|
@ -52,7 +52,7 @@ impl WalletCommAdapter for FileWalletCommAdapter {
|
||||||
let mut pub_tx_f = File::open(params)?;
|
let mut pub_tx_f = File::open(params)?;
|
||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
pub_tx_f.read_to_string(&mut content)?;
|
pub_tx_f.read_to_string(&mut content)?;
|
||||||
Ok(json::from_str(&content).map_err(|_| ErrorKind::Format)?)
|
Ok(json::from_str(&content).map_err(|err| ErrorKind::Format(err.to_string()))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn listen(
|
fn listen(
|
||||||
|
|
|
@ -112,8 +112,8 @@ pub enum ErrorKind {
|
||||||
Restore,
|
Restore,
|
||||||
|
|
||||||
/// An error in the format of the JSON structures exchanged by the wallet
|
/// An error in the format of the JSON structures exchanged by the wallet
|
||||||
#[fail(display = "JSON format error")]
|
#[fail(display = "JSON format error: {}", _0)]
|
||||||
Format,
|
Format(String),
|
||||||
|
|
||||||
/// Other serialization errors
|
/// Other serialization errors
|
||||||
#[fail(display = "Ser/Deserialization error")]
|
#[fail(display = "Ser/Deserialization error")]
|
||||||
|
|
Loading…
Reference in a new issue