Tron - Getting Started (SDK)
Use the Tatum SDK with the Tron Wallet Provider to generate a Tron wallet mnemonic, xpub, private key and address, and to sign and broadcast TRX transfers.
Steps
Step_1: Get Started
Install the TRON wallet provider (@tatumio/tron-wallet-provider) together with the Tatum SDK (@tatumio/tatum).
npm install @tatumio/tatum @tatumio/tron-wallet-providerEach example below is a complete script. The examples are ES modules that use top-level await: run the JavaScript ones as .mjs files or set "type": "module" in your package.json. Replace {YOUR_API_KEY} with your API key from the Tatum Dashboard. Every example ends with destroy(), which stops the SDK's background jobs so the script can exit.
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 Managing Mnemonics and PrivateKeys.
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 the necessary libraries and initialize the SDK
import { TatumSDK, Network, Tron } from '@tatumio/tatum';
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
const tatum = await TatumSDK.init<Tron>({
network: Network.TRON,
apiKey: '{YOUR_API_KEY}',
configureWalletProviders: [TronWalletProvider],
});
// Generate a mnemonic using the Tron Wallet Provider submodule
const mnemonic = tatum.walletProvider.use(TronWalletProvider).generateMnemonic();
console.log(mnemonic); // This will print the generated mnemonic
await tatum.destroy();// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network } from '@tatumio/tatum';
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
const tatum = await TatumSDK.init({
network: Network.TRON,
apiKey: '{YOUR_API_KEY}',
configureWalletProviders: [TronWalletProvider],
});
// Generate a mnemonic using the Tron Wallet Provider submodule
const mnemonic = tatum.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 an xpub with or without a mnemonic. If you call generateXpub() without a mnemonic, the SDK generates a new one and returns it in xpubDetails.mnemonic.
// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network, Tron } from '@tatumio/tatum';
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
const tatum = await TatumSDK.init<Tron>({
network: Network.TRON,
apiKey: '{YOUR_API_KEY}',
configureWalletProviders: [TronWalletProvider],
});
const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2
// Generate an xpub from the mnemonic
const xpubDetails = await tatum.walletProvider.use(TronWalletProvider)
.generateXpub(mnemonic);
console.log(xpubDetails.xpub); // This will print the generated xpub
await tatum.destroy();// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network } from '@tatumio/tatum';
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
const tatum = await TatumSDK.init({
network: Network.TRON,
apiKey: '{YOUR_API_KEY}',
configureWalletProviders: [TronWalletProvider],
});
const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2
// Generate an xpub from the mnemonic
const xpubDetails = await tatum.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. The second argument is the derivation index: 0 returns the first private key derived from the mnemonic.
// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network, Tron } from '@tatumio/tatum';
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
const tatum = await TatumSDK.init<Tron>({
network: Network.TRON,
apiKey: '{YOUR_API_KEY}',
configureWalletProviders: [TronWalletProvider],
});
const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2
// Generate the private key at index 0 from the mnemonic
const privateKey = await tatum.walletProvider.use(TronWalletProvider)
.generatePrivateKeyFromMnemonic(mnemonic, 0);
console.log(privateKey); // This will print the generated private key
await tatum.destroy();// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network } from '@tatumio/tatum';
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
const tatum = await TatumSDK.init({
network: Network.TRON,
apiKey: '{YOUR_API_KEY}',
configureWalletProviders: [TronWalletProvider],
});
const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2
// Generate the private key at index 0 from the mnemonic
const privateKey = await tatum.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. When the xpub comes from the same mnemonic, both return the same address for the same index.
// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network, Tron } from '@tatumio/tatum';
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
const tatum = await TatumSDK.init<Tron>({
network: Network.TRON,
apiKey: '{YOUR_API_KEY}',
configureWalletProviders: [TronWalletProvider],
});
const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2
const xpub = '{YOUR_XPUB}'; // The xpub from Step 3
// Derive address from mnemonic
const addressFromMnemonic = await tatum.walletProvider
.use(TronWalletProvider)
.generateAddressFromMnemonic(mnemonic, 0);
// Derive address from xpub
const addressFromXpub = tatum.walletProvider
.use(TronWalletProvider)
.generateAddressFromXpub(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 libraries and initialize the SDK
import { TatumSDK, Network } from '@tatumio/tatum';
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
const tatum = await TatumSDK.init({
network: Network.TRON,
apiKey: '{YOUR_API_KEY}',
configureWalletProviders: [TronWalletProvider],
});
const mnemonic = '{YOUR_MNEMONIC}'; // The mnemonic from Step 2
const xpub = '{YOUR_XPUB}'; // The xpub from Step 3
// Derive address from mnemonic
const addressFromMnemonic = await tatum.walletProvider
.use(TronWalletProvider)
.generateAddressFromMnemonic(mnemonic, 0);
// Derive address from xpub
const addressFromXpub = tatum.walletProvider
.use(TronWalletProvider)
.generateAddressFromXpub(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.
The Tron Wallet Provider sends TRX. The payload follows the TronTxPayload type: the sender's privateKey, the recipient address to (Base58), and amount in TRX as a string.
// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network, Tron } from '@tatumio/tatum';
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
const tatum = await TatumSDK.init<Tron>({
network: Network.TRON,
apiKey: '{YOUR_API_KEY}',
configureWalletProviders: [TronWalletProvider],
});
// Define your transaction details
const payloadTron = {
privateKey: '{YOUR_PRIVATE_KEY}',
to: '{TARGET_WALLET_ADDRESS}',
amount: '0.01', // Amount in TRX
};
// Sign and broadcast the transaction using the Tron Wallet Provider submodule
const txHash = await tatum.walletProvider.use(TronWalletProvider)
.signAndBroadcast(payloadTron);
// This will print the transaction hash of the broadcasted transaction
console.log(txHash);
await tatum.destroy();// Import the necessary libraries and initialize the SDK
import { TatumSDK, Network } from '@tatumio/tatum';
import { TronWalletProvider } from '@tatumio/tron-wallet-provider';
const tatum = await TatumSDK.init({
network: Network.TRON,
apiKey: '{YOUR_API_KEY}',
configureWalletProviders: [TronWalletProvider],
});
// Define your transaction details
const payloadTron = {
privateKey: '{YOUR_PRIVATE_KEY}',
to: '{TARGET_WALLET_ADDRESS}',
amount: '0.01', // Amount in TRX
};
// Sign and broadcast the transaction using the Tron Wallet Provider submodule
const txHash = await tatum.walletProvider.use(TronWalletProvider)
.signAndBroadcast(payloadTron);
// This will print the transaction hash of the broadcasted transaction
console.log(txHash);
await tatum.destroy();Good to Know
- When you create a Tron address, it will be disregarded by the Tron Blockchain until you send funds to it. Find additional information in Tron - Unable to find account.
- To send TRC-10 tokens, use the Send TRC-10 tokens endpoint. For TRC-20 tokens, use the Send TRC-20 tokens endpoint.
- Be mindful of Tatum's derivation path. Find additional information in Tatum's Derivation Path.
- To do the same with the REST API instead of the SDK, see Tron wallet API: generate addresses and send TRX.
- Familiarize yourself with the Safety & Security Basics in Best Practices for Security.
Updated about 13 hours ago