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
mod ed25519;
pub use ed25519::Ed25519Signature;
use crate::Error;
use bee_common::packable::{Packable, Read, Write};
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", content = "data")
)]
pub enum SignatureUnlock {
Ed25519(Ed25519Signature),
}
impl SignatureUnlock {
pub const KIND: u8 = 0;
pub fn kind(&self) -> u8 {
match self {
Self::Ed25519(_) => Ed25519Signature::KIND,
}
}
}
impl From<Ed25519Signature> for SignatureUnlock {
fn from(signature: Ed25519Signature) -> Self {
Self::Ed25519(signature)
}
}
impl Packable for SignatureUnlock {
type Error = Error;
fn packed_len(&self) -> usize {
match self {
Self::Ed25519(signature) => Ed25519Signature::KIND.packed_len() + signature.packed_len(),
}
}
fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
match self {
Self::Ed25519(signature) => {
Ed25519Signature::KIND.pack(writer)?;
signature.pack(writer)?;
}
}
Ok(())
}
fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error> {
Ok(match u8::unpack_inner::<R, CHECK>(reader)? {
Ed25519Signature::KIND => Ed25519Signature::unpack_inner::<R, CHECK>(reader)?.into(),
k => return Err(Self::Error::InvalidSignatureKind(k)),
})
}
}