From bf9a276342d21ad1efba37d0d84307a52f6e085f Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Mon, 12 Apr 2021 15:25:50 +0200 Subject: [PATCH] Adding schnorrsig::KeyPair::from_secret_key convenience function --- src/schnorrsig.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/schnorrsig.rs b/src/schnorrsig.rs index 486d4b9..f520734 100644 --- a/src/schnorrsig.rs +++ b/src/schnorrsig.rs @@ -113,6 +113,26 @@ impl KeyPair { &mut self.0 } + /// Creates a Schnorr KeyPair directly from generic Secp256k1 secret key + /// + /// Panics if internal representation of the provided [`SecretKey`] does not + /// holds correct secret key value obtained from Secp256k1 library + /// previously + #[inline] + pub fn from_secret_key( + secp: &Secp256k1, + sk: ::key::SecretKey, + ) -> KeyPair { + unsafe { + let mut kp = ffi::KeyPair::new(); + if ffi::secp256k1_keypair_create(secp.ctx, &mut kp, sk.as_c_ptr()) == 1 { + KeyPair(kp) + } else { + panic!("the provided secret key is invalid: it is corrupted or was not produced by Secp256k1 library") + } + } + } + /// Creates a Schnorr KeyPair directly from a secret key slice #[inline] pub fn from_seckey_slice(