@@ -156,6 +156,7 @@ pub mod key;
156
156
pub use key:: SecretKey ;
157
157
pub use key:: PublicKey ;
158
158
use core:: marker:: PhantomData ;
159
+ use core:: ops:: Deref ;
159
160
160
161
/// A tag used for recovering the public key from a compact signature
161
162
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
@@ -165,6 +166,13 @@ pub struct RecoveryId(i32);
165
166
#[ derive( Copy , Clone , PartialEq , Eq ) ]
166
167
pub struct Signature ( ffi:: Signature ) ;
167
168
169
+ /// A DER serialized Signature
170
+ #[ derive( Copy , Clone ) ]
171
+ pub struct SerSignature {
172
+ data : [ u8 ; 72 ] ,
173
+ len : usize ,
174
+ }
175
+
168
176
impl fmt:: Debug for Signature {
169
177
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
170
178
fmt:: Display :: fmt ( self , f)
@@ -231,6 +239,40 @@ pub fn to_i32(self) -> i32 {
231
239
}
232
240
}
233
241
242
+ impl SerSignature {
243
+ /// Get a pointer to the underlying data with the specified capacity.
244
+ pub fn get_data_mut_ptr ( & mut self ) -> * mut u8 {
245
+ self . data . as_mut_ptr ( )
246
+ }
247
+
248
+ /// Get the capacity of the underlying data buffer.
249
+ pub fn capacity ( & self ) -> usize {
250
+ self . data . len ( )
251
+ }
252
+
253
+ /// Get the len of the used data.
254
+ pub fn len ( & self ) -> usize {
255
+ self . len
256
+ }
257
+
258
+ /// Set the length of the object.
259
+ pub ( crate ) fn set_len ( & mut self , len : usize ) {
260
+ self . len = len;
261
+ }
262
+
263
+ /// Convert the serialized signature into the Signature struct.
264
+ /// (This basically DER deserialize it)
265
+ pub fn to_signature ( & self ) -> Result < Signature , Error > {
266
+ Signature :: from_der ( & self )
267
+ }
268
+
269
+ /// Create a SerSignature from a Signature.
270
+ /// (this basically DER serialize it)
271
+ pub fn from_signature ( sig : & Signature ) -> SerSignature {
272
+ sig. serialize_der ( )
273
+ }
274
+ }
275
+
234
276
impl Signature {
235
277
#[ inline]
236
278
/// Converts a DER-encoded byte slice to a signature
@@ -337,18 +379,18 @@ impl Signature {
337
379
338
380
#[ inline]
339
381
/// Serializes the signature in DER format
340
- pub fn serialize_der ( & self ) -> Vec < u8 > {
341
- let mut ret = Vec :: with_capacity ( 72 ) ;
342
- let mut len: usize = ret. capacity ( ) as usize ;
382
+ pub fn serialize_der ( & self ) -> SerSignature {
383
+ let mut ret = SerSignature :: default ( ) ;
384
+ let mut len: usize = ret. capacity ( ) ;
343
385
unsafe {
344
386
let err = ffi:: secp256k1_ecdsa_signature_serialize_der (
345
387
ffi:: secp256k1_context_no_precomp,
346
- ret. as_mut_ptr ( ) ,
388
+ ret. get_data_mut_ptr ( ) ,
347
389
& mut len,
348
390
self . as_ptr ( ) ,
349
391
) ;
350
392
debug_assert ! ( err == 1 ) ;
351
- ret. set_len ( len as usize ) ;
393
+ ret. set_len ( len) ;
352
394
}
353
395
ret
354
396
}
@@ -590,6 +632,37 @@ impl<C> PartialEq for Secp256k1<C> {
590
632
fn eq ( & self , _other : & Secp256k1 < C > ) -> bool { true }
591
633
}
592
634
635
+ impl Default for SerSignature {
636
+ fn default ( ) -> SerSignature {
637
+ SerSignature {
638
+ data : [ 0u8 ; 72 ] ,
639
+ len : 0 ,
640
+ }
641
+ }
642
+ }
643
+
644
+ impl PartialEq for SerSignature {
645
+ fn eq ( & self , other : & SerSignature ) -> bool {
646
+ & self . data [ ..] == & other. data [ ..] &&
647
+ self . len == other. len
648
+ }
649
+ }
650
+
651
+ impl AsRef < [ u8 ] > for SerSignature {
652
+ fn as_ref ( & self ) -> & [ u8 ] {
653
+ & self . data [ ..self . len ]
654
+ }
655
+ }
656
+
657
+ impl Deref for SerSignature {
658
+ type Target = [ u8 ] ;
659
+ fn deref ( & self ) -> & [ u8 ] {
660
+ & self . data [ ..self . len ]
661
+ }
662
+ }
663
+
664
+ impl Eq for SerSignature { }
665
+
593
666
impl < C > Eq for Secp256k1 < C > { }
594
667
595
668
impl < C > Drop for Secp256k1 < C > {
0 commit comments