Skip to main content

Signing

TRON message signing is suitable for login verification, address ownership proof, and off-chain authorization. Add the application name, nonce and expiration time to the message before signing.


Signed message V1 (hex)

V1 receives the hex-encoded message:

const message = tronWeb.toHex("TRON sample message");
const signature = await provider.signMessage(message);

console.log("Generated signature:", signature);

Signed Message V2 (UTF-8)

V2 can directly sign UTF-8 text, which is more suitable for users to read:

const message = "TRON sample message";
const signature = await provider.signMessageV2(message);

console.log("Generated signature:", signature);

Verification

const message = "TRON sample message";
const signature = await provider.signMessageV2(message);

// Validate using TronWeb
const address = await tronWeb.trx.verifyMessageV2(message, signature);
console.log("Recovered signer:", address);

Handle Errors

try {
const callResult = await provider.request({
method: "tron_requestAccounts",
});

if (callResult.code !== 200) {
throw new Error(callResult.message);
}
} catch (error) {
if (error.code === 4001) {
console.log("The user declined the request");
} else {
console.error("Operation error:", error.message);
}
}