1- //! An implementation for `EnrKey` for `k256::SecretKey `
1+ //! An implementation for `EnrKey` for `k256::ecdsa::SigningKey `
22
33use super :: { EnrKey , EnrPublicKey , SigningError } ;
44use crate :: Key ;
5- use ecdsa:: elliptic_curve:: sec1:: { FromEncodedPoint , ToEncodedPoint } ;
6- use k256_crate:: ecdsa:: signature:: { DigestVerifier , RandomizedDigestSigner , Signature } ;
5+ use k256_crate:: {
6+ ecdsa:: {
7+ signature:: { DigestVerifier , RandomizedDigestSigner , Signature as _} ,
8+ Signature , SigningKey , VerifyKey ,
9+ } ,
10+ EncodedPoint ,
11+ } ;
712use rand:: rngs:: OsRng ;
813use rlp:: DecoderError ;
914use sha3:: { Digest , Keccak256 } ;
@@ -12,24 +17,21 @@ use std::{collections::BTreeMap, convert::TryFrom};
1217/// The ENR key that stores the public key in the ENR record.
1318pub const ENR_KEY : & str = "secp256k1" ;
1419
15- type Signer = ecdsa:: SigningKey < k256_crate:: Secp256k1 > ;
16- type Verifier = ecdsa:: VerifyKey < k256_crate:: Secp256k1 > ;
17-
18- impl EnrKey for k256_crate:: SecretKey {
19- type PublicKey = k256_crate:: EncodedPoint ;
20+ impl EnrKey for SigningKey {
21+ type PublicKey = VerifyKey ;
2022
2123 fn sign_v4 ( & self , msg : & [ u8 ] ) -> Result < Vec < u8 > , SigningError > {
2224 // take a keccak256 hash then sign.
2325 let digest = Keccak256 :: new ( ) . chain ( msg) ;
24- let signature: k256_crate :: ecdsa :: Signature = Signer :: new ( self . to_bytes ( ) . as_slice ( ) )
25- . map_err ( |_| SigningError :: new ( "failed to create signer" ) ) ?
26- . sign_digest_with_rng ( & mut OsRng , digest ) ;
26+ let signature: Signature = self
27+ . try_sign_digest_with_rng ( & mut OsRng , digest )
28+ . map_err ( |_| SigningError :: new ( "failed to sign" ) ) ? ;
2729
2830 Ok ( signature. as_bytes ( ) . to_vec ( ) )
2931 }
3032
3133 fn public ( & self ) -> Self :: PublicKey {
32- k256_crate :: EncodedPoint :: from_secret_key ( self , false )
34+ self . verify_key ( )
3335 }
3436
3537 fn enr_to_public ( content : & BTreeMap < Key , Vec < u8 > > ) -> Result < Self :: PublicKey , DecoderError > {
@@ -38,35 +40,28 @@ impl EnrKey for k256_crate::SecretKey {
3840 . ok_or_else ( || DecoderError :: Custom ( "Unknown signature" ) ) ?;
3941
4042 // should be encoded in compressed form, i.e 33 byte raw secp256k1 public key
41- Ok ( k256_crate :: EncodedPoint :: from_bytes ( pubkey_bytes)
43+ Ok ( VerifyKey :: new ( pubkey_bytes)
4244 . map_err ( |_| DecoderError :: Custom ( "Invalid Secp256k1 Signature" ) ) ?)
4345 }
4446}
4547
46- impl EnrPublicKey for k256_crate :: EncodedPoint {
48+ impl EnrPublicKey for VerifyKey {
4749 fn verify_v4 ( & self , msg : & [ u8 ] , sig : & [ u8 ] ) -> bool {
48- let digest = Keccak256 :: new ( ) . chain ( msg) ;
4950 if let Ok ( sig) = k256_crate:: ecdsa:: Signature :: try_from ( sig) {
50- if let Ok ( verifier) = Verifier :: new ( self . as_bytes ( ) ) {
51- if verifier. verify_digest ( digest, & sig) . is_ok ( ) {
52- return true ;
53- }
54- }
51+ return self
52+ . verify_digest ( Keccak256 :: new ( ) . chain ( msg) , & sig)
53+ . is_ok ( ) ;
5554 }
5655 false
5756 }
5857
5958 fn encode ( & self ) -> Vec < u8 > {
6059 // serialize in compressed form: 33 bytes
61- self . compress ( ) . as_bytes ( ) . to_vec ( )
60+ self . to_bytes ( ) . to_vec ( )
6261 }
6362
6463 fn encode_uncompressed ( & self ) -> Vec < u8 > {
65- k256_crate:: AffinePoint :: from_encoded_point ( self )
66- . unwrap ( )
67- . to_encoded_point ( false )
68- . as_bytes ( ) [ 1 ..]
69- . to_vec ( )
64+ EncodedPoint :: from ( self ) . decompress ( ) . unwrap ( ) . to_bytes ( ) [ 1 ..] . to_vec ( )
7065 }
7166
7267 fn enr_key ( & self ) -> Key {
0 commit comments