Tron - Getting Started (SDK)

Steps

Step_1: Get Started

Ensure to have @tatumio/tron-wallet-provider installed alongside Tatum SDK.

npm install @tatumio/tron-wallet-provider

Step_2: Generate a Mnemonic

Generating a mnemonic means creating a 24-word phrase that will be the foundation for your wallet. This phrase is like a master key from which all your wallet addresses and their private keys can be generated.

  • Tatum does not store Mnemonics and or Private Keys.
  • The responsibility of keeping your Mnemonics and Private Keys secure rests solely with you, the User.
  • Additional information about Mnemonics and Private Keys is available in the following article.

The Tron Wallet Provider submodule helps in generating a mnemonic easily. This mnemonic phrase, also known as a seed phrase, is vital for creating and recovering wallets on Tron.

// Import necessary library and initialize the SDK
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
import { TatumSDK, Network, Tron } from '@tatumio/tatum';

const tatumSdk = await TatumSDK.init<Tron>({network: Network.TRON,
     configureWalletProviders: [
         TronWalletProvider,
     ]});

// Generate mnemonic using the Tron Wallet Provider submodule
const mnemonic = tatumSdk.walletProvider.use(TronWalletProvider)
.generateMnemonic();

console.log(mnemonic);  // This will print the generated mnemonic

await tatum.destroy()
// Import necessary library and initialize the SDK
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
import { TatumSDK, Network, Tron } from '@tatumio/tatum';

const tatumSdk = await TatumSDK.init({network: Network.TRON,
     configureWalletProviders: [
         TronWalletProvider,
     ]});

// Generate mnemonic using the Tron Wallet Provider submodule
const mnemonic = tatumSdk.walletProvider.use(TronWalletProvider)
.generateMnemonic();

console.log(mnemonic);  // This will print the generated mnemonic

await tatum.destroy()

Step_3: Generate XPUB

Generating an extended public key (xpub) is a way to allow the creation of public addresses without exposing the corresponding private keys. It's an important function for maintaining security while still being able to receive funds.

You can generate Xpub with or without mnemonic.

// Import the necessary library and initialize the SDK
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
import { TatumSDK, Network, Ethereum } from '@tatumio/tatum';

const tatumSdk = await TatumSDK.init<Tron>({network: Network.TRON,
     configureWalletProviders: [
         TronWalletProvider,
     ]});

// Generate xpub using the generated mnemonic
const xpubDetails = await tatumSdk.walletProvider.use(TronWalletProvider)
.generateXpub(mnemonic)


console.log(xpubDetails.xpub);  // This will print the generated xpub

await tatum.destroy()
// Import the necessary library and initialize the SDK
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
import { TatumSDK, Network } from '@tatumio/tatum';

// Initialize the SDK for Tron
const tatumSdk = await TatumSDK.init({
  network: Network.TRON,
  configureWalletProviders: [TronWalletProvider]
});

// Generate xpub using the generated mnemonic
const xpubDetails = await tatumSdk.walletProvider
  .use(TronWalletProvider)
  .generateXpub(mnemonic);

console.log(xpubDetails.xpub);  // This will print the generated xpub

await tatum.destroy();

Step_4: Generate Private Key

Creating a private key is creating a unique and secret key that belongs to you. This key is crucial for accessing and controlling your funds.

// Import the necessary library and initialize the SDK
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
import { TatumSDK, Network, Tron } from '@tatumio/tatum';

const tatumSdk = await TatumSDK.init<Tron>({network: Network.TRON,
     configureWalletProviders: [
         TronWalletProvider,
     ]});

// Generate private key from mnemonic
const privateKey = await tatumSdk.walletProvider.use(TronWalletProvider)
.generatePrivateKeyFromMnemonic(mnemonic, 0);

console.log(privateKey);  // This will print the generated private key

await tatum.destroy()

// Import the necessary library and initialize the SDK
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
import { TatumSDK, Network, Tron } from '@tatumio/tatum';

