-
Notifications
You must be signed in to change notification settings - Fork 281
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
Rebased version of [Alternative] Allow deserializing from owned types
+ support for new schnorr module
#270
Changes from all commits
1f08a31
b4040f0
18890d3
e6e23e9
c2fd5ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,7 +213,32 @@ impl SecretKey { | |
} | ||
} | ||
|
||
serde_impl!(SecretKey, constants::SECRET_KEY_SIZE); | ||
#[cfg(feature = "serde")] | ||
impl ::serde::Serialize for SecretKey { | ||
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[..]) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
impl<'de> ::serde::Deserialize<'de> for SecretKey { | ||
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 SecretKey" | ||
)) | ||
} else { | ||
d.deserialize_bytes(super::serde_util::BytesVisitor::new( | ||
"raw 32 bytes SecretKey", | ||
SecretKey::from_slice | ||
)) | ||
} | ||
} | ||
} | ||
|
||
impl PublicKey { | ||
/// Obtains a raw const pointer suitable for use with FFI functions | ||
|
@@ -402,7 +427,32 @@ impl From<ffi::PublicKey> for PublicKey { | |
} | ||
} | ||
|
||
serde_impl_from_slice!(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() { | ||
d.deserialize_str(super::serde_util::FromStrVisitor::new( | ||
"an ASCII hex string representing a public key" | ||
)) | ||
} else { | ||
d.deserialize_bytes(super::serde_util::BytesVisitor::new( | ||
"a bytestring representing a public key", | ||
PublicKey::from_slice | ||
Comment on lines
+449
to
+451
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this a breaking change? currently bytes will deserialize as hex: https://github.com/rust-bitcoin/rust-secp256k1/blob/master/src/macros.rs#L116-L125 (I'm not saying it's bad, just trying to know if this might break downstream users) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it is! The lines you commented on are within the else branch of Also, I think the linked piece of code was never actually called because I deleted a legacy of it in c2fd5ce and all tests still pass! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm I see, you're right. |
||
)) | ||
} | ||
} | ||
} | ||
|
||
impl PartialOrd for PublicKey { | ||
fn partial_cmp(&self, other: &PublicKey) -> Option<::core::cmp::Ordering> { | ||
|
@@ -818,7 +868,7 @@ mod test { | |
|
||
#[cfg(feature = "serde")] | ||
#[test] | ||
fn test_signature_serde() { | ||
fn test_serde() { | ||
use serde_test::{Configure, Token, assert_tokens}; | ||
static SK_BYTES: [u8; 32] = [ | ||
1, 1, 1, 1, 1, 1, 1, 1, | ||
|
@@ -846,8 +896,20 @@ mod test { | |
let pk = PublicKey::from_secret_key(&s, &sk); | ||
|
||
assert_tokens(&sk.compact(), &[Token::BorrowedBytes(&SK_BYTES[..])]); | ||
assert_tokens(&sk.compact(), &[Token::Bytes(&SK_BYTES)]); | ||
assert_tokens(&sk.compact(), &[Token::ByteBuf(&SK_BYTES)]); | ||
|
||
assert_tokens(&sk.readable(), &[Token::BorrowedStr(SK_STR)]); | ||
assert_tokens(&sk.readable(), &[Token::Str(SK_STR)]); | ||
assert_tokens(&sk.readable(), &[Token::String(SK_STR)]); | ||
|
||
assert_tokens(&pk.compact(), &[Token::BorrowedBytes(&PK_BYTES[..])]); | ||
assert_tokens(&pk.compact(), &[Token::Bytes(&PK_BYTES)]); | ||
assert_tokens(&pk.compact(), &[Token::ByteBuf(&PK_BYTES)]); | ||
|
||
assert_tokens(&pk.readable(), &[Token::BorrowedStr(PK_STR)]); | ||
assert_tokens(&pk.readable(), &[Token::Str(PK_STR)]); | ||
assert_tokens(&pk.readable(), &[Token::String(PK_STR)]); | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps remove
super::
and add a use statement that imports using the fully qualified path (although I would removeserde_util::
also). Then we can removesuper::
at no additional cost all the way through this PR. (I'm still singing the same old tune :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure how useful a
use
statement will be.This code is feature-gated so I would either have to feature-gate the
use
statement at the top of the file as well or accept that rustc will issue a dead-code warning if we compile without theserde
feature flag.For feature-gated code, I usually err on the side of having full-qualified paths, esp. if they only occur once within the code that is gated.
We could potentially include a
use super::serde_util;
at the beginning of the function and then reference bothFromStrVisitor
andBytesVisitor
asserde_util::FromStrVisitor
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the response, I didn't catch the feature gated nuance when I commented. Not sure which part of the comment @sgeisler is ok'ing but I like use statement inside the function option. Its pretty minor though, so don't sweat it :)