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

Commit 9cc5ede

Browse files
committed
update serialization
1 parent 120a0d3 commit 9cc5ede

File tree

2 files changed

+24
-88
lines changed

2 files changed

+24
-88
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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
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},
78
spl_token_2022::{
89
extension::confidential_transfer,
910
instruction,
10-
solana_zk_token_sdk::zk_token_elgamal::pod::{AeCiphertext, ElGamalPubkey},
11+
solana_zk_sdk::encryption::pod::{
12+
auth_encryption::PodAeCiphertext, elgamal::PodElGamalPubkey,
13+
},
1114
},
1215
std::str::FromStr,
1316
};
@@ -51,10 +54,12 @@ fn serde_instruction_optional_nonzero_pubkeys_podbool() {
5154
Some(Pubkey::from_str("4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM").unwrap());
5255
let authority: OptionalNonZeroPubkey = authority_option.try_into().unwrap();
5356

54-
let elgamal_pubkey_pod_option: Option<ElGamalPubkey> = Some(ElGamalPubkey([
57+
let pubkey_string = STANDARD.encode([
5558
162, 23, 108, 36, 130, 143, 18, 219, 196, 134, 242, 145, 179, 49, 229, 193, 74, 64, 3, 158,
5659
68, 235, 124, 88, 247, 144, 164, 254, 228, 12, 173, 85,
57-
]));
60+
]);
61+
let elgamal_pubkey_pod_option = Some(FromStr::from_str(&pubkey_string).unwrap());
62+
5863
let auditor_elgamal_pubkey: OptionalNonZeroElGamalPubkey =
5964
elgamal_pubkey_pod_option.try_into().unwrap();
6065

@@ -105,10 +110,11 @@ fn serde_instruction_optional_nonzero_pubkeys_podbool_with_none() {
105110

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

113119
let inst = confidential_transfer::instruction::ConfigureAccountInstructionData {
114120
decryptable_zero_balance,
@@ -131,10 +137,11 @@ fn serde_instruction_decryptable_balance_podu64() {
131137
fn serde_instruction_elgamal_pubkey() {
132138
use spl_token_2022::extension::confidential_transfer_fee::instruction::InitializeConfidentialTransferFeeConfigData;
133139

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

139146
let inst = InitializeConfidentialTransferFeeConfigData {
140147
authority: OptionalNonZeroPubkey::default(),

0 commit comments

Comments
 (0)