@@ -59,6 +59,7 @@ use crate::secp256k1;
5959
6060#[ cfg( feature = "ecdsa" ) ]
6161use crate :: ecdsa;
62+ use crate :: KeyType ;
6263
6364/// Identity keypair of a node.
6465///
@@ -319,6 +320,20 @@ impl Keypair {
319320 ) ) ) ]
320321 unreachable ! ( )
321322 }
323+
324+ /// Return a [`KeyType`] of the [`Keypair`].
325+ pub fn key_type ( & self ) -> KeyType {
326+ match self . keypair {
327+ #[ cfg( feature = "ed25519" ) ]
328+ KeyPairInner :: Ed25519 ( _) => KeyType :: Ed25519 ,
329+ #[ cfg( all( feature = "rsa" , not( target_arch = "wasm32" ) ) ) ]
330+ KeyPairInner :: Rsa ( _) => KeyType :: RSA ,
331+ #[ cfg( feature = "secp256k1" ) ]
332+ KeyPairInner :: Secp256k1 ( _) => KeyType :: Secp256k1 ,
333+ #[ cfg( feature = "ecdsa" ) ]
334+ KeyPairInner :: Ecdsa ( _) => KeyType :: Ecdsa ,
335+ }
336+ }
322337}
323338
324339#[ cfg( feature = "ecdsa" ) ]
@@ -552,6 +567,20 @@ impl PublicKey {
552567 pub fn to_peer_id ( & self ) -> crate :: PeerId {
553568 self . into ( )
554569 }
570+
571+ /// Return a [`KeyType`] of the [`PublicKey`].
572+ pub fn key_type ( & self ) -> KeyType {
573+ match self . publickey {
574+ #[ cfg( feature = "ed25519" ) ]
575+ PublicKeyInner :: Ed25519 ( _) => KeyType :: Ed25519 ,
576+ #[ cfg( all( feature = "rsa" , not( target_arch = "wasm32" ) ) ) ]
577+ PublicKeyInner :: Rsa ( _) => KeyType :: RSA ,
578+ #[ cfg( feature = "secp256k1" ) ]
579+ PublicKeyInner :: Secp256k1 ( _) => KeyType :: Secp256k1 ,
580+ #[ cfg( feature = "ecdsa" ) ]
581+ PublicKeyInner :: Ecdsa ( _) => KeyType :: Ecdsa ,
582+ }
583+ }
555584}
556585
557586#[ cfg( any(
@@ -750,7 +779,7 @@ mod tests {
750779 . unwrap ( ) ;
751780 let pub_key = PublicKey :: try_decode_protobuf ( & hex_literal:: hex!( "0803125b3059301306072a8648ce3d020106082a8648ce3d03010703420004de3d300fa36ae0e8f5d530899d83abab44abf3161f162a4bc901d8e6ecda020e8b6d5f8da30525e71d6851510c098e5c47c646a597fb4dcec034e9f77c409e62" ) ) . unwrap ( ) ;
752781
753- roundtrip_protobuf_encoding ( & priv_key, & pub_key) ;
782+ roundtrip_protobuf_encoding ( & priv_key, & pub_key, KeyType :: Ecdsa ) ;
754783 }
755784
756785 #[ test]
@@ -765,11 +794,11 @@ mod tests {
765794 ) )
766795 . unwrap ( ) ;
767796
768- roundtrip_protobuf_encoding ( & priv_key, & pub_key) ;
797+ roundtrip_protobuf_encoding ( & priv_key, & pub_key, KeyType :: Secp256k1 ) ;
769798 }
770799
771800 #[ cfg( feature = "peerid" ) ]
772- fn roundtrip_protobuf_encoding ( private_key : & Keypair , public_key : & PublicKey ) {
801+ fn roundtrip_protobuf_encoding ( private_key : & Keypair , public_key : & PublicKey , tpe : KeyType ) {
773802 assert_eq ! ( & private_key. public( ) , public_key) ;
774803
775804 let encoded_priv = private_key. to_protobuf_encoding ( ) . unwrap ( ) ;
@@ -789,6 +818,7 @@ mod tests {
789818 decoded_public. to_peer_id( ) ,
790819 "PeerId from roundtripped public key should be the same"
791820 ) ;
821+ assert_eq ! ( private_key. key_type( ) , tpe)
792822 }
793823
794824 #[ test]
@@ -845,6 +875,7 @@ mod tests {
845875 let converted_pubkey = PublicKey :: from ( ed25519_pubkey) ;
846876
847877 assert_eq ! ( converted_pubkey, pubkey) ;
878+ assert_eq ! ( converted_pubkey. key_type( ) , KeyType :: Ed25519 )
848879 }
849880
850881 #[ test]
@@ -858,6 +889,7 @@ mod tests {
858889 let converted_pubkey = PublicKey :: from ( secp256k1_pubkey) ;
859890
860891 assert_eq ! ( converted_pubkey, pubkey) ;
892+ assert_eq ! ( converted_pubkey. key_type( ) , KeyType :: Secp256k1 )
861893 }
862894
863895 #[ test]
@@ -868,5 +900,6 @@ mod tests {
868900 let converted_pubkey = PublicKey :: from ( ecdsa_pubkey) ;
869901
870902 assert_eq ! ( converted_pubkey, pubkey) ;
903+ assert_eq ! ( converted_pubkey. key_type( ) , KeyType :: Ecdsa )
871904 }
872905}
0 commit comments