Bläddra i källkod

Make constants publicly available

develop
Silvestr Predko 2 år sedan
förälder
incheckning
fe64dfd024

+ 15
- 13
common-api/src/crypto/ed25519.rs Visa fil

3
 //! Used Dalek cryptography, and implemented [`Borsh`](https://borsh.io/) serialization for them
3
 //! Used Dalek cryptography, and implemented [`Borsh`](https://borsh.io/) serialization for them
4
 
4
 
5
 use borsh::{BorshDeserialize, BorshSerialize};
5
 use borsh::{BorshDeserialize, BorshSerialize};
6
-use ed25519_dalek::{
7
-    ExpandedSecretKey, PublicKey, SecretKey, Signature, Verifier, PUBLIC_KEY_LENGTH,
8
-    SECRET_KEY_LENGTH, SIGNATURE_LENGTH,
9
-};
6
+use ed25519_dalek::{ExpandedSecretKey, PublicKey, SecretKey, Signature, Verifier};
10
 use itertools::Itertools;
7
 use itertools::Itertools;
11
 use serde::{Deserialize, Serialize};
8
 use serde::{Deserialize, Serialize};
12
 use std::{
9
 use std::{
17
 
14
 
18
 use super::{split_encoded_str, Error, Key, Result, ED25519};
15
 use super::{split_encoded_str, Error, Key, Result, ED25519};
19
 
16
 
17
+pub use ed25519_dalek::{
18
+    PUBLIC_KEY_LENGTH as ED25519_PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH as ED25519_SECRET_KEY_LENGTH,
19
+    SIGNATURE_LENGTH as ED25519_SIGNATURE_LENGTH,
20
+};
21
+
20
 #[derive(Copy, Clone, Default, Eq, PartialEq)]
22
 #[derive(Copy, Clone, Default, Eq, PartialEq)]
21
 pub struct Ed25519PublicKey(PublicKey);
23
 pub struct Ed25519PublicKey(PublicKey);
22
 
24
 
28
     }
30
     }
29
 
31
 
30
     #[inline]
32
     #[inline]
31
-    pub fn as_bytes(&self) -> &[u8; PUBLIC_KEY_LENGTH] {
33
+    pub fn as_bytes(&self) -> &[u8; ED25519_PUBLIC_KEY_LENGTH] {
32
         self.0.as_bytes()
34
         self.0.as_bytes()
33
     }
35
     }
34
 }
36
 }
35
 
37
 
36
-impl Key<PUBLIC_KEY_LENGTH> for Ed25519PublicKey {
38
+impl Key<ED25519_PUBLIC_KEY_LENGTH> for Ed25519PublicKey {
37
     const KEY_TYPE: &'static str = ED25519;
39
     const KEY_TYPE: &'static str = ED25519;
38
 
40
 
39
     #[inline]
41
     #[inline]
40
-    fn to_bytes(&self) -> [u8; PUBLIC_KEY_LENGTH] {
42
+    fn to_bytes(&self) -> [u8; ED25519_PUBLIC_KEY_LENGTH] {
41
         self.0.to_bytes()
43
         self.0.to_bytes()
42
     }
44
     }
43
 
45
 
115
                 Error::from_string::<Ed25519SecretKey>(bs58_encoded.to_owned(), err.to_string())
117
                 Error::from_string::<Ed25519SecretKey>(bs58_encoded.to_owned(), err.to_string())
116
             })?
118
             })?
117
             .into_iter()
119
             .into_iter()
118
-            .take(SECRET_KEY_LENGTH)
120
+            .take(ED25519_SECRET_KEY_LENGTH)
119
             .collect_vec();
121
             .collect_vec();
120
         Self::try_from_bytes(&expanded_key_bytes)
122
         Self::try_from_bytes(&expanded_key_bytes)
121
     }
123
     }
122
 
124
 
123
     #[inline]
125
     #[inline]
