Skip to content

Commit 23b87a6

Browse files
committed
Allow infallible construction of Signature
* Add `Signature::from_byte_array` constructor * Add `Signature::to_byte_array` and `Signature::as_byte_array` methods * Deprecate `Signature::serialize` method Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 41a6d43 commit 23b87a6

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/schnorr.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,17 @@ impl str::FromStr for Signature {
6666
fn from_str(s: &str) -> Result<Signature, Error> {
6767
let mut res = [0u8; constants::SCHNORR_SIGNATURE_SIZE];
6868
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)),
7170
_ => Err(Error::InvalidSignature),
7271
}
7372
}
7473
}
7574

7675
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+
7780
/// Creates a `Signature` directly from a slice.
7881
#[inline]
7982
pub fn from_slice(data: &[u8]) -> Result<Signature, Error> {
@@ -88,9 +91,17 @@ impl Signature {
8891
}
8992

9093
/// Returns a signature as a byte array.
91-
#[inline]
94+
#[deprecated(since = "0.30.0", note = "Use `to_byte_array` instead.")]
9295
pub fn serialize(&self) -> [u8; constants::SCHNORR_SIGNATURE_SIZE] { self.0 }
9396

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+
94105
/// Verifies a schnorr signature for `msg` using `pk` and the global [`SECP256K1`] context.
95106
#[inline]
96107
#[cfg(feature = "global-context")]
@@ -294,7 +305,7 @@ mod tests {
294305
#[test]
295306
fn test_serialize() {
296307
let sig = Signature::from_str("6470FD1303DDA4FDA717B9837153C24A6EAB377183FC438F939E0ED2B620E9EE5077C4A8B8DCA28963D772A94F5F0DDF598E1C47C137F91933274C7C3EDADCE8").unwrap();
297-
let sig_bytes = sig.serialize();
308+
let sig_bytes = sig.to_byte_array();
298309
let bytes = [
299310
100, 112, 253, 19, 3, 221, 164, 253, 167, 23, 185, 131, 113, 83, 194, 74, 110, 171, 55,
300311
113, 131, 252, 67, 143, 147, 158, 14, 210, 182, 32, 233, 238, 80, 119, 196, 168, 184,
@@ -697,9 +708,9 @@ mod tests {
697708
let keypair = Keypair::from_seckey_slice(&secp, &secret_key).unwrap();
698709
assert_eq!(keypair.x_only_public_key().0.serialize(), public_key);
699710
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);
701712
}
702-
let sig = Signature::from_slice(&signature).unwrap();
713+
let sig = Signature::from_byte_array(signature);
703714
let is_verified = if let Ok(pubkey) = XOnlyPublicKey::from_slice(&public_key) {
704715
secp.verify_schnorr(&sig, &message, &pubkey).is_ok()
705716
} else {

0 commit comments

Comments
 (0)