@@ -3,56 +3,26 @@ use std::hash::Hash;
3
3
use derive_more:: derive:: Into ;
4
4
use eyre:: Result ;
5
5
use generic_array:: GenericArray ;
6
- use k256:: {
7
- ecdsa:: { Signature as EcdsaSignatureInner , VerifyingKey as EcdsaPublicKeyInner } ,
8
- AffinePoint ,
9
- } ;
6
+ use k256:: ecdsa:: { Signature as EcdsaSignatureInner , VerifyingKey as EcdsaPublicKeyInner } ;
10
7
use serde:: { Deserialize , Serialize } ;
11
8
use ssz_types:: { typenum:: U33 , FixedVector } ;
12
9
use tree_hash:: TreeHash ;
13
10
14
- use crate :: signer:: { GenericPubkey , PubKey , SecretKey } ;
11
+ use crate :: signer:: { GenericPubkey , PubKey , SecretKey , Verifier } ;
15
12
16
13
pub type EcdsaSecretKey = k256:: ecdsa:: SigningKey ;
17
- type EcdsaCompressedKey = GenericArray < u8 , U33 > ;
14
+
15
+ type CompressedPublicKey = GenericArray < u8 , U33 > ;
18
16
19
17
#[ derive( Debug , Clone , Copy , Into , Serialize , Deserialize , PartialEq , Eq ) ]
20
- #[ serde( from = "JSONEcdsaPublicKey" ) ]
21
18
pub struct EcdsaPublicKey {
22
- #[ serde( skip_serializing) ]
23
- inner : EcdsaPublicKeyInner ,
24
- encoded : EcdsaCompressedKey ,
19
+ encoded : CompressedPublicKey ,
25
20
}
26
21
27
22
impl EcdsaPublicKey {
28
23
/// Size of the public key in bytes. We store the SEC1 encoded affine point
29
24
/// compressed, thus 33 bytes.
30
25
const SIZE : usize = 33 ;
31
-
32
- pub fn new ( inner : EcdsaPublicKeyInner ) -> Self {
33
- use elliptic_curve:: sec1:: ToEncodedPoint ;
34
-
35
- let affine: & AffinePoint = inner. as_ref ( ) ;
36
- let encoded: [ u8 ; Self :: SIZE ] =
37
- affine. to_encoded_point ( true ) . as_bytes ( ) . try_into ( ) . unwrap ( ) ;
38
-
39
- let encoded = GenericArray :: from_array ( encoded) ;
40
-
41
- EcdsaPublicKey { inner, encoded }
42
- }
43
-
44
- pub fn new_from_bytes ( encoded : Vec < u8 > ) -> Result < Self , k256:: ecdsa:: Error > {
45
- let inner = EcdsaPublicKeyInner :: from_sec1_bytes ( & encoded) ?;
46
- let encoded = GenericArray :: from_array :: < { Self :: SIZE } > ( encoded. try_into ( ) . unwrap ( ) ) ;
47
- Ok ( Self { inner, encoded } )
48
- }
49
-
50
- fn new_from_bytes_infallible ( encoded : [ u8 ; Self :: SIZE ] ) -> Self {
51
- Self {
52
- inner : EcdsaPublicKeyInner :: from_sec1_bytes ( & encoded) . unwrap ( ) ,
53
- encoded : GenericArray :: from_array ( encoded) ,
54
- }
55
- }
56
26
}
57
27
58
28
impl Hash for EcdsaPublicKey {
@@ -131,38 +101,21 @@ impl ssz::Decode for EcdsaPublicKey {
131
101
let mut fixed_array = [ 0_u8 ; Self :: SIZE ] ;
132
102
fixed_array. copy_from_slice ( bytes) ;
133
103
134
- Ok ( EcdsaPublicKey :: new_from_bytes_infallible ( fixed_array) )
104
+ Ok ( EcdsaPublicKey { encoded : fixed_array. into ( ) } )
135
105
}
136
106
}
137
107
138
- impl TryFrom < Vec < u8 > > for EcdsaPublicKey {
139
- type Error = k256:: ecdsa:: Error ;
140
-
141
- fn try_from ( value : Vec < u8 > ) -> std:: result:: Result < Self , Self :: Error > {
142
- Self :: new_from_bytes ( value)
143
- }
144
- }
145
-
146
- impl From < EcdsaCompressedKey > for EcdsaPublicKey {
147
- fn from ( value : EcdsaCompressedKey ) -> Self {
148
- Self :: new_from_bytes_infallible ( value. into ( ) )
149
- }
150
- }
151
-
152
- #[ derive( Deserialize ) ]
153
- struct JSONEcdsaPublicKey {
154
- encoded : EcdsaCompressedKey ,
155
- }
156
-
157
- impl From < JSONEcdsaPublicKey > for EcdsaPublicKey {
158
- fn from ( value : JSONEcdsaPublicKey ) -> Self {
159
- Self :: from ( value. encoded )
108
+ impl From < CompressedPublicKey > for EcdsaPublicKey {
109
+ fn from ( value : CompressedPublicKey ) -> Self {
110
+ Self { encoded : value }
160
111
}
161
112
}
162
113
163
114
impl From < EcdsaPublicKeyInner > for EcdsaPublicKey {
164
115
fn from ( value : EcdsaPublicKeyInner ) -> Self {
165
- EcdsaPublicKey :: new ( value)
116
+ let encoded: [ u8 ; Self :: SIZE ] = value. to_encoded_point ( true ) . as_bytes ( ) . try_into ( ) . unwrap ( ) ;
117
+
118
+ EcdsaPublicKey { encoded : encoded. into ( ) }
166
119
}
167
120
}
168
121
@@ -210,8 +163,6 @@ impl SecretKey for EcdsaSecretKey {
210
163
211
164
type Signature = EcdsaSignature ;
212
165
213
- type VerificationError = k256:: ecdsa:: Error ;
214
-
215
166
fn new_random ( ) -> Self {
216
167
EcdsaSecretKey :: random ( & mut rand:: thread_rng ( ) )
217
168
}
@@ -227,17 +178,22 @@ impl SecretKey for EcdsaSecretKey {
227
178
EcdsaPublicKeyInner :: from ( self ) . into ( )
228
179
}
229
180
230
- fn sign ( & self , msg : & [ u8 ; 32 ] ) -> Self :: Signature {
181
+ fn sign ( & self , msg : & [ u8 ] ) -> Self :: Signature {
231
182
k256:: ecdsa:: signature:: Signer :: < EcdsaSignatureInner > :: sign ( self , msg) . into ( )
232
183
}
184
+ }
185
+
186
+ impl Verifier < EcdsaSecretKey > for PubKey < EcdsaSecretKey > {
187
+ type VerificationError = k256:: ecdsa:: Error ;
233
188
234
189
fn verify_signature (
235
- pubkey : & Self :: PubKey ,
190
+ & self ,
236
191
msg : & [ u8 ] ,
237
- signature : & Self :: Signature ,
192
+ signature : & < EcdsaSecretKey as SecretKey > :: Signature ,
238
193
) -> Result < ( ) , Self :: VerificationError > {
239
194
use k256:: ecdsa:: signature:: Verifier ;
240
- pubkey. inner . verify ( msg, & signature. inner )
195
+ let ecdsa_pubkey = EcdsaPublicKeyInner :: from_sec1_bytes ( & self . encoded ) ?;
196
+ ecdsa_pubkey. verify ( msg, & signature. inner )
241
197
}
242
198
}
243
199
0 commit comments