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

# Create TON Wallet

> Learn how to create and manage TON server wallets with Dynamic's Node SDK

## Overview

This guide walks you through creating TON wallets using Dynamic's Node SDK. You'll learn how to set up different threshold signature schemes and understand the security implications for TON blockchain operations.

## Prerequisites

Before you begin, make sure you have:

* [Set up your Dynamic project](/node/quickstart)
* [Created an authenticated client](/node/quickstart)
* [Enabled TON chain in your dashboard](https://app.dynamic.xyz/dashboard/chains)

## Step 1: Choose Your Security Model

Dynamic supports three threshold signature schemes for TON wallets:

### TWO\_OF\_TWO (Recommended for most use cases)

* **Security**: Highest - requires both your server and Dynamic's infrastructure
* **Availability**: Lower - both parties must be online
* **Use case**: High-value transactions, maximum security

### TWO\_OF\_THREE

* **Security**: High - requires 2 out of 3 shares
* **Availability**: Medium - can tolerate one party being offline
* **Use case**: Balanced security and availability

### THREE\_OF\_FIVE

* **Security**: High - requires 3 out of 5 shares
* **Availability**: Highest - can tolerate two parties being offline
* **Use case**: Maximum fault tolerance and availability

## Step 2: Create Your First TON Wallet

Here's a complete example of creating a TON wallet:

```typescript theme={"system"}
import { DynamicTonWalletClient } from '@dynamic-labs-wallet/node-ton';
import { ThresholdSignatureScheme } from '@dynamic-labs-wallet/node';

// Create authenticated client
export const authenticatedTonClient = async () => {
  const client = new DynamicTonWalletClient({
    environmentId: process.env.DYNAMIC_ENVIRONMENT_ID!,
  });

  await client.authenticateApiToken(process.env.DYNAMIC_AUTH_TOKEN!);
  return client;
};

export const createTonWallet = async ({
  thresholdSignatureScheme = ThresholdSignatureScheme.TWO_OF_TWO,
  password,
  onError
}: {
  thresholdSignatureScheme?: ThresholdSignatureScheme;
  password?: string;
  onError?: (error: Error) => void;
}) => {
  const tonClient = await authenticatedTonClient();

  const wallet = await tonClient.createWalletAccount({
    thresholdSignatureScheme,
    password,
    onError,
    backUpToDynamic: true,
  });

  return {
    accountAddress: walletMetadata.accountAddress,
    rawPublicKey: wallet.rawPublicKey,
    walletId: walletMetadata.walletId,
    externalServerKeyShares: wallet.externalServerKeyShares,
  };
};

// Usage example
const wallet = await createTonWallet({
  thresholdSignatureScheme: ThresholdSignatureScheme.TWO_OF_TWO,
  password: 'your-secure-password',
  onError: (error: Error) => {
    console.error('TON wallet creation error:', error);
  },
});

console.log('TON wallet created:', walletMetadata.accountAddress);
console.log('External server key shares:', wallet.externalServerKeyShares);
```

## Step 3: Handle Errors Gracefully

Always implement proper error handling for TON wallet creation:

```typescript theme={"system"}
try {
  const wallet = await createTonWallet({
    thresholdSignatureScheme: ThresholdSignatureScheme.TWO_OF_TWO,
    onError: (error: Error) => {
      console.error('Wallet creation error:', error);
    },
  });
  console.log('TON wallet created successfully:', walletMetadata.accountAddress);
} catch (error) {
  if (error.message.includes('invalid session')) {
    console.error('Invalid session ID - please re-authenticate');
  } else {
    console.error('TON wallet creation failed:', error.message);
  }
}
```

## Step 4: Store Wallet Information Securely

After creating a TON wallet, you'll receive important information that should be stored securely:

```typescript theme={"system"}
const wallet = await tonClient.createWalletAccount({
  thresholdSignatureScheme: ThresholdSignatureScheme.TWO_OF_TWO,
  backUpToDynamic: true,
});

// Store these securely in your database
const walletData = {
  accountAddress: walletMetadata.accountAddress,
  walletId: walletMetadata.walletId,
  thresholdScheme: ThresholdSignatureScheme.TWO_OF_TWO,
  chainName: 'TON',
  createdAt: new Date().toISOString(),
  externalServerKeyShares: wallet.externalServerKeyShares,
};

// Never store externalServerKeyShares in plain text
// They should be encrypted and stored securely
```

## Step 5: Derive the TON Address

You can also derive the TON address from a public key using `deriveTonAddress()`. This is a synchronous method:

```typescript theme={"system"}
const address = tonClient.deriveTonAddress({
  publicKeyHex: wallet.rawPublicKey,
  workchain: 0, // 0 = basechain (default)
});

console.log('Derived TON address:', address); // e.g. EQD...
```

## Best Practices

1. **Password Security**: Use strong, unique passwords for each TON wallet
2. **Error Handling**: Always handle potential errors during wallet creation with the `onError` callback
3. **Monitoring**: Log wallet creation events for audit purposes
4. **Backup Strategy**: Implement secure backup strategies for key shares with `backUpToDynamic: true`

## TON-Specific Considerations

### Workchain

TON organizes accounts into workchains. Most user wallets live on workchain `0` (basechain). The masterchain (`-1`) is used for system contracts.

### Address Format

TON wallet addresses are user-friendly base64url strings (e.g., `EQD...` for non-bounceable). The SDK derives these using `WalletContractV5R1`.

### Nanotons

TON uses **nanotons** as the smallest unit: `1 TON = 1,000,000,000 nanotons`.

## Next Steps

Now that you've created a TON wallet, you can:

* [Sign TON transactions](/node/ton/sign-transactions)
* [Sign TON messages](/node/ton/sign-messages)
* [Import existing TON private keys](/node/ton/import-private-keys)
* [Use imported TON wallets](/node/ton/use-imported-wallets)
