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

Commit 656f0eb

Browse files
committed
Reduce test code duplication
1 parent e111e99 commit 656f0eb

File tree

2 files changed

+71
-34
lines changed

2 files changed

+71
-34
lines changed

token/client/src/token.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use spl_token_2022::{
1919
StateWithExtensionsOwned,
2020
},
2121
instruction, native_mint,
22-
solana_zk_token_sdk::encryption::{auth_encryption::AeCiphertext, elgamal::ElGamalPubkey},
22+
solana_zk_token_sdk::encryption::{auth_encryption::*, elgamal::*},
2323
state::{Account, AccountState, Mint},
2424
};
2525
use std::{
@@ -949,6 +949,24 @@ where
949949
.await
950950
}
951951

952+
pub async fn confidential_transfer_configure_token_account_and_keypairs<S2: Signer>(
953+
&self,
954+
token_account: &Pubkey,
955+
authority: &S2,
956+
) -> TokenResult<(ElGamalKeypair, AeKey)> {
957+
let elgamal_keypair = ElGamalKeypair::new_rand();
958+
let ae_key = AeKey::new(authority, token_account).unwrap();
959+
960+
self.confidential_transfer_configure_token_account(
961+
token_account,
962+
authority,
963+
elgamal_keypair.public,
964+
ae_key.encrypt(0_u64),
965+
)
966+
.await
967+
.map(|_| (elgamal_keypair, ae_key))
968+
}
969+
952970
/// Approves a token account for confidential transfers
953971
pub async fn confidential_transfer_approve_token_account<S2: Signer>(
954972
&self,

token/program-2022-test/tests/confidential_transfer.rs

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,56 @@ use {
1313
confidential_transfer::{ConfidentialTransferAccount, ConfidentialTransferMint},
1414
ExtensionType,
1515
},
16-
solana_zk_token_sdk::encryption::{auth_encryption::*, elgamal::*},
16+
solana_zk_token_sdk::encryption::elgamal::*,
1717
},
1818
spl_token_client::token::{ExtensionInitializationParams, TokenError as TokenClientError},
1919
std::convert::TryInto,
2020
};
2121

22+
struct ConfidentialTransferMintWithKeypairs {
23+
ct_mint: ConfidentialTransferMint,
24+
ct_mint_authority: Keypair,
25+
#[allow(dead_code)]
26+
ct_mint_auditor: ElGamalKeypair,
27+
#[allow(dead_code)]
28+
ct_mint_withdraw_withheld_authority: ElGamalKeypair,
29+
}
30+
31+
impl ConfidentialTransferMintWithKeypairs {
32+
fn new() -> Self {
33+
let ct_mint_authority = Keypair::new();
34+
let ct_mint_auditor = ElGamalKeypair::new_rand();
35+
let ct_mint_withdraw_withheld_authority = ElGamalKeypair::new_rand();
36+
let ct_mint = ConfidentialTransferMint {
37+
authority: ct_mint_authority.pubkey().into(),
38+
auto_approve_new_accounts: true.into(),
39+
auditor_pubkey: ct_mint_auditor.public.into(),
40+
withdraw_withheld_authority_pubkey: ct_mint_withdraw_withheld_authority.public.into(),
41+
};
42+
Self {
43+
ct_mint,
44+
ct_mint_authority,
45+
ct_mint_auditor,
46+
ct_mint_withdraw_withheld_authority,
47+
}
48+
}
49+
50+
fn without_auto_approve() -> Self {
51+
let mut x = Self::new();
52+
x.ct_mint.auto_approve_new_accounts = false.into();
53+
x
54+
}
55+
}
56+
2257
#[tokio::test]
2358
async fn ct_initialize_and_update_mint() {
2459
let wrong_keypair = Keypair::new();
2560

26-
let ct_mint_authority = Keypair::new();
27-
let ct_mint = ConfidentialTransferMint {
28-
authority: ct_mint_authority.pubkey(),
29-
..ConfidentialTransferMint::default()
30-
};
31-
61+
let ConfidentialTransferMintWithKeypairs {
62+
ct_mint,
63+
ct_mint_authority,
64+
..
65+
} = ConfidentialTransferMintWithKeypairs::new();
3266
let mut context = TestContext::new().await;
3367
context
3468
.init_token_with_mint(vec![
@@ -91,11 +125,11 @@ async fn ct_initialize_and_update_mint() {
91125

92126
#[tokio::test]
93127
async fn ct_configure_token_account() {
94-
let ct_mint_authority = Keypair::new();
95-
let ct_mint = ConfidentialTransferMint {
96-
authority: ct_mint_authority.pubkey(),
97-
..ConfidentialTransferMint::default()
98-
};
128+
let ConfidentialTransferMintWithKeypairs {
129+
ct_mint,
130+
ct_mint_authority,
131+
..
132+
} = ConfidentialTransferMintWithKeypairs::without_auto_approve();
99133

100134
let mut context = TestContext::new().await;
101135
context
@@ -116,16 +150,8 @@ async fn ct_configure_token_account() {
116150
.await
117151
.unwrap();
118152

119-
let alice_elgamal_keypair = ElGamalKeypair::new_rand();
120-
let alice_ae_key = AeKey::new(&alice, &alice_token_account).unwrap();
121-
122-
token
123-
.confidential_transfer_configure_token_account(
124-
&alice_token_account,
125-
&alice,
126-
alice_elgamal_keypair.public,
127-
alice_ae_key.encrypt(0_u64),
128-
)
153+
let (alice_elgamal_keypair, alice_ae_key) = token
154+
.confidential_transfer_configure_token_account_and_keypairs(&alice_token_account, &alice)
129155
.await
130156
.unwrap();
131157

@@ -160,7 +186,8 @@ async fn ct_configure_token_account() {
160186

161187
#[tokio::test]
162188
async fn ct_enable_disable_balance_credits() {
163-
let ct_mint = ConfidentialTransferMint::default();
189+
let ConfidentialTransferMintWithKeypairs { ct_mint, .. } =
190+
ConfidentialTransferMintWithKeypairs::new();
164191
let mut context = TestContext::new().await;
165192
context
166193
.init_token_with_mint(vec![
@@ -180,16 +207,8 @@ async fn ct_enable_disable_balance_credits() {
180207
.await
181208
.unwrap();
182209

183-
let alice_elgamal_keypair = ElGamalKeypair::new_rand();
184-
let alice_ae_key = AeKey::new(&alice, &alice_token_account).unwrap();
185-
186-
token
187-
.confidential_transfer_configure_token_account(
188-
&alice_token_account,
189-
&alice,
190-
alice_elgamal_keypair.public,
191-
alice_ae_key.encrypt(0_u64),
192-
)
210+
let _ = token
211+
.confidential_transfer_configure_token_account_and_keypairs(&alice_token_account, &alice)
193212
.await
194213
.unwrap();
195214

0 commit comments

Comments
 (0)