// Initialize the SDK for Tron
const tatumSdk = await TatumSDK.init({
  network: Network.TRON,
  configureWalletProviders: [TronWalletProvider]
});

// Generate private key from mnemonic
const privateKey = await tatumSdk.walletProvider
  .use(TronWalletProvider)
  .generatePrivateKeyFromMnemonic(mnemonic, 0);

console.log(privateKey);  // This will print the generated private key

await tatum.destroy()

Step_5: Generate Address

These functions allow you to derive a wallet address from a mnemonic or an xpub using the TronWalletProvider from the Tatum SDK.

// Import the necessary library and initialize the SDK
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
import { TatumSDK, Network } from '@tatumio/tatum';

// Initialize the SDK for Tron
const tatumSdk = await TatumSDK.init<Tron>({
  network: Network.TRON,
  configureWalletProviders: [TronWalletProvider]
});

// Derive address from mnemonic
const addressFromMnemonic = await tatumSdk.walletProvider
  .use(TronWalletProvider)
  .generateAddressFromMnemonic(mnemonic, 0);

// Derive address from xpub
const addressFromXpub = await tatumSdk.walletProvider
  .use(TronWalletProvider)
  .generateAddressFromXpub(xpubDetails.xpub, 0);

// Output the derived addresses
console.log(addressFromMnemonic);  // Prints the address derived from mnemonic
console.log(addressFromXpub);  // Prints the address derived from xpub

await tatum.destroy();
// Import the necessary library and initialize the SDK
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
import { TatumSDK, Network } from '@tatumio/tatum';

// Initialize the SDK for Tron
const tatumSdk = await TatumSDK.init({
  network: Network.TRON,
  configureWalletProviders: [TronWalletProvider]
});

// Derive address from mnemonic
const addressFromMnemonic = await tatumSdk.walletProvider
  .use(TronWalletProvider)
  .generateAddressFromMnemonic(mnemonic, 0);

// Derive address from xpub
const addressFromXpub = await tatumSdk.walletProvider
  .use(TronWalletProvider)
  .generateAddressFromXpub(xpubDetails.xpub, 0);

// Output the derived addresses
console.log(addressFromMnemonic);  // Prints the address derived from mnemonic
console.log(addressFromXpub);  // Prints the address derived from xpub

await tatum.destroy();

Step_6: Sign and Broadcast a Transaction

Signing and broadcasting a transaction is about authorising a transfer of funds or interaction with a smart contract and then sending that authorisation to the blockchain to be processed.

// Import the necessary library and initialize the SDK
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
import { TatumSDK, Network, Tron } from '@tatumio/tatum';

const tatumSdk = await TatumSDK.init<Tron>({network: Network.TRON,
     configureWalletProviders: [
         TronWalletProvider,
     ]});

// Define your transaction details
const payloadTron = {
  privateKey: 'YOUR_PRIVATE_KEY',
  to: 'TARGET_WALLET_ADDRESS',
  amount: '0.01'  // TRX_AMOUNT
}

// Sign and broadcast the transaction using the Tron Wallet Provider submodule
const txHash = await tatumSdk.walletProvider.use(TronWalletProvider)
.signAndBroadcast(payloadTron);

// This will print the transaction hash of the broadcasted transaction
console.log(txHash);

await tatum.destroy();
// Import the necessary library and initialize the SDK
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
import { TatumSDK, Network, Tron } from '@tatumio/tatum';

const tatumSdk = await TatumSDK.init<Tron>({network: Network.TRON,
     configureWalletProviders: [
         TronWalletProvider,
     ]});

// Define your transaction details
const payloadTron = {
  privateKey: 'YOUR_PRIVATE_KEY',
  to: 'TARGET_WALLET_ADDRESS',
  amount: '0.01'  // TRX_AMOUNT
}

// Sign and broadcast the transaction using the Tron Wallet Provider submodule
const txHash = await tatumSdk.walletProvider.use(TronWalletProvider)
.signAndBroadcast(payloadTron);

// This will print the transaction hash of the broadcasted transaction
console.log(txHash);

await tatum.destroy();

Good to Know