> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cloak.ag/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom program integration

> Why your on-chain program cannot call Cloak through CPI, and the patterns that compose with it instead

Cloak is integrated from the client or a backend, not from another on-chain program. This page
explains the constraint and the patterns that work around it.

## No CPI into the pool

The Shield Pool program rejects its tree-appending instructions (`Transact`, `TransactSwap` and
`CloseSwapState`) when they are reached through a cross-program invocation. It checks the invocation
depth and fails with `CpiNotAllowed` (`0x10D8`).

The reason is consistency: off-chain indexers rebuild the pool's Merkle tree by reading transaction
instructions. An append made from inside another program would be invisible to them and would
desynchronize every reconstructed tree from the on-chain one.

Two more properties rule out wrapping a shield inside another program's instruction:

* A shield carries a signed risk assessment that the program reads, by instruction introspection,
  from index 0 of the transaction.
* The depositor must sign the transaction directly and pay its fee.

## Patterns that work

### Your program pays out, the user shields

Your program sends tokens to the user's wallet as usual. The user's client then shields them with
`cloak.shield(...)`. The two transactions are separate, and the shield is signed by the user.

### Your backend acts on behalf of your program

When the funds belong to your protocol rather than a user, hold them in an ordinary key controlled by
your backend and use [business mode](https://docs.cloak.ag/sdk/backend-integration.md). Your program's logic decides when
to pay; the backend executes the Cloak call.

### Your program pays into a private balance

To pay a user privately, withdraw from your program to a wallet your backend controls, shield, then
`send` to the user's `cloak_` address. The user receives a private note; the only public record is
your program paying your own wallet.

### React to Cloak activity

To trigger program logic after a private payment, have the recipient's client or your backend observe
it (`cloak.balance()`, `cloak.notes()`, or `scanTransactions` with the viewing key) and then call your
program. Cloak activity is private, so no on-chain hook can see who received what.

## Reading Cloak state from a program

The pool's accounts are ordinary program-derived accounts, but their contents (commitments,
nullifiers, roots) reveal nothing about balances or owners by design. There is no on-chain way to
check that a given user holds a private balance. When a counterparty needs to verify a user's
history, the user discloses it off chain with their viewing key. See
[Viewing keys and selective disclosure](https://docs.cloak.ag/guides/viewing-keys.md).

## Related

* [Shield Pool program](https://docs.cloak.ag/concepts/shield-pool-program.md): instructions, accounts and errors.
* [Backend integration](https://docs.cloak.ag/sdk/backend-integration.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.