1
1
use dkn_p2p:: { libp2p:: PeerId , libp2p_identity} ;
2
- use ecies:: PublicKey ;
3
- use libsecp256k1:: SecretKey ;
4
2
use sha2:: { Digest , Sha256 } ;
5
3
use sha3:: Keccak256 ;
6
4
@@ -16,33 +14,33 @@ pub fn keccak256hash(data: impl AsRef<[u8]>) -> [u8; 32] {
16
14
Keccak256 :: digest ( data) . into ( )
17
15
}
18
16
17
+ /// Converts a `libsecp256k1::SecretKey` to a `libp2p_identity::secp256k1::Keypair`.
18
+ /// To do this, we serialize the secret key and create a new keypair from it.
19
+ #[ inline]
20
+ pub fn secret_to_keypair ( secret_key : & libsecp256k1:: SecretKey ) -> libp2p_identity:: Keypair {
21
+ let bytes = secret_key. serialize ( ) ;
22
+
23
+ let secret_key = dkn_p2p:: libp2p_identity:: secp256k1:: SecretKey :: try_from_bytes ( bytes)
24
+ . expect ( "Failed to create secret key" ) ;
25
+ libp2p_identity:: secp256k1:: Keypair :: from ( secret_key) . into ( )
26
+ }
27
+
19
28
/// Given a secp256k1 public key, finds the corresponding Ethereum address.
20
29
///
21
30
/// Internally, the public key is serialized in uncompressed format at 65 bytes (0x04 || x || y),
22
31
/// and then (x || y) is hashed using Keccak256. The last 20 bytes of this hash is taken as the address.
23
32
#[ inline]
24
- pub fn to_address ( public_key : & PublicKey ) -> [ u8 ; 20 ] {
33
+ pub fn public_key_to_address ( public_key : & libsecp256k1 :: PublicKey ) -> [ u8 ; 20 ] {
25
34
let public_key_xy = & public_key. serialize ( ) [ 1 ..] ;
26
35
let mut addr = [ 0u8 ; 20 ] ;
27
36
addr. copy_from_slice ( & keccak256hash ( public_key_xy) [ 12 ..32 ] ) ;
28
37
addr
29
38
}
30
39
31
- /// Converts a `libsecp256k1::SecretKey` to a `libp2p_identity::secp256k1::Keypair`.
32
- /// To do this, we serialize the secret key and create a new keypair from it.
33
- #[ inline]
34
- pub fn secret_to_keypair ( secret_key : & SecretKey ) -> libp2p_identity:: Keypair {
35
- let bytes = secret_key. serialize ( ) ;
36
-
37
- let secret_key = dkn_p2p:: libp2p_identity:: secp256k1:: SecretKey :: try_from_bytes ( bytes)
38
- . expect ( "Failed to create secret key" ) ;
39
- libp2p_identity:: secp256k1:: Keypair :: from ( secret_key) . into ( )
40
- }
41
-
42
40
/// Converts a `libsecp256k1::PublicKey` to a `libp2p_identity::PeerId`.
43
41
/// To do this, we serialize the secret key and create a new keypair from it.
44
42
#[ inline]
45
- pub fn public_key_to_peer_id ( public_key : & PublicKey ) -> libp2p_identity:: PeerId {
43
+ pub fn public_key_to_peer_id ( public_key : & libsecp256k1 :: PublicKey ) -> libp2p_identity:: PeerId {
46
44
let bytes = public_key. serialize_compressed ( ) ;
47
45
48
46
let public_key = dkn_p2p:: libp2p_identity:: secp256k1:: PublicKey :: try_from_bytes ( & bytes)
@@ -73,7 +71,7 @@ mod tests {
73
71
fn test_address ( ) {
74
72
let sk = SecretKey :: parse_slice ( DUMMY_SECRET_KEY ) . expect ( "Should parse key." ) ;
75
73
let pk = PublicKey :: from_secret_key ( & sk) ;
76
- let addr = to_address ( & pk) ;
74
+ let addr = public_key_to_address ( & pk) ;
77
75
assert_eq ! (
78
76
"D79Fdf178547614CFdd0dF6397c53569716Bd596" . to_lowercase( ) ,
79
77
hex:: encode( addr)
0 commit comments