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

Commit 677acf1

Browse files
token-2022: limit incoming transfers for confidential transfer extension (#3208)
* token-2022: limit incoming transfers for confidential transfer extension * token-2022: add maximum pending counter for confidential extension in client * token-2022: update tests for ct configure account * token-2022: cargo fmt * divide pending balance into lo and hi * token-2022: update ct processor for lo and hi pending balances * token-2022: limit confidential transfer deposit and transfer amount in token client * token_2022: rename TokenError::IllegalAmount * token-2022: compare max pending credit as u64 * token-2022: fmt * token-2022: minor fix error from rebase * Update token/program-2022/src/extension/confidential_transfer/processor.rs Co-authored-by: Tyera Eulberg <[email protected]> * token-2022: update configure account initialization comments * token-2022: use strict comparison for max pending balance credit counter check * token-2022: update deposit tests to check `pending_balance_hi` populated * token-2022: update deposit tests to check that deposit and transfer fails when max pending exceeded * token-2022: reset pending balance counter on `ApplyPendingBalance` * token-2022: fix cargo test cases for credit counter Co-authored-by: Tyera Eulberg <[email protected]>
1 parent c2a3ecd commit 677acf1

File tree

7 files changed

+242
-64
lines changed

7 files changed

+242
-64
lines changed

token/client/src/token.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ pub enum TokenError {
5151
AccountInvalidMint,
5252
#[error("proof error: {0}")]
5353
Proof(ProofError),
54+
#[error("maximum deposit transfer amount exceeded")]
55+
MaximumDepositTransferAmountExceeded,
5456
}
5557
impl PartialEq for TokenError {
5658
fn eq(&self, other: &Self) -> bool {
@@ -976,6 +978,7 @@ where
976978
authority: &S2,
977979
elgamal_pubkey: ElGamalPubkey,
978980
decryptable_zero_balance: AeCiphertext,
981+
maximum_pending_balance_credit_counter: u64,
979982
) -> TokenResult<T::Output> {
980983
self.process_ixs(
981984
&[confidential_transfer::instruction::configure_account(
@@ -984,6 +987,7 @@ where
984987
&self.pubkey,
985988
elgamal_pubkey.into(),
986989
decryptable_zero_balance,
990+
maximum_pending_balance_credit_counter,
987991
&authority.pubkey(),
988992
&[],
989993
)?],
@@ -996,6 +1000,7 @@ where
9961000
&self,
9971001
token_account: &Pubkey,
9981002
authority: &S2,
1003+
maximum_pending_balance_credit_counter: u64,
9991004
) -> TokenResult<(ElGamalKeypair, AeKey)> {
10001005
let elgamal_keypair = ElGamalKeypair::new_rand();
10011006
let ae_key = AeKey::new(authority, token_account).unwrap();
@@ -1005,6 +1010,7 @@ where
10051010
authority,
10061011
elgamal_keypair.public,
10071012
ae_key.encrypt(0_u64),
1013+
maximum_pending_balance_credit_counter,
10081014
)
10091015
.await
10101016
.map(|_| (elgamal_keypair, ae_key))
@@ -1067,6 +1073,10 @@ where
10671073
amount: u64,
10681074
decimals: u8,
10691075
) -> TokenResult<T::Output> {
1076+
if amount >> confidential_transfer::MAXIMUM_DEPOSIT_TRANSFER_AMOUNT_BIT_LENGTH != 0 {
1077+
return Err(TokenError::MaximumDepositTransferAmountExceeded);
1078+
}
1079+
10701080
self.process_ixs(
10711081
&[confidential_transfer::instruction::deposit(
10721082
&self.program_id,
@@ -1138,6 +1148,10 @@ where
11381148
source_elgamal_keypair: &ElGamalKeypair,
11391149
new_source_decryptable_available_balance: AeCiphertext,
11401150
) -> TokenResult<T::Output> {
1151+
if amount >> confidential_transfer::MAXIMUM_DEPOSIT_TRANSFER_AMOUNT_BIT_LENGTH != 0 {
1152+
return Err(TokenError::MaximumDepositTransferAmountExceeded);
1153+
}
1154+
11411155
let source_state = self.get_account_info(source_token_account).await.unwrap();
11421156
let source_extension =
11431157
source_state.get_extension::<confidential_transfer::ConfidentialTransferAccount>()?;
@@ -1198,6 +1212,10 @@ where
11981212
new_source_decryptable_available_balance: AeCiphertext,
11991213
epoch_info: &EpochInfo,
12001214
) -> TokenResult<T::Output> {
1215+
if amount >> confidential_transfer::MAXIMUM_DEPOSIT_TRANSFER_AMOUNT_BIT_LENGTH != 0 {
1216+
return Err(TokenError::MaximumDepositTransferAmountExceeded);
1217+
}
1218+
12011219
let source_state = self.get_account_info(source_token_account).await.unwrap();
12021220
let source_extension =
12031221
source_state.get_extension::<confidential_transfer::ConfidentialTransferAccount>()?;

0 commit comments

Comments
 (0)