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

# Transaction history

> Scan an account's private history with its viewing key and export it as a report or CSV

A private account's history is not visible on a block explorer. Every Cloak transaction posts a small
encrypted record that only the account's **viewing key** (`nk`) opens. `scanTransactions` reads the
program's transactions from your RPC, opens the records that belong to the key, and returns a
history with amounts, fees and a running balance.

The scan is read-only: it signs nothing and prompts nobody.

## Get the viewing key

`createCloak` has no history call, so derive the account's keys with the exported helpers. For a
business-mode account you already hold the account key:

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

const keys = await deriveCloakAccountKeys(hexToBytes(process.env.ACCOUNT_KEY_HEX!));
const nk = keys.viewingKeyNk; // view-only: can read, can never spend
```

For an account opened from a wallet, the keys come from the wallet's signature of
`CLOAK_ACCOUNT_MESSAGE`, which costs the user one more signature:

```ts theme={null}
import { CLOAK_ACCOUNT_MESSAGE, deriveCloakAccountKeys, spendKeyFromSignature } from "@cloak.dev/sdk";

declare function signMessage(message: Uint8Array): Promise<Uint8Array>; // the same Solana wallet

const signature = await signMessage(new TextEncoder().encode(CLOAK_ACCOUNT_MESSAGE));
const keys = await deriveCloakAccountKeys(spendKeyFromSignature("solana", signature));
```

This rebuilds the account the wallet opened from its own signature. It does not apply to a wallet
that was linked to another account, or to an account opened with a Cloak Key or a stored key.

> [!WARNING]
>
> `keys` also contains the spending key. Keep only `keys.viewingKeyNk` and `keys.owner.publicKey` for
> history, and drop the rest.

## Scan

```ts theme={null}
import {
  CLOAK_PROGRAM_ID,
  CLOAK_TOKENS,
  createCloakRpc,
  formatComplianceCsv,
  scanTransactions,
  toComplianceReport,
} from "@cloak.dev/sdk";

declare const nk: Uint8Array;
declare const ownerUtxoPublicKey: bigint; // keys.owner.publicKey

const result = await scanTransactions({
  connection: createCloakRpc(process.env.SOLANA_RPC_URL!),
  programId: CLOAK_PROGRAM_ID,
  viewingKeyNk: nk,
  ownerUtxoPublicKey, // also finds notes others sent to this account
  deliveryMints: Object.values(CLOAK_TOKENS).map((t) => t.mint), // default is SOL only
  limit: 500,
  onStatus: (status) => console.log(status),
});

for (const tx of result.transactions) {
  console.log(tx.txType, tx.symbol, tx.amount, tx.fee, tx.netAmount, tx.signature);
}
console.log(result.summary);

const report = toComplianceReport(result); // JSON-safe: bigints become strings
const csv = formatComplianceCsv(report);
```

Each `ScannedTransaction` has `txType`, `amount`, `fee`, `netAmount` and `runningBalance` (bigints in
base units), `mint`, `symbol`, `decimals`, `timestamp`, `recipient` and `signature`. `summary` totals
deposits, withdrawals and fees.

## Scan incrementally

A full scan reads every program transaction, which is slow on a busy pool. Save `lastSignature` and
pass it back as `untilSignature` next time to read only what is newer:

```ts theme={null}
import { CLOAK_PROGRAM_ID, createCloakRpc, scanTransactions } from "@cloak.dev/sdk";

declare const nk: Uint8Array;
declare const savedLastSignature: string | undefined;

const next = await scanTransactions({
  connection: createCloakRpc(process.env.SOLANA_RPC_URL!),
  programId: CLOAK_PROGRAM_ID,
  viewingKeyNk: nk,
  untilSignature: savedLastSignature,
});
// merge next.transactions into what you stored, then save next.lastSignature
```

`afterTimestamp` and `beforeTimestamp` (milliseconds) bound a scan by time.

## What a scan is not

`scanTransactions` produces history, not spendable notes. Two of its fields are the exception:
`deliveredNotes` (notes others sent to this account) and `recoveredDepositNotes` (this account's own
deposits). Change from the account's own spends cannot be rebuilt from a scan. See
[Notes and balances](https://docs.cloak.ag/concepts/notes-and-balances.md).

## Related

* [Viewing keys and selective disclosure](https://docs.cloak.ag/guides/viewing-keys.md): give this capability to an auditor.
* [Network and RPC](https://docs.cloak.ag/resources/network-and-rpc.md): scanning is the RPC-heaviest thing the SDK does.


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