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
pub use ed25519::{Signature as Ed25519Signature, SIGNATURE_LENGTH as ED25519_SIGNATURE_SIZE};
pub use signature::{Signer, Verifier};
#[cfg(feature = "secp256k1")]
pub use k256::ecdsa::Signature as Secp256k1;
use crate::{Error, Kind};
use std::convert::TryFrom;
use tendermint_proto::Protobuf;
#[derive(Copy, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Signature {
Ed25519(Ed25519Signature),
None,
}
impl Protobuf<Vec<u8>> for Signature {}
impl TryFrom<Vec<u8>> for Signature {
type Error = Error;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
if value.is_empty() {
return Ok(Self::default());
}
if value.len() != ED25519_SIGNATURE_SIZE {
return Err(Kind::InvalidSignatureIdLength.into());
}
let mut slice: [u8; ED25519_SIGNATURE_SIZE] = [0; ED25519_SIGNATURE_SIZE];
slice.copy_from_slice(&value[..]);
Ok(Signature::Ed25519(Ed25519Signature::new(slice)))
}
}
impl From<Signature> for Vec<u8> {
fn from(value: Signature) -> Self {
value.as_bytes().to_vec()
}
}
impl Default for Signature {
fn default() -> Self {
Signature::None
}
}
impl Signature {
pub fn algorithm(&self) -> Algorithm {
match self {
Signature::Ed25519(_) => Algorithm::Ed25519,
Signature::None => Algorithm::Ed25519,
}
}
pub fn ed25519(self) -> Option<Ed25519Signature> {
match self {
Signature::Ed25519(sig) => Some(sig),
Signature::None => None,
}
}
pub fn as_bytes(&self) -> &[u8] {
self.as_ref()
}
pub fn to_bytes(self) -> Vec<u8> {
self.as_bytes().to_vec()
}
}
impl AsRef<[u8]> for Signature {
fn as_ref(&self) -> &[u8] {
match self {
Signature::Ed25519(sig) => sig.as_ref(),
Signature::None => &[],
}
}
}
impl From<Ed25519Signature> for Signature {
fn from(pk: Ed25519Signature) -> Signature {
Signature::Ed25519(pk)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Algorithm {
EcdsaSecp256k1,
Ed25519,
}