1
1
use anyhow:: { anyhow, bail, Result } ;
2
- use base64:: { engine:: general_purpose:: STANDARD as engine, Engine as _} ;
3
2
use ed25519_consensus:: { SigningKey as Ed25519SigningKey , VerificationKey as Ed25519VerifyingKey } ;
4
3
use p256:: ecdsa:: {
5
4
signature:: DigestVerifier , SigningKey as Secp256r1SigningKey ,
@@ -18,7 +17,10 @@ use std::{
18
17
} ;
19
18
20
19
use crate :: { Signature , SigningKey } ;
21
- use prism_serde:: CryptoPayload ;
20
+ use prism_serde:: {
21
+ base64:: { FromBase64 , ToBase64 } ,
22
+ CryptoPayload ,
23
+ } ;
22
24
23
25
#[ derive( Clone , Serialize , Deserialize , Debug , PartialEq , Eq ) ]
24
26
#[ serde( try_from = "CryptoPayload" , into = "CryptoPayload" ) ]
@@ -162,7 +164,7 @@ impl From<Ed25519SigningKey> for VerifyingKey {
162
164
163
165
impl From < Secp256k1SigningKey > for VerifyingKey {
164
166
fn from ( sk : Secp256k1SigningKey ) -> Self {
165
- sk. public_key ( SECP256K1 ) . into ( )
167
+ VerifyingKey :: Secp256k1 ( sk. public_key ( SECP256K1 ) )
166
168
}
167
169
}
168
170
@@ -182,14 +184,14 @@ impl From<SigningKey> for VerifyingKey {
182
184
}
183
185
}
184
186
185
- impl TryFrom < String > for VerifyingKey {
187
+ impl FromBase64 for VerifyingKey {
186
188
type Error = anyhow:: Error ;
187
189
188
190
/// Attempts to create a `VerifyingKey` from a base64-encoded string.
189
191
///
190
192
/// # Arguments
191
193
///
192
- /// * `s ` - The base64-encoded string representation of the public key.
194
+ /// * `base64 ` - The base64-encoded string representation of the public key.
193
195
///
194
196
/// Depending on the length of the input string, the function will attempt to
195
197
/// decode it and create a `VerifyingKey` instance. According to the specifications,
@@ -200,9 +202,8 @@ impl TryFrom<String> for VerifyingKey {
200
202
///
201
203
/// * `Ok(VerifyingKey)` if the conversion was successful.
202
204
/// * `Err` if the input is invalid or the conversion failed.
203
- fn try_from ( s : String ) -> std:: result:: Result < Self , Self :: Error > {
204
- let bytes =
205
- engine. decode ( s) . map_err ( |e| anyhow ! ( "Failed to decode base64 string: {}" , e) ) ?;
205
+ fn from_base64 < T : AsRef < [ u8 ] > > ( base64 : T ) -> Result < Self , Self :: Error > {
206
+ let bytes = Vec :: < u8 > :: from_base64 ( base64) ?;
206
207
207
208
match bytes. len ( ) {
208
209
32 => {
@@ -220,9 +221,17 @@ impl TryFrom<String> for VerifyingKey {
220
221
}
221
222
}
222
223
224
+ impl TryFrom < String > for VerifyingKey {
225
+ type Error = anyhow:: Error ;
226
+
227
+ fn try_from ( s : String ) -> std:: result:: Result < Self , Self :: Error > {
228
+ Self :: from_base64 ( s)
229
+ }
230
+ }
231
+
223
232
impl std:: fmt:: Display for VerifyingKey {
224
233
fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
225
- let encoded = engine . encode ( self . to_bytes ( ) ) ;
234
+ let encoded = self . to_bytes ( ) . to_base64 ( ) ;
226
235
write ! ( f, "{}" , encoded)
227
236
}
228
237
}
0 commit comments