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