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

Commit 70f0372

Browse files
committed
update serialization
1 parent 0a5bd2f commit 70f0372

File tree

2 files changed

+22
-93
lines changed

2 files changed

+22
-93
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-
}

token/program-2022/tests/serialization.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
#![cfg(feature = "serde-traits")]
22

33
use {
4+
base64::{engine::general_purpose::STANDARD, Engine},
45
solana_program::program_option::COption,
56
solana_sdk::pubkey::Pubkey,
67
spl_pod::optional_keys::{OptionalNonZeroElGamalPubkey, OptionalNonZeroPubkey},
7-
spl_token_2022::{
8-
extension::confidential_transfer,
9-
instruction,
10-
solana_zk_token_sdk::zk_token_elgamal::pod::{AeCiphertext, ElGamalPubkey},
11-
},
8+
spl_token_2022::{extension::confidential_transfer, instruction},
129
std::str::FromStr,
1310
};
1411

@@ -33,7 +30,6 @@ fn serde_instruction_coption_pubkey_with_none() {
3330
let inst = instruction::TokenInstruction::InitializeMintCloseAuthority {
3431
close_authority: COption::None,
3532
};
36-
3733
let serialized = serde_json::to_string(&inst).unwrap();
3834
assert_eq!(
3935
&serialized,
@@ -51,10 +47,12 @@ fn serde_instruction_optional_nonzero_pubkeys_podbool() {
5147
Some(Pubkey::from_str("4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM").unwrap());
5248
let authority: OptionalNonZeroPubkey = authority_option.try_into().unwrap();
5349

54-
let elgamal_pubkey_pod_option: Option<ElGamalPubkey> = Some(ElGamalPubkey([
50+
let pubkey_string = STANDARD.encode([
5551
162, 23, 108, 36, 130, 143, 18, 219, 196, 134, 242, 145, 179, 49, 229, 193, 74, 64, 3, 158,
5652
68, 235, 124, 88, 247, 144, 164, 254, 228, 12, 173, 85,
57-
]));
53+
]);
54+
let elgamal_pubkey_pod_option = Some(FromStr::from_str(&pubkey_string).unwrap());
55+
5856
let auditor_elgamal_pubkey: OptionalNonZeroElGamalPubkey =
5957
elgamal_pubkey_pod_option.try_into().unwrap();
6058

@@ -105,10 +103,11 @@ fn serde_instruction_optional_nonzero_pubkeys_podbool_with_none() {
105103

106104
#[test]
107105
fn serde_instruction_decryptable_balance_podu64() {
108-
let decryptable_zero_balance = AeCiphertext([
106+
let ciphertext_string = STANDARD.encode([
109107
56, 22, 102, 48, 112, 106, 58, 25, 25, 244, 194, 217, 73, 137, 73, 38, 24, 26, 36, 25, 235,
110108
234, 68, 181, 11, 82, 170, 163, 89, 205, 113, 160, 55, 16, 35, 151,
111109
]);
110+
let decryptable_zero_balance = FromStr::from_str(&ciphertext_string).unwrap();
112111

113112
let inst = confidential_transfer::instruction::ConfigureAccountInstructionData {
114113
decryptable_zero_balance,
@@ -131,10 +130,11 @@ fn serde_instruction_decryptable_balance_podu64() {
131130
fn serde_instruction_elgamal_pubkey() {
132131
use spl_token_2022::extension::confidential_transfer_fee::instruction::InitializeConfidentialTransferFeeConfigData;
133132

134-
let withdraw_withheld_authority_elgamal_pubkey = ElGamalPubkey([
133+
let pubkey_string = STANDARD.encode([
135134
162, 23, 108, 36, 130, 143, 18, 219, 196, 134, 242, 145, 179, 49, 229, 193, 74, 64, 3, 158,
136135
68, 235, 124, 88, 247, 144, 164, 254, 228, 12, 173, 85,
137136
]);
137+
let withdraw_withheld_authority_elgamal_pubkey = FromStr::from_str(&pubkey_string).unwrap();
138138

139139
let inst = InitializeConfidentialTransferFeeConfigData {
140140
authority: OptionalNonZeroPubkey::default(),

0 commit comments

Comments
 (0)