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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use crate::{error::CryptoError, signature::Signature};
use ed25519_dalek::{PublicKey, SecretKey, Signer};
use rand_7::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
use super::bytes_key_from_str;
pub type PubKey = [u8; 32];
pub type SecKey = [u8; 32];
#[derive(Debug)]
pub struct SigningKeypair(ed25519_dalek::Keypair);
impl<'de> Deserialize<'de> for SigningKeypair {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let hex_encoded_str = String::deserialize(deserializer)?;
let bytes = hex::decode(hex_encoded_str).map_err(serde::de::Error::custom)?;
Ok(Self(
ed25519_dalek::Keypair::from_bytes(&bytes).map_err(serde::de::Error::custom)?,
))
}
}
impl Serialize for SigningKeypair {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let hex = hex::encode(self.0.to_bytes());
String::serialize(&hex, serializer)
}
}
impl TryFrom<Vec<u8>> for SigningKeypair {
type Error = CryptoError;
#[tracing::instrument(level = "debug", skip(value))]
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
Ok(Self(
ed25519_dalek::Keypair::from_bytes(value.as_slice())
.map_err(|e| CryptoError::InvalidSignatureBytesError(e.to_string()))?,
))
}
}
impl PartialEq for SigningKeypair {
fn eq(&self, other: &Self) -> bool {
self.public() == other.public() && self.secret() == other.secret()
}
}
impl From<&SigningKeypair> for SigningKeypair {
fn from(other: &SigningKeypair) -> Self {
Self(ed25519_dalek::Keypair {
public: PublicKey::from_bytes(&other.public()).unwrap(),
secret: SecretKey::from_bytes(&other.secret()).unwrap(),
})
}
}
impl SigningKeypair {
#[tracing::instrument(level = "debug", skip(csprng))]
pub fn generate<R>(csprng: &mut R) -> Self
where
R: CryptoRng + RngCore,
{
Self(ed25519_dalek::Keypair::generate(csprng))
}
#[tracing::instrument(level = "debug", skip(pubkey, seckey))]
pub fn try_from_bytes_slices(pubkey: PubKey, seckey: SecKey) -> Result<Self, CryptoError> {
Ok(Self(
ed25519_dalek::Keypair::from_bytes([seckey, pubkey].concat().as_slice())
.map_err(|e| CryptoError::InvalidSignatureBytesError(e.to_string()))?,
))
}
#[tracing::instrument(level = "debug", skip(public_key))]
pub fn try_from_str(public_key: &str, secret_key: &str) -> Result<Self, CryptoError> {
let pubkey = bytes_key_from_str(public_key)?;
let seckey = bytes_key_from_str(secret_key)?;
Self::try_from_bytes_slices(pubkey, seckey)
}
#[tracing::instrument(level = "debug", skip(secret_key_bytes))]
pub fn try_from_secret_bytes(secret_key_bytes: &SecKey) -> Result<Self, CryptoError> {
let sec_key = ed25519_dalek::SecretKey::from_bytes(secret_key_bytes)
.map_err(|e| CryptoError::InvalidSignatureBytesError(e.to_string()))?;
let pub_key = ed25519_dalek::PublicKey::from(&sec_key);
Ok(Self(ed25519_dalek::Keypair {
secret: sec_key,
public: pub_key,
}))
}
#[tracing::instrument(level = "debug", skip(self))]
pub fn public(&self) -> PubKey {
self.0.public.to_bytes()
}
#[tracing::instrument(level = "debug", skip(self))]
pub fn secret(&self) -> SecKey {
self.0.secret.to_bytes()
}
#[tracing::instrument(level = "debug", skip(self))]
pub fn to_bytes(&self) -> Vec<u8> {
Vec::from(self.0.to_bytes())
}
#[tracing::instrument(level = "debug", skip(self))]
pub fn sign(&self, msg: &[u8]) -> Signature {
Signature(self.0.sign(msg))
}
}
#[cfg(test)]
mod tests {
use crate::common::test_utilities::{SIGNING_PUBLIC_KEY, SIGNING_SECRET_KEY};
use crate::identity::signing_keypair::SigningKeypair;
#[test]
fn should_create_keypair_when_keys_have_proper_length() {
let keypair = SigningKeypair::try_from_str(SIGNING_PUBLIC_KEY, SIGNING_SECRET_KEY);
assert!(keypair.is_ok());
}
#[test]
fn should_not_create_keypair_when_pub_key_is_too_short() {
let keypair = SigningKeypair::try_from_str("", SIGNING_SECRET_KEY);
assert!(keypair.is_err());
}
#[test]
fn should_not_create_keypair_when_pub_key_is_too_long() {
let keypair = SigningKeypair::try_from_str(
"1234567890123456789012345678901234567890123456789012345678901234567890",
SIGNING_SECRET_KEY,
);
assert!(keypair.is_err());
}
#[test]
fn should_not_create_keypair_when_sec_key_is_too_short() {
let keypair = SigningKeypair::try_from_str(SIGNING_PUBLIC_KEY, "");
assert!(keypair.is_err());
}
#[test]
fn should_not_create_keypair_when_sec_key_is_too_long() {
let keypair = SigningKeypair::try_from_str(
SIGNING_PUBLIC_KEY,
"1234567890123456789012345678901234567890123456789012345678901234567890",
);
assert!(keypair.is_err());
}
}