Skip to content

Commit dee1c95

Browse files
authored
change extension bytes data to base58 encoded (#232)
1 parent e9fce97 commit dee1c95

File tree

5 files changed

+63
-266
lines changed

5 files changed

+63
-266
lines changed

blockbuster/src/programs/token_extensions/extension.rs

Lines changed: 43 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,12 @@ use spl_token_2022::extension::{
2121
transfer_fee::{TransferFee, TransferFeeAmount, TransferFeeConfig},
2222
transfer_hook::TransferHook,
2323
};
24-
use std::fmt;
2524

2625
use spl_token_group_interface::state::{TokenGroup, TokenGroupMember};
2726
use spl_token_metadata_interface::state::TokenMetadata;
2827

29-
const AE_CIPHERTEXT_LEN: usize = 36;
30-
const UNIT_LEN: usize = 32;
31-
const RISTRETTO_POINT_LEN: usize = UNIT_LEN;
32-
pub(crate) const DECRYPT_HANDLE_LEN: usize = RISTRETTO_POINT_LEN;
33-
pub(crate) const PEDERSEN_COMMITMENT_LEN: usize = RISTRETTO_POINT_LEN;
34-
const ELGAMAL_PUBKEY_LEN: usize = RISTRETTO_POINT_LEN;
35-
const ELGAMAL_CIPHERTEXT_LEN: usize = PEDERSEN_COMMITMENT_LEN + DECRYPT_HANDLE_LEN;
3628
type PodAccountState = u8;
3729
pub type UnixTimestamp = PodI64;
38-
pub type EncryptedBalance = ShadowElGamalCiphertext;
39-
pub type DecryptableBalance = ShadowAeCiphertext;
40-
pub type EncryptedWithheldAmount = ShadowElGamalCiphertext;
41-
42-
use serde::{
43-
de::{self, SeqAccess, Visitor},
44-
Deserializer, Serializer,
45-
};
4630

4731
/// Bs58 encoded public key string. Used for storing Pubkeys in a human readable format.
4832
/// Ideally we'd store them as is in the DB and later convert them to bs58 for display on the API.
@@ -53,19 +37,6 @@ use serde::{
5337
/// - `Pubkey` doesn't implement something like `schemars::JsonSchema` so we can't convert them back to the rust struct either.
5438
type PublicKeyString = String;
5539

56-
struct ShadowAeCiphertextVisitor;
57-
58-
struct ShadowElGamalCiphertextVisitor;
59-
60-
#[derive(Clone, Copy, Debug, PartialEq)]
61-
pub struct ShadowAeCiphertext(pub [u8; AE_CIPHERTEXT_LEN]);
62-
63-
#[derive(Clone, Copy, Debug, PartialEq, Zeroable)]
64-
pub struct ShadowElGamalCiphertext(pub [u8; ELGAMAL_CIPHERTEXT_LEN]);
65-
66-
#[derive(Clone, Copy, Debug, Default, Zeroable, PartialEq, Eq, Serialize, Deserialize)]
67-
pub struct ShadowElGamalPubkey(pub [u8; ELGAMAL_PUBKEY_LEN]);
68-
6940
#[derive(Clone, Copy, Debug, Default, PartialEq, Zeroable, Serialize, Deserialize)]
7041
pub struct ShadowCpiGuard {
7142
pub lock_cpi: PodBool,
@@ -164,14 +135,14 @@ pub struct ShadowConfidentialTransferMint {
164135
pub auditor_elgamal_pubkey: OptionalNonZeroElGamalPubkey,
165136
}
166137

167-
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
138+
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
168139
pub struct ShadowConfidentialTransferAccount {
169140
pub approved: PodBool,
170-
pub elgamal_pubkey: ShadowElGamalPubkey,
171-
pub pending_balance_lo: EncryptedBalance,
172-
pub pending_balance_hi: EncryptedBalance,
173-
pub available_balance: EncryptedBalance,
174-
pub decryptable_available_balance: DecryptableBalance,
141+
pub elgamal_pubkey: String,
142+
pub pending_balance_lo: String,
143+
pub pending_balance_hi: String,
144+
pub available_balance: String,
145+
pub decryptable_available_balance: String,
175146
pub allow_confidential_credits: PodBool,
176147
pub allow_non_confidential_credits: PodBool,
177148
pub pending_balance_credit_counter: PodU64,
@@ -180,16 +151,16 @@ pub struct ShadowConfidentialTransferAccount {
180151
pub actual_pending_balance_credit_counter: PodU64,
181152
}
182153

183-
#[derive(Clone, Copy, Debug, Default, PartialEq, Zeroable, Serialize, Deserialize)]
154+
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
184155
pub struct ShadowConfidentialTransferFeeConfig {
185156
pub authority: OptionalNonZeroPubkey,
186-
pub withdraw_withheld_authority_elgamal_pubkey: ShadowElGamalPubkey,
157+
pub withdraw_withheld_authority_elgamal_pubkey: String,
187158
pub harvest_to_mint_enabled: PodBool,
188-
pub withheld_amount: EncryptedWithheldAmount,
159+
pub withheld_amount: String,
189160
}
190161

191162
pub struct ShadowConfidentialTransferFeeAmount {
192-
pub withheld_amount: EncryptedWithheldAmount,
163+
pub withheld_amount: String,
193164
}
194165

195166
#[derive(Clone, Copy, Debug, Default, PartialEq, Zeroable, Serialize, Deserialize)]
@@ -216,114 +187,6 @@ pub struct ShadowMetadata {
216187
pub additional_metadata: Vec<(String, String)>,
217188
}
218189

219-
impl Serialize for ShadowAeCiphertext {
220-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
221-
where
222-
S: Serializer,
223-
{
224-
serializer.serialize_bytes(&self.0)
225-
}
226-
}
227-
228-
impl<'de> Visitor<'de> for ShadowElGamalCiphertextVisitor {
229-
type Value = ShadowElGamalCiphertext;
230-
231-
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
232-
formatter.write_str("a byte array of length ELGAMAL_CIPHERTEXT_LEN")
233-
}
234-
235-
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
236-
where
237-
E: de::Error,
238-
{
239-
if v.len() == ELGAMAL_CIPHERTEXT_LEN {
240-
let mut arr = [0u8; ELGAMAL_CIPHERTEXT_LEN];
241-
arr.copy_from_slice(v);
242-
Ok(ShadowElGamalCiphertext(arr))
243-
} else {
244-
Err(E::invalid_length(v.len(), &self))
245-
}
246-
}
247-
}
248-
249-
impl<'de> Deserialize<'de> for ShadowElGamalCiphertext {
250-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
251-
where
252-
D: Deserializer<'de>,
253-
{
254-
deserializer.deserialize_bytes(ShadowElGamalCiphertextVisitor)
255-
}
256-
}
257-
258-
impl Default for ShadowElGamalCiphertext {
259-
fn default() -> Self {
260-
ShadowElGamalCiphertext([0u8; ELGAMAL_CIPHERTEXT_LEN])
261-
}
262-
}
263-
264-
impl Default for ShadowAeCiphertext {
265-
fn default() -> Self {
266-
ShadowAeCiphertext([0u8; AE_CIPHERTEXT_LEN])
267-
}
268-
}
269-
270-
impl<'de> Visitor<'de> for ShadowAeCiphertextVisitor {
271-
type Value = ShadowAeCiphertext;
272-
273-
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
274-
formatter.write_str("a byte array of length AE_CIPHERTEXT_LEN")
275-
}
276-
277-
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
278-
where
279-
A: SeqAccess<'de>,
280-
{
281-
let mut arr = [0u8; AE_CIPHERTEXT_LEN];
282-
for (i, item) in arr.iter_mut().enumerate().take(AE_CIPHERTEXT_LEN) {
283-
*item = seq
284-
.next_element()?
285-
.ok_or(de::Error::invalid_length(i, &self))?;
286-
}
287-
Ok(ShadowAeCiphertext(arr))
288-
}
289-
}
290-
291-
impl<'de> Deserialize<'de> for ShadowAeCiphertext {
292-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
293-
where
294-
D: Deserializer<'de>,
295-
{
296-
deserializer.deserialize_tuple(AE_CIPHERTEXT_LEN, ShadowAeCiphertextVisitor)
297-
}
298-
}
299-
300-
impl Serialize for ShadowElGamalCiphertext {
301-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
302-
where
303-
S: Serializer,
304-
{
305-
serializer.serialize_bytes(&self.0)
306-
}
307-
}
308-
309-
impl From<AeCiphertext> for ShadowAeCiphertext {
310-
fn from(original: AeCiphertext) -> Self {
311-
ShadowAeCiphertext(original.0)
312-
}
313-
}
314-
315-
impl From<ElGamalCiphertext> for ShadowElGamalCiphertext {
316-
fn from(original: ElGamalCiphertext) -> Self {
317-
ShadowElGamalCiphertext(original.0)
318-
}
319-
}
320-
321-
impl From<ElGamalPubkey> for ShadowElGamalPubkey {
322-
fn from(original: ElGamalPubkey) -> Self {
323-
ShadowElGamalPubkey(original.0)
324-
}
325-
}
326-
327190
impl From<CpiGuard> for ShadowCpiGuard {
328191
fn from(original: CpiGuard) -> Self {
329192
ShadowCpiGuard {
@@ -343,7 +206,7 @@ impl From<DefaultAccountState> for ShadowDefaultAccountState {
343206
impl From<ConfidentialTransferFeeAmount> for ShadowConfidentialTransferFeeAmount {
344207
fn from(original: ConfidentialTransferFeeAmount) -> Self {
345208
ShadowConfidentialTransferFeeAmount {
346-
withheld_amount: original.withheld_amount.into(),
209+
withheld_amount: original.withheld_amount.to_base58(),
347210
}
348211
}
349212
}
@@ -384,7 +247,7 @@ impl From<TokenGroup> for ShadowTokenGroup {
384247
fn from(original: TokenGroup) -> Self {
385248
ShadowTokenGroup {
386249
update_authority: original.update_authority,
387-
mint: bs58::encode(original.mint).into_string(),
250+
mint: original.mint.to_string(),
388251
size: original.size,
389252
max_size: original.max_size,
390253
}
@@ -394,8 +257,8 @@ impl From<TokenGroup> for ShadowTokenGroup {
394257
impl From<TokenGroupMember> for ShadowTokenGroupMember {
395258
fn from(original: TokenGroupMember) -> Self {
396259
ShadowTokenGroupMember {
397-
mint: bs58::encode(original.mint).into_string(),
398-
group: bs58::encode(original.group).into_string(),
260+
mint: original.mint.to_string(),
261+
group: original.group.to_string(),
399262
member_number: original.member_number,
400263
}
401264
}
@@ -482,11 +345,11 @@ impl From<ConfidentialTransferAccount> for ShadowConfidentialTransferAccount {
482345
fn from(original: ConfidentialTransferAccount) -> Self {
483346
ShadowConfidentialTransferAccount {
484347
approved: original.approved,
485-
elgamal_pubkey: original.elgamal_pubkey.into(),
486-
pending_balance_lo: original.pending_balance_lo.into(),
487-
pending_balance_hi: original.pending_balance_hi.into(),
488-
available_balance: original.available_balance.into(),
489-
decryptable_available_balance: original.decryptable_available_balance.into(),
348+
elgamal_pubkey: original.elgamal_pubkey.to_base58(),
349+
pending_balance_lo: original.pending_balance_lo.to_base58(),
350+
pending_balance_hi: original.pending_balance_hi.to_base58(),
351+
available_balance: original.available_balance.to_base58(),
352+
decryptable_available_balance: original.decryptable_available_balance.to_base58(),
490353
allow_confidential_credits: original.allow_confidential_credits,
491354
allow_non_confidential_credits: original.allow_non_confidential_credits,
492355
pending_balance_credit_counter: original.pending_balance_credit_counter,
@@ -504,9 +367,9 @@ impl From<ConfidentialTransferFeeConfig> for ShadowConfidentialTransferFeeConfig
504367
authority: original.authority,
505368
withdraw_withheld_authority_elgamal_pubkey: original
506369
.withdraw_withheld_authority_elgamal_pubkey
507-
.into(),
370+
.to_base58(),
508371
harvest_to_mint_enabled: original.harvest_to_mint_enabled,
509-
withheld_amount: original.withheld_amount.into(),
372+
withheld_amount: original.withheld_amount.to_base58(),
510373
}
511374
}
512375
}
@@ -523,3 +386,25 @@ impl From<TokenMetadata> for ShadowMetadata {
523386
}
524387
}
525388
}
389+
390+
trait FromBytesToBase58 {
391+
fn to_base58(&self) -> String;
392+
}
393+
394+
impl FromBytesToBase58 for ElGamalPubkey {
395+
fn to_base58(&self) -> String {
396+
bs58::encode(self.0).into_string()
397+
}
398+
}
399+
400+
impl FromBytesToBase58 for ElGamalCiphertext {
401+
fn to_base58(&self) -> String {
402+
bs58::encode(self.0).into_string()
403+
}
404+
}
405+
406+
impl FromBytesToBase58 for AeCiphertext {
407+
fn to_base58(&self) -> String {
408+
bs58::encode(self.0).into_string()
409+
}
410+
}

digital_asset_types/src/dao/scopes/asset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ pub async fn get_nft_editions(
761761
Pagination::Page { page } => (Some(*page as u32), None, None, None),
762762
Pagination::Cursor(_) => {
763763
if let Some(last_asset) = nft_editions.last() {
764-
let cursor_str = bs58::encode(last_asset.edition_address.clone()).into_string();
764+
let cursor_str = last_asset.edition_address.clone();
765765
(None, None, None, Some(cursor_str))
766766
} else {
767767
(None, None, None, None)

digital_asset_types/tests/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ pub fn create_asset_grouping(
234234
asset_grouping::ActiveModel {
235235
asset_id: Set(asset_id.clone()),
236236
group_key: Set(String::from("collection")),
237-
group_value: Set(Some(bs58::encode(collection).into_string())),
237+
group_value: Set(Some(collection.to_string())),
238238
..Default::default()
239239
},
240240
asset_grouping::Model {
241241
asset_id,
242-
group_value: Some(bs58::encode(collection).into_string()),
242+
group_value: Some(collection.to_string()),
243243
seq: Some(0),
244244
id: row_num,
245245
group_key: "collection".to_string(),

docker-compose.yaml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,25 @@ services:
8787
ports:
8888
- "6379:6379"
8989
db:
90-
image: 'postgres:14'
91-
command: [ "postgres", "-c", "log_statement=all", "-c", "log_destination=stderr" ,"-c","max_connections=200" ]
90+
image: "postgres:14"
91+
command:
92+
[
93+
"postgres",
94+
"-c",
95+
"log_statement=all",
96+
"-c",
97+
"log_destination=stderr",
98+
"-c",
99+
"max_connections=200",
100+
]
92101
ports:
93102
- 5432:5432
94103
environment:
95104
POSTGRES_USER: solana # The PostgreSQL user (useful to connect to the database)
96105
POSTGRES_PASSWORD: solana # The PostgreSQL password (useful to connect to the database)
97106
POSTGRES_DB: solana
98107
volumes:
99-
- ./db-data/:/var/lib/postgresql/data/:rw
108+
- ./db-data/:/var/lib/postgresql/data/:z
100109
solana:
101110
image: ghcr.io/metaplex-foundation/plerkle-test-validator:v1.9.0-1.75.0-v1.18.11
102111
volumes:
@@ -113,5 +122,5 @@ services:
113122
- "8899:8899"
114123
- "9900:9900"
115124
volumes:
116-
grafana_data: { }
117-
graphite_data: { }
125+
grafana_data: {}
126+
graphite_data: {}

0 commit comments

Comments
 (0)