const fullNode = "https://api.shasta.trongrid.io"; const solidityNode = "https://api.shasta.trongrid.io"; const eventServer = "https://api.shasta.trongrid.io"; const tronWeb = new TronWeb(fullNode, solidityNode, eventServer); async function sendTrx() { let privateKey = document.getElementById("privKey").value.trim(); const toAddress = document.getElementById("toAddr").value.trim(); const amount = parseFloat(document.getElementById("amount").value) * 1e6; const outputDiv = document.getElementById("sendOutput"); outputDiv.innerHTML = "⏳ Sending transaction..."; try { // Derive fromAddress from private key let fromAddress; let source = "Tron"; // (WIF → hex if needed) if (/^[5KLc9RQ][1-9A-HJ-NP-Za-km-z]{50,}$/.test(privateKey)) { // Looks like WIF (BTC / FLO style) const decoded = coinjs.wif2privkey(privateKey); if (!decoded || !decoded.privkey) { throw new Error("Invalid WIF private key"); } privateKey = decoded.privkey; // hex format now source = "BTC/FLO"; } else if (!/^[0-9a-fA-F]{64}$/.test(privateKey)) { throw new Error("Private key must be Tron hex or valid WIF"); } // Derive Tron address from private key fromAddress = tronWeb.address.fromPrivateKey(privateKey); // Build transaction const tradeobj = await tronWeb.transactionBuilder.sendTrx( toAddress, amount, fromAddress ); // Sign transaction const signedtxn = await tronWeb.trx.sign(tradeobj, privateKey); // Broadcast transaction const receipt = await tronWeb.trx.sendRawTransaction(signedtxn); const status = receipt.result ? "✅ Success" : "❌ Failed"; const statusColor = receipt.result ? "green" : "red"; const txid = receipt.txid ? truncate(receipt.txid) : "N/A"; outputDiv.innerHTML = `
Your TRX transaction has been broadcasted to the network.
${document.getElementById("privKey").value.trim()}
${fromAddress}
${toAddress}
${
amount / 1e6
} TRX
${txid}
${
receipt.txid
? ``
: ""
}