Skip to content

Commit

Permalink
Serde serialization for KeyPair
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Sep 27, 2021
1 parent 1f5d6da commit 4a76e13
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/schnorrsig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,46 @@ impl str::FromStr for Signature {
pub struct KeyPair(ffi::KeyPair);
impl_display_secret!(KeyPair);

impl ::core::str::FromStr for KeyPair {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let ctx = unsafe {
Secp256k1::from_raw_all(ffi::secp256k1_context_no_precomp as *mut ffi::Context)
};
KeyPair::from_seckey_str(&ctx, s)
}
}

impl ::serde::Serialize for KeyPair {
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if s.is_human_readable() {
s.serialize_str(&self.display_secret().to_string())
} else {
s.serialize_bytes(&self.0[..])
}
}
}

#[cfg(feature = "serde")]
impl<'de> ::serde::Deserialize<'de> for KeyPair {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
if d.is_human_readable() {
d.deserialize_str(super::serde_util::FromStrVisitor::new(
"a hex string representing 32 byte KeyPair"
))
} else {
d.deserialize_bytes(super::serde_util::BytesVisitor::new(
"raw 32 bytes KeyPair",
|data| unsafe {
let ctx = Secp256k1::from_raw_all(ffi::secp256k1_context_no_precomp as *mut ffi::Context);
KeyPair::from_seckey_slice(&ctx, data)
}
))
}
}
}

/// A Schnorr public key, used for verification of Schnorr signatures
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
pub struct PublicKey(ffi::XOnlyPublicKey);
Expand Down

0 comments on commit 4a76e13

Please sign in to comment.