Skip to content

Commit 0f25474

Browse files
committed
Implement hex human-readable serde for PublicKey
1 parent ccac3ea commit 0f25474

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

src/key.rs

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,66 @@ impl From<ffi::PublicKey> for PublicKey {
366366
#[cfg(feature = "serde")]
367367
impl ::serde::Serialize for PublicKey {
368368
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
369-
s.serialize_bytes(&self.serialize())
369+
if s.is_human_readable() {
370+
s.collect_str(self)
371+
} else {
372+
s.serialize_bytes(&self.serialize())
373+
}
370374
}
371375
}
372376

373377
#[cfg(feature = "serde")]
374378
impl<'de> ::serde::Deserialize<'de> for PublicKey {
375379
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<PublicKey, D::Error> {
376-
use ::serde::de::Error;
380+
if d.is_human_readable() {
381+
struct HexVisitor;
382+
383+
impl<'de> ::serde::de::Visitor<'de> for HexVisitor {
384+
type Value = PublicKey;
385+
386+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
387+
formatter.write_str("an ASCII hex string")
388+
}
389+
390+
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
391+
where
392+
E: ::serde::de::Error,
393+
{
394+
if let Ok(hex) = str::from_utf8(v) {
395+
str::FromStr::from_str(hex).map_err(E::custom)
396+
} else {
397+
Err(E::invalid_value(::serde::de::Unexpected::Bytes(v), &self))
398+
}
399+
}
400+
401+
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
402+
where
403+
E: ::serde::de::Error,
404+
{
405+
str::FromStr::from_str(v).map_err(E::custom)
406+
}
407+
}
408+
d.deserialize_str(HexVisitor)
409+
} else {
410+
struct BytesVisitor;
411+
412+
impl<'de> ::serde::de::Visitor<'de> for BytesVisitor {
413+
type Value = PublicKey;
377414

378-
let sl: &[u8] = ::serde::Deserialize::deserialize(d)?;
379-
PublicKey::from_slice(sl).map_err(D::Error::custom)
415+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
416+
formatter.write_str("a bytestring")
417+
}
418+
419+
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
420+
where
421+
E: ::serde::de::Error,
422+
{
423+
PublicKey::from_slice(v).map_err(E::custom)
424+
}
425+
}
426+
427+
d.deserialize_bytes(BytesVisitor)
428+
}
380429
}
381430
}
382431

@@ -745,6 +794,9 @@ mod test {
745794
0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54,
746795
0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66,
747796
];
797+
static PK_STR: &'static str = "\
798+
0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166\
799+
";
748800

749801
let s = Secp256k1::new();
750802

@@ -753,6 +805,7 @@ mod test {
753805

754806
assert_tokens(&sk.compact(), &[Token::BorrowedBytes(&SK_BYTES[..])]);
755807
assert_tokens(&sk.readable(), &[Token::BorrowedStr(SK_STR)]);
756-
assert_tokens(&pk, &[Token::BorrowedBytes(&PK_BYTES[..])]);
808+
assert_tokens(&pk.compact(), &[Token::BorrowedBytes(&PK_BYTES[..])]);
809+
assert_tokens(&pk.readable(), &[Token::BorrowedStr(PK_STR)]);
757810
}
758811
}

0 commit comments

Comments
 (0)