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