> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inco.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Privy

> Set up Privy wallet integration with Inco

## Scaffold

<CodeGroup>
  ```bash npm theme={null}
  npx create-inco-app@latest my-app --wallet privy --framework hardhat --chain evm --yes
  ```

  ```bash pnpm theme={null}
  pnpm create inco-app@latest my-app --wallet privy --framework hardhat --chain evm --yes
  ```

  ```bash yarn theme={null}
  yarn create inco-app my-app --wallet privy --framework hardhat --chain evm --yes
  ```

  ```bash bun theme={null}
  bunx create-inco-app@latest my-app --wallet privy --framework hardhat --chain evm --yes
  ```
</CodeGroup>

This scaffolds the full monorepo with Privy as the wallet provider. For just the frontend, add `--template frontend`.

## Environment Setup

Get your App ID from [dashboard.privy.io](https://dashboard.privy.io) and set it in `frontend/.env`:

```bash frontend/.env theme={null}
# Base network: "testnet" (Base Sepolia, default) or "mainnet" (Base Mainnet)
NEXT_PUBLIC_NETWORK=testnet

NEXT_PUBLIC_PRIVY_APP_ID=your_app_id_here

# Your deployed contract address
NEXT_PUBLIC_CONFLOTTERY_ADDRESS=<deployed_contract_address>
```

## Provider Setup

Privy wraps your app with `PrivyProvider` and Privy's wagmi connector (`@privy-io/wagmi`), giving you email, social, and embedded-wallet login. The chain comes from `activeChain` in `lib/network.ts`, which follows `NEXT_PUBLIC_NETWORK` — no per-provider chain edits needed.

```tsx components/Providers.tsx theme={null}
"use client";

import { ReactNode } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { PrivyProvider } from "@privy-io/react-auth";
import { WagmiProvider, createConfig } from "@privy-io/wagmi";
import { http } from "wagmi";
import { ThemeProvider } from "next-themes";
import { activeChain } from "@/lib/network";

const queryClient = new QueryClient();

const appId = process.env.NEXT_PUBLIC_PRIVY_APP_ID || "";

const config = createConfig({
  chains: [activeChain],
  transports: {
    [activeChain.id]: http(),
  },
  ssr: true,
});

const Providers = ({ children }: { children: ReactNode }) => {
  if (!appId) {
    console.warn(
      "Missing NEXT_PUBLIC_PRIVY_APP_ID. Get one at https://dashboard.privy.io/"
    );
  }

  return (
    <ThemeProvider attribute="class" defaultTheme="dark" enableSystem>
      <PrivyProvider
        appId={appId}
        config={{
          appearance: {
            theme: "dark",
          },
          embeddedWallets: {
            createOnLogin: "users-without-wallets",
          },
          defaultChain: activeChain,
          supportedChains: [activeChain],
        }}
      >
        <QueryClientProvider client={queryClient}>
          <WagmiProvider config={config}>{children}</WagmiProvider>
        </QueryClientProvider>
      </PrivyProvider>
    </ThemeProvider>
  );
};

export { Providers };
```

**Provider hierarchy:** `ThemeProvider` → `PrivyProvider` → `QueryClientProvider` → `WagmiProvider`

Note the wagmi `createConfig` is imported from `@privy-io/wagmi` (not `wagmi`), so Privy's embedded wallets connect through wagmi automatically.

## Inco SDK Integration

The Inco SDK works with the `walletClient` provided by wagmi (which Privy supplies). The network-aware client comes from `lib/network.ts`:

```tsx hooks/useConfLottery.ts theme={null}
import { getIncoLightning } from "@/lib/network";
import { handleTypes } from "@inco/lightning-js";
import { useAccount, useWalletClient } from "wagmi";
import { parseEther } from "viem";

const { address } = useAccount();
const { data: walletClient } = useWalletClient();

// Network (Base Sepolia / Mainnet) is selected centrally in lib/network.ts via NEXT_PUBLIC_NETWORK.
const zap = await getIncoLightning();

// Encrypt a value before sending on-chain
const ciphertext = await zap.encrypt(parseEther(amount), {
  accountAddress: address,
  dappAddress: LOTTERY_ADDRESS,
  handleType: handleTypes.euint256,
});

// Decrypt a handle with attestation (e.g. check if the user won)
const [result] = await zap.attestedDecrypt(walletClient, [encryptedHandle]);
const isWinner = result.plaintext.value; // boolean for an ebool handle
```

## Dependencies

| Package                | Purpose                                 |
| ---------------------- | --------------------------------------- |
| `@privy-io/react-auth` | Privy authentication & embedded wallets |
| `@privy-io/wagmi`      | Privy ↔ wagmi connector                 |
| `@inco/lightning-js`   | Inco encryption/decryption              |
| `wagmi`                | EVM wallet hooks                        |
| `viem`                 | Ethereum utilities                      |
