Sign and Verify Message
signMessageV2
This returns a Promise which resolves to the Raw Signature of message.
A signed message is prefixd with "\x19TRON Signed Message:\n" and the length of the message, using the hashMessage method in ethers.js, so that it is TIP-191 compliant.
❗️Note:
tronWeb.trx.signMessage
will be deprecated soon, as it is prefixd with "\x19TRON Signed Message:\32" which customs the length of message and only accepts hex string. Consequently, we add signMessageV2 function to support plaintext string signature. It is always highly recommended that you use tronWeb.trx.signMessageV2 above the TronWeb version 4.4.0.
In signMessageV2 function, we should note that:
-
If message is a string, it is treated as a string and converted to its representation in UTF8 bytes.
-
If and only if a message is a Bytes Array will it be treated as binary data.
For example, , signMessageV2 will take the string "0x1234" as a 6 characters string not the hex string.
-
If you only want to sign a hash, you should convert your hash string to an Bytes Array first using
TronWeb.utils.ethersUtils.arrayify
utility function.
Usage
If you keep the private key, you can use as follows:
// way1: initiate a tronWeb instance and pass the private key
> const tronWeb = new TronWeb({fullHost: 'xxx', privateKey: 'privateKey'});
> const signature = await tronWeb.trx.signMessageV2('message')
// way2: just call by TronWeb Class without initiating tronWeb
> const signature = await TronWeb.Trx.signMessageV2('message', 'privateKey');
If the private key is in wallets such as TronLink extension, TronLink wallet APP, you can use the exported tronWeb instance by wallets.
> const signature = await tronWeb.trx.signMessageV2('message');
This will popup a signature confirmation dialog to request the confirmation of users.
verifyMessageV2
verifyMessageV2
function is corresponding to signMessageV2
. It will return the recovering address in Base58 format. Users should check if the recovering address is the signed account.
❗️Note:
tronWeb.trx.verifyMessage
will be deprecated soon, is corresponding totronWeb.trx.signMessage
. And if you get a signature with v2, you can not usetronWeb.trx.verifyMessage
to verify it as they are not incompatible. It is always highly recommended that you use tronWeb.trx.signMessageV2 to sign and tronWeb.trx.verifyMessageV2 to verify the signature.
Usage
This is only a utility function, you can use like this:
> const base58Address = await TronWeb.Trx.verifyMessageV2(messge, signature);
or
> const base58Address = await tronWeb.trx.verifyMessageV2(messge, signature);