77use core:: ptr;
88
99use self :: super_ffi:: CPtr ;
10- use super :: ffi as super_ffi;
10+ use super :: { ffi as super_ffi, RecoveryIdError , SignatureError } ;
1111use crate :: ecdsa:: Signature ;
1212use crate :: ffi:: recovery as ffi;
13- use crate :: { key, Error , Message , Secp256k1 , Signing , Verification } ;
13+ use crate :: { key, Message , Secp256k1 , Signing , Verification } ;
1414
1515/// A tag used for recovering the public key from a compact signature.
1616#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
@@ -23,10 +23,10 @@ pub struct RecoverableSignature(ffi::RecoverableSignature);
2323impl RecoveryId {
2424 #[ inline]
2525 /// Allows library users to create valid recovery IDs from i32.
26- pub fn from_i32 ( id : i32 ) -> Result < RecoveryId , Error > {
26+ pub fn from_i32 ( id : i32 ) -> Result < RecoveryId , RecoveryIdError > {
2727 match id {
2828 0 ..=3 => Ok ( RecoveryId ( id) ) ,
29- _ => Err ( Error :: InvalidRecoveryId ) ,
29+ _ => Err ( RecoveryIdError ) ,
3030 }
3131 }
3232
@@ -39,16 +39,19 @@ impl RecoverableSignature {
3939 #[ inline]
4040 /// Converts a compact-encoded byte slice to a signature. This
4141 /// representation is nonstandard and defined by the libsecp256k1 library.
42- pub fn from_compact ( data : & [ u8 ] , recid : RecoveryId ) -> Result < RecoverableSignature , Error > {
42+ pub fn from_compact (
43+ data : & [ u8 ] ,
44+ recid : RecoveryId ,
45+ ) -> Result < RecoverableSignature , SignatureError > {
4346 if data. is_empty ( ) {
44- return Err ( Error :: InvalidSignature ) ;
47+ return Err ( SignatureError ) ;
4548 }
4649
4750 let mut ret = ffi:: RecoverableSignature :: new ( ) ;
4851
4952 unsafe {
5053 if data. len ( ) != 64 {
51- Err ( Error :: InvalidSignature )
54+ Err ( SignatureError )
5255 } else if ffi:: secp256k1_ecdsa_recoverable_signature_parse_compact (
5356 super_ffi:: secp256k1_context_no_precomp,
5457 & mut ret,
@@ -58,7 +61,7 @@ impl RecoverableSignature {
5861 {
5962 Ok ( RecoverableSignature ( ret) )
6063 } else {
61- Err ( Error :: InvalidSignature )
64+ Err ( SignatureError )
6265 }
6366 }
6467 }
@@ -113,7 +116,7 @@ impl RecoverableSignature {
113116 /// verify-capable context.
114117 #[ inline]
115118 #[ cfg( feature = "global-context" ) ]
116- pub fn recover ( & self , msg : & Message ) -> Result < key:: PublicKey , Error > {
119+ pub fn recover ( & self , msg : & Message ) -> Result < key:: PublicKey , SignatureError > {
117120 crate :: SECP256K1 . recover_ecdsa ( msg, self )
118121 }
119122}
@@ -191,7 +194,7 @@ impl<C: Verification> Secp256k1<C> {
191194 & self ,
192195 msg : & Message ,
193196 sig : & RecoverableSignature ,
194- ) -> Result < key:: PublicKey , Error > {
197+ ) -> Result < key:: PublicKey , SignatureError > {
195198 unsafe {
196199 let mut pk = super_ffi:: PublicKey :: new ( ) ;
197200 if ffi:: secp256k1_ecdsa_recover (
@@ -201,7 +204,7 @@ impl<C: Verification> Secp256k1<C> {
201204 msg. as_c_ptr ( ) ,
202205 ) != 1
203206 {
204- return Err ( Error :: InvalidSignature ) ;
207+ return Err ( SignatureError ) ;
205208 }
206209 Ok ( key:: PublicKey :: from ( pk) )
207210 }
@@ -214,9 +217,9 @@ mod tests {
214217 #[ cfg( target_arch = "wasm32" ) ]
215218 use wasm_bindgen_test:: wasm_bindgen_test as test;
216219
217- use super :: { RecoverableSignature , RecoveryId } ;
220+ use super :: * ;
218221 use crate :: constants:: ONE ;
219- use crate :: { Error , Message , Secp256k1 , SecretKey } ;
222+ use crate :: { Message , Secp256k1 , SecretKey } ;
220223
221224 #[ test]
222225 #[ cfg( feature = "rand-std" ) ]
@@ -316,7 +319,8 @@ mod tests {
316319
317320 let msg = crate :: random_32_bytes ( & mut rand:: thread_rng ( ) ) ;
318321 let msg = Message :: from_slice ( & msg) . unwrap ( ) ;
319- assert_eq ! ( s. verify_ecdsa( & msg, & sig, & pk) , Err ( Error :: IncorrectSignature ) ) ;
322+ // TODO: Is this ok, or do we want IncorrectSignatureError as well?
323+ assert_eq ! ( s. verify_ecdsa( & msg, & sig, & pk) , Err ( SignatureError ) ) ;
320324
321325 let recovered_key = s. recover_ecdsa ( & msg, & sigr) . unwrap ( ) ;
322326 assert ! ( recovered_key != pk) ;
@@ -366,7 +370,7 @@ mod tests {
366370
367371 // Zero is not a valid sig
368372 let sig = RecoverableSignature :: from_compact ( & [ 0 ; 64 ] , RecoveryId ( 0 ) ) . unwrap ( ) ;
369- assert_eq ! ( s. recover_ecdsa( & msg, & sig) , Err ( Error :: InvalidSignature ) ) ;
373+ assert_eq ! ( s. recover_ecdsa( & msg, & sig) , Err ( SignatureError ) ) ;
370374 // ...but 111..111 is
371375 let sig = RecoverableSignature :: from_compact ( & [ 1 ; 64 ] , RecoveryId ( 0 ) ) . unwrap ( ) ;
372376 assert ! ( s. recover_ecdsa( & msg, & sig) . is_ok( ) ) ;
0 commit comments