1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::Signature;
use ::serde::{de, ser, Deserialize, Serialize};
use core::fmt;
#[cfg(feature = "serde_bytes")]
use serde_bytes_crate as serde_bytes;
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl Serialize for Signature {
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use ser::SerializeTuple;
let mut seq = serializer.serialize_tuple(Signature::BYTE_SIZE)?;
for byte in &self.0[..] {
seq.serialize_element(byte)?;
}
seq.end()
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> Deserialize<'de> for Signature {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct ByteArrayVisitor;
impl<'de> de::Visitor<'de> for ByteArrayVisitor {
type Value = [u8; Signature::BYTE_SIZE];
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("bytestring of length 64")
}
fn visit_seq<A>(self, mut seq: A) -> Result<[u8; Signature::BYTE_SIZE], A::Error>
where
A: de::SeqAccess<'de>,
{
use de::Error;
let mut arr = [0u8; Signature::BYTE_SIZE];
for (i, byte) in arr.iter_mut().enumerate() {
*byte = seq
.next_element()?
.ok_or_else(|| Error::invalid_length(i, &self))?;
}
Ok(arr)
}
}
let bytes = deserializer.deserialize_tuple(Signature::BYTE_SIZE, ByteArrayVisitor)?;
Self::from_bytes(&bytes).map_err(de::Error::custom)
}
}
#[cfg(feature = "serde_bytes")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde_bytes")))]
impl serde_bytes::Serialize for Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_bytes(&self.0)
}
}
#[cfg(feature = "serde_bytes")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde_bytes")))]
impl<'de> serde_bytes::Deserialize<'de> for Signature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct ByteArrayVisitor;
impl<'de> de::Visitor<'de> for ByteArrayVisitor {
type Value = [u8; Signature::BYTE_SIZE];
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("bytestring of length 64")
}
fn visit_bytes<E>(self, bytes: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
use de::Error;
bytes
.try_into()
.map_err(|_| Error::invalid_length(bytes.len(), &self))
}
}
let bytes = deserializer.deserialize_bytes(ByteArrayVisitor)?;
Self::from_bytes(&bytes).map_err(de::Error::custom)
}
}
#[cfg(test)]
mod tests {
use crate::Signature;
use hex_literal::hex;
const SIGNATURE_BYTES: [u8; Signature::BYTE_SIZE] = hex!(
"
e5564300c360ac729086e2cc806e828a
84877f1eb8e5d974d873e06522490155
5fb8821590a33bacc61e39701cf9b46b
d25bf5f0595bbe24655141438e7a100b
"
);
#[test]
fn round_trip() {
let signature = Signature::from_bytes(&SIGNATURE_BYTES).unwrap();
let serialized = bincode::serialize(&signature).unwrap();
let deserialized = bincode::deserialize(&serialized).unwrap();
assert_eq!(signature, deserialized);
}
#[test]
fn overflow() {
let bytes = hex!("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
assert!(bincode::deserialize::<Signature>(&bytes).is_err());
}
}