Skip to content

Commit a5dfd09

Browse files
authored
Merge pull request #294 from LNP-BP/feat/from_secret_key
Adding schnorrsig::KeyPair::from_secret_key convenience function
2 parents b686de7 + bf9a276 commit a5dfd09

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
@@ -139,6 +139,26 @@ impl KeyPair {
139139
&mut self.0
140140
}
141141

142+
/// Creates a Schnorr KeyPair directly from generic Secp256k1 secret key
143+
///
144+
/// Panics if internal representation of the provided [`SecretKey`] does not
145+
/// holds correct secret key value obtained from Secp256k1 library
146+
/// previously
147+
#[inline]
148+
pub fn from_secret_key<C: Signing>(
149+
secp: &Secp256k1<C>,
150+
sk: ::key::SecretKey,
151+
) -> KeyPair {
152+
unsafe {
153+
let mut kp = ffi::KeyPair::new();
154+
if ffi::secp256k1_keypair_create(secp.ctx, &mut kp, sk.as_c_ptr()) == 1 {
155+
KeyPair(kp)
156+
} else {
157+
panic!("the provided secret key is invalid: it is corrupted or was not produced by Secp256k1 library")
158+
}
159+
}
160+
}
161+
142162
/// Creates a Schnorr KeyPair directly from a secret key slice
143163
#[inline]
144164
pub fn from_seckey_slice<C: Signing>(

0 commit comments

Comments
 (0)