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

# Privy integration

> A complete Next.js app: log in with email, Google or a passkey, get a Privy embedded Solana wallet, and use a private balance

A complete Next.js (App Router) project wiring Privy embedded wallets to Cloak. The step-by-step
explanation is in the [Privy guide](https://docs.cloak.ag/wallets/privy.md); this page is the code. Built with `next build`
against `@privy-io/react-auth` 3.52.1 and `@cloak.dev/sdk` 0.2.20.

## Install

```bash theme={null}
npx create-next-app@15 cloak-privy --ts --app --no-tailwind --no-eslint --no-src-dir --import-alias "@/*"
cd cloak-privy
npm install @cloak.dev/sdk@^0.2.20 @solana/kit @privy-io/react-auth @stripe/stripe-js@^1
```

`@stripe/stripe-js` is a dependency of a Privy module that webpack resolves at build time. Without it,
`next build` fails with `Can't resolve '@stripe/stripe-js'`.

```bash .env.local theme={null}
NEXT_PUBLIC_PRIVY_APP_ID=your-privy-app-id
NEXT_PUBLIC_SOLANA_RPC_URL=https://your-mainnet-rpc
NEXT_PUBLIC_SOLANA_WS_URL=wss://your-mainnet-rpc
```

In the Privy dashboard, enable Solana embedded wallets and the login methods you use.

## Files

```tsx title="privy/providers.tsx" theme={null}
"use client";

import { PrivyProvider } from "@privy-io/react-auth";
import { createSolanaRpc, createSolanaRpcSubscriptions } from "@solana/kit";
import type { ReactNode } from "react";

const RPC_URL = process.env.NEXT_PUBLIC_SOLANA_RPC_URL!;
const WS_URL = process.env.NEXT_PUBLIC_SOLANA_WS_URL!;

export function Providers({ children }: { children: ReactNode }) {
  return (
    <PrivyProvider
      appId={process.env.NEXT_PUBLIC_PRIVY_APP_ID!}
      config={{
        loginMethods: ["email", "google", "passkey"],
        embeddedWallets: {
          solana: { createOnLogin: "users-without-wallets" },
        },
        solana: {
          rpcs: {
            "solana:mainnet": {
              rpc: createSolanaRpc(RPC_URL),
              rpcSubscriptions: createSolanaRpcSubscriptions(WS_URL),
            },
          },
        },
      }}
    >
      {children}
    </PrivyProvider>
  );
}
```

```ts title="privy/use-cloak.ts" theme={null}
"use client";

import { usePrivy } from "@privy-io/react-auth";
import { useWallets } from "@privy-io/react-auth/solana";
import { createCloak, isCloakCallError, type Cloak } from "@cloak.dev/sdk";
import { useCallback, useState } from "react";

export function useCloak() {
  const { ready, authenticated } = usePrivy();
  const { ready: walletsReady, wallets } = useWallets();
  const [cloak, setCloak] = useState<Cloak | null>(null);
  const [error, setError] = useState<string | null>(null);

  const embedded = wallets.find((w) => w.standardWallet.name === "Privy");

  const open = useCallback(async () => {
    if (!ready || !authenticated || !walletsReady || !embedded) return;
    setError(null);
    try {
      const opened = await createCloak({
        wallet: embedded,
        rpc: process.env.NEXT_PUBLIC_SOLANA_RPC_URL,
        onWarning: (message) => console.warn("[cloak]", message),
      });
      setCloak(opened);
    } catch (e) {
      setError(isCloakCallError(e) ? `${e.code}: ${e.message}` : String(e));
    }
  }, [ready, authenticated, walletsReady, embedded]);

  return { cloak, open, error, wallet: embedded };
}
```

```tsx title="privy/private-balance.tsx" theme={null}
"use client";

import { usePrivy } from "@privy-io/react-auth";
import { isCloakCallError } from "@cloak.dev/sdk";
import { useState } from "react";
import { useCloak } from "./use-cloak";

export function PrivyPrivateBalance() {
  const { ready, authenticated, login, logout } = usePrivy();
  const { cloak, open, error, wallet } = useCloak();
  const [status, setStatus] = useState("");
  const [balance, setBalance] = useState<Record<string, string> | null>(null);

  async function run(label: string, action: () => Promise<unknown>) {
    setStatus(`${label}…`);
    try {
      await action();
      setBalance(await cloak!.balance());
      setStatus(`${label}: done`);
    } catch (e) {
      setStatus(isCloakCallError(e) ? `${e.code}: ${e.message}` : String(e));
    }
  }

  if (!ready) return <p>Loading…</p>;
  if (!authenticated) return <button onClick={login}>Log in</button>;
  if (!wallet) return <p>Creating your wallet…</p>;
  if (!cloak) return <button onClick={open}>Open private balance</button>;

  return (
    <div>
      <p>Wallet: {wallet.address}</p>
      <p>Cloak address: {cloak.address}</p>
      <p>Balance: {balance ? `${balance.USDC} USDC` : "…"}</p>
      <button onClick={() => run("Shielding", () => cloak.shield({ from: "solana", token: "USDC", amount: "5", onProgress: setStatus }))}>
        Shield 5 USDC
      </button>
      <button onClick={logout}>Log out</button>
      <p>{status}</p>
      {error && <p role="alert">{error}</p>}
    </div>
  );
}
```

```tsx title="app/page.tsx" theme={null}
import { Providers } from "../privy/providers";
import { PrivyPrivateBalance } from "../privy/private-balance";

export default function Home() {
  return (
    <Providers>
      <main style={{ padding: 32 }}>
        <PrivyPrivateBalance />
      </main>
    </Providers>
  );
}
```

## What happens

| Step | What the user sees |
| - | - |
| Log in | Privy's login modal. A Solana embedded wallet is created on first login |
| Open private balance | Two signatures of the Cloak account message on first use, then the `cloak_…` address |
| Shield 5 USDC | One transaction signature from the embedded wallet. The wallet needs the USDC and a little SOL |
| Sends and withdrawals | No signature prompts |

## Related

* [Privy guide](https://docs.cloak.ag/wallets/privy.md): configuration, funding and troubleshooting.
* [Custom signers](https://docs.cloak.ag/wallets/custom-signers.md#privy-server-wallets): Privy server wallets on a backend.


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