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

# Smart wallets

> Squads, Swig, passkey wallets and ERC-4337 accounts: what Cloak supports, what it does not, and the workable patterns

A smart wallet is an account controlled by a program rather than by one private key: a Squads vault,
a Swig account, a passkey wallet, or an ERC-4337 account on an EVM chain (including Privy smart
wallets). Cloak has no direct support for these as the owner of a private balance. This page
explains why, and the patterns that do work.

## Why a smart wallet cannot shield directly

A Cloak shield from Solana has three requirements that a program-owned account cannot meet:

* **A top-level ed25519 signer that is also the fee payer.** The shield's first account is the payer,
  as a transaction signer. A PDA has no private key, so it cannot sign a top-level transaction.
* **A risk-assessment instruction at index 0 of the transaction.** The program checks it by
  instruction introspection, so the shield cannot be wrapped in a vault program's execute call.
* **No CPI.** The program rejects its tree-appending instructions when another program invokes them
  (`CpiNotAllowed`, `0x10D8`). See [Custom program integration](https://docs.cloak.ag/sdk/custom-programs.md).

## Why a smart wallet cannot open an account by signature

`createCloak` derives the account from a signature of `CLOAK_ACCOUNT_MESSAGE`, so the wallet must
produce the same signature every time:

| Wallet | Signs the account message repeatably | Result |
| - | - | - |
| Squads or Swig vault (PDA) | No: a PDA cannot sign messages | Refused |
| A member key of a multisig | Yes, if it is an ordinary ed25519 key | Opens an account owned by that member, not the vault |
| Passkey wallet | No: WebAuthn signatures differ each time | Refused, unless you allow a stored account |
| ERC-4337 account, including Privy smart wallets | No: an ERC-1271 contract signature is not a 65-byte key signature | Refused, unless you allow a stored account |
| Sui zkLogin or multisig | No | Refused, unless you allow a stored account |

A refused wallet throws `StoredAccountRequiredError` on the first open.

## Pattern 1: a stored account key

For a wallet that cannot sign repeatably, `createCloak` can create a random account key, keep it in
`storage`, and return its 24 backup words once. The wallet no longer opens the account. Whether it
can still shield its own funds depends on whether it can sign a top-level shield transaction, which a
program-owned account cannot.

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

declare const smartWallet: unknown; // what your wallet library hands you
declare const db: { get(key: string): Promise<string | null>; set(key: string, value: string): Promise<void> };

const cloak = await createCloak({
  wallet: smartWallet,
  allowStoredAccount: true,
  // A stored key needs a storage that protects secrets: localStorage does not.
  storage: encryptedStorage(db, process.env.STORAGE_SECRET!),
  rpc: process.env.SOLANA_RPC_URL,
});

if (cloak.backupWords) {
  // Shown once. Ask the user to write them down: they restore the account as a Cloak Key.
  console.log(cloak.backupWords.join(" "));
}
```

> [!WARNING]
>
> The account key now lives in your storage, not in the wallet. Losing both the storage and the backup
> words loses the private balance. `memoryStorage()` and `encryptedStorage(...)` protect secrets;
> the browser default (`localStorage`) does not, so pass one of the first two.

## Pattern 2: business mode

An organization that wants a treasury-style private balance can hold the account key itself and skip
wallet signatures entirely:

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

declare const db: { get(key: string): Promise<string | null>; set(key: string, value: string): Promise<void> };

const treasury = await createCloak({
  account: process.env.TREASURY_ACCOUNT_KEY, // 32 bytes as 64 hex characters, kept in your secret store
  storage: encryptedStorage(db, process.env.STORAGE_SECRET!),
  rpc: process.env.SOLANA_RPC_URL,
});
```

Fund it by shielding from an ordinary wallet the organization controls, or by having anyone send to
`treasury.address`. Spends need no wallet. Gate who may trigger them in your backend, since whoever
holds the key can spend. See [Backend integration](https://docs.cloak.ag/sdk/backend-integration.md).

## Pattern 3: an operational hot key

Keep the multisig as the cold store, and give Cloak an ordinary key that the multisig funds:

1. The multisig transfers USDC to a hot wallet with a normal ed25519 key.
2. The hot wallet shields into its private balance with `createCloak({ wallet: hotKey })`.
3. Withdrawals can go back to the vault's address.

The multisig's approvals control what enters the hot wallet. The private balance belongs to the hot
key's Cloak account.

## Withdrawing to a program-owned address

Payouts to other networks refuse an address that is not a plain wallet. A withdrawal on Solana to a
PDA is not something the SDK documents as supported. Withdraw to an ordinary address the vault
controls, or test with a small amount first.

## Summary

| You have | Use |
| - | - |
| Squads or Swig vault | Pattern 3, or Pattern 2 for an organization |
| Passkey wallet | Pattern 1 |
| Privy smart wallet (ERC-4337) | Privy's embedded Solana wallet instead. See [Privy](https://docs.cloak.ag/wallets/privy.md) |
| A member key of a multisig | Ordinary `createCloak({ wallet })`, owned by that member |


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