> For the complete documentation index, see [llms.txt](/llms.txt).

# Integrate Embedded Wallets with the TON Blockchain

While using the Embedded Wallets Web SDK (formerly Web3Auth) for a non-EVM chain like [TON](https://ton.org/), you can obtain the user's private key from the provider. Using this private key, you can use the corresponding libraries of the blockchain to make blockchain calls like getting the user's `account`, `balance`, `send transaction`, etc. We have highlighted a few methods here to get you started quickly.

note

The SDKs are now branded as MetaMask Embedded Wallet SDKs (formerly Web3Auth Plug and Play SDKs). Package names and APIs remain Web3Auth (for example, Web3Auth React SDK), and code snippets may reference `web3auth` identifiers.

## Installation

**npm**

```bash
npm install --save @web3auth/modal tonweb @orbs-network/ton-access

```

**Yarn**

```bash
yarn add @web3auth/modal tonweb @orbs-network/ton-access

```

**pnpm**

```bash
pnpm add @web3auth/modal tonweb @orbs-network/ton-access

```

**Bun**

```bash
bun add @web3auth/modal tonweb @orbs-network/ton-access

```

## Initialize Embedded Wallets

After v10, you don't need chain-specific setup in code for non-EVM chains. Configure standard EVM chains on the [Embedded Wallets dashboard](/embedded-wallets/dashboard/chains-and-networks/).

```tsx
import { Web3Auth, WEB3AUTH_NETWORK } from '@web3auth/modal'

const web3auth = new Web3Auth({
  clientId,
  web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
})

await web3auth.init()
await web3auth.connect()

const web3authProvider = web3auth.connection?.ethereumProvider ?? null

```

Non-EVM chains derive keys from `web3authProvider` using the `private_key` JSON-RPC method, as shown in the examples below.

## Initializing provider

### Getting the `rpc`

```typescript
import { getHttpEndpoint } from '@orbs-network/ton-access'

const rpc = await getHttpEndpoint()

```

## TON Libraries

`TonWeb` is a JavaScript/TypeScript library that provides a high-level interface for interacting with the TON blockchain. It allows for the creation and management of wallets, signing transactions, and interacting with smart contracts.

`@orbs-network/ton-access` is a lightweight library that helps discover and connect to TON blockchain nodes. It provides an endpoint to the TON network, ensuring you can reliably connect to the blockchain for your operations.

## Get accounts

Using `getKeyPairFromPrivateKey()` and TON's wallet libraries, you can retrieve the user's account address.

```typescript
import type { IProvider } from '@web3auth/modal'
import TonWeb from 'tonweb'

const rpc = await getHttpEndpoint()

export default class TonRPC {
  private provider: IProvider
  private tonweb: TonWeb

  constructor(provider: IProvider) {
    this.provider = provider
    this.tonweb = new TonWeb(new TonWeb.HttpProvider(rpc))
  }

  async getAccounts(): Promise<string> {
    try {
      const privateKey = await this.getPrivateKey()
      const keyPair = this.getKeyPairFromPrivateKey(privateKey)
      const WalletClass = this.tonweb.wallet.all['v3R2']
      const wallet = new WalletClass(this.tonweb.provider, {
        publicKey: keyPair.publicKey,
      })
      const address = await wallet.getAddress()
      return address.toString(true, true, true)
    } catch (error) {
      console.error('Error getting accounts:', error)
      return ''
    }
  }

  async getPrivateKey(): Promise<string> {
    try {
      return await this.provider.request({
        method: 'private_key',
      })
    } catch (error) {
      console.error('Error getting private key:', error)
      throw error
    }
  }

  public getKeyPairFromPrivateKey(privateKey: string): {
    publicKey: Uint8Array
    secretKey: Uint8Array
  } {
    const privateKeyBytes = new Uint8Array(
      privateKey.match(/.{1,2}/g)!.map(byte => parseInt(byte, 16))
    )

    if (privateKeyBytes.length !== 32) {
      const adjustedPrivateKey = new Uint8Array(32)
      adjustedPrivateKey.set(privateKeyBytes.slice(0, 32))
      return TonWeb.utils.nacl.sign.keyPair.fromSeed(adjustedPrivateKey)
    }

    return TonWeb.utils.nacl.sign.keyPair.fromSeed(privateKeyBytes)
  }
}

```

## Get balance

Using the account address we received in the previous step, we can fetch the balance using an RPC call to `tonweb.getBalance()`.

```typescript
import TonWeb from 'tonweb'

export default class TonRPC {
  async getBalance(): Promise<string> {
    try {
      const address = await this.getAccounts()
      const balance = await this.tonweb.getBalance(address)
      return TonWeb.utils.fromNano(balance)
    } catch (error) {
      console.error('Error getting balance:', error)
      return '0'
    }
  }
}

```

## Send transaction

```typescript
import TonWeb from 'tonweb'

export default class TonRPC {
  async sendTransaction(): Promise<any> {
    try {
      const privateKey = await this.getPrivateKey()
      const keyPair = this.getKeyPairFromPrivateKey(privateKey)
      const WalletClass = this.tonweb.wallet.all['v3R2']
      const wallet = new WalletClass(this.tonweb.provider, {
        publicKey: keyPair.publicKey,
      })

      const address = await wallet.getAddress()
      console.log('Wallet address:', address.toString(true, true, true))

      const balance = await this.tonweb.getBalance(address)
      console.log('Wallet balance:', TonWeb.utils.fromNano(balance))

      const isDeployed = balance !== '0'
      console.log('Is wallet deployed:', isDeployed)

      if (!isDeployed) {
        console.log(
          'Wallet not deployed or has zero balance. Please deploy the wallet and fund it before sending transactions.'
        )
        return { error: 'Wallet not deployed or has zero balance' }
      }

      let seqno
      for (let i = 0; i < 3; i++) {
        try {
          seqno = await wallet.methods.seqno().call()
          console.log('Current seqno:', seqno)
          if (seqno !== null) break
        } catch (seqnoError) {
          console.error(`Error getting seqno (attempt ${i + 1}):`, seqnoError)
        }
        await new Promise(resolve => setTimeout(resolve, 2000))
      }

      if (seqno === null) {
        throw new Error('Failed to retrieve seqno after multiple attempts')
      }

      const transfer = wallet.methods.transfer({
        secretKey: keyPair.secretKey,
        toAddress: 'EQD4FPq-PRDieyQKkizFTRtSDyucUIqrj0v_zXJmqaDp6_0t',
        amount: TonWeb.utils.toNano('0.01'),
        seqno: seqno,
        payload: 'Hello, TON!',
        sendMode: 3,
      })

      console.log('Prepared transfer:', transfer)

      const result = await transfer.send()
      console.log('Transaction result:', result)

      return {
        transactionHash: result.hash,
      }
    } catch (error) {
      console.error('Error sending transaction:', error)
      return { error: error.message }
    }
  }
}

```
