Skip to content

Commit 41af612

Browse files
committed
Added a new struct for the DER serialized signature
1 parent 4a8599b commit 41af612

File tree

1 file changed

+78
-5
lines changed

1 file changed

+78
-5
lines changed

src/lib.rs

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ pub mod key;
156156
pub use key::SecretKey;
157157
pub use key::PublicKey;
158158
use core::marker::PhantomData;
159+
use core::ops::Deref;
159160

160161
/// A tag used for recovering the public key from a compact signature
161162
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -165,6 +166,13 @@ pub struct RecoveryId(i32);
165166
#[derive(Copy, Clone, PartialEq, Eq)]
166167
pub struct Signature(ffi::Signature);
167168

169+
/// A DER serialized Signature
170+
#[derive(Copy, Clone)]
171+
pub struct SerSignature {
172+
data: [u8; 72],
173+
len: usize,
174+
}
175+
168176
impl fmt::Debug for Signature {
169177
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
170178
fmt::Display::fmt(self, f)
@@ -231,6 +239,40 @@ pub fn to_i32(self) -> i32 {
231239
}
232240
}
233241

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+
234276
impl Signature {
235277
#[inline]
236278
/// Converts a DER-encoded byte slice to a signature
@@ -337,18 +379,18 @@ impl Signature {
337379

338380
#[inline]
339381
/// 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();
343385
unsafe {
344386
let err = ffi::secp256k1_ecdsa_signature_serialize_der(
345387
ffi::secp256k1_context_no_precomp,
346-
ret.as_mut_ptr(),
388+
ret.get_data_mut_ptr(),
347389
&mut len,
348390
self.as_ptr(),
349391
);
350392
debug_assert!(err == 1);
351-
ret.set_len(len as usize);
393+
ret.set_len(len);
352394
}
353395
ret
354396
}
@@ -590,6 +632,37 @@ impl<C> PartialEq for Secp256k1<C> {
590632
fn eq(&self, _other: &Secp256k1<C>) -> bool { true }
591633
}
592634

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+
593666
impl<C> Eq for Secp256k1<C> { }
594667

595668
impl<C> Drop for Secp256k1<C> {

0 commit comments

Comments
 (0)