Skip to content
Italiano

​How to create a token account with Confidential Balances

Confidential Balances enables private token transfers by adding extra state to the token account. This section explains how to create a token account with the required extension enabled. The following diagram shows the steps involved in creating a token account with Confidential Balances enabled:
Create Token Account with Confidential BalancesCreate Token Account with Confidential Balances

Create Token Account with Confidential Balances

​Confidential Balances Token Account State

The extension adds the ConfidentialTransferAccount state to the token account:
Confidential Token Account State
The ConfidentialTransferAccount contains several fields that manage Confidential Balances:
  • approved: The account’s approval status for Confidential Balances. If the mint account’s auto_approve_new_accounts configuration is set as true, all token accounts are automatically approved for Confidential Balances.
  • elgamal_pubkey: The ElGamal public key used to encrypt balances and transfer amounts.
  • pending_balance_lo: The encrypted lower 16 bits of the pending balance. The balance is split into high and low parts for efficient decryption.
  • pending_balance_hi: The encrypted higher 48 bits of the pending balance. The balance is split into high and low parts for efficient decryption.
  • available_balance: The encrypted balance available for transfers.
  • decryptable_available_balance: The available balance encrypted with an Advanced Encryption Standard (AES) key for efficient decryption by the account owner.
  • allow_confidential_credits: If true, allows incoming confidential transfers.
  • allow_non_confidential_credits: If true, allows incoming non-confidential transfers.
  • pending_balance_credit_counter: Counts incoming pending balance credits from deposit and transfer instructions.
  • maximum_pending_balance_credit_counter: The count limit of pending credits before requiring an ApplyPendingBalance instruction to convert the pending balance to the available balance.
  • expected_pending_balance_credit_counter: The pending_balance_credit_counter value provided by the client through the instruction data the last time the ApplyPendingBalance instruction was processed.
  • actual_pending_balance_credit_counter: The pending_balance_credit_counter value on the token account at the time the last ApplyPendingBalance instruction was processed.
Pending vs Available BalanceConfidential balances are separated into pending and available balances to prevent DoS attacks. Without this separation, an attacker could repeatedly send tokens to a token account, blocking the token account owner’s ability to transfer tokens. The token account owner would be unable to transfer tokens because the encrypted balance would change between when the transaction is submitted and when it is processed, resulting in a failed transaction.All deposits and transfer amounts are initially added to the pending balance. Token account owners must use the ApplyPendingBalance instruction to convert the pending balance to the available balance. Incoming transfers or deposits don’t affect a token account’s available balance.
Pending Balance High/Low SplitThe confidential pending balance is split into pending_balance_lo and pending_balance_hi because ElGamal decryption requires more computation for larger numbers. You can find the ciphertext arithmetic implementation here, which is used in the ApplyPendingBalance instruction here.
Pending Balance Credit CountersWhen calling the ApplyPendingBalance instruction to convert the pending balance to the available balance:
  1. The client looks up current pending and available balances, encrypts the sum, and provides a decryptable_available_balance encrypted using the token account owner’s AES key.
  2. The expected and actual pending credit counters track changes to the counter value between when the ApplyPendingBalance instruction is created and processed:
    • expected_pending_balance_credit_counter: The pending_balance_credit_counter value when the client creates the ApplyPendingBalance instruction
    • actual_pending_balance_credit_counter: The pending_balance_credit_counter value on the token account at the time the ApplyPendingBalance instruction is processed
Matching expected/actual counters indicate the decryptable_available_balance matches the available_balance.When fetching a token account’s state to read the decryptable_available_balance, different expected/actual counters values require the client to look up recent deposit/transfer instructions matching the counter difference to calculate the correct balance.Balance Reconciliation ProcessWhen the expected and actual pending balance counters differ, follow these steps to reconcile the decryptable_available_balance:
  1. Start with the decryptable_available_balance from the token account
  2. Fetch the most recent transactions including deposit and transfer instructions up to the counter difference (actual - expected):
    • Add public amounts from deposit instructions
    • Decrypt and add destination ciphertext amounts from transfer instructions

​Required Instructions

Creating and configuring a token account for Confidential Balances uses the following instructions, which all fit in a single transaction:
  1. Create the Token Account: Invoke the Associated Token Program’s AssociatedTokenAccountInstruction::Create instruction to create the token account at its deterministic address.
  2. Reallocate Account Space: Invoke the Token Extension Program’s TokenInstruction::Reallocate instruction to add space for the ConfidentialTransferAccount state.
  3. Verify the Pubkey Validity Proof: Create an account owned by the ZK ElGamal Proof program, then invoke its VerifyPubkeyValidity instruction to verify the proof and store the verified result in that context state account.
  4. Configure Confidential Balances: Invoke the Token Extension Program’s ConfidentialTransferInstruction::ConfigureAccount instruction, referencing the proof context state account via ProofLocation::ContextStateAccount, to initialize the ConfidentialTransferAccount state.
Only the token account owner can enable Confidential Balances on a token account.The ConfigureAccount instruction requires client-side generation of encryption keys and a proof that can only be generated by the token account owner.The pubkey validity proof verifies that the account’s ElGamal public key is valid. It is generated with build_pubkey_validity_proof_data, verified on chain by the ZK ElGamal Proof program into a context state account, and then referenced from ConfigureAccount via ProofLocation::ContextStateAccount, so no proof bytes travel in the token instruction itself. For implementation details, see:

​Example Code

The following code creates an associated token account and configures it to use Confidential Balances with an existing compatible mint. Confidential Balances depends on the ZK ElGamal Proof program, which is enabled on mainnet and devnet. The Token-2022 program bundled with solana-test-validator is built without confidential-transfer support, so Deposit and the steps after it fail with InvalidInstructionData. Run the examples on devnet (the code’s default; get devnet SOL from the Solana faucet) or on a mainnet-forking local validator such as Surfpool. The payer is the wallet at ~/.config/solana/id.json. The example first creates its own confidential mint (as in Create a Mint, with a throwaway auditor key), so there are no addresses to replace.

​Rust

​Typescript

The TypeScript helpers live in the @solana-program/token-2022/confidential subpath and build on @solana/zk-sdk for the encryption primitives. owner and client come from your @solana/kit setup; the returned instruction plan is sent with @solana/kit’s instruction-plan support, which splits the work across transactions when the proofs are too large for one.