@@ -236,7 +236,10 @@ impl Keypair {
236
236
#[ cfg( all( feature = "rsa" , not( target_arch = "wasm32" ) ) ) ]
237
237
Self :: Rsa ( _) => return Err ( DecodingError :: encoding_unsupported ( "RSA" ) ) ,
238
238
#[ cfg( feature = "secp256k1" ) ]
239
- Self :: Secp256k1 ( _) => return Err ( DecodingError :: encoding_unsupported ( "secp256k1" ) ) ,
239
+ Self :: Secp256k1 ( data) => proto:: PrivateKey {
240
+ Type : proto:: KeyType :: Secp256k1 ,
241
+ Data : data. secret ( ) . to_bytes ( ) . to_vec ( ) ,
242
+ } ,
240
243
#[ cfg( feature = "ecdsa" ) ]
241
244
Self :: Ecdsa ( data) => proto:: PrivateKey {
242
245
Type : proto:: KeyType :: ECDSA ,
@@ -269,7 +272,12 @@ impl Keypair {
269
272
Err ( DecodingError :: missing_feature ( "ed25519" ) )
270
273
}
271
274
proto:: KeyType :: RSA => Err ( DecodingError :: decoding_unsupported ( "RSA" ) ) ,
272
- proto:: KeyType :: Secp256k1 => Err ( DecodingError :: decoding_unsupported ( "secp256k1" ) ) ,
275
+ proto:: KeyType :: Secp256k1 => {
276
+ #[ cfg( feature = "secp256k1" ) ]
277
+ return secp256k1:: SecretKey :: try_from_bytes ( & mut private_key. Data )
278
+ . map ( |key| Keypair :: Secp256k1 ( key. into ( ) ) ) ;
279
+ Err ( DecodingError :: missing_feature ( "secp256k1" ) )
280
+ }
273
281
proto:: KeyType :: ECDSA => {
274
282
#[ cfg( feature = "ecdsa" ) ]
275
283
return ecdsa:: SecretKey :: try_decode_der ( & mut private_key. Data )
@@ -726,6 +734,21 @@ mod tests {
726
734
roundtrip_protobuf_encoding ( & priv_key, & pub_key) ;
727
735
}
728
736
737
+ #[ test]
738
+ #[ cfg( all( feature = "secp256k1" , feature = "peerid" ) ) ]
739
+ fn keypair_protobuf_roundtrip_secp256k1 ( ) {
740
+ let priv_key = Keypair :: from_protobuf_encoding ( & hex_literal:: hex!(
741
+ "0802122053DADF1D5A164D6B4ACDB15E24AA4C5B1D3461BDBD42ABEDB0A4404D56CED8FB"
742
+ ) )
743
+ . unwrap ( ) ;
744
+ let pub_key = PublicKey :: try_decode_protobuf ( & hex_literal:: hex!(
745
+ "08021221037777e994e452c21604f91de093ce415f5432f701dd8cd1a7a6fea0e630bfca99"
746
+ ) )
747
+ . unwrap ( ) ;
748
+
749
+ roundtrip_protobuf_encoding ( & priv_key, & pub_key) ;
750
+ }
751
+
729
752
#[ cfg( feature = "peerid" ) ]
730
753
fn roundtrip_protobuf_encoding ( private_key : & Keypair , public_key : & PublicKey ) {
731
754
assert_eq ! ( & private_key. public( ) , public_key) ;
0 commit comments