@@ -11,6 +11,7 @@ use secp256k1::{
11
11
PublicKey , Secp256k1 , SecretKey , SignOnly , ThirtyTwoByteHash ,
12
12
} ;
13
13
use sha3:: { Digest , Keccak256 } ;
14
+ use thiserror:: Error ;
14
15
15
16
use crate :: { utils:: hash_message, Address , B256 , U256 } ;
16
17
@@ -51,30 +52,23 @@ fn private_to_public_key(
51
52
}
52
53
53
54
/// An error involving a signature.
54
- #[ derive( Debug ) ]
55
- #[ cfg_attr( feature = "std" , derive( thiserror:: Error ) ) ]
55
+ #[ derive( Debug , Error ) ]
56
56
pub enum SignatureError {
57
57
/// Invalid length, secp256k1 signatures are 65 bytes
58
- #[ cfg_attr(
59
- feature = "std" ,
60
- error( "invalid signature length, got {0}, expected 65" )
61
- ) ]
58
+ #[ error( "invalid signature length, got {0}, expected 65" ) ]
62
59
InvalidLength ( usize ) ,
63
60
/// When parsing a signature from string to hex
64
- #[ cfg_attr ( feature = "std" , error( transparent) ) ]
65
- DecodingError ( #[ cfg_attr ( feature = "std" , from) ] hex:: FromHexError ) ,
61
+ #[ error( transparent) ]
62
+ DecodingError ( #[ from] hex:: FromHexError ) ,
66
63
/// Thrown when signature verification failed (i.e. when the address that
67
64
/// produced the signature did not match the expected address)
68
- #[ cfg_attr(
69
- feature = "std" ,
70
- error( "Signature verification failed. Expected {0}, got {1}" )
71
- ) ]
65
+ #[ error( "Signature verification failed. Expected {0}, got {1}" ) ]
72
66
VerificationError ( Address , Address ) ,
73
67
/// Internal error during signature recovery
74
- #[ cfg_attr ( feature = "std" , error( transparent) ) ]
75
- K256Error ( #[ cfg_attr ( feature = "std" , from) ] secp256k1:: Error ) ,
68
+ #[ error( transparent) ]
69
+ K256Error ( #[ from] secp256k1:: Error ) ,
76
70
/// Error in recovering public key from signature
77
- #[ cfg_attr ( feature = "std" , error( "Public key recovery error" ) ) ]
71
+ #[ error( "Public key recovery error" ) ]
78
72
RecoveryError ,
79
73
}
80
74
@@ -192,9 +186,7 @@ impl Signature {
192
186
let ( recoverable_sig, _recovery_id) = self . as_signature ( ) ?;
193
187
194
188
let context = Secp256k1 :: verification_only ( ) ;
195
- let public_key = context
196
- . recover_ecdsa ( & message_hash. into ( ) , & recoverable_sig)
197
- . map_err ( SignatureError :: K256Error ) ?;
189
+ let public_key = context. recover_ecdsa ( & message_hash. into ( ) , & recoverable_sig) ?;
198
190
199
191
Ok ( public_key_to_address ( public_key) )
200
192
}
@@ -210,8 +202,7 @@ impl Signature {
210
202
bytes[ ..32 ] . copy_from_slice ( & r_bytes) ;
211
203
bytes[ 32 ..64 ] . copy_from_slice ( & s_bytes) ;
212
204
213
- RecoverableSignature :: from_compact ( & bytes, recovery_id)
214
- . map_err ( SignatureError :: K256Error ) ?
205
+ RecoverableSignature :: from_compact ( & bytes, recovery_id) ?
215
206
} ;
216
207
217
208
Ok ( ( signature, recovery_id) )
@@ -220,7 +211,7 @@ impl Signature {
220
211
/// Retrieve the recovery ID.
221
212
pub fn recovery_id ( & self ) -> Result < RecoveryId , SignatureError > {
222
213
let standard_v = normalize_recovery_id ( self . v ) ;
223
- RecoveryId :: from_i32 ( standard_v) . map_err ( SignatureError :: K256Error )
214
+ Ok ( RecoveryId :: from_i32 ( standard_v) ? )
224
215
}
225
216
226
217
/// Copies and serializes `self` into a new `Vec` with the recovery id included
@@ -292,13 +283,12 @@ impl<'a> TryFrom<&'a [u8]> for Signature {
292
283
}
293
284
}
294
285
295
- #[ cfg( feature = "std" ) ]
296
286
impl FromStr for Signature {
297
287
type Err = SignatureError ;
298
288
299
289
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
300
290
let s = s. strip_prefix ( "0x" ) . unwrap_or ( s) ;
301
- let bytes = hex:: decode ( s) . map_err ( SignatureError :: DecodingError ) ?;
291
+ let bytes = hex:: decode ( s) ?;
302
292
Signature :: try_from ( & bytes[ ..] )
303
293
}
304
294
}
0 commit comments