Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serde serialization for Option<Commitment> #3074

Merged
merged 2 commits into from
Oct 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions core/src/libtx/secp_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,38 @@ pub mod sig_serde {
}
}

/// Serializes an Option<secp::Commitment> to and from hex
pub mod option_commitment_serde {
use crate::serde::{Deserialize, Deserializer, Serializer};
use crate::util::secp::pedersen::Commitment;
use crate::util::{from_hex, to_hex};
use serde::de::Error;

///
pub fn serialize<S>(commit: &Option<Commitment>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match commit {
Some(c) => serializer.serialize_str(&to_hex(c.0.to_vec())),
None => serializer.serialize_none(),
}
}

///
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Commitment>, D::Error>
where
D: Deserializer<'de>,
{
Option::<String>::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>| Ok(Some(Commitment::from_vec(bytes.to_vec())))),
None => Ok(None),
})
}

}
/// Creates a BlindingFactor from a hex string
pub fn blind_from_hex<'de, D>(deserializer: D) -> Result<BlindingFactor, D::Error>
where
Expand Down Expand Up @@ -334,6 +366,8 @@ mod test {
pub pub_key: PublicKey,
#[serde(with = "option_sig_serde")]
pub opt_sig: Option<Signature>,
#[serde(with = "option_commitment_serde")]
pub opt_commit: Option<Commitment>,
#[serde(with = "sig_serde")]
pub sig: Signature,
#[serde(with = "string_or_u64")]
Expand All @@ -351,10 +385,15 @@ mod test {
thread_rng().fill(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let sig = aggsig::sign_single(&secp, &msg, &sk, None, None).unwrap();
let mut commit = [0u8; 33];
commit[0] = 0x09;
thread_rng().fill(&mut commit[1..]);
let commit = Commitment::from_vec(commit.to_vec());
SerTest {
opt_skey: Some(sk.clone()),
pub_key: PublicKey::from_secret_key(&secp, &sk).unwrap(),
opt_sig: Some(sig.clone()),
opt_commit: Some(commit),
sig: sig.clone(),
num: 30,
opt_num: Some(33),
Expand Down