7
7
use core:: ptr;
8
8
9
9
use self :: super_ffi:: CPtr ;
10
- use super :: ffi as super_ffi;
10
+ use super :: { ffi as super_ffi, RecoveryIdError , SignatureError } ;
11
11
use crate :: ecdsa:: Signature ;
12
12
use crate :: ffi:: recovery as ffi;
13
- use crate :: { key, Error , Message , Secp256k1 , Signing , Verification } ;
13
+ use crate :: { key, Message , Secp256k1 , Signing , Verification } ;
14
14
15
15
/// A tag used for recovering the public key from a compact signature.
16
16
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
@@ -23,10 +23,10 @@ pub struct RecoverableSignature(ffi::RecoverableSignature);
23
23
impl RecoveryId {
24
24
#[ inline]
25
25
/// 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 > {
27
27
match id {
28
28
0 ..=3 => Ok ( RecoveryId ( id) ) ,
29
- _ => Err ( Error :: InvalidRecoveryId ) ,
29
+ _ => Err ( RecoveryIdError ) ,
30
30
}
31
31
}
32
32
@@ -39,16 +39,19 @@ impl RecoverableSignature {
39
39
#[ inline]
40
40
/// Converts a compact-encoded byte slice to a signature. This
41
41
/// 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 > {
43
46
if data. is_empty ( ) {
44
- return Err ( Error :: InvalidSignature ) ;
47
+ return Err ( SignatureError ) ;
45
48
}
46
49
47
50
let mut ret = ffi:: RecoverableSignature :: new ( ) ;
48
51
49
52
unsafe {
50
53
if data. len ( ) != 64 {
51
- Err ( Error :: InvalidSignature )
54
+ Err ( SignatureError )
52
55
} else if ffi:: secp256k1_ecdsa_recoverable_signature_parse_compact (
53
56
super_ffi:: secp256k1_context_no_precomp,
54
57
& mut ret,
@@ -58,7 +61,7 @@ impl RecoverableSignature {
58
61
{
59
62
Ok ( RecoverableSignature ( ret) )
60
63
} else {
61
- Err ( Error :: InvalidSignature )
64
+ Err ( SignatureError )
62
65
}
63
66
}
64
67
}
@@ -113,7 +116,7 @@ impl RecoverableSignature {
113
116
/// verify-capable context.
114
117
#[ inline]
115
118
#[ 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 > {
117
120
crate :: SECP256K1 . recover_ecdsa ( msg, self )
118
121
}
119
122
}
@@ -191,7 +194,7 @@ impl<C: Verification> Secp256k1<C> {
191
194
& self ,
192
195
msg : & Message ,
193
196
sig : & RecoverableSignature ,
194
- ) -> Result < key:: PublicKey , Error > {
197
+ ) -> Result < key:: PublicKey , SignatureError > {
195
198
unsafe {
196
199
let mut pk = super_ffi:: PublicKey :: new ( ) ;
197
200
if ffi:: secp256k1_ecdsa_recover (
@@ -201,7 +204,7 @@ impl<C: Verification> Secp256k1<C> {
201
204
msg. as_c_ptr ( ) ,
202
205
) != 1
203
206
{
204
- return Err ( Error :: InvalidSignature ) ;
207
+ return Err ( SignatureError ) ;
205
208
}
206
209
Ok ( key:: PublicKey :: from ( pk) )
207
210
}
@@ -214,7 +217,7 @@ mod tests {
214
217
#[ cfg( target_arch = "wasm32" ) ]
215
218
use wasm_bindgen_test:: wasm_bindgen_test as test;
216
219
217
- use super :: { RecoverableSignature , RecoveryId } ;
220
+ use super :: * ;
218
221
use crate :: constants:: ONE ;
219
222
use crate :: { Error , Message , Secp256k1 , SecretKey } ;
220
223
@@ -316,7 +319,8 @@ mod tests {
316
319
317
320
let msg = crate :: random_32_bytes ( & mut rand:: thread_rng ( ) ) ;
318
321
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 ) ) ;
320
324
321
325
let recovered_key = s. recover_ecdsa ( & msg, & sigr) . unwrap ( ) ;
322
326
assert ! ( recovered_key != pk) ;
@@ -366,7 +370,7 @@ mod tests {
366
370
367
371
// Zero is not a valid sig
368
372
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 ) ) ;
370
374
// ...but 111..111 is
371
375
let sig = RecoverableSignature :: from_compact ( & [ 1 ; 64 ] , RecoveryId ( 0 ) ) . unwrap ( ) ;
372
376
assert ! ( s. recover_ecdsa( & msg, & sig) . is_ok( ) ) ;
0 commit comments