@@ -81,6 +81,48 @@ impl str::FromStr for Signature {
8181pub struct KeyPair ( ffi:: KeyPair ) ;
8282impl_safe_debug ! ( KeyPair ) ;
8383
84+ impl :: core:: str:: FromStr for KeyPair {
85+ type Err = Error ;
86+
87+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
88+ let ctx = unsafe {
89+ Secp256k1 :: from_raw_all ( ffi:: secp256k1_context_no_precomp as * mut ffi:: Context )
90+ } ;
91+ KeyPair :: from_seckey_str ( & ctx, s)
92+ }
93+ }
94+
95+ #[ cfg( all( feature = "serde" , feature = "serde-secrets" ) ) ]
96+ impl :: serde:: Serialize for KeyPair {
97+ fn serialize < S : :: serde:: Serializer > ( & self , s : S ) -> Result < S :: Ok , S :: Error > {
98+ if s. is_human_readable ( ) {
99+ #[ allow( deprecated) ]
100+ s. serialize_str ( & self . format_secret_key ( ) )
101+ } else {
102+ s. serialize_bytes ( & self . 0 [ ..] )
103+ }
104+ }
105+ }
106+
107+ #[ cfg( feature = "serde" ) ]
108+ impl < ' de > :: serde:: Deserialize < ' de > for KeyPair {
109+ fn deserialize < D : :: serde:: Deserializer < ' de > > ( d : D ) -> Result < Self , D :: Error > {
110+ if d. is_human_readable ( ) {
111+ d. deserialize_str ( super :: serde_util:: FromStrVisitor :: new (
112+ "a hex string representing 32 byte KeyPair"
113+ ) )
114+ } else {
115+ d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
116+ "raw 32 bytes KeyPair" ,
117+ |data| unsafe {
118+ let ctx = Secp256k1 :: from_raw_all ( ffi:: secp256k1_context_no_precomp as * mut ffi:: Context ) ;
119+ KeyPair :: from_seckey_slice ( & ctx, data)
120+ }
121+ ) )
122+ }
123+ }
124+ }
125+
84126/// A Schnorr public key, used for verification of Schnorr signatures
85127#[ derive( Copy , Clone , PartialEq , Eq , Debug , PartialOrd , Ord , Hash ) ]
86128pub struct PublicKey ( ffi:: XOnlyPublicKey ) ;
0 commit comments