Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 4a0fdb7

Browse files
committed
update serialization
1 parent 0a5bd2f commit 4a0fdb7

File tree

1 file changed

+12
-83
lines changed

1 file changed

+12
-83
lines changed

token/program-2022/src/serialization.rs

Lines changed: 12 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,6 @@ use {
66
serde::de::Error,
77
};
88

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-
259
/// helper function to ser/deser COption wrapped values
2610
pub mod coption_fromstr {
2711
use {
@@ -106,14 +90,12 @@ pub mod aeciphertext_fromstr {
10690
de::{Error, Visitor},
10791
Deserializer, Serializer,
10892
},
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},
11195
};
11296

113-
const AE_CIPHERTEXT_LEN: usize = 36;
114-
11597
/// 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>
11799
where
118100
S: Serializer,
119101
{
@@ -123,7 +105,7 @@ pub mod aeciphertext_fromstr {
123105
struct AeCiphertextVisitor;
124106

125107
impl<'de> Visitor<'de> for AeCiphertextVisitor {
126-
type Value = AeCiphertext;
108+
type Value = PodAeCiphertext;
127109

128110
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
129111
formatter.write_str("a FromStr type")
@@ -133,13 +115,12 @@ pub mod aeciphertext_fromstr {
133115
where
134116
E: Error,
135117
{
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)
138119
}
139120
}
140121

141122
/// 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>
143124
where
144125
D: Deserializer<'de>,
145126
{
@@ -154,14 +135,12 @@ pub mod elgamalpubkey_fromstr {
154135
de::{Error, Visitor},
155136
Deserializer, Serializer,
156137
},
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},
159140
};
160141

161-
const ELGAMAL_PUBKEY_LEN: usize = 32;
162-
163142
/// 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>
165144
where
166145
S: Serializer,
167146
{
@@ -171,7 +150,7 @@ pub mod elgamalpubkey_fromstr {
171150
struct ElGamalPubkeyVisitor;
172151

173152
impl<'de> Visitor<'de> for ElGamalPubkeyVisitor {
174-
type Value = ElGamalPubkey;
153+
type Value = PodElGamalPubkey;
175154

176155
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
177156
formatter.write_str("a FromStr type")
@@ -181,65 +160,15 @@ pub mod elgamalpubkey_fromstr {
181160
where
182161
E: Error,
183162
{
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)
186164
}
187165
}
188166

189167
/// 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>
191169
where
192170
D: Deserializer<'de>,
193171
{
194172
d.deserialize_str(ElGamalPubkeyVisitor)
195173
}
196174
}
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

Comments
 (0)