@@ -211,6 +211,12 @@ impl KeyPair {
211211 }
212212 }
213213
214+ /// Serialize the key pair as a secret key byte value
215+ #[ inline]
216+ pub fn serialize_secret ( & self ) -> [ u8 ; constants:: SECRET_KEY_SIZE ] {
217+ * SecretKey :: from_keypair ( self ) . as_ref ( )
218+ }
219+
214220 /// Tweak a keypair by adding the given tweak to the secret key and updating the
215221 /// public key accordingly.
216222 /// Will return an error if the resulting key would be invalid or if
@@ -450,35 +456,31 @@ impl<'de> ::serde::Deserialize<'de> for PublicKey {
450456 }
451457}
452458
453- impl SecretKey {
454- /// Creates a new secret key using data from BIP-340 [`KeyPair`]
455- pub fn from_keypair < V : Verification > ( secp : & Secp256k1 < V > , keypair : & KeyPair ) -> Self {
456- let mut sk = [ 0 ; constants:: SECRET_KEY_SIZE ] ;
457- unsafe {
458- let ret = ffi:: secp256k1_keypair_sec (
459- secp. ctx ,
460- sk. as_mut_c_ptr ( ) ,
461- keypair. as_ptr ( )
462- ) ;
463- debug_assert_eq ! ( ret, 1 ) ;
464- }
465- SecretKey ( sk)
459+ impl From < KeyPair > for SecretKey {
460+ #[ inline]
461+ fn from ( pair : KeyPair ) -> Self {
462+ SecretKey :: from_keypair ( & pair)
466463 }
467464}
468465
469- impl :: key:: PublicKey {
470- /// Creates a new compressed public key key using data from BIP-340 [`KeyPair`]
471- pub fn from_keypair < C : Signing > ( secp : & Secp256k1 < C > , keypair : & KeyPair ) -> Self {
472- unsafe {
473- let mut pk = ffi:: PublicKey :: new ( ) ;
474- let ret = ffi:: secp256k1_keypair_pub (
475- secp. ctx ,
476- & mut pk,
477- keypair. as_ptr ( )
478- ) ;
479- debug_assert_eq ! ( ret, 1 ) ;
480- :: key:: PublicKey ( pk)
481- }
466+ impl < ' a > From < & ' a KeyPair > for SecretKey {
467+ #[ inline]
468+ fn from ( pair : & ' a KeyPair ) -> Self {
469+ SecretKey :: from_keypair ( pair)
470+ }
471+ }
472+
473+ impl From < KeyPair > for :: key:: PublicKey {
474+ #[ inline]
475+ fn from ( pair : KeyPair ) -> Self {
476+ :: key:: PublicKey :: from_keypair ( & pair)
477+ }
478+ }
479+
480+ impl < ' a > From < & ' a KeyPair > for :: key:: PublicKey {
481+ #[ inline]
482+ fn from ( pair : & ' a KeyPair ) -> Self {
483+ :: key:: PublicKey :: from_keypair ( pair)
482484 }
483485}
484486
@@ -728,9 +730,9 @@ mod tests {
728730 let secp = Secp256k1 :: new ( ) ;
729731 let sk_str = "688C77BC2D5AAFF5491CF309D4753B732135470D05B7B2CD21ADD0744FE97BEF" ;
730732 let keypair = KeyPair :: from_seckey_str ( & secp, sk_str) . unwrap ( ) ;
731- let sk = SecretKey :: from_keypair ( & secp , & keypair) ;
733+ let sk = SecretKey :: from_keypair ( & keypair) ;
732734 assert_eq ! ( SecretKey :: from_str( sk_str) . unwrap( ) , sk) ;
733- let pk = :: key:: PublicKey :: from_keypair ( & secp , & keypair) ;
735+ let pk = :: key:: PublicKey :: from_keypair ( & keypair) ;
734736 assert_eq ! ( :: key:: PublicKey :: from_secret_key( & secp, & sk) , pk) ;
735737 let xpk = PublicKey :: from_keypair ( & secp, & keypair) ;
736738 assert_eq ! ( PublicKey :: from( pk) , xpk) ;
0 commit comments