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
133
134
135
136
137
138
139
use crate::ed25519;
use crate::error::DecodingError;
use crate::key_pair::KeyFormat;
#[cfg(not(target_arch = "wasm32"))]
use crate::rsa;
use crate::secp256k1;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum Signature {
Ed25519(ed25519::Signature),
#[cfg(not(target_arch = "wasm32"))]
Rsa(rsa::Signature),
Secp256k1(secp256k1::Signature),
}
pub struct RawSignature {
pub bytes: Vec<u8>,
pub sig_type: KeyFormat,
}
impl Signature {
fn get_prefix(&self) -> u8 {
use Signature::*;
match self {
Ed25519(_) => KeyFormat::Ed25519.into(),
#[cfg(not(target_arch = "wasm32"))]
Rsa(_) => KeyFormat::Rsa.into(),
Secp256k1(_) => KeyFormat::Secp256k1.into(),
}
}
pub fn encode(&self) -> Vec<u8> {
use Signature::*;
let mut result: Vec<u8> = vec![self.get_prefix()];
match self {
Ed25519(sig) => result.extend(sig.0.clone()),
#[cfg(not(target_arch = "wasm32"))]
Rsa(sig) => result.extend(sig.0.clone()),
Secp256k1(sig) => result.extend(sig.0.clone()),
}
result
}
pub fn decode(bytes: Vec<u8>) -> Result<Self, DecodingError> {
match KeyFormat::try_from(bytes[0])? {
KeyFormat::Ed25519 => Ok(Signature::Ed25519(ed25519::Signature(bytes[1..].to_vec()))),
#[cfg(not(target_arch = "wasm32"))]
KeyFormat::Rsa => Ok(Signature::Rsa(rsa::Signature(bytes[1..].to_vec()))),
KeyFormat::Secp256k1 => Ok(Signature::Secp256k1(secp256k1::Signature(
bytes[1..].to_vec(),
))),
}
}
pub fn to_vec(&self) -> &[u8] {
use Signature::*;
match self {
Ed25519(sig) => &sig.0,
#[cfg(not(target_arch = "wasm32"))]
Rsa(sig) => &sig.0,
Secp256k1(sig) => &sig.0,
}
}
pub fn get_signature_type(&self) -> KeyFormat {
use Signature::*;
match self {
Ed25519(_) => KeyFormat::Ed25519,
#[cfg(not(target_arch = "wasm32"))]
Rsa(_) => KeyFormat::Rsa,
Secp256k1(_) => KeyFormat::Secp256k1,
}
}
pub fn get_raw_signature(&self) -> RawSignature {
RawSignature {
bytes: self.to_vec().to_vec(),
sig_type: self.get_signature_type(),
}
}
pub fn from_bytes(key_format: KeyFormat, bytes: Vec<u8>) -> Self {
match key_format {
KeyFormat::Ed25519 => Signature::Ed25519(ed25519::Signature(bytes)),
#[cfg(not(target_arch = "wasm32"))]
KeyFormat::Rsa => Signature::Rsa(rsa::Signature(bytes)),
KeyFormat::Secp256k1 => Signature::Secp256k1(secp256k1::Signature(bytes)),
}
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn signature_encode_decode() {
let bytes: Vec<u8> = (0..10).collect();
let ed25519_sig = Signature::Ed25519(crate::ed25519::Signature(bytes.clone()));
let secp256k1_sig = Signature::Secp256k1(crate::secp256k1::Signature(bytes.clone()));
#[cfg(not(target_arch = "wasm32"))]
let rsa_sig = Signature::Rsa(crate::rsa::Signature(bytes.clone()));
assert_eq!(
Signature::decode(ed25519_sig.encode()).unwrap(),
ed25519_sig
);
assert_eq!(
Signature::decode(secp256k1_sig.encode()).unwrap(),
secp256k1_sig
);
#[cfg(not(target_arch = "wasm32"))]
assert_eq!(Signature::decode(rsa_sig.encode()).unwrap(), rsa_sig);
}
}