core_crypto/
public_key.rs

1// Copyright 2021 BlockPuppets developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9#[cfg(feature = "serde")]
10use hex::ToHex;
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Deserializer, Serialize, Serializer};
13#[cfg(feature = "serde")]
14use serde_bytes::{ByteBuf as SerdeByteBuf, Bytes as SerdeBytes};
15
16use super::KEY_BYTES_SIZE;
17
18construct_fixed_hash! {
19    /// 256 bit hash type.
20    pub struct PublicKey(KEY_BYTES_SIZE);
21}
22
23#[cfg(feature = "serde")]
24impl Serialize for PublicKey {
25    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
26    where
27        S: Serializer,
28    {
29        let bytes = self.as_bytes();
30        SerdeBytes::new(bytes).serialize(serializer)
31    }
32}
33
34#[cfg(feature = "serde")]
35impl<'d> Deserialize<'d> for PublicKey {
36    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
37    where
38        D: Deserializer<'d>,
39    {
40        let bytes = <SerdeByteBuf>::deserialize(deserializer)?;
41        Ok(PublicKey::from_slice(bytes.as_ref()))
42    }
43}