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

# Notes and balances

> How a private balance is made of notes, who stores them, and what can and cannot be recovered

A private balance is not a number stored anywhere. It is the sum of the account's unspent notes
in each pool. This page explains where those notes live and what that means for your app's storage
and recovery story.

## A note is the money

A note is the tuple `(amount, owner key, blinding, mint)`. The chain stores only its commitment, a
Poseidon hash of the tuple. To spend a note, the client must hold the full tuple and the owner's
private key. The SDK derives the `blinding` of deposits and change from the account's viewing key,
so a scan can rebuild those notes. A note built with plain `createUtxo` gets a random `blinding`
that is written nowhere on chain.

That shapes every integration: the client's storage is the fast and complete record of what the
account owns, and the chain is a slower fallback that covers only the recoverable shapes. Lose the
storage and a note built with `createUtxo` is unspendable by anyone.

## With `createCloak`, the SDK stores them

`createCloak` picks notes, merges them when a call needs more than two, and keeps what it must
remember in `storage`:

| Where you run | Default `storage` | What to do |
| - | - | - |
| Browser | `localStorage` | Nothing for a single-device app. Offer an encrypted export for backup |
| Server, script | memory, lost on exit | Pass a durable store, ideally `encryptedStorage(db, secret)` |

Read the balance and the notes behind it:

```ts theme={null}
const balance = await cloak.balance(); // { USDC: "12.5", USDT: "0", SOL: "0.02", BRS: "0" }
const notes = await cloak.notes(); // [{ token, amount, from, tx, exposed }, ...]
```

Back up the notes as one encrypted string, and import them on any device that opens the same
account:

```ts theme={null}
const saved = await (await cloak.notes()).encryptToSave(secret);
const { imported, skipped } = await cloak.importNotes(saved, secret); // only unspent notes
```

`secret` is 32 bytes or more, or a passphrase of 12 characters or more. See
[Balances](https://docs.cloak.ag/guides/balances.md) for the full read path.

## With the low-level API, you store them

The low-level functions (`transact`, `transfer`, `partialWithdraw`, `fullWithdraw`, the swap
helpers) return the notes they create on `result.outputUtxos` and keep nothing. Persist them before
you report success:

```ts theme={null}
import { serializeUtxo, transact } from "@cloak.dev/sdk";

declare function persistNotes(bytes: Uint8Array[]): Promise<void>; // your durable store

const result = await transact(params, options);
await persistNotes(result.outputUtxos.filter((u) => u.amount > 0n).map(serializeUtxo));
```

Zero-amount outputs are circuit padding; drop them. The full storage contract, including
`UtxoWallet`, `selectUtxos` and multi-device reconciliation with `verifyUtxos`, is in
[Solana wallet adapter](https://docs.cloak.ag/wallets/wallet-adapter.md#where-your-notes-live-between-sessions).

## What can be recovered from the chain

| Lost note | Recoverable | Why |
| - | - | - |
| A deposit built with `createRecoverableDepositUtxo` | Yes | Its secrets derive from the viewing key and a salt the chain note carries |
| A note someone sent you with recipient discovery | Yes | The sender sealed its secrets on chain to your viewing key |
| A swap input stranded by a timeout, if built with a viewing key | Yes | `discoverSwapRefunds` rebuilds it |
| Change from your own withdrawal or swap, and notes merged by a withdrawal | Yes | Its blinding derives from the viewing key and the chain note's salt, and the chain note carries its amount. `scanTransactions` returns it in `recoveredChangeNotes` |
| Change from your own `transfer` | Once you know the amount | The blinding is derivable, but the chain note describes the recipient's output, not the change, so the scan cannot learn the change amount by itself |
| A note built with plain `createUtxo`, including change from clients that predate recoverable change | **No** | Its blinding is random and never published |

`createCloak` builds recoverable deposits and uses recipient discovery for you. With the low-level
API, use `createRecoverableDepositUtxo` for every deposit and pass `recipientViewingPublicKey` on
every send. The spend flows build recoverable change for you. If you call `transact` directly, build
any output you own with `createRecoverableChangeUtxo`; `transact` throws on a self-owned output built
with `createUtxo`.

## The two-input rule

The circuit spends at most two notes per transaction. A payment larger than any two notes needs
merges first. `createCloak` does this for you. With the low-level API, `partialWithdraw` and
`fullWithdraw` merge on chain automatically, and if the call fails part way the merged note never
reaches your storage. It is not lost, because a scan with the viewing key rebuilds it, but your app
does not see it until that scan runs. Cap inputs at two and merge deliberately. See
[a withdrawal of three or more notes](https://docs.cloak.ag/wallets/wallet-adapter.md#a-withdrawal-of-three-or-more-notes-is-not-exempt).

## Two devices, one account

Two devices holding the same notes can both try to spend one. The second spend fails with
`DoubleSpend` (`0x1020`). With the low-level API, run `verifyUtxos` on load and after any failure to drop spent notes.

## Related

* [UTXO model](https://docs.cloak.ag/concepts/utxo-model.md): commitments, nullifiers and keys.
* [Balances](https://docs.cloak.ag/guides/balances.md): reading balances in an app.
* [Transaction history](https://docs.cloak.ag/guides/transaction-history.md): scanning past activity with the viewing key.


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