Skip to content

Commit

Permalink
Add serde feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Apr 25, 2024
1 parent 2bf35ab commit d3619d1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
/.vscode/
/target/
Cargo.lock
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "everscale-crypto"
description = "Cryptography primitives for Everscale"
repository = "https://github.com/broxus/everscale-crypto"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
include = ["src/**/*.rs", "README.md", "LICENSE"]
license-file = "./LICENSE"
Expand All @@ -14,6 +14,8 @@ hex = "0.4.3"
rand = "0.8.4"
sha2 = "0.10"
tl-proto = { version = "0.4.0", features = ["derive"], optional = true }
serde = { version = "1", optional = true }

[features]
default = ["tl-proto"]
serde = ["dep:serde"]
62 changes: 61 additions & 1 deletion src/ed25519/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl PartialEq for PublicKey {

impl Eq for PublicKey {}

impl std::fmt::Debug for PublicKey {
impl std::fmt::Display for PublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut output = [0u8; 64];
hex::encode_to_slice(self.compressed.as_bytes(), &mut output).ok();
Expand All @@ -198,6 +198,66 @@ impl std::fmt::Debug for PublicKey {
}
}

impl std::fmt::Debug for PublicKey {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}

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

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::de::{Error, Visitor};

struct BytesVisitor;

impl<'de> Visitor<'de> for BytesVisitor {
type Value = [u8; 32];

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("hex-encoded public key")
}

fn visit_str<E: Error>(self, value: &str) -> Result<Self::Value, E> {
let mut result = [0; 32];
match hex::decode_to_slice(value, &mut result) {
Ok(()) => Ok(result),
Err(_) => Err(Error::invalid_value(
serde::de::Unexpected::Str(value),
&self,
)),
}
}
}

let bytes = if deserializer.is_human_readable() {
deserializer.deserialize_str(BytesVisitor)
} else {
<[u8; 32]>::deserialize(deserializer)
}?;

Self::from_bytes(bytes).ok_or_else(|| Error::custom("invalid public key"))
}
}

#[derive(Copy, Clone)]
pub struct ExpandedSecretKey {
key: Scalar,
Expand Down

0 comments on commit d3619d1

Please sign in to comment.