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
use diem_crypto::PrivateKey;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
pub struct ConfigKey<T: PrivateKey + Serialize> {
#[serde(bound(deserialize = "T: Deserialize<'de>"))]
pub(crate) key: T,
}
impl<T: DeserializeOwned + PrivateKey + Serialize> ConfigKey<T> {
pub(crate) fn new(key: T) -> Self {
Self { key }
}
pub fn private_key(&self) -> T {
self.clone().key
}
pub fn public_key(&self) -> T::PublicKeyMaterial {
diem_crypto::PrivateKey::public_key(&self.key)
}
}
impl<T: DeserializeOwned + PrivateKey + Serialize> Clone for ConfigKey<T> {
fn clone(&self) -> Self {
bcs::from_bytes(&bcs::to_bytes(self).unwrap()).unwrap()
}
}
#[cfg(test)]
impl<T: PrivateKey + Serialize + diem_crypto::Uniform> Default for ConfigKey<T> {
fn default() -> Self {
Self {
key: diem_crypto::Uniform::generate_for_testing(),
}
}
}
impl<T: PrivateKey + Serialize> PartialEq for ConfigKey<T> {
fn eq(&self, other: &Self) -> bool {
bcs::to_bytes(&self).unwrap() == bcs::to_bytes(&other).unwrap()
}
}