Skip to main content
Author(s): STON.fi Team
This guide was contributed by the STON.fi Team, and has not been validated by Privy. Please review and test thoroughly before using in production. If you find a bug or problem with this resource, please let Privy support know and we’ll alert STON.fi Team.
This guide demonstrates how to integrate Privy with the TON blockchain in a Vite + React app to enable wallet login as well as message and transaction signing.

Resources

Privy TON Wallets

Learn how to use Privy embedded wallets with TON for signing messages and transactions.

React quickstart

Get started with Privy’s React SDK.

Creating your Privy app

If you haven’t set up Privy yet, follow our React quickstart guide to get your app ID and configure your app.

Installing with Vite

Create a new Vite + React + TypeScript application and install the required dependencies:
Install the required packages for TON integration:
Additionally, install the Node.js polyfills plugin for Vite, which is necessary to provide Buffer and other Node.js APIs in the browser environment (required by TON libraries):
Next, install Tailwind CSS and its Vite plugin:
Configure the Vite plugin by updating vite.config.js file:
Then, import Tailwind CSS in your main CSS file. Open src/index.css and replace any existing code with:
You can also remove src/App.css (we don’t need it), and remove the import statement import './App.css' from src/App.tsx if it exists.
For simplicity, this guide uses Tailwind CSS for styling the components. The setup for Tailwind CSS is already included in the instructions above, so you don’t need to set it up separately.

Environment variables

Vite requires environment variables to be prefixed with VITE_:

Setting up the app entry point

Set up the main entry point with the Privy provider directly in your main.tsx file:

Using Privy in your app

With Privy integrated, you can authenticate users, generate embedded wallets, and facilitate message and transaction signing.

Log in with Privy

To log in users with Privy, use the useLogin hook:

Creating a TON embedded wallet

First, create a custom hook to access the TON wallet from Privy’s linked accounts:
Now use the useCreateWallet hook from extended chains to create a wallet:

Getting wallet balance

To check the balance of a TON wallet using our custom hooks and utilities:

Checking wallet deployment status

TON wallets are smart contracts that need to be deployed before they can be used. Create a hook to check the deployment status:

Deploying the wallet

Ensure your wallet has sufficient balance (minimum 0.05 TON) before attempting deployment. The deployment transaction requires gas fees.

Signing a message

To sign a message with a TON embedded wallet, we use the browser’s crypto.subtle API to hash the message to 32 bytes (SHA-256), which is then signed via signRawHash:

Sending a transaction

To send TON from the embedded wallet using browser-compatible methods:

Wallet deployment status component

Create a component to inform users about wallet deployment:

Complete example container

Here’s the complete application that brings together all the components we’ve created:

Creating the main App component

The App.tsx component will contain your main application logic:

Run

Open the printed local URL (typically http://localhost:5173).

Summary

  1. Configure env
    • In .env (or .env.development), set:
    • VITE_PRIVY_APP_ID=<your app id>
    • VITE_TON_API_KEY=<optional toncenter api key>
  2. Start the app
    • Run npm run dev and open the local URL.
  3. Log in
    • Click Log in with Privy (email auth).
  4. Create a TON wallet (once)
    • If you don’t have one yet, click Create TON Wallet.
  5. Fund the wallet
    • Send ≥ 0.05 TON to the wallet address shown in the UI.
  6. Deploy the wallet
    • Click Deploy Wallet once funded.
    • Wait until you see ✓ Wallet is deployed.
  7. Sign a message
    • Click Sign Message to sign an example message (check the console for the signature).
  8. Send TON
    • Enter Recipient address (EQ…) and Amount in TON.
    • Click Send … TON and wait for the success notice.