Skip to content

Commit

Permalink
Serde ser for SchnorrPublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Sep 25, 2020
1 parent e2c31f7 commit b9821a0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 75 deletions.
66 changes: 1 addition & 65 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,71 +404,7 @@ impl From<ffi::PublicKey> for PublicKey {
}
}

#[cfg(feature = "serde")]
impl ::serde::Serialize for PublicKey {
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if s.is_human_readable() {
s.collect_str(self)
} else {
s.serialize_bytes(&self.serialize())
}
}
}

#[cfg(feature = "serde")]
impl<'de> ::serde::Deserialize<'de> for PublicKey {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<PublicKey, D::Error> {
if d.is_human_readable() {
struct HexVisitor;

impl<'de> ::serde::de::Visitor<'de> for HexVisitor {
type Value = PublicKey;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an ASCII hex string")
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
if let Ok(hex) = str::from_utf8(v) {
str::FromStr::from_str(hex).map_err(E::custom)
} else {
Err(E::invalid_value(::serde::de::Unexpected::Bytes(v), &self))
}
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
str::FromStr::from_str(v).map_err(E::custom)
}
}
d.deserialize_str(HexVisitor)
} else {
struct BytesVisitor;

impl<'de> ::serde::de::Visitor<'de> for BytesVisitor {
type Value = PublicKey;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a bytestring")
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
PublicKey::from_slice(v).map_err(E::custom)
}
}

d.deserialize_bytes(BytesVisitor)
}
}
}
serde_impl_from_slice!(PublicKey);

#[cfg(test)]
mod test {
Expand Down
74 changes: 74 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,77 @@ macro_rules! serde_impl(
macro_rules! serde_impl(
($t:ident, $len:expr) => ()
);

#[cfg(feature = "serde")]
macro_rules! serde_impl_from_slice {
($t: ident) => {
impl ::serde::Serialize for $t {
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if s.is_human_readable() {
s.collect_str(self)
} else {
s.serialize_bytes(&self.serialize())
}
}
}

impl<'de> ::serde::Deserialize<'de> for $t {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<$t, D::Error> {
if d.is_human_readable() {
struct HexVisitor;

impl<'de> ::serde::de::Visitor<'de> for HexVisitor {
type Value = $t;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an ASCII hex string")
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
if let Ok(hex) = str::from_utf8(v) {
str::FromStr::from_str(hex).map_err(E::custom)
} else {
Err(E::invalid_value(::serde::de::Unexpected::Bytes(v), &self))
}
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
str::FromStr::from_str(v).map_err(E::custom)
}
}
d.deserialize_str(HexVisitor)
} else {
struct BytesVisitor;

impl<'de> ::serde::de::Visitor<'de> for BytesVisitor {
type Value = $t;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a bytestring")
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
$t::from_slice(v).map_err(E::custom)
}
}

d.deserialize_bytes(BytesVisitor)
}
}
}
};
}

#[cfg(not(feature = "serde"))]
macro_rules! serde_impl_from_slice(
($t:ident) => ()
);
11 changes: 1 addition & 10 deletions src/schnorrsig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,7 @@ impl From<ffi::XOnlyPublicKey> for SchnorrPublicKey {
}
}

#[cfg(feature = "serde")]
impl ::serde::Serialize for SchnorrPublicKey {
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if s.is_human_readable() {
s.collect_str(self)
} else {
s.serialize_bytes(&self.serialize())
}
}
}
serde_impl_from_slice!(SchnorrPublicKey);

impl<C: Signing> Secp256k1<C> {
/// Create a schnorr signature.
Expand Down

0 comments on commit b9821a0

Please sign in to comment.