> For the complete documentation index, see [llms.txt](https://tmawallet.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tmawallet.gitbook.io/docs/tutorials-and-examples.md).

# Tutorials and Examples

**Basic Wallet Operations**

1. **Create a Wallet**

   * This tutorial demonstrates how to create a new wallet using the `TMAWalletClient`.

   ```javascript
   import { TMAWalletClient } from '@tmawallet/sdk';

   (async () => {
     const tmaWallet = new TMAWalletClient('your_project_public_token');
     await tmaWallet.init();
     if (!tmaWallet.isBundleExists) {
       await tmaWallet.createBundle();
     }
     console.log('Wallet address:', tmaWallet.walletAddress);
   })();
   ```
2. **Restore a Wallet**

   * This tutorial shows how to restore a wallet by retrieving an existing bundle from Telegram's cloud storage.

   ```javascript
   import { TMAWalletClient } from '@tmawallet/sdk';

   (async () => {
     const tmaWallet = new TMAWalletClient('your_project_public_token');
     await tmaWallet.init();
     if (tmaWallet.isBundleExists) {
       console.log('Restored wallet address:', tmaWallet.walletAddress);
     } else {
       console.log('No existing wallet found.');
     }
   })();
   ```
3. **Make a Transaction**

   * Example of using TMAWallet to sign a transaction on the Ethereum blockchain.

   ```javascript
   import { TMAWalletClient } from '@tmawallet/sdk';
   import { ethers } from 'ethers';

   (async () => {
     const tmaWallet = new TMAWalletClient('your_project_public_token');
     await tmaWallet.init();
     if (tmaWallet.isBundleExists) {
       const wallet = new ethers.Wallet(tmaWallet.privateKey);
       const provider = ethers.getDefaultProvider('ropsten');
       const signer = wallet.connect(provider);

       const tx = {
         to: 'recipient_address',
         value: ethers.utils.parseEther('0.01'),
         gasLimit: 21000,
       };

       const transactionResponse = await signer.sendTransaction(tx);
       console.log('Transaction Hash:', transactionResponse.hash);
     } else {
       console.log('No wallet found to make a transaction.');
     }
   })();
   ```

**Telegram Cloud Integration**

* **Using TelegramCloudStorage**: Learn how to store and retrieve data using `TelegramCloudStorage` for wallet management.

  ```javascript
  import { TelegramCloudStorage } from '@tmawallet/sdk';

  (async () => {
    const storage = new TelegramCloudStorage();
    await storage.setItem('test_key', 'test_value');
    const value = await storage.getItem('test_key');
    console.log('Stored Value:', value);
  })();
  ```

**Advanced Usage**

* **Managing Bundles**: A detailed guide on using `ClientBundleController` and `WalletBundleController` to create, retrieve, and clear bundles.

  ```javascript
  import { ClientBundleController, WalletBundleController } from '@tmawallet/sdk';
  import { TelegramCloudStorage } from '@tmawallet/sdk';

  (async () => {
    const storage = new TelegramCloudStorage();
    const clientBundleController = new ClientBundleController(storage);
    const walletBundleController = new WalletBundleController(storage);

    // Create a new client bundle
    const newBundle = await clientBundleController.createNewBundle();
    console.log('New client bundle created:', newBundle);

    // Retrieve the existing bundle
    const existingBundle = await clientBundleController.getClientBundle();
    console.log('Existing bundle:', existingBundle);

    // Clear the client bundle
    await clientBundleController.clearClientBundle();
    console.log('Client bundle cleared');
  })();
  ```
* **Handling User Sessions**: Best practices for managing sessions and key security across different user devices.
  * **Session Persistence**: Use Telegram's cloud storage to persist session data, ensuring that users can access their wallets across devices.
  * **Key Security**: Avoid storing sensitive keys locally. Instead, leverage MPC and TelegramCloudStorage to manage keys securely.
  * **Session Expiry**: Implement session expiry mechanisms to ensure that inactive sessions are securely terminated, reducing the risk of unauthorized access.
