@@ -18,7 +18,33 @@ use {Message, Signing, Verification};
1818pub struct Signature ( [ u8 ; constants:: SCHNORRSIG_SIGNATURE_SIZE ] ) ;
1919impl_array_newtype ! ( Signature , u8 , constants:: SCHNORRSIG_SIGNATURE_SIZE ) ;
2020impl_pretty_debug ! ( Signature ) ;
21- serde_impl ! ( Signature , constants:: SCHNORRSIG_SIGNATURE_SIZE ) ;
21+
22+ #[ cfg( feature = "serde" ) ]
23+ impl :: serde:: Serialize for Signature {
24+ fn serialize < S : :: serde:: Serializer > ( & self , s : S ) -> Result < S :: Ok , S :: Error > {
25+ if s. is_human_readable ( ) {
26+ s. collect_str ( self )
27+ } else {
28+ s. serialize_bytes ( & self [ ..] )
29+ }
30+ }
31+ }
32+
33+ #[ cfg( feature = "serde" ) ]
34+ impl < ' de > :: serde:: Deserialize < ' de > for Signature {
35+ fn deserialize < D : :: serde:: Deserializer < ' de > > ( d : D ) -> Result < Self , D :: Error > {
36+ if d. is_human_readable ( ) {
37+ d. deserialize_str ( super :: serde_util:: FromStrVisitor :: new (
38+ "a hex string representing 64 byte schnorr signature"
39+ ) )
40+ } else {
41+ d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
42+ "raw 64 bytes schnorr signature" ,
43+ Signature :: from_slice
44+ ) )
45+ }
46+ }
47+ }
2248
2349impl fmt:: LowerHex for Signature {
2450 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -376,7 +402,32 @@ impl From<::key::PublicKey> for PublicKey {
376402 }
377403}
378404
379- serde_impl_from_slice ! ( PublicKey ) ;
405+ #[ cfg( feature = "serde" ) ]
406+ impl :: serde:: Serialize for PublicKey {
407+ fn serialize < S : :: serde:: Serializer > ( & self , s : S ) -> Result < S :: Ok , S :: Error > {
408+ if s. is_human_readable ( ) {
409+ s. collect_str ( self )
410+ } else {
411+ s. serialize_bytes ( & self . serialize ( ) )
412+ }
413+ }
414+ }
415+
416+ #[ cfg( feature = "serde" ) ]
417+ impl < ' de > :: serde:: Deserialize < ' de > for PublicKey {
418+ fn deserialize < D : :: serde:: Deserializer < ' de > > ( d : D ) -> Result < Self , D :: Error > {
419+ if d. is_human_readable ( ) {
420+ d. deserialize_str ( super :: serde_util:: FromStrVisitor :: new (
421+ "a hex string representing 32 byte schnorr public key"
422+ ) )
423+ } else {
424+ d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
425+ "raw 32 bytes schnorr public key" ,
426+ PublicKey :: from_slice
427+ ) )
428+ }
429+ }
430+ }
380431
381432impl < C : Signing > Secp256k1 < C > {
382433 fn schnorrsig_sign_helper (
@@ -724,7 +775,7 @@ mod tests {
724775 #[ cfg( feature = "serde" ) ]
725776 #[ cfg( not( fuzzing) ) ] // fixed sig vectors can't work with fuzz-sigs
726777 #[ test]
727- fn test_signature_serde ( ) {
778+ fn test_serde ( ) {
728779 use serde_test:: { assert_tokens, Configure , Token } ;
729780
730781 let s = Secp256k1 :: new ( ) ;
@@ -745,8 +796,30 @@ mod tests {
745796 14d0bf1a8953506fb460f58be141af767fd112535fb3922ef217308e2c26706f1eeb432b3dba9a01082f9e4d4ef5678ad0d9d532c0dfa907b568722d0b0119ba\
746797 ";
747798
799+ static PK_BYTES : [ u8 ; 32 ] = [
800+ 24 , 132 , 87 , 129 , 246 , 49 , 196 , 143 , 28 , 151 , 9 , 226 , 48 , 146 , 6 , 125 , 6 , 131 , 127 ,
801+ 48 , 170 , 12 , 208 , 84 , 74 , 200 , 135 , 254 , 145 , 221 , 209 , 102
802+ ] ;
803+ static PK_STR : & ' static str = "\
804+ 18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166\
805+ ";
806+ let pk = PublicKey :: from_slice ( & PK_BYTES ) . unwrap ( ) ;
807+
748808 assert_tokens ( & sig. compact ( ) , & [ Token :: BorrowedBytes ( & SIG_BYTES [ ..] ) ] ) ;
809+ assert_tokens ( & sig. compact ( ) , & [ Token :: Bytes ( & SIG_BYTES [ ..] ) ] ) ;
810+ assert_tokens ( & sig. compact ( ) , & [ Token :: ByteBuf ( & SIG_BYTES [ ..] ) ] ) ;
811+
749812 assert_tokens ( & sig. readable ( ) , & [ Token :: BorrowedStr ( SIG_STR ) ] ) ;
813+ assert_tokens ( & sig. readable ( ) , & [ Token :: Str ( SIG_STR ) ] ) ;
814+ assert_tokens ( & sig. readable ( ) , & [ Token :: String ( SIG_STR ) ] ) ;
815+
816+ assert_tokens ( & pk. compact ( ) , & [ Token :: BorrowedBytes ( & PK_BYTES [ ..] ) ] ) ;
817+ assert_tokens ( & pk. compact ( ) , & [ Token :: Bytes ( & PK_BYTES [ ..] ) ] ) ;
818+ assert_tokens ( & pk. compact ( ) , & [ Token :: ByteBuf ( & PK_BYTES [ ..] ) ] ) ;
819+
820+ assert_tokens ( & pk. readable ( ) , & [ Token :: BorrowedStr ( PK_STR ) ] ) ;
821+ assert_tokens ( & pk. readable ( ) , & [ Token :: Str ( PK_STR ) ] ) ;
822+ assert_tokens ( & pk. readable ( ) , & [ Token :: String ( PK_STR ) ] ) ;
750823 }
751824 #[ test]
752825 fn test_addition ( ) {
0 commit comments