@@ -66,14 +66,17 @@ impl str::FromStr for Signature {
66
66
fn from_str ( s : & str ) -> Result < Signature , Error > {
67
67
let mut res = [ 0u8 ; constants:: SCHNORR_SIGNATURE_SIZE ] ;
68
68
match from_hex ( s, & mut res) {
69
- Ok ( constants:: SCHNORR_SIGNATURE_SIZE ) =>
70
- Signature :: from_slice ( & res[ 0 ..constants:: SCHNORR_SIGNATURE_SIZE ] ) ,
69
+ Ok ( constants:: SCHNORR_SIGNATURE_SIZE ) => Ok ( Signature :: from_byte_array ( res) ) ,
71
70
_ => Err ( Error :: InvalidSignature ) ,
72
71
}
73
72
}
74
73
}
75
74
76
75
impl Signature {
76
+ /// Construct a `Signature` from a 64 bytes array.
77
+ #[ inline]
78
+ pub fn from_byte_array ( sig : [ u8 ; constants:: SCHNORR_SIGNATURE_SIZE ] ) -> Self { Self ( sig) }
79
+
77
80
/// Creates a `Signature` directly from a slice.
78
81
#[ inline]
79
82
pub fn from_slice ( data : & [ u8 ] ) -> Result < Signature , Error > {
@@ -88,9 +91,17 @@ impl Signature {
88
91
}
89
92
90
93
/// Returns a signature as a byte array.
91
- #[ inline ]
94
+ #[ deprecated ( since = "0.30.0" , note = "Use `to_byte_array` instead." ) ]
92
95
pub fn serialize ( & self ) -> [ u8 ; constants:: SCHNORR_SIGNATURE_SIZE ] { self . 0 }
93
96
97
+ /// Returns a signature as a byte array.
98
+ #[ inline]
99
+ pub fn to_byte_array ( self ) -> [ u8 ; constants:: SCHNORR_SIGNATURE_SIZE ] { self . 0 }
100
+
101
+ /// Returns a signature as a byte array.
102
+ #[ inline]
103
+ pub fn as_byte_array ( & self ) -> & [ u8 ; constants:: SCHNORR_SIGNATURE_SIZE ] { & self . 0 }
104
+
94
105
/// Verifies a schnorr signature for `msg` using `pk` and the global [`SECP256K1`] context.
95
106
#[ inline]
96
107
#[ cfg( feature = "global-context" ) ]
@@ -294,7 +305,7 @@ mod tests {
294
305
#[ test]
295
306
fn test_serialize ( ) {
296
307
let sig = Signature :: from_str ( "6470FD1303DDA4FDA717B9837153C24A6EAB377183FC438F939E0ED2B620E9EE5077C4A8B8DCA28963D772A94F5F0DDF598E1C47C137F91933274C7C3EDADCE8" ) . unwrap ( ) ;
297
- let sig_bytes = sig. serialize ( ) ;
308
+ let sig_bytes = sig. to_byte_array ( ) ;
298
309
let bytes = [
299
310
100 , 112 , 253 , 19 , 3 , 221 , 164 , 253 , 167 , 23 , 185 , 131 , 113 , 83 , 194 , 74 , 110 , 171 , 55 ,
300
311
113 , 131 , 252 , 67 , 143 , 147 , 158 , 14 , 210 , 182 , 32 , 233 , 238 , 80 , 119 , 196 , 168 , 184 ,
@@ -697,9 +708,9 @@ mod tests {
697
708
let keypair = Keypair :: from_seckey_slice ( & secp, & secret_key) . unwrap ( ) ;
698
709
assert_eq ! ( keypair. x_only_public_key( ) . 0 . serialize( ) , public_key) ;
699
710
let sig = secp. sign_schnorr_with_aux_rand ( & message, & keypair, & aux_rand) ;
700
- assert_eq ! ( sig. serialize ( ) , signature) ;
711
+ assert_eq ! ( sig. to_byte_array ( ) , signature) ;
701
712
}
702
- let sig = Signature :: from_slice ( & signature) . unwrap ( ) ;
713
+ let sig = Signature :: from_byte_array ( signature) ;
703
714
let is_verified = if let Ok ( pubkey) = XOnlyPublicKey :: from_slice ( & public_key) {
704
715
secp. verify_schnorr ( & sig, & message, & pubkey) . is_ok ( )
705
716
} else {
0 commit comments