> ## 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.

# UTXO model

> Notes, commitments, nullifiers, the Merkle tree and the keys that own them

## Canonical transaction path

The active runtime path is UTXO transactions through:

* `transact` for deposit/withdraw/transfer
* `swapWithChange` / `swapUtxo` for swap flows
* SDK-managed submit and status lifecycle

Every transaction in the current SDK (0.2.20) goes through the UTXO free functions: `transact` / `transfer` / `partialWithdraw` / `fullWithdraw` / `swapWithChange`. [`createCloak`](https://docs.cloak.ag/sdk/create-cloak) is built on them and makes each action one call, deriving the keys below from the account; this page explains what it does for you. An earlier note-model client class no longer exists.

## Keys

One persistent 32-byte seed (`generateMasterSeed()`, stored like a private key) yields everything
the wallet needs. A random key that is lost loses the notes it owns, so do not use
`generateUtxoKeypair()` for anything that holds real funds.

```ts theme={null}
import {
  deriveSpendKey,
  deriveUtxoKeypairFromSpendKey,
  generateMasterSeed,
  getNkFromUtxoPrivateKey,
} from "@cloak.dev/sdk";

const seed = generateMasterSeed().seed; // generate once, then store and reload it
const { sk_spend } = deriveSpendKey(seed);
const ownerUtxoKeypair = await deriveUtxoKeypairFromSpendKey(sk_spend);
const nk = getNkFromUtxoPrivateKey(ownerUtxoKeypair.privateKey);
```

* `ownerUtxoKeypair` owns your notes. Every spend flow (`transfer`, `partialWithdraw`,
  `fullWithdraw`, `swapUtxo`, `swapWithChange`, `sweepToWalletKey`) requires it, in the types and at
  runtime; pass it on deposits too.
* `nk` is view-only. It finds and decrypts your notes and derives their blindings, but it can never
  spend them, so it is safe to register with the relay and to share with an auditor. Pass it as
  `chainNoteViewingKeyNk`.
* Create deposit notes with `createRecoverableDepositUtxo({ amount, viewingKeyNk: nk,
  ownerUtxoKeypair, mintAddress })` and pass the returned `noteSalt` to `transact` as
  `chainNoteSalt`, so a scan can find the note again.

Notes created before 0.2.6 may have an owner key derived from `nk`; those are flagged
`legacyExposed` and move under the wallet key only when spent.

## Commitments and Merkle tree

Each deposit/output creates a Poseidon commitment and appends it to the on-chain Merkle tree.

* Height: `32`
* Root history: `100` recent roots

Proof generation needs:

* Leaf index
* Path elements/indices
* A root still present in root history

## Nullifiers

Spending creates a nullifier. The program rejects a reused nullifier.

This is the core double-spend guard for both note and UTXO flows.

## Stale-root retries

If your proof root is no longer in history, transactions can fail with `RootNotFound` (`0x1001`).

The SDK includes retry paths that:

* fetch fresh Merkle data
* regenerate proof
* resubmit

## Fee model: gross, fee, net

Fees are enforced on-chain by the program from a per-pool `PoolConfig`; the SDK reads the live
config before building a fee-bearing proof. Fees apply only when value leaves the pool
(`externalAmount < 0`); deposits and shielded→shielded transfers are fee-free.

For SOL withdrawals/swaps:

* `gross` = absolute public withdrawal amount
* `fee` = `5_000_000` lamports fixed (0.005 SOL) + `gross * 3 / 1000`
* `net` = `gross - fee`

For SPL (USDC/USDT) withdrawals:

* `fee` = `450_000` base units fixed (0.45 USDC/USDT) + `floor(gross * 3 / 1000)` (0.3%), charged in the withdrawn token
* For BRS the fixed part is `2_300_000` base units (2.3 BRS), with the same 0.3%

Minimum deposit: `10_000_000` lamports (`0.01 SOL`) on the SOL pool; `1_000_000` base units
(`1.00 token`) on the USDC and USDT pools. See [Fee Model](https://docs.cloak.ag/concepts/fees.md).

## Proof data shapes

* UTXO transact and swap requests use `264`-byte public inputs:
  `root + publicAmount + extDataHash + mint + nullifiers + commitments + chainNoteHash`.
* Proof bytes are always `256` bytes (Groth16).

## Viewing key registration

* SDK flows enforce viewing-key registration by default before protocol transactions.
* Registration is a signed wallet challenge bound to a 32-byte viewing key (`nk`).
* Registration is cached in-process to avoid repeat calls.

## Chain-native scanner and cache model

* `scanTransactions` reads chain transactions directly from RPC.
* It decrypts compact chain notes with viewing key `nk` and verifies `chainNoteHash` integrity.
* It computes per-tx `gross`, `fee`, `netAmount`, and running balance.
* Client apps can store encrypted report snapshots locally and expose explicit cache clear + rescan.

## Related pages

* UTXO details: [UTXO Transactions](https://docs.cloak.ag/sdk/low-level-api.md)
* Protocol rules: [Shield Pool Program](https://docs.cloak.ag/concepts/shield-pool-program.md)
* Runtime flow: [Transaction Flows](https://docs.cloak.ag/concepts/transaction-lifecycle.md)


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