@@ -25,23 +25,31 @@ pub const PUBLIC_KEY_SIZE: usize = 32;
25
25
/// Size of a X25519 shared secret
26
26
pub const SHARED_SECRET_SIZE : usize = 32 ;
27
27
28
- #[ derive( Clone , Copy , Eq , PartialEq , Hash , Debug ) ]
29
- pub enum EncryptionKeyError {
30
- InvalidPublicKey ,
31
- InvalidPrivateKey ,
28
+ #[ derive( Clone , Copy , Eq , PartialEq , Debug ) ]
29
+ pub enum KeyRecoveryError {
30
+ InvalidPublicKeyBytes ,
31
+ InvalidPrivateKeyBytes ,
32
+ MalformedString ( bs58:: decode:: Error ) ,
33
+ }
34
+
35
+ impl From < bs58:: decode:: Error > for KeyRecoveryError {
36
+ fn from ( err : bs58:: decode:: Error ) -> Self {
37
+ KeyRecoveryError :: MalformedString ( err)
38
+ }
32
39
}
33
40
34
41
// required for std::error::Error
35
- impl Display for EncryptionKeyError {
42
+ impl Display for KeyRecoveryError {
36
43
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
37
44
match self {
38
- EncryptionKeyError :: InvalidPrivateKey => write ! ( f, "Invalid private key" ) ,
39
- EncryptionKeyError :: InvalidPublicKey => write ! ( f, "Invalid public key" ) ,
45
+ KeyRecoveryError :: InvalidPrivateKeyBytes => write ! ( f, "Invalid private key bytes" ) ,
46
+ KeyRecoveryError :: InvalidPublicKeyBytes => write ! ( f, "Invalid public key bytes" ) ,
47
+ KeyRecoveryError :: MalformedString ( err) => write ! ( f, "malformed string - {}" , err) ,
40
48
}
41
49
}
42
50
}
43
51
44
- impl std:: error:: Error for EncryptionKeyError { }
52
+ impl std:: error:: Error for KeyRecoveryError { }
45
53
46
54
pub struct KeyPair {
47
55
pub ( crate ) private_key : PrivateKey ,
@@ -72,7 +80,7 @@ impl KeyPair {
72
80
& self . public_key
73
81
}
74
82
75
- pub fn from_bytes ( priv_bytes : & [ u8 ] , pub_bytes : & [ u8 ] ) -> Result < Self , EncryptionKeyError > {
83
+ pub fn from_bytes ( priv_bytes : & [ u8 ] , pub_bytes : & [ u8 ] ) -> Result < Self , KeyRecoveryError > {
76
84
Ok ( KeyPair {
77
85
private_key : PrivateKey :: from_bytes ( priv_bytes) ?,
78
86
public_key : PublicKey :: from_bytes ( pub_bytes) ?,
@@ -108,9 +116,9 @@ impl PublicKey {
108
116
* self . 0 . as_bytes ( )
109
117
}
110
118
111
- pub fn from_bytes ( b : & [ u8 ] ) -> Result < Self , EncryptionKeyError > {
119
+ pub fn from_bytes ( b : & [ u8 ] ) -> Result < Self , KeyRecoveryError > {
112
120
if b. len ( ) != PUBLIC_KEY_SIZE {
113
- return Err ( EncryptionKeyError :: InvalidPublicKey ) ;
121
+ return Err ( KeyRecoveryError :: InvalidPublicKeyBytes ) ;
114
122
}
115
123
let mut bytes = [ 0 ; PUBLIC_KEY_SIZE ] ;
116
124
bytes. copy_from_slice ( & b[ ..PUBLIC_KEY_SIZE ] ) ;
@@ -121,16 +129,14 @@ impl PublicKey {
121
129
bs58:: encode ( & self . to_bytes ( ) ) . into_string ( )
122
130
}
123
131
124
- pub fn from_base58_string < S : Into < String > > ( val : S ) -> Result < Self , EncryptionKeyError > {
125
- let bytes = bs58:: decode ( val. into ( ) )
126
- . into_vec ( )
127
- . expect ( "TODO: deal with this failure case" ) ;
132
+ pub fn from_base58_string < S : Into < String > > ( val : S ) -> Result < Self , KeyRecoveryError > {
133
+ let bytes = bs58:: decode ( val. into ( ) ) . into_vec ( ) ?;
128
134
Self :: from_bytes ( & bytes)
129
135
}
130
136
}
131
137
132
138
impl PemStorableKey for PublicKey {
133
- type Error = EncryptionKeyError ;
139
+ type Error = KeyRecoveryError ;
134
140
135
141
fn pem_type ( ) -> & ' static str {
136
142
"X25519 PUBLIC KEY"
@@ -159,9 +165,9 @@ impl PrivateKey {
159
165
self . 0 . to_bytes ( )
160
166
}
161
167
162
- pub fn from_bytes ( b : & [ u8 ] ) -> Result < Self , EncryptionKeyError > {
168
+ pub fn from_bytes ( b : & [ u8 ] ) -> Result < Self , KeyRecoveryError > {
163
169
if b. len ( ) != PRIVATE_KEY_SIZE {
164
- return Err ( EncryptionKeyError :: InvalidPrivateKey ) ;
170
+ return Err ( KeyRecoveryError :: InvalidPrivateKeyBytes ) ;
165
171
}
166
172
let mut bytes = [ 0 ; 32 ] ;
167
173
bytes. copy_from_slice ( & b[ ..PRIVATE_KEY_SIZE ] ) ;
@@ -172,10 +178,8 @@ impl PrivateKey {
172
178
bs58:: encode ( & self . to_bytes ( ) ) . into_string ( )
173
179
}
174
180
175
- pub fn from_base58_string < S : Into < String > > ( val : S ) -> Result < Self , EncryptionKeyError > {
176
- let bytes = bs58:: decode ( val. into ( ) )
177
- . into_vec ( )
178
- . expect ( "TODO: deal with this failure case" ) ;
181
+ pub fn from_base58_string < S : Into < String > > ( val : S ) -> Result < Self , KeyRecoveryError > {
182
+ let bytes = bs58:: decode ( val. into ( ) ) . into_vec ( ) ?;
179
183
Self :: from_bytes ( & bytes)
180
184
}
181
185
@@ -186,7 +190,7 @@ impl PrivateKey {
186
190
}
187
191
188
192
impl PemStorableKey for PrivateKey {
189
- type Error = EncryptionKeyError ;
193
+ type Error = KeyRecoveryError ;
190
194
191
195
fn pem_type ( ) -> & ' static str {
192
196
"X25519 PRIVATE KEY"
0 commit comments