Skip to content

base/base-account-privy

Repository files navigation

Build on Base with Privy

A Next.js starter template for building on Base with Privy's authentication and wallet infrastructure. This can serve as a template for developers looking to integrate Base Account as an option in Privy.

πŸš€ Features

This demo showcases the full spectrum of Base Account capabilities:

  • Base Account Authentication - Using wallet_connect with SignInWithEthereum to authenticate
  • Sub Accounts Management - Create and manage app-specific wallet accounts to allow your give your users a pop-up-less experience
  • Wallet Operations - Complete wallet interaction functionality including transactions and message signing
  • Account Linking/Unlinking - Connect and disconnect various account types (Google, Email etc)
  • Multi-Factor Authentication - Enhanced security features

πŸ—οΈ Architecture

Provider Configuration (src/providers/providers.tsx)

The application is configured to prioritize Base Account as the primary wallet option:

<PrivyProvider
  config={{
    appearance: {
      walletList: ['base_account'],
      showWalletLoginFirst: true
    },
    defaultChain: base,
  }}
>

This configuration ensures that Base Account appears first in the account authentification flow, providing the optimal user experience for Base Account users.

πŸ” Authentication Section

The authentication component implements the "Sign in with Base" flow using Base Account's wallet signature authentication:

Key Features:

  • Passwordless Authentication - No passwords required, uses wallet signatures
  • SIWE Standard - Follows the "Sign in with Ethereum" (EIP-4361) standard
  • Nonce Generation - Secure random nonce generation for each authentication
  • Custom Button - Branded "Sign in with Base" imported from Base Account SDK
  • Backend Verification - Anti-replay and backend verification using Viem's verifyMessage

Implementation:

  • Uses wallet_connect RPC method with signInWithEthereum capabilities
  • Generates secure nonces using window.crypto.randomUUID()
  • Provides complete authentication data (address, message, signature) for backend verification
  • Ready for backend integration with viem's verifyMessage function

Learn more: Base Account Authentication Guide

🏦 Sub Accounts Section

Sub Accounts allow you to create app-specific wallet accounts that provide a frictionless transaction experience:

Key Features:

  • Get Existing Sub Accounts - Retrieve sub accounts for the current domain
  • Create New Sub Accounts - Generate new sub accounts tied to your app
  • Domain-Specific - Each sub account is bound to your application's domain
  • Frictionless Transactions - Eliminate repeated signing prompts
  • Spend Permissions Ready - Can spend from parent account balance

Implementation:

  • Uses wallet_getSubAccounts RPC method to fetch existing accounts
  • Uses wallet_addSubAccount RPC method to create new sub accounts
  • Automatically switches to Base Sepolia for testing
  • Displays sub account details including addresses and public keys

Learn more: Base Account Sub Accounts Guide

πŸ’Έ Spend Permissions Section

Spend Permissions enable trusted spenders to move assets from your Base Account without requiring additional signatures for each transaction:

Key Features:

  • Create Spend Permissions - Grant spending daily/weekly/monthly allowances to trusted addresses
  • Load Existing Permissions - View and manage current spend permissions
  • Use Permissions - Execute transactions using granted permissions
  • Permission Status Checking - Monitor active permissions and remaining allowances
  • Secure Allowance Management - Set specific token amounts and time periods

Implementation:

  • Uses requestSpendPermission to create new spending allowances
  • Uses fetchPermissions to retrieve existing permissions for an account
  • Uses prepareSpendCallData to prepare transactions using permissions
  • Uses getPermissionStatus to check permission validity and remaining balances
  • Supports USDC token permissions with configurable allowances and periods

Configuration:

  • Default Token: USDC on Base (0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913)
  • Default Spender: Configurable trusted address
  • Default Allowance: 1 USDC per day (customizable)
  • Network: Base Mainnet (Chain ID: 8453)

Learn more: Base Account Spend Permissions Guide

πŸ”§ Additional Sections

Wallet Actions

Comprehensive wallet operation functionality including:

  • Transaction sending and management
  • Smart contract interactions
  • Balance checking and transfers

Link Accounts

Connect various account types to your Base Account:

  • Social accounts integration
  • Email account linking
  • Additional wallet connections

Unlink Accounts

Manage and disconnect linked accounts:

  • Remove connected social accounts
  • Unlink email addresses
  • Disconnect additional wallets

Multi-Factor Authentication (MFA)

Enhanced security features:

  • Enable/disable MFA
  • Manage authentication factors
  • Security settings configuration

πŸ› οΈ Getting Started

Prerequisites

Make sure you have the following environment variables set:

NEXT_PUBLIC_PRIVY_APP_ID=your_privy_app_id
NEXT_PUBLIC_PRIVY_CLIENT_ID=your_privy_client_id

Installation

# Clone the repository
git clone <repository-url>
cd base-account-privy

# Install dependencies
npm install
# or
pnpm install
# or
yarn install

# Run the development server
npm run dev
# or
pnpm dev
# or
yarn dev

Open http://localhost:3000 to see the application.

πŸ—οΈ Project Structure

src/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── auth/
β”‚   β”‚       β”œβ”€β”€ nonce/
β”‚   β”‚       β”‚   └── route.ts     # Generate authentication nonces
β”‚   β”‚       └── verify/
β”‚   β”‚           └── route.ts     # Verify SIWE signatures
β”‚   β”œβ”€β”€ page.tsx                 # Main application page
β”‚   β”œβ”€β”€ layout.tsx              # Root layout
β”‚   └── globals.css             # Global styles and button classes
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ sections/
β”‚   β”‚   β”œβ”€β”€ authentication.tsx   # Base Account authentication & SIWE
β”‚   β”‚   β”œβ”€β”€ sub-accounts.tsx    # Sub accounts management
β”‚   β”‚   β”œβ”€β”€ spend-permissions.tsx # Spend permissions management
β”‚   β”‚   β”œβ”€β”€ wallet-actions.tsx  # Wallet operations
β”‚   β”‚   β”œβ”€β”€ link-accounts.tsx   # Account linking
β”‚   β”‚   β”œβ”€β”€ unlink-accounts.tsx # Account unlinking
β”‚   β”‚   β”œβ”€β”€ mfa.tsx            # Multi-factor auth
β”‚   β”‚   └── user-object.tsx    # User information display
β”‚   β”œβ”€β”€ reusables/
β”‚   β”‚   └── section.tsx        # Reusable section component
β”‚   └── ui/                    # UI components (toasts, loaders, etc.)
β”œβ”€β”€ lib/
β”‚   └── nonce-store.ts         # In-memory nonce management
β”œβ”€β”€ providers/
β”‚   └── providers.tsx          # Privy provider configuration

πŸ”— Resources

Base Account Documentation

Privy Documentation

Development Resources

πŸš€ Deployment

The easiest way to deploy this Next.js app is to use the Vercel Platform:

  1. Push your code to a Git repository
  2. Import your project to Vercel
  3. Add your environment variables
  4. Deploy!

Check out the Next.js deployment documentation for more details.

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“„ License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published