124
-    pub fn as_bytes(&self) -> &[u8; PUBLIC_KEY_LENGTH] {
126
+    pub fn as_bytes(&self) -> &[u8; ED25519_PUBLIC_KEY_LENGTH] {
125
         self.0.as_bytes()
127
         self.0.as_bytes()
126
     }
128
     }
127
 }
129
 }
128
 
130
 
129
-impl Key<SECRET_KEY_LENGTH> for Ed25519SecretKey {
131
+impl Key<ED25519_SECRET_KEY_LENGTH> for Ed25519SecretKey {
130
     const KEY_TYPE: &'static str = ED25519;
132
     const KEY_TYPE: &'static str = ED25519;
131
 
133
 
132
     #[inline]
134
     #[inline]
133
-    fn to_bytes(&self) -> [u8; SECRET_KEY_LENGTH] {
135
+    fn to_bytes(&self) -> [u8; ED25519_SECRET_KEY_LENGTH] {
134
         self.0.to_bytes()
136
         self.0.to_bytes()
135
     }
137
     }
136
 
138
 
157
 #[derive(Copy, Clone, Eq, PartialEq)]
159
 #[derive(Copy, Clone, Eq, PartialEq)]
158
 pub struct Ed25519Signature(Signature);
160
 pub struct Ed25519Signature(Signature);
159
 
161
 
160
-impl Key<SIGNATURE_LENGTH> for Ed25519Signature {
162
+impl Key<ED25519_SIGNATURE_LENGTH> for Ed25519Signature {
161
     const KEY_TYPE: &'static str = ED25519;
163
     const KEY_TYPE: &'static str = ED25519;
162
 
164
 
163
     #[inline]
165
     #[inline]
164
-    fn to_bytes(&self) -> [u8; SIGNATURE_LENGTH] {
166
+    fn to_bytes(&self) -> [u8; ED25519_SIGNATURE_LENGTH] {
165
         self.0.to_bytes()
167
         self.0.to_bytes()
166
     }
168
     }
167
 
169
 

+ 13
- 13
common-api/src/crypto/key_exchange.rs Visa fil

11
 
11
 
12
 use super::{Error, Key, Result, X25519};
12
 use super::{Error, Key, Result, X25519};
13
 
13
 
14
-const PUBLIC_LENGTH: usize = 32_usize;
15
-const SECRET_LENGTH: usize = 32_usize;
14
+pub const PUBLIC_KEY_LENGTH: usize = 32_usize;
15
+pub const SECRET_KEY_LENGTH: usize = 32_usize;
16
 
16
 
17
 pub struct Secret(StaticSecret);
17
 pub struct Secret(StaticSecret);
18
 
18
 
53
     }
53
     }
54
 }
54
 }
55
 
55
 
56
-impl Key<SECRET_LENGTH> for Secret {
56
+impl Key<SECRET_KEY_LENGTH> for Secret {
57
     const KEY_TYPE: &'static str = X25519;
57
     const KEY_TYPE: &'static str = X25519;
58
 
58
 
59
     #[inline]
59
     #[inline]
60
-    fn to_bytes(&self) -> [u8; SECRET_LENGTH] {
60
+    fn to_bytes(&self) -> [u8; SECRET_KEY_LENGTH] {
61
         self.0.to_bytes()
61
         self.0.to_bytes()
62
     }
62
     }
63
 
63
 
64
     fn try_from_bytes(buf: &[u8]) -> Result<Self> {
64
     fn try_from_bytes(buf: &[u8]) -> Result<Self> {
65
-        if buf.len() != SECRET_LENGTH {
65
+        if buf.len() != SECRET_KEY_LENGTH {
66
             return Err(Error::from_bytes::<Public>(
66
             return Err(Error::from_bytes::<Public>(
67
                 buf,
67
                 buf,
68
                 format!(
68
                 format!(
69
-                    "input buffer size \"{}\" not equal to secret key size \"{SECRET_LENGTH}\"",
69
+                    "input buffer size \"{}\" not equal to secret key size \"{SECRET_KEY_LENGTH}\"",
70
                     buf.len()
70
                     buf.len()
71
                 ),
71
                 ),
72
             ));
72
             ));
73
         }
73
         }
74
 
74
 
75
-        let mut temp_buf = [0_u8; SECRET_LENGTH];
75
+        let mut temp_buf = [0_u8; SECRET_KEY_LENGTH];
76
         temp_buf.copy_from_slice(buf);
76
         temp_buf.copy_from_slice(buf);
77
 
77
 
78
         Ok(Self(StaticSecret::from(temp_buf)))
78
         Ok(Self(StaticSecret::from(temp_buf)))
79
     }
79
     }
80
 }
80
 }
