next build
against @privy-io/react-auth 3.52.1 and @cloak.dev/sdk 0.2.20.
Install
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'.
.env.local
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
Files
privy/providers.tsx
"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>
);
}
privy/use-cloak.ts
"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 };
}
privy/private-balance.tsx
"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>
);
}
app/page.tsx
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: configuration, funding and troubleshooting.
- Custom signers: Privy server wallets on a backend.