NodeJs Function to Check if a Tron Wallet Address is Valid and Activated


tron-blockchain NodeJs Function to Check if a Tron Wallet Address is Valid and Activated blockchain javascript nodejs Tron TRX

tron-blockchain

On Tron Blockchain, a wallet could be inactivated as it doesn’t appear on the chain yet. The way to activate a Tron Wallet is to send a 1 TRX to it, so that the wallet/transaction is recorded on the Tron Blockchain.

How to Check if a String is a Valid TRON wallet?

We can use the TronWeb to validate if a given string is a valid TRON wallet address (although it can be inactive):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Function to check if a TRON wallet address is valid
// param: address to check if it is valid tron wallet
// param: tron network is "mainnet", "shasta" or "nile"
// param: AppKey is the TronGrid APP KEY
function isValidAddress(address, network, AppKey) {
    network = network.toLowerCase();
    let url = null;
    if (network === "mainnet") {
        url = "https://api.trongrid.io";
    } else if (network === "shasta") {
        url = "https://api.shasta.trongrid.io";
    } else if (network === "nile") {
        url = "https://nile.trongrid.io";
    }
    const tronWebParam = {
        fullHost: url,
    };
    if (AppKey != null && typeof AppKey !== "undefined" && network === "mainnet") {
        tronWebParam["headers"] = { "TRON-PRO-API-KEY": AppKey };
    }
    const tronWeb = new TronWeb(tronWebParam);
    return tronWeb.isAddress(address);
}
// Function to check if a TRON wallet address is valid
// param: address to check if it is valid tron wallet
// param: tron network is "mainnet", "shasta" or "nile"
// param: AppKey is the TronGrid APP KEY
function isValidAddress(address, network, AppKey) {
    network = network.toLowerCase();
    let url = null;
    if (network === "mainnet") {
        url = "https://api.trongrid.io";
    } else if (network === "shasta") {
        url = "https://api.shasta.trongrid.io";
    } else if (network === "nile") {
        url = "https://nile.trongrid.io";
    }
    const tronWebParam = {
        fullHost: url,
    };
    if (AppKey != null && typeof AppKey !== "undefined" && network === "mainnet") {
        tronWebParam["headers"] = { "TRON-PRO-API-KEY": AppKey };
    }
    const tronWeb = new TronWeb(tronWebParam);
    return tronWeb.isAddress(address);
}

How to Check if a wallet is Valid and Activated?

We can also use the following Javascript (NodeJs) function to check if an address is valid and activated. This is done by using the tronweb.trx.getAccount to obtain the information from the Tron Blockchain. If it succeeds, then the address is valid and activated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Function to check if a TRON wallet address is valid and activated
// param: address is the given tron wallet address tring
// param: network is "mainnet", "shasta" or "nile"
// param: AppKey is the TronGrid APP KEY
async function isValidAndActivated(address, network, AppKey) {
    network = network.toLowerCase();
    let url = null;
    if (network === "mainnet") {
        url = "https://api.trongrid.io";
    } else if (network === "shasta") {
        url = "https://api.shasta.trongrid.io";
    } else if (network === "nile") {
        url = "https://nile.trongrid.io";
    }
    const tronWebParam = {
        fullHost: url,
    };
    if (AppKey != null && typeof AppKey !== "undefined" && network === "mainnet") {
        tronWebParam["headers"] = { "TRON-PRO-API-KEY": AppKey };
    }
    const tronWeb = new TronWeb(tronWebParam);
 
    try {
        // Check if the address is a valid TRON address
        if (!tronWeb.isAddress(address)) {
            return false; // Invalid address
        }
 
        // Fetch account information
        const accountInfo = await tronWeb.trx.getAccount(address);
 
        if (accountInfo && accountInfo.address) {
            return true; // Address is activated
            // use the following if you require the account balance non-zero
            // return accountInfo.balance > 0; 
        } else {
            return false; // Address is not activated
        }
    } catch (error) {
        console.error('Error checking address:', error);
        return false; // Something went wrong
    }
}
// Function to check if a TRON wallet address is valid and activated
// param: address is the given tron wallet address tring
// param: network is "mainnet", "shasta" or "nile"
// param: AppKey is the TronGrid APP KEY
async function isValidAndActivated(address, network, AppKey) {
    network = network.toLowerCase();
    let url = null;
    if (network === "mainnet") {
        url = "https://api.trongrid.io";
    } else if (network === "shasta") {
        url = "https://api.shasta.trongrid.io";
    } else if (network === "nile") {
        url = "https://nile.trongrid.io";
    }
    const tronWebParam = {
        fullHost: url,
    };
    if (AppKey != null && typeof AppKey !== "undefined" && network === "mainnet") {
        tronWebParam["headers"] = { "TRON-PRO-API-KEY": AppKey };
    }
    const tronWeb = new TronWeb(tronWebParam);

    try {
        // Check if the address is a valid TRON address
        if (!tronWeb.isAddress(address)) {
            return false; // Invalid address
        }

        // Fetch account information
        const accountInfo = await tronWeb.trx.getAccount(address);

        if (accountInfo && accountInfo.address) {
            return true; // Address is activated
            // use the following if you require the account balance non-zero
            // return accountInfo.balance > 0; 
        } else {
            return false; // Address is not activated
        }
    } catch (error) {
        console.error('Error checking address:', error);
        return false; // Something went wrong
    }
}

Tron Blockchain Posts

Here are some popular posts regarding the Tron Blockchain:

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
522 words
Last Post: Teaching Kids Programming - Split the Numbers into Two Distinct Arrays
Next Post: Teaching Kids Programming - Existence of a Substring in a String and Its Reverse

The Permanent URL is: NodeJs Function to Check if a Tron Wallet Address is Valid and Activated

Leave a Reply