@@ -4,7 +4,9 @@ use enr::{CombinedPublicKey, NodeId};
4
4
use std:: net:: SocketAddr ;
5
5
6
6
#[ cfg( feature = "libp2p" ) ]
7
- use libp2p_core:: { identity:: PublicKey , multiaddr:: Protocol , multihash, Multiaddr } ;
7
+ use libp2p_core:: { multiaddr:: Protocol , Multiaddr } ;
8
+ #[ cfg( feature = "libp2p" ) ]
9
+ use libp2p_identity:: { KeyType , PublicKey } ;
8
10
9
11
/// This type relaxes the requirement of having an ENR to connect to a node, to allow for unsigned
10
12
/// connection types, such as multiaddrs.
@@ -94,36 +96,34 @@ impl NodeContact {
94
96
Protocol :: Udp ( port) => udp_port = Some ( port) ,
95
97
Protocol :: Ip4 ( addr) => ip_addr = Some ( addr. into ( ) ) ,
96
98
Protocol :: Ip6 ( addr) => ip_addr = Some ( addr. into ( ) ) ,
97
- Protocol :: P2p ( multihash ) => p2p = Some ( multihash ) ,
99
+ Protocol :: P2p ( peer_id ) => p2p = Some ( peer_id ) ,
98
100
_ => { }
99
101
}
100
102
}
101
103
102
104
let udp_port = udp_port. ok_or ( "A UDP port must be specified in the multiaddr" ) ?;
103
105
let ip_addr = ip_addr. ok_or ( "An IP address must be specified in the multiaddr" ) ?;
104
- let multihash = p2p. ok_or ( "The p2p protocol must be specified in the multiaddr" ) ?;
105
-
106
- // verify the correct key type
107
- if multihash. code ( ) != u64:: from ( multihash:: Code :: Identity ) {
108
- return Err ( "The key type is unsupported" ) ;
109
- }
110
-
111
- let public_key: CombinedPublicKey =
112
- match PublicKey :: from_protobuf_encoding ( & multihash. to_bytes ( ) [ 2 ..] )
113
- . map_err ( |_| "Invalid public key" ) ?
114
- {
115
- PublicKey :: Secp256k1 ( pk) => {
116
- enr:: k256:: ecdsa:: VerifyingKey :: from_sec1_bytes ( & pk. encode_uncompressed ( ) )
117
- . expect ( "Libp2p key conversion, always valid" )
118
- . into ( )
119
- }
120
- PublicKey :: Ed25519 ( pk) => {
121
- enr:: ed25519_dalek:: VerifyingKey :: from_bytes ( & pk. encode ( ) )
122
- . expect ( "Libp2p key conversion, always valid" )
123
- . into ( )
124
- }
106
+ let peer_id = p2p. ok_or ( "The p2p protocol must be specified in the multiaddr" ) ?;
107
+
108
+ let public_key: CombinedPublicKey = {
109
+ let pk = PublicKey :: try_decode_protobuf ( & peer_id. to_bytes ( ) [ 2 ..] )
110
+ . map_err ( |_| "Invalid public key" ) ?;
111
+ match pk. key_type ( ) {
112
+ KeyType :: Secp256k1 => enr:: k256:: ecdsa:: VerifyingKey :: from_sec1_bytes (
113
+ & pk. try_into_secp256k1 ( )
114
+ . expect ( "Must be secp256k1" )
115
+ . to_bytes_uncompressed ( ) ,
116
+ )
117
+ . expect ( "Libp2p key conversion, always valid" )
118
+ . into ( ) ,
119
+ KeyType :: Ed25519 => enr:: ed25519_dalek:: VerifyingKey :: from_bytes (
120
+ & pk. try_into_ed25519 ( ) . expect ( "Must be ed25519" ) . to_bytes ( ) ,
121
+ )
122
+ . expect ( "Libp2p key conversion, always valid" )
123
+ . into ( ) ,
125
124
_ => return Err ( "The key type is not supported" ) ,
126
- } ;
125
+ }
126
+ } ;
127
127
128
128
Ok ( NodeContact {
129
129
public_key,
0 commit comments