11use std:: collections:: HashMap ;
22
33use base64:: prelude:: * ;
4- use serde :: ser :: { SerializeStruct , Serializer } ;
4+ use schemars :: JsonSchema ;
55use serde:: { Deserialize , Serialize } ;
66
77use crate :: backends:: plonky2:: mock_main:: MockMainPod ;
88use crate :: backends:: plonky2:: mock_signed:: MockSignedPod ;
99use crate :: frontend:: containers:: Dictionary ;
1010use crate :: frontend:: Statement ;
11+ use crate :: frontend:: StatementSerdeHelper ;
1112use crate :: middleware:: { PodId , F } ;
1213use crate :: middleware:: { HASH_SIZE , VALUE_SIZE } ;
1314use plonky2:: field:: types:: Field ;
1415
1516use super :: Value ;
1617use super :: { MainPod , SignedPod } ;
1718
18- impl Serialize for SignedPod {
19- fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
20- where
21- S : Serializer ,
22- {
23- let mut state = serializer. serialize_struct ( "SignedPod" , 3 ) ?;
24- state. serialize_field (
25- "entries" ,
26- & self
27- . kvs
28- . iter ( )
29- . map ( |( k, v) | ( k. clone ( ) , v) )
30- . collect :: < HashMap < String , & Value > > ( ) ,
31- ) ?;
32-
33- let signature = self . pod . serialized_proof ( ) ;
34-
35- state. serialize_field ( "id" , & self . id ( ) ) ?;
36- state. serialize_field ( "proof" , & signature) ?;
37- state. serialize_field ( "pod_class" , "Signed" ) ?;
38- state. serialize_field ( "pod_type" , "Mock" ) ?;
39- state. end ( )
40- }
19+ #[ derive( Serialize , Deserialize , JsonSchema ) ]
20+ pub struct SignedPodHelper {
21+ entries : HashMap < String , Value > ,
22+ proof : String ,
23+ pod_class : String ,
24+ pod_type : String ,
4125}
4226
43- impl < ' de > Deserialize < ' de > for SignedPod {
44- fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
45- where
46- D : serde:: Deserializer < ' de > ,
47- {
48- #[ derive( Deserialize ) ]
49- struct SignedPodHelper {
50- entries : HashMap < String , Value > ,
51- proof : String ,
52- pod_class : String ,
53- pod_type : String ,
54- }
27+ impl TryFrom < SignedPodHelper > for SignedPod {
28+ type Error = anyhow:: Error ;
5529
56- let helper = SignedPodHelper :: deserialize ( deserializer ) ? ;
30+ fn try_from ( helper : SignedPodHelper ) -> Result < SignedPod , Self :: Error > {
5731 if helper. pod_class != "Signed" {
58- return Err ( serde :: de :: Error :: custom ( "pod_class is not Signed" ) ) ;
32+ return Err ( anyhow :: anyhow! ( "pod_class is not Signed" ) ) ;
5933 }
6034 if helper. pod_type != "Mock" {
61- return Err ( serde :: de :: Error :: custom ( "pod_type is not Mock" ) ) ;
35+ return Err ( anyhow :: anyhow! ( "pod_type is not Mock" ) ) ;
6236 }
63- let kvs = helper. entries ;
64- let dict = Dictionary :: new ( kvs. clone ( ) ) . middleware_dict ( ) . clone ( ) ;
37+
38+ let dict = Dictionary :: new ( helper. entries . clone ( ) )
39+ . middleware_dict ( )
40+ . clone ( ) ;
6541 let pod = MockSignedPod :: new ( PodId ( dict. commitment ( ) ) , helper. proof , dict) ;
42+
6643 Ok ( SignedPod {
6744 pod : Box :: new ( pod) ,
68- kvs : kvs ,
45+ kvs : helper . entries ,
6946 } )
7047 }
7148}
7249
73- impl Serialize for MainPod {
74- fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
75- where
76- S : Serializer ,
77- {
78- let mut state = serializer. serialize_struct ( "MainPod" , 2 ) ?;
79- state. serialize_field ( "public_statements" , & self . public_statements ) ?;
80- state. serialize_field ( "id" , & self . pod . id ( ) ) ?;
81- state. serialize_field ( "proof" , & self . pod . serialized_proof ( ) ) ?;
82- state. serialize_field ( "pod_class" , "Main" ) ?;
83- state. serialize_field ( "pod_type" , "Mock" ) ?;
84- state. end ( )
50+ impl From < SignedPod > for SignedPodHelper {
51+ fn from ( pod : SignedPod ) -> Self {
52+ SignedPodHelper {
53+ entries : pod. kvs ,
54+ proof : pod. pod . serialized_proof ( ) ,
55+ pod_class : "Signed" . to_string ( ) ,
56+ pod_type : "Mock" . to_string ( ) ,
57+ }
8558 }
8659}
8760
88- impl < ' de > Deserialize < ' de > for MainPod {
89- fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
90- where
91- D : serde:: Deserializer < ' de > ,
92- {
93- #[ derive( Deserialize ) ]
94- struct MainPodHelper {
95- public_statements : Vec < Statement > ,
96- proof : String ,
97- pod_class : String ,
98- pod_type : String ,
99- }
61+ #[ derive( Serialize , Deserialize , JsonSchema ) ]
62+ pub struct MainPodHelper {
63+ #[ schemars( with = "Vec<StatementSerdeHelper>" ) ]
64+ public_statements : Vec < Statement > ,
65+ proof : String ,
66+ pod_class : String ,
67+ pod_type : String ,
68+ }
10069
101- let helper = MainPodHelper :: deserialize ( deserializer) ?;
70+ impl TryFrom < MainPodHelper > for MainPod {
71+ type Error = anyhow:: Error ; // or you can create a custom error type
72+
73+ fn try_from ( helper : MainPodHelper ) -> Result < Self , Self :: Error > {
10274 if helper. pod_class != "Main" {
103- return Err ( serde :: de :: Error :: custom ( "pod_class is not Main" ) ) ;
75+ return Err ( anyhow :: anyhow! ( "pod_class is not Main" ) ) ;
10476 }
10577 if helper. pod_type != "Mock" {
106- return Err ( serde :: de :: Error :: custom ( "pod_type is not Mock" ) ) ;
78+ return Err ( anyhow :: anyhow! ( "pod_type is not Mock" ) ) ;
10779 }
108- let proof = String :: from_utf8 ( BASE64_STANDARD . decode ( & helper. proof ) . unwrap ( ) ) . unwrap ( ) ;
109- let pod: MockMainPod = serde_json:: from_str ( & proof) . unwrap ( ) ;
80+
81+ let proof = String :: from_utf8 ( BASE64_STANDARD . decode ( & helper. proof ) ?)
82+ . map_err ( |e| anyhow:: anyhow!( "Invalid base64 encoding: {}" , e) ) ?;
83+
84+ let pod: MockMainPod = serde_json:: from_str ( & proof)
85+ . map_err ( |e| anyhow:: anyhow!( "Failed to parse proof: {}" , e) ) ?;
11086
11187 Ok ( MainPod {
11288 pod : Box :: new ( pod) ,
@@ -115,6 +91,17 @@ impl<'de> Deserialize<'de> for MainPod {
11591 }
11692}
11793
94+ impl From < MainPod > for MainPodHelper {
95+ fn from ( pod : MainPod ) -> Self {
96+ MainPodHelper {
97+ public_statements : pod. public_statements ,
98+ proof : pod. pod . serialized_proof ( ) ,
99+ pod_class : "Main" . to_string ( ) ,
100+ pod_type : "Mock" . to_string ( ) ,
101+ }
102+ }
103+ }
104+
118105pub fn serialize_i64 < S > ( value : & i64 , serializer : S ) -> Result < S :: Ok , S :: Error >
119106where
120107 S : serde:: Serializer ,
@@ -305,4 +292,15 @@ mod tests {
305292 assert_eq ! ( kyc_pod. pod. id( ) , deserialized. pod. id( ) ) ;
306293 assert_eq ! ( kyc_pod. pod. verify( ) , deserialized. pod. verify( ) ) ;
307294 }
295+
296+ #[ test]
297+ fn test_gen_schema ( ) {
298+ let schema = schemars:: schema_for!( SignedPodHelper ) ;
299+ let schema = serde_json:: to_string_pretty ( & schema) . unwrap ( ) ;
300+ println ! ( "schema: {}" , schema) ;
301+
302+ let schema = schemars:: schema_for!( MainPodHelper ) ;
303+ let schema = serde_json:: to_string_pretty ( & schema) . unwrap ( ) ;
304+ println ! ( "schema: {}" , schema) ;
305+ }
308306}
0 commit comments