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