|
@@ -2,14 +2,18 @@
|
2
|
2
|
//! ---
|
3
|
3
|
//! Used Dalek cryptography, and implemented [`Borsh`](https://borsh.io/) serialization for it
|
4
|
4
|
|
5
|
|
-use borsh::{BorshDeserialize, BorshSerialize};
|
|
5
|
+use super::{
|
|
6
|
+ ed25519::{Ed25519PublicKey, Ed25519SecretKey},
|
|
7
|
+ Error, Key, Result, X25519,
|
|
8
|
+};
|
6
|
9
|
use std::{
|
7
|
10
|
fmt::Display,
|
8
|
11
|
io::{Error as IoError, ErrorKind},
|
9
|
12
|
};
|
10
|
|
-use x25519_dalek::{PublicKey as DalekPublicKey, StaticSecret};
|
11
|
13
|
|
12
|
|
-use super::{Error, Key, Result, X25519};
|
|
14
|
+use borsh::{BorshDeserialize, BorshSerialize};
|
|
15
|
+use curve25519_dalek::edwards::CompressedEdwardsY;
|
|
16
|
+use x25519_dalek::{PublicKey as DalekPublicKey, StaticSecret};
|
13
|
17
|
|
14
|
18
|
pub const PUBLIC_KEY_LENGTH: usize = 32_usize;
|
15
|
19
|
pub const SECRET_KEY_LENGTH: usize = 32_usize;
|
|
@@ -125,5 +129,37 @@ impl SecretKey {
|
125
|
129
|
}
|
126
|
130
|
}
|
127
|
131
|
|
|
132
|
+impl TryFrom<Ed25519PublicKey> for PublicKey {
|
|
133
|
+ type Error = Error;
|
|
134
|
+
|
|
135
|
+ fn try_from(key: Ed25519PublicKey) -> Result<Self> {
|
|
136
|
+ let edwards_point = CompressedEdwardsY::from_slice(key.as_bytes())
|
|
137
|
+ .decompress()
|
|
138
|
+ .ok_or_else(|| {
|
|
139
|
+ Error::from_bytes::<Ed25519PublicKey>(
|
|
140
|
+ key.as_bytes(),
|
|
141
|
+ "Couldn't decompress an Edwards point".to_owned(),
|
|
142
|
+ )
|
|
143
|
+ })?;
|
|
144
|
+
|
|
145
|
+ PublicKey::try_from_bytes(edwards_point.to_montgomery().as_bytes())
|
|
146
|
+ }
|
|
147
|
+}
|
|
148
|
+
|
|
149
|
+impl TryFrom<Ed25519SecretKey> for SecretKey {
|
|
150
|
+ type Error = Error;
|
|
151
|
+
|
|
152
|
+ fn try_from(key: Ed25519SecretKey) -> Result<Self> {
|
|
153
|
+ use ed25519_dalek::{ExpandedSecretKey, SecretKey as S};
|
|
154
|
+
|
|
155
|
+ let expanded_key =
|
|
156
|
+ ExpandedSecretKey::from(&S::from_bytes(key.as_bytes()).map_err(|err| {
|
|
157
|
+ Error::from_bytes::<Ed25519SecretKey>(key.as_bytes(), err.to_string())
|
|
158
|
+ })?);
|
|
159
|
+
|
|
160
|
+ Self::try_from_bytes(&expanded_key.to_bytes()[..32])
|
|
161
|
+ }
|
|
162
|
+}
|
|
163
|
+
|
128
|
164
|
serde_impl!(SecretKey);
|
129
|
165
|
serde_impl!(PublicKey);
|