1
1
//! Structs and functionality related to the ECDSA signature algorithm.
2
2
3
- use core:: { fmt, str, ops , ptr} ;
3
+ use core:: { fmt, str, ptr} ;
4
4
5
5
use crate :: { Signing , Verification , Message , PublicKey , Secp256k1 , SecretKey , from_hex, Error , ffi} ;
6
6
use crate :: ffi:: CPtr ;
7
7
8
+ pub mod serialized_signature;
9
+
8
10
#[ cfg( feature = "recovery" ) ]
9
11
mod recovery;
10
12
11
13
#[ cfg( feature = "recovery" ) ]
12
14
#[ cfg_attr( docsrs, doc( cfg( feature = "recovery" ) ) ) ]
13
15
pub use self :: recovery:: { RecoveryId , RecoverableSignature } ;
14
16
17
+ pub use serialized_signature:: SerializedSignature ;
18
+
15
19
#[ cfg( feature = "global-context" ) ]
16
20
use crate :: SECP256K1 ;
17
21
18
22
/// An ECDSA signature
19
23
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
20
24
pub struct Signature ( pub ( crate ) ffi:: Signature ) ;
21
25
22
- /// A DER serialized Signature
23
- #[ derive( Copy , Clone ) ]
24
- pub struct SerializedSignature {
25
- data : [ u8 ; 72 ] ,
26
- len : usize ,
27
- }
28
-
29
26
impl fmt:: Debug for Signature {
30
27
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
31
28
fmt:: Display :: fmt ( self , f)
@@ -39,21 +36,6 @@ impl fmt::Display for Signature {
39
36
}
40
37
}
41
38
42
- impl fmt:: Debug for SerializedSignature {
43
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
44
- fmt:: Display :: fmt ( self , f)
45
- }
46
- }
47
-
48
- impl fmt:: Display for SerializedSignature {
49
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
50
- for v in self . data . iter ( ) . take ( self . len ) {
51
- write ! ( f, "{:02x}" , v) ?;
52
- }
53
- Ok ( ( ) )
54
- }
55
- }
56
-
57
39
impl str:: FromStr for Signature {
58
40
type Err = Error ;
59
41
fn from_str ( s : & str ) -> Result < Signature , Error > {
@@ -65,83 +47,6 @@ impl str::FromStr for Signature {
65
47
}
66
48
}
67
49
68
- impl Default for SerializedSignature {
69
- fn default ( ) -> SerializedSignature {
70
- SerializedSignature {
71
- data : [ 0u8 ; 72 ] ,
72
- len : 0 ,
73
- }
74
- }
75
- }
76
-
77
- impl PartialEq for SerializedSignature {
78
- fn eq ( & self , other : & SerializedSignature ) -> bool {
79
- * * self == * * other
80
- }
81
- }
82
-
83
- impl AsRef < [ u8 ] > for SerializedSignature {
84
- fn as_ref ( & self ) -> & [ u8 ] {
85
- & * self
86
- }
87
- }
88
-
89
- impl ops:: Deref for SerializedSignature {
90
- type Target = [ u8 ] ;
91
-
92
- fn deref ( & self ) -> & [ u8 ] {
93
- & self . data [ ..self . len ]
94
- }
95
- }
96
-
97
- impl Eq for SerializedSignature { }
98
-
99
- impl < ' a > IntoIterator for & ' a SerializedSignature {
100
- type IntoIter = core:: slice:: Iter < ' a , u8 > ;
101
- type Item = & ' a u8 ;
102
-
103
- fn into_iter ( self ) -> Self :: IntoIter {
104
- self . iter ( )
105
- }
106
- }
107
-
108
- impl SerializedSignature {
109
- /// Get a pointer to the underlying data with the specified capacity.
110
- pub ( crate ) fn get_data_mut_ptr ( & mut self ) -> * mut u8 {
111
- self . data . as_mut_ptr ( )
112
- }
113
-
114
- /// Get the capacity of the underlying data buffer.
115
- pub fn capacity ( & self ) -> usize {
116
- self . data . len ( )
117
- }
118
-
119
- /// Get the len of the used data.
120
- pub fn len ( & self ) -> usize {
121
- self . len
122
- }
123
-
124
- /// Set the length of the object.
125
- pub ( crate ) fn set_len ( & mut self , len : usize ) {
126
- self . len = len;
127
- }
128
-
129
- /// Convert the serialized signature into the Signature struct.
130
- /// (This DER deserializes it)
131
- pub fn to_signature ( & self ) -> Result < Signature , Error > {
132
- Signature :: from_der ( self )
133
- }
134
-
135
- /// Create a SerializedSignature from a Signature.
136
- /// (this DER serializes it)
137
- pub fn from_signature ( sig : & Signature ) -> SerializedSignature {
138
- sig. serialize_der ( )
139
- }
140
-
141
- /// Check if the space is zero.
142
- pub fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
143
- }
144
-
145
50
impl Signature {
146
51
#[ inline]
147
52
/// Converts a DER-encoded byte slice to a signature
0 commit comments