@@ -366,17 +366,66 @@ impl From<ffi::PublicKey> for PublicKey {
366
366
#[ cfg( feature = "serde" ) ]
367
367
impl :: serde:: Serialize for PublicKey {
368
368
fn serialize < S : :: serde:: Serializer > ( & self , s : S ) -> Result < S :: Ok , S :: Error > {
369
- s. serialize_bytes ( & self . serialize ( ) )
369
+ if s. is_human_readable ( ) {
370
+ s. collect_str ( self )
371
+ } else {
372
+ s. serialize_bytes ( & self . serialize ( ) )
373
+ }
370
374
}
371
375
}
372
376
373
377
#[ cfg( feature = "serde" ) ]
374
378
impl < ' de > :: serde:: Deserialize < ' de > for PublicKey {
375
379
fn deserialize < D : :: serde:: Deserializer < ' de > > ( d : D ) -> Result < PublicKey , D :: Error > {
376
- use :: serde:: de:: Error ;
380
+ if d. is_human_readable ( ) {
381
+ struct HexVisitor ;
382
+
383
+ impl < ' de > :: serde:: de:: Visitor < ' de > for HexVisitor {
384
+ type Value = PublicKey ;
385
+
386
+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
387
+ formatter. write_str ( "an ASCII hex string" )
388
+ }
389
+
390
+ fn visit_bytes < E > ( self , v : & [ u8 ] ) -> Result < Self :: Value , E >
391
+ where
392
+ E : :: serde:: de:: Error ,
393
+ {
394
+ if let Ok ( hex) = str:: from_utf8 ( v) {
395
+ str:: FromStr :: from_str ( hex) . map_err ( E :: custom)
396
+ } else {
397
+ Err ( E :: invalid_value ( :: serde:: de:: Unexpected :: Bytes ( v) , & self ) )
398
+ }
399
+ }
400
+
401
+ fn visit_str < E > ( self , v : & str ) -> Result < Self :: Value , E >
402
+ where
403
+ E : :: serde:: de:: Error ,
404
+ {
405
+ str:: FromStr :: from_str ( v) . map_err ( E :: custom)
406
+ }
407
+ }
408
+ d. deserialize_str ( HexVisitor )
409
+ } else {
410
+ struct BytesVisitor ;
411
+
412
+ impl < ' de > :: serde:: de:: Visitor < ' de > for BytesVisitor {
413
+ type Value = PublicKey ;
377
414
378
- let sl: & [ u8 ] = :: serde:: Deserialize :: deserialize ( d) ?;
379
- PublicKey :: from_slice ( sl) . map_err ( D :: Error :: custom)
415
+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
416
+ formatter. write_str ( "a bytestring" )
417
+ }
418
+
419
+ fn visit_bytes < E > ( self , v : & [ u8 ] ) -> Result < Self :: Value , E >
420
+ where
421
+ E : :: serde:: de:: Error ,
422
+ {
423
+ PublicKey :: from_slice ( v) . map_err ( E :: custom)
424
+ }
425
+ }
426
+
427
+ d. deserialize_bytes ( BytesVisitor )
428
+ }
380
429
}
381
430
}
382
431
@@ -745,6 +794,9 @@ mod test {
745
794
0x06 , 0x83 , 0x7f , 0x30 , 0xaa , 0x0c , 0xd0 , 0x54 ,
746
795
0x4a , 0xc8 , 0x87 , 0xfe , 0x91 , 0xdd , 0xd1 , 0x66 ,
747
796
] ;
797
+ static PK_STR : & ' static str = "\
798
+ 0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166\
799
+ ";
748
800
749
801
let s = Secp256k1 :: new ( ) ;
750
802
@@ -753,6 +805,7 @@ mod test {
753
805
754
806
assert_tokens ( & sk. compact ( ) , & [ Token :: BorrowedBytes ( & SK_BYTES [ ..] ) ] ) ;
755
807
assert_tokens ( & sk. readable ( ) , & [ Token :: BorrowedStr ( SK_STR ) ] ) ;
756
- assert_tokens ( & pk, & [ Token :: BorrowedBytes ( & PK_BYTES [ ..] ) ] ) ;
808
+ assert_tokens ( & pk. compact ( ) , & [ Token :: BorrowedBytes ( & PK_BYTES [ ..] ) ] ) ;
809
+ assert_tokens ( & pk. readable ( ) , & [ Token :: BorrowedStr ( PK_STR ) ] ) ;
757
810
}
758
811
}
0 commit comments