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
#![no_std]
pub use signature::{self, Error};
use core::fmt::{self, Debug};
pub const SIGNATURE_LENGTH: usize = 64;
#[derive(Copy, Clone)]
pub struct Signature(pub [u8; SIGNATURE_LENGTH]);
impl Signature {
pub fn new(bytes: [u8; SIGNATURE_LENGTH]) -> Self {
Self::from(bytes)
}
pub fn to_bytes(&self) -> [u8; SIGNATURE_LENGTH] {
self.0.clone()
}
}
impl signature::Signature for Signature {
fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> Result<Self, Error> {
let bytes = bytes.as_ref();
if bytes.len() == SIGNATURE_LENGTH {
let mut arr = [0u8; SIGNATURE_LENGTH];
arr.copy_from_slice(bytes);
Ok(Signature(arr))
} else {
Err(Error::new())
}
}
}
impl AsRef<[u8]> for Signature {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl From<[u8; SIGNATURE_LENGTH]> for Signature {
fn from(bytes: [u8; SIGNATURE_LENGTH]) -> Signature {
Signature(bytes)
}
}
impl Debug for Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Signature({:?})", &self.0[..])
}
}
impl PartialEq for Signature {
fn eq(&self, other: &Self) -> bool {
self.as_ref().eq(other.as_ref())
}
}
impl Eq for Signature {}