2
2
3
3
use super :: { EnrKey , EnrPublicKey , SigningError } ;
4
4
use crate :: Key ;
5
- use k256_crate:: {
6
- ecdsa:: signature:: { DigestVerifier , RandomizedDigestSigner , Signature } ,
7
- elliptic_curve:: weierstrass:: public_key:: FromPublicKey ,
8
- } ;
5
+ use ecdsa:: elliptic_curve:: sec1:: { FromEncodedPoint , ToEncodedPoint } ;
6
+ use k256_crate:: ecdsa:: signature:: { DigestVerifier , RandomizedDigestSigner , Signature } ;
9
7
use rand:: rngs:: OsRng ;
10
8
use rlp:: DecoderError ;
11
9
use sha3:: { Digest , Keccak256 } ;
12
- use std:: {
13
- collections:: BTreeMap ,
14
- convert:: { TryFrom , TryInto } ,
15
- } ;
10
+ use std:: { collections:: BTreeMap , convert:: TryFrom } ;
16
11
17
12
/// The ENR key that stores the public key in the ENR record.
18
13
pub const ENR_KEY : & str = "secp256k1" ;
19
14
20
- type Signer = ecdsa:: Signer < k256_crate:: Secp256k1 > ;
21
- type Verifier = ecdsa:: Verifier < k256_crate:: Secp256k1 > ;
15
+ type Signer = ecdsa:: SigningKey < k256_crate:: Secp256k1 > ;
16
+ type Verifier = ecdsa:: VerifyKey < k256_crate:: Secp256k1 > ;
22
17
23
18
impl EnrKey for k256_crate:: SecretKey {
24
- type PublicKey = k256_crate:: PublicKey ;
19
+ type PublicKey = k256_crate:: EncodedPoint ;
25
20
26
21
fn sign_v4 ( & self , msg : & [ u8 ] ) -> Result < Vec < u8 > , SigningError > {
27
22
// take a keccak256 hash then sign.
28
23
let digest = Keccak256 :: new ( ) . chain ( msg) ;
29
- let signature: k256_crate:: ecdsa:: Signature = Signer :: new ( self )
24
+ let signature: k256_crate:: ecdsa:: Signature = Signer :: new ( self . to_bytes ( ) . as_slice ( ) )
30
25
. map_err ( |_| SigningError :: new ( "failed to create signer" ) ) ?
31
26
. sign_digest_with_rng ( & mut OsRng , digest) ;
32
27
33
28
Ok ( signature. as_bytes ( ) . to_vec ( ) )
34
29
}
35
30
36
31
fn public ( & self ) -> Self :: PublicKey {
37
- self . try_into ( ) . unwrap ( )
32
+ k256_crate :: EncodedPoint :: from_secret_key ( self , false )
38
33
}
39
34
40
35
fn enr_to_public ( content : & BTreeMap < Key , Vec < u8 > > ) -> Result < Self :: PublicKey , DecoderError > {
@@ -43,16 +38,16 @@ impl EnrKey for k256_crate::SecretKey {
43
38
. ok_or_else ( || DecoderError :: Custom ( "Unknown signature" ) ) ?;
44
39
45
40
// should be encoded in compressed form, i.e 33 byte raw secp256k1 public key
46
- Ok ( k256_crate:: PublicKey :: from_bytes ( pubkey_bytes)
47
- . ok_or_else ( | | DecoderError :: Custom ( "Invalid Secp256k1 Signature" ) ) ?)
41
+ Ok ( k256_crate:: EncodedPoint :: from_bytes ( pubkey_bytes)
42
+ . map_err ( |_ | DecoderError :: Custom ( "Invalid Secp256k1 Signature" ) ) ?)
48
43
}
49
44
}
50
45
51
- impl EnrPublicKey for k256_crate:: PublicKey {
46
+ impl EnrPublicKey for k256_crate:: EncodedPoint {
52
47
fn verify_v4 ( & self , msg : & [ u8 ] , sig : & [ u8 ] ) -> bool {
53
48
let digest = Keccak256 :: new ( ) . chain ( msg) ;
54
49
if let Ok ( sig) = k256_crate:: ecdsa:: Signature :: try_from ( sig) {
55
- if let Ok ( verifier) = Verifier :: new ( self ) {
50
+ if let Ok ( verifier) = Verifier :: new ( self . as_bytes ( ) ) {
56
51
if verifier. verify_digest ( digest, & sig) . is_ok ( ) {
57
52
return true ;
58
53
}
@@ -63,15 +58,13 @@ impl EnrPublicKey for k256_crate::PublicKey {
63
58
64
59
fn encode ( & self ) -> Vec < u8 > {
65
60
// serialize in compressed form: 33 bytes
66
- let mut s = * self ;
67
- s. compress ( ) ;
68
- s. as_bytes ( ) . to_vec ( )
61
+ self . compress ( ) . as_bytes ( ) . to_vec ( )
69
62
}
70
63
71
64
fn encode_uncompressed ( & self ) -> Vec < u8 > {
72
- k256_crate:: AffinePoint :: from_public_key ( self )
65
+ k256_crate:: AffinePoint :: from_encoded_point ( self )
73
66
. unwrap ( )
74
- . to_pubkey ( false )
67
+ . to_encoded_point ( false )
75
68
. as_bytes ( ) [ 1 ..]
76
69
. to_vec ( )
77
70
}
0 commit comments