# 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tmawallet.gitbook.io/docs/tutorials-and-examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
