Can you interact with a public-state application with private addresses

I am trying to understand how private transactions can work with applications within the Aztec protocol.

For example, if you have an AMM implemented on the L2 that have an ETH-USDC pool, can a user execute a private swap transaction with this pool?

Here is how I imagine the transaction execution would work. The user sends the ETH to be swapped to the AMM as a private TX. The AMM then exposes the ETH mount to execute the swap. The resulting USDC will be sent as a private balance to the user’s address.

Is this flow possible?

1 Like

Hello @Fouda, yes the flow you described is possible and quite easy to do. You would use the unshielding and shielding flow for that. Here is how it would work:

  1. you choose a random secret,
  2. you compute a hash of that secret (secret_hash)
  3. you call a private function swap(token_in, token_out, secret_hash, amount)
  4. This swap function spends/nullifies a note (or multiple notes) containing your private eth balance,
  5. then a pubic function would get called by the private function. The function would be something like swap_public(token_in, token_out, secret_hash, amount). This function would do the actual swap on AMM and then it would shield the resulting funds (USDC) by creating a TransparentNote with the secret hash. See this function for a real-world example of shielding.
  6. User would call redeem_shield(secret) function, the function would check whether hash(secret) matches secret hash and if yes, it would create a new note representing the usdc balance. Example is here.

Let me know if something is not clear. Feel free to also check the e2e_token.test.ts in aztec-packages repository to see how the flow is triggered from typescript.

1 Like

Thanks @Jan for the detailed answer and even mentioning the functions.

1 Like