Skip to content
العربية

​How to withdraw tokens from confidential available balance

To withdraw tokens from confidential available balance to public balance:
  1. Create two proofs client side: Equality Proof (CiphertextCommitmentEqualityProofData): Verifies that the remaining available balance ciphertext after the withdrawal matches its corresponding Pedersen commitment, ensuring the account’s new available balance is correctly computed as remaining_balance = current_balance - withdraw_amount. Range Proof (BatchedRangeProofU64Data): Verifies that the remaining available balance after withdrawal is non-negative and within a specified range.
  2. For each proof:
    • Invoke the ZK ElGamal proof program to verify the proof data.
    • Store the proof-specific metadata in a proof “context state” account to use in other instructions.
  3. Invoke the ConfidentialTransferInstruction::Withdraw instruction providing the two proof accounts.
  4. Close the proof accounts to recover the SOL used to create them.
The following diagram shows the steps involved in withdrawing tokens from confidential available balance to public balance:
Withdraw TokensWithdraw Tokens

Withdraw Tokens

​Required Instructions

To withdraw tokens from confidential available balance to public balance, you must:
  • Generate an equality proof and range proof client-side
  • Invoke the Zk ElGamal proof program to verify the proofs and initialize the “context state” accounts
  • Invoke the ConfidentialTransferInstruction::Withdraw instruction providing the two proof accounts.
  • Close the two proof accounts to recover rent.
The Rust example below generates the proofs with the spl-token-confidential-transfer-proof-generation crate, verifies each into a context state account through the ZK ElGamal Proof program, references both accounts in the withdraw instruction, and closes them afterward. The TypeScript example uses the getConfidentialWithdrawInstructionPlan helper from @solana-program/token-2022/confidential, which assembles the same proof accounts, withdraw, and closes as a multi-transaction instruction plan.

​Example Code

The following example withdraws tokens from the confidential available balance back to the public balance. The account must already be configured for Confidential Balances and hold an available confidential balance. 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 creates its own mint and confidential token account and moves tokens into the available balance first, so there are no addresses to replace.

​Rust

​Typescript