6
6
serde:: de:: Error ,
7
7
} ;
8
8
9
- /// helper function to convert base64 encoded string into a bytes array
10
- fn base64_to_bytes < const N : usize , E : Error > ( v : & str ) -> Result < [ u8 ; N ] , E > {
11
- let bytes = BASE64_STANDARD . decode ( v) . map_err ( Error :: custom) ?;
12
-
13
- if bytes. len ( ) != N {
14
- return Err ( Error :: custom ( format ! (
15
- "Length of base64 decoded bytes is not {}" ,
16
- N
17
- ) ) ) ;
18
- }
19
-
20
- let mut array = [ 0 ; N ] ;
21
- array. copy_from_slice ( & bytes[ 0 ..N ] ) ;
22
- Ok ( array)
23
- }
24
-
25
9
/// helper function to ser/deser COption wrapped values
26
10
pub mod coption_fromstr {
27
11
use {
@@ -106,14 +90,12 @@ pub mod aeciphertext_fromstr {
106
90
de:: { Error , Visitor } ,
107
91
Deserializer , Serializer ,
108
92
} ,
109
- solana_zk_token_sdk :: zk_token_elgamal :: pod:: AeCiphertext ,
110
- std:: fmt,
93
+ solana_zk_sdk :: encryption :: pod:: auth_encryption :: PodAeCiphertext ,
94
+ std:: { fmt, str :: FromStr } ,
111
95
} ;
112
96
113
- const AE_CIPHERTEXT_LEN : usize = 36 ;
114
-
115
97
/// serialize AeCiphertext values supporting Display trait
116
- pub fn serialize < S > ( x : & AeCiphertext , s : S ) -> Result < S :: Ok , S :: Error >
98
+ pub fn serialize < S > ( x : & PodAeCiphertext , s : S ) -> Result < S :: Ok , S :: Error >
117
99
where
118
100
S : Serializer ,
119
101
{
@@ -123,7 +105,7 @@ pub mod aeciphertext_fromstr {
123
105
struct AeCiphertextVisitor ;
124
106
125
107
impl < ' de > Visitor < ' de > for AeCiphertextVisitor {
126
- type Value = AeCiphertext ;
108
+ type Value = PodAeCiphertext ;
127
109
128
110
fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
129
111
formatter. write_str ( "a FromStr type" )
@@ -133,13 +115,12 @@ pub mod aeciphertext_fromstr {
133
115
where
134
116
E : Error ,
135
117
{
136
- let array = super :: base64_to_bytes :: < AE_CIPHERTEXT_LEN , E > ( v) ?;
137
- Ok ( AeCiphertext ( array) )
118
+ FromStr :: from_str ( v) . map_err ( Error :: custom)
138
119
}
139
120
}
140
121
141
122
/// deserialize AeCiphertext values from str
142
- pub fn deserialize < ' de , D > ( d : D ) -> Result < AeCiphertext , D :: Error >
123
+ pub fn deserialize < ' de , D > ( d : D ) -> Result < PodAeCiphertext , D :: Error >
143
124
where
144
125
D : Deserializer < ' de > ,
145
126
{
@@ -154,14 +135,12 @@ pub mod elgamalpubkey_fromstr {
154
135
de:: { Error , Visitor } ,
155
136
Deserializer , Serializer ,
156
137
} ,
157
- solana_zk_token_sdk :: zk_token_elgamal :: pod:: ElGamalPubkey ,
158
- std:: fmt,
138
+ solana_zk_sdk :: encryption :: pod:: elgamal :: PodElGamalPubkey ,
139
+ std:: { fmt, str :: FromStr } ,
159
140
} ;
160
141
161
- const ELGAMAL_PUBKEY_LEN : usize = 32 ;
162
-
163
142
/// serialize ElGamalPubkey values supporting Display trait
164
- pub fn serialize < S > ( x : & ElGamalPubkey , s : S ) -> Result < S :: Ok , S :: Error >
143
+ pub fn serialize < S > ( x : & PodElGamalPubkey , s : S ) -> Result < S :: Ok , S :: Error >
165
144
where
166
145
S : Serializer ,
167
146
{
@@ -171,7 +150,7 @@ pub mod elgamalpubkey_fromstr {
171
150
struct ElGamalPubkeyVisitor ;
172
151
173
152
impl < ' de > Visitor < ' de > for ElGamalPubkeyVisitor {
174
- type Value = ElGamalPubkey ;
153
+ type Value = PodElGamalPubkey ;
175
154
176
155
fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
177
156
formatter. write_str ( "a FromStr type" )
@@ -181,65 +160,15 @@ pub mod elgamalpubkey_fromstr {
181
160
where
182
161
E : Error ,
183
162
{
184
- let array = super :: base64_to_bytes :: < ELGAMAL_PUBKEY_LEN , E > ( v) ?;
185
- Ok ( ElGamalPubkey ( array) )
163
+ FromStr :: from_str ( v) . map_err ( Error :: custom)
186
164
}
187
165
}
188
166
189
167
/// deserialize ElGamalPubkey values from str
190
- pub fn deserialize < ' de , D > ( d : D ) -> Result < ElGamalPubkey , D :: Error >
168
+ pub fn deserialize < ' de , D > ( d : D ) -> Result < PodElGamalPubkey , D :: Error >
191
169
where
192
170
D : Deserializer < ' de > ,
193
171
{
194
172
d. deserialize_str ( ElGamalPubkeyVisitor )
195
173
}
196
174
}
197
-
198
- /// helper to ser/deser pod::DecryptHandle values
199
- pub mod decrypthandle_fromstr {
200
- use {
201
- base64:: { prelude:: BASE64_STANDARD , Engine } ,
202
- serde:: {
203
- de:: { Error , Visitor } ,
204
- Deserializer , Serializer ,
205
- } ,
206
- solana_zk_token_sdk:: zk_token_elgamal:: pod:: DecryptHandle ,
207
- std:: fmt,
208
- } ;
209
-
210
- const DECRYPT_HANDLE_LEN : usize = 32 ;
211
-
212
- /// Serialize a decrypt handle as a base64 string
213
- pub fn serialize < S > ( x : & DecryptHandle , s : S ) -> Result < S :: Ok , S :: Error >
214
- where
215
- S : Serializer ,
216
- {
217
- s. serialize_str ( & BASE64_STANDARD . encode ( x. 0 ) )
218
- }
219
-
220
- struct DecryptHandleVisitor ;
221
-
222
- impl < ' de > Visitor < ' de > for DecryptHandleVisitor {
223
- type Value = DecryptHandle ;
224
-
225
- fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
226
- formatter. write_str ( "a FromStr type" )
227
- }
228
-
229
- fn visit_str < E > ( self , v : & str ) -> Result < Self :: Value , E >
230
- where
231
- E : Error ,
232
- {
233
- let array = super :: base64_to_bytes :: < DECRYPT_HANDLE_LEN , E > ( v) ?;
234
- Ok ( DecryptHandle ( array) )
235
- }
236
- }
237
-
238
- /// Deserialize a DecryptHandle from a base64 string
239
- pub fn deserialize < ' de , D > ( d : D ) -> Result < DecryptHandle , D :: Error >
240
- where
241
- D : Deserializer < ' de > ,
242
- {
243
- d. deserialize_str ( DecryptHandleVisitor )
244
- }
245
- }
0 commit comments