81
 
81
 
82
-impl Key<PUBLIC_LENGTH> for Public {
82
+impl Key<PUBLIC_KEY_LENGTH> for Public {
83
     const KEY_TYPE: &'static str = X25519;
83
     const KEY_TYPE: &'static str = X25519;
84
 
84
 
85
     #[inline]
85
     #[inline]
86
-    fn to_bytes(&self) -> [u8; PUBLIC_LENGTH] {
86
+    fn to_bytes(&self) -> [u8; PUBLIC_KEY_LENGTH] {
87
         self.0.to_bytes()
87
         self.0.to_bytes()
88
     }
88
     }
89
 
89
 
90
     fn try_from_bytes(buf: &[u8]) -> Result<Self> {
90
     fn try_from_bytes(buf: &[u8]) -> Result<Self> {
91
-        if buf.len() != PUBLIC_LENGTH {
91
+        if buf.len() != PUBLIC_KEY_LENGTH {
92
             return Err(Error::from_bytes::<Public>(
92
             return Err(Error::from_bytes::<Public>(
93
                 buf,
93
                 buf,
94
                 format!(
94
                 format!(
95
-                    "input buffer size \"{}\" not equal to public key size \"{PUBLIC_LENGTH}\"",
95
+                    "input buffer size \"{}\" not equal to public key size \"{PUBLIC_KEY_LENGTH}\"",
96
                     buf.len()
96
                     buf.len()
97
                 ),
97
                 ),
98
             ));
98
             ));
99
         }
99
         }
100
 
100
 
101
-        let mut temp_buf = [0_u8; PUBLIC_LENGTH];
101
+        let mut temp_buf = [0_u8; PUBLIC_KEY_LENGTH];
102
         temp_buf.copy_from_slice(buf);
102
         temp_buf.copy_from_slice(buf);
103
 
103
 
104
         Ok(Self(PublicKey::from(temp_buf)))
104
         Ok(Self(PublicKey::from(temp_buf)))
120
     /// ## Returns
120
     /// ## Returns
121
     /// Byte array with a shared secret key
121
     /// Byte array with a shared secret key
122
     ///
122
     ///
123
-    pub fn exchange(&self, other_public: &Public) -> [u8; SECRET_LENGTH] {
123
+    pub fn exchange(&self, other_public: &Public) -> [u8; SECRET_KEY_LENGTH] {
124
         self.0.diffie_hellman(&other_public.0).to_bytes()
124
         self.0.diffie_hellman(&other_public.0).to_bytes()
125
     }
125
     }
126
 }
126
 }

+ 5
- 2
common-api/src/crypto/mod.rs Visa fil

38
 pub mod key_exchange;
38
 pub mod key_exchange;
39
 pub mod prelude {
39
 pub mod prelude {
40
     pub use super::{
40
     pub use super::{
41
-        ed25519::{Ed25519PublicKey, Ed25519SecretKey, Ed25519Signature, Keypair},
42
-        key_exchange::{Public, Secret},
41
+        ed25519::{
42
+            Ed25519PublicKey, Ed25519SecretKey, Ed25519Signature, Keypair,
43
+            ED25519_PUBLIC_KEY_LENGTH, ED25519_SECRET_KEY_LENGTH, ED25519_SIGNATURE_LENGTH,
44
+        },
45
+        key_exchange::{Public, Secret, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH},
43
         Error, Key,
46
         Error, Key,
44
     };
47
     };
45
 }
48
 }

Laddar…
Avbryt
Spara