@@ -211,6 +211,12 @@ impl KeyPair {
211
211
}
212
212
}
213
213
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
+
214
220
/// Tweak a keypair by adding the given tweak to the secret key and updating the
215
221
/// public key accordingly.
216
222
/// Will return an error if the resulting key would be invalid or if
@@ -450,35 +456,31 @@ impl<'de> ::serde::Deserialize<'de> for PublicKey {
450
456
}
451
457
}
452
458
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)
466
463
}
467
464
}
468
465
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)
482
484
}
483
485
}
484
486
@@ -728,9 +730,9 @@ mod tests {
728
730
let secp = Secp256k1 :: new ( ) ;
729
731
let sk_str = "688C77BC2D5AAFF5491CF309D4753B732135470D05B7B2CD21ADD0744FE97BEF" ;
730
732
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) ;
732
734
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) ;
734
736
assert_eq ! ( :: key:: PublicKey :: from_secret_key( & secp, & sk) , pk) ;
735
737
let xpk = PublicKey :: from_keypair ( & secp, & keypair) ;
736
738
assert_eq ! ( PublicKey :: from( pk) , xpk) ;
0 commit comments