import { Turnkey } from "@turnkey/sdk-server";
import { TronWeb } from "tronweb";
// Initialize Turnkey client
const turnkeyClient = new Turnkey({
apiBaseUrl: "https://api.turnkey.com",
apiPrivateKey: process.env.API_PRIVATE_KEY,
apiPublicKey: process.env.API_PUBLIC_KEY,
defaultOrganizationId: process.env.ORGANIZATION_ID,
});
// Initialize TronWeb without a private key
const tronWeb = new TronWeb({
fullHost: "https://api.shasta.trongrid.io", // Testnet
});
const turnkeyAddress = process.env.TRON_ADDRESS; // Your Tron address in Turnkey
const recipientAddress = "TYour_Recipient_Address";
const amount = 100; // Amount in SUN (1 TRX = 1,000,000 SUN)
// 1. Create an unsigned transaction
const unsignedTx = await tronWeb.transactionBuilder.sendTrx(
recipientAddress,
amount,
turnkeyAddress
);
// Sign with Turnkey
const { r, s, v } = await turnkeyClient.apiClient().signRawPayload({
organizationId: process.env.ORGANIZATION_ID,
signWith: turnkeyAddress,
payload: unsignedTx.raw_data_hex,
encoding: "PAYLOAD_ENCODING_HEXADECIMAL",
});
// Add the signature to the transaction
unsignedTx["signature"] = r + s + v;
// 3. Broadcast the signed transaction
const result = await tronWeb.trx.sendRawTransaction(unsignedTx);
console.log("Transaction sent! ID:", result.txid);