Skip to content

Commit bf9a276

Browse files
committed
Adding schnorrsig::KeyPair::from_secret_key convenience function
1 parent 63f4de7 commit bf9a276

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/schnorrsig.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ impl KeyPair {
113113
&mut self.0
114114
}
115115

116+
/// Creates a Schnorr KeyPair directly from generic Secp256k1 secret key
117+
///
118+
/// Panics if internal representation of the provided [`SecretKey`] does not
119+
/// holds correct secret key value obtained from Secp256k1 library
120+
/// previously
121+
#[inline]
122+
pub fn from_secret_key<C: Signing>(
123+
secp: &Secp256k1<C>,
124+
sk: ::key::SecretKey,
125+
) -> KeyPair {
126+
unsafe {
127+
let mut kp = ffi::KeyPair::new();
128+
if ffi::secp256k1_keypair_create(secp.ctx, &mut kp, sk.as_c_ptr()) == 1 {
129+
KeyPair(kp)
130+
} else {
131+
panic!("the provided secret key is invalid: it is corrupted or was not produced by Secp256k1 library")
132+
}
133+
}
134+
}
135+
116136
/// Creates a Schnorr KeyPair directly from a secret key slice
117137
#[inline]
118138
pub fn from_seckey_slice<C: Signing>(

0 commit comments

Comments
 